CXXR (C++ R)
RHandle.hpp
Go to the documentation of this file.
1 /*CXXR $Id: RHandle.hpp 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-2007 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 
42 #ifndef RHANDLE_HPP
43 #define RHANDLE_HPP
44 
45 #include "CXXR/ElementTraits.hpp"
46 #include "CXXR/GCEdge.hpp"
47 
48 namespace CXXR {
49  class RObject;
50 
64  template <class T = RObject>
65  class RHandle : public GCEdge<T> {
66  public:
67  RHandle()
68  {}
69 
75  explicit RHandle(T* target)
76  : GCEdge<T>(target)
77  {}
78 
90  RHandle(const RHandle<T>& pattern)
91  : GCEdge<T>(cloneOrSelf(pattern))
92  {}
93 
97  {
98  GCEdge<T>::operator=(cloneOrSelf(source));
99  return *this;
100  }
101 
107  RHandle<T>& operator=(T* newtarget)
108  {
109  GCEdge<T>::operator=(newtarget);
110  return *this;
111  }
112  private:
113  static T* cloneOrSelf(T*);
114  }; // class template RHandle
115 
116  // Partial specializations of ElementTraits:
117  namespace ElementTraits {
118  template <class T>
119  struct DetachReferents<RHandle<T> >
120  : std::unary_function<T, void> {
121  void operator()(RHandle<T>& t) const
122  {
123  t = 0;
124  }
125  };
126 
127  template <class T>
128  struct HasReferents<RHandle<T> > : boost::mpl::true_
129  {};
130 
131  template <class T>
132  struct MustConstruct<RHandle<T> > : boost::mpl::true_
133  {};
134 
135  template <class T>
136  struct MustDestruct<RHandle<T> > : boost::mpl::true_
137  {};
138 
139  template <class T>
140  struct Serialize<RHandle<T> > {
141  template <class Archive>
142  void operator()(Archive& ar, RHandle<T>& item)
143  {
144  GCNPTR_SERIALIZE(ar, item);
145  }
146  };
147 
148  template <class T>
149  class VisitReferents<RHandle<T> >
150  : public std::unary_function<T, void> {
151  public:
152  VisitReferents(GCNode::const_visitor* v)
153  : m_v(v)
154  {}
155 
156  void operator()(const RHandle<T>& t) const
157  {
158  if (t.get())
159  (*m_v)(t);
160  }
161  private:
162  GCNode::const_visitor* m_v;
163  };
164 
165  template <class T>
166  struct NAFunc<RHandle<T> > {
167  const RHandle<T>& operator()() const
168  {
169  static RHandle<T> na;
170  return na;
171  }
172  };
173 
174  template <class T>
175  struct IsNA<RHandle<T> > {
176  bool operator()(const RHandle<T>& t) const
177  {
178  return false;
179  }
180  };
181  } // namespace ElementTraits
182 } // namespace CXXR
183 
184 
185 // ***** Implementations of non-inlined templated functions. *****
186 
187 template <class T>
188 T* CXXR::RHandle<T>::cloneOrSelf(T* pattern)
189 {
190  T* t = pattern ? static_cast<T*>(pattern->clone()) : 0;
191  return (t ? t : pattern);
192 }
193 
194 #endif // RHANDLE_HPP