CXXR (C++ R)
ProtectStack.h
Go to the documentation of this file.
1 /*CXXR $Id: ProtectStack.h 1397 2013-08-16 18:25:59Z arr $
2  *CXXR
3  *CXXR This file is part of CXXR, a project to refactor the R interpreter
4  *CXXR into C++. It may consist in whole or in part of program code and
5  *CXXR documentation taken from the R project itself, incorporated into
6  *CXXR CXXR (and possibly MODIFIED) under the terms of the GNU General Public
7  *CXXR Licence.
8  *CXXR
9  *CXXR CXXR is Copyright (C) 2008-13 Andrew R. Runnalls, subject to such other
10  *CXXR copyrights and copyright restrictions as may be stated below.
11  *CXXR
12  *CXXR CXXR is not part of the R project, and bugs and other issues should
13  *CXXR not be reported via r-bugs or other R project channels; instead refer
14  *CXXR to the CXXR website.
15  *CXXR */
16 
17 /*
18  * R : A Computer Language for Statistical Data Analysis
19  *
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation; either version 2.1 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, a copy is available at
32  * http://www.r-project.org/Licenses/
33  */
34 
46 #ifndef PROTECTSTACK_H
47 #define PROTECTSTACK_H 1
48 
49 #include "CXXR/RObject.h"
50 
51 #ifdef __cplusplus
52 
53 #include "CXXR/NodeStack.hpp"
54 
55 namespace CXXR {
60  class ProtectStack {
61  public:
70  class Scope : public NodeStack::Scope {
71  public:
72  Scope()
73  : NodeStack::Scope(s_stack)
74  {}
75  };
76 
87  static unsigned int protect(RObject* node)
88  {
89  return s_stack->push(node);
90  }
91 
109  static void reprotect(RObject* node, unsigned int index)
110  {
111  s_stack->retarget(node, index);
112  }
113 
127  static void restoreSize(size_t new_size);
128 
136  static size_t size()
137  {
138  return s_stack->size();
139  }
140 
152  static void unprotect(unsigned int count = 1)
153  {
154  s_stack->pop(count);
155  }
156 
167  static void unprotectPtr(RObject* node)
168  {
169  s_stack->eraseTopmost(node);
170  }
171 
180  {
181  s_stack->visitRoots(v);
182  }
183  private:
184  friend class GCNode;
185 
186  static NodeStack* s_stack;
187 
188  // Not implemented:
189  ProtectStack();
190 
191  // Clean up static data at end of run (called by
192  // GCNode::SchwarzCtr destructor):
193  static void cleanup();
194 
195  // Initialize static data (called by GCNode::SchwarzCtr
196  // constructor):
197  static void initialize();
198 
199  // Put all entries into the protecting state:
200  static void protectAll()
201  {
202  s_stack->protectAll();
203  }
204  };
205 } // namespace CXXR
206 
207 extern "C" {
208 #endif /* __cplusplus */
209 
210  /* ***** C interface ***** */
211 
212  typedef unsigned int PROTECT_INDEX;
213 
226 #ifndef __cplusplus
227  void R_ProtectWithIndex(SEXP node, PROTECT_INDEX *iptr);
228 #else
229  inline void R_ProtectWithIndex(SEXP node, PROTECT_INDEX *iptr)
230  {
231  *iptr = CXXR::ProtectStack::protect(node);
232  }
233 #endif
234 
252 #ifndef __cplusplus
253  void R_Reprotect(SEXP node, PROTECT_INDEX index);
254 #else
255  inline void R_Reprotect(SEXP node, PROTECT_INDEX index)
256  {
257  CXXR::ProtectStack::reprotect(node, index);
258  }
259 #endif
260 
275  void Rf_ppsRestoreSize(size_t new_size);
276 
285  size_t Rf_ppsSize();
286 
293 #ifndef __cplusplus
294  SEXP Rf_protect(SEXP node);
295 #else
296  inline SEXP Rf_protect(SEXP node)
297  {
299  return node;
300  }
301 #endif
302 
313 #ifndef __cplusplus
314  void Rf_unprotect(int count);
315 #else
316  inline void Rf_unprotect(int count)
317  {
319  }
320 #endif
321 
333 #ifndef __cplusplus
334  void Rf_unprotect_ptr(SEXP node);
335 #else
336  inline void Rf_unprotect_ptr(SEXP node)
337  {
339  }
340 #endif
341 
342 #ifdef __cplusplus
343 } /* extern "C" */
344 #endif
345 
346 #endif // PROTECTSTACK_H