CXXR (C++ R) API
GCNode_PtrS11n.hpp
Go to the documentation of this file.
1 /*CXXR $Id: GCNode_PtrS11n.hpp 1391 2013-06-19 10:27:40Z 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 GCNODE_PTRS11N_HPP
41 #define GCNODE_PTRS11N_HPP 1
42 
43 #include <boost/mpl/eval_if.hpp>
44 #include <boost/mpl/identity.hpp>
45 
46 #include "CXXR/GCNode.hpp"
47 #include "CXXR/S11nScope.hpp"
48 
49 namespace CXXR {
114  private:
115  friend class GCNode;
116 
117  template <class Ptr>
118  struct Payload {
119  typedef typename Ptr::type type;
120  };
121 
122  template <class T>
123  struct Payload<T*> {
124  typedef T type;
125  };
126 
127  template<class Archive>
128  struct Loader {
129  template <typename Ptr>
130  static void invoke(Archive& ar, Ptr& ptr, const char* name)
131  {
132  GCNode* target;
133  ar >> boost::serialization::make_nvp(name, target);
134  if (target) {
135  GCNode* reloc = S11nScope::relocate(target);
136  if (reloc)
137  target = reloc;
138  // Note that the target may already have been
139  // exposed, e.g. as a result of deserialising
140  // another pointer pointing to it.
141  else if (!target->isExposed())
142  target->expose();
143  }
144  ptr = static_cast<typename Payload<Ptr>::type*>(target);
145  }
146  };
147 
148  template<class Archive>
149  struct Saver {
150  template <typename Ptr>
151  static void invoke(Archive& ar, const Ptr& ptr, const char* name)
152  {
153  const GCNode* target
154  = static_cast<typename Payload<Ptr>::type*>(ptr);
155  ar << boost::serialization::make_nvp(name, target);
156  }
157  };
158  public:
185  template<class Archive, typename Ptr>
186  static void invoke(Archive& ar, Ptr& ptr, const char* name)
187  {
188  using namespace boost::mpl;
189  typedef typename eval_if<typename Archive::is_saving,
190  identity<Saver<Archive> >,
191  identity<Loader<Archive> > >::type typex;
192  typex::invoke(ar, ptr, name);
193  }
194  };
195 
196 } // namespace CXXR
197 
201 #define GCNPTR_SERIALIZE(ar, name) \
202  GCNode::PtrS11n::invoke(ar, name, #name )
203 
204 #endif // GCNODE_PTRS11N_HPP