CXXR (C++ R) API
ExternalPointer.h
Go to the documentation of this file.
1 /*CXXR $Id: ExternalPointer.h 1351 2013-03-08 15:12:28Z 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  * Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka
20  * Copyright (C) 1999-2006 The R Development Core Team.
21  *
22  * This program is free software; you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation; either version 2.1 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program; if not, a copy is available at
34  * http://www.r-project.org/Licenses/
35  */
36 
41 #ifndef EXTERNALPOINTER_H
42 #define EXTERNALPOINTER_H
43 
44 #include "CXXR/RObject.h"
45 
46 #ifdef __cplusplus
47 
48 #include <boost/serialization/access.hpp>
49 #include <boost/serialization/base_object.hpp>
50 #include <boost/serialization/nvp.hpp>
51 
52 #include "CXXR/SEXP_downcast.hpp"
53 
54 namespace CXXR {
73  class ExternalPointer : public RObject {
74  public:
83  explicit ExternalPointer(void* ptr = 0, RObject* tag = 0,
84  RObject* prot = 0)
85  : RObject(EXTPTRSXP), m_ptr(ptr), m_tag(tag), m_protege(prot)
86  {}
87 
93  const RObject* protege() const
94  {
95  return m_protege;
96  }
97 
104  {
105  return m_protege;
106  }
107 
112  const void* ptr() const
113  {
114  return m_ptr;
115  }
116 
121  void* ptr()
122  {
123  return m_ptr;
124  }
125 
131  void setProtege(RObject* prot)
132  {
133  m_protege = prot;
134  }
135 
140  void setPtr(void* ptr)
141  {
142  m_ptr = ptr;
143  }
144 
151  {
152  m_tag = tag;
153  }
154 
159  static const char* staticTypeName()
160  {
161  return "externalptr";
162  }
163 
168  const RObject* tag() const
169  {
170  return m_tag;
171  }
172 
178  {
179  return m_tag;
180  }
181 
182  // Virtual function of RObject:
183  const char* typeName() const;
184 
185  // Virtual function of GCNode:
186  void visitReferents(const_visitor* v) const;
187  protected:
188  // Declared protected to ensure that ExternalPointer objects are
189  // allocated only using 'new':
190  ~ExternalPointer() {}
191 
192  // Virtual function of GCNode:
193  void detachReferents();
194  private:
195  friend class boost::serialization::access;
196 
197  void* m_ptr;
198  GCEdge<> m_tag;
199  GCEdge<> m_protege;
200 
201  // Not implemented yet. Declared to prevent
202  // compiler-generated versions:
204  ExternalPointer& operator=(const ExternalPointer&);
205 
206  // FIXME: note that this doesn't at present serialise the
207  // encapsulated pointer!
208  template <class Archive>
209  void serialize(Archive& ar, const unsigned int version);
210  };
211 } // namespace CXXR
212 
213 BOOST_CLASS_EXPORT_KEY(CXXR::ExternalPointer)
214 
215 // ***** Implementation of non-inlined templated members *****
216 
217 template <class Archive>
218 void CXXR::ExternalPointer::serialize(Archive& ar, const unsigned int version)
219 {
220  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(RObject);
221  GCNPTR_SERIALIZE(ar, m_tag);
222  GCNPTR_SERIALIZE(ar, m_protege);
223 }
224 
225 extern "C" {
226 #endif /* __cplusplus */
227 
239  SEXP R_MakeExternalPtr(void *p, SEXP tag, SEXP prot);
240 
247 #ifndef __cplusplus
248  void *R_ExternalPtrAddr(SEXP s);
249 #else
250  inline void *R_ExternalPtrAddr(SEXP s)
251  {
253  = *CXXR::SEXP_downcast<CXXR::ExternalPointer*>(s);
254  return ep.ptr();
255  }
256 #endif
257 
264 #ifndef __cplusplus
266 #else
268  {
270  = *CXXR::SEXP_downcast<CXXR::ExternalPointer*>(s);
271  return ep.tag();
272  }
273 #endif
274 
281 #ifndef __cplusplus
283 #else
285  {
287  = *CXXR::SEXP_downcast<CXXR::ExternalPointer*>(s);
288  return ep.protege();
289  }
290 #endif
291 
298  void R_SetExternalPtrAddr(SEXP s, void *p);
299 
304 #ifndef __cplusplus
305  void R_ClearExternalPtr(SEXP s);
306 #else
307  inline void R_ClearExternalPtr(SEXP s)
308  {
309  R_SetExternalPtrAddr(s, 0);
310  }
311 #endif
312 
320  void R_SetExternalPtrTag(SEXP s, SEXP tag);
321 
330 
331 #ifdef __cplusplus
332 }
333 #endif
334 
335 #endif /* EXTERNALPOINTER_H */