CXXR (C++ R)
GCEdge.hpp
Go to the documentation of this file.
1 /*CXXR $Id: GCEdge.hpp 1353 2013-03-18 16:59:38Z 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 Lesser 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 Lesser General Public License
31  * along with this program; if not, a copy is available at
32  * http://www.r-project.org/Licenses/
33  */
34 
40 #ifndef GCEDGE_HPP
41 #define GCEDGE_HPP 1
42 
43 #include "CXXR/GCNode.hpp"
44 #include "CXXR/GCStackRoot.hpp"
45 
46 namespace CXXR {
47  class RObject;
48 
51  class GCEdgeBase {
52  public:
55  void detach();
56  protected:
57  GCEdgeBase()
58  : m_target(0)
59  {}
60 
67  : m_target(target)
68  {
69  GCNode::maybeCheckExposed(m_target);
70  GCNode::incRefCount(m_target);
71  }
72 
77  GCEdgeBase(const GCEdgeBase& source)
78  : m_target(source.m_target)
79  {
80  GCNode::incRefCount(m_target);
81  }
82 
83  ~GCEdgeBase()
84  {
85  GCNode::decRefCount(m_target);
86  }
87 
92  const GCNode* target() const
93  {
94  return m_target;
95  }
96  protected:
102  void retarget(const GCNode* newtarget)
103  {
104  GCEdgeBase tmp(newtarget);
105  std::swap(m_target, tmp.m_target);
106  }
107  private:
108  const GCNode* m_target;
109 
110  static void abortIfNotExposed(const GCNode* target);
111  };
112 
131  template <class T = RObject>
132  class GCEdge : public GCEdgeBase {
133  public:
134  typedef T type;
135 
136  GCEdge()
137  {}
138 
144  explicit GCEdge(T* target)
145  : GCEdgeBase(target)
146  {}
147 
153  GCEdge(const GCEdge<T>& source)
154  : GCEdgeBase(source)
155  {}
156 
157  GCEdge<T>& operator=(const GCEdge<T>& source)
158  {
159  retarget(source);
160  return *this;
161  }
162 
163  GCEdge<T>& operator=(T* newtarget)
164  {
165  retarget(newtarget);
166  return *this;
167  }
168 
169  T* operator->() const
170  {
171  return get();
172  }
173 
178  operator T*() const
179  {
180  return get();
181  }
182 
187  T* get() const
188  {
189  return static_cast<T*>(const_cast<GCNode*>(target()));
190  }
191  };
192 } // namespace CXXR
193 
194 #endif // GCEDGE_HPP