CXXR (C++ R)
ElementTraits.hpp
Go to the documentation of this file.
1 /*CXXR $Id: ElementTraits.hpp 1375 2013-04-15 17:16:50Z 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 
40 #ifndef ELEMENTTRAITS_HPP
41 #define ELEMENTTRAITS_HPP 1
42 
43 #include <boost/mpl/bool.hpp>
44 #include "CXXR/GCNode.hpp"
45 
46 namespace CXXR {
54  namespace ElementTraits {
78  template <typename T>
79  struct Data {
83  typedef T type;
84 
92  static const type& get(const T& t)
93  {
94  return t;
95  }
96  }; // struct Data
97 
111  template <typename T>
112  inline const typename ElementTraits::Data<T>::type&
113  data(const T& t)
114  {
115  return Data<T>::get(t);
116  }
117 
127  template <typename T>
128  struct DetachReferents : std::unary_function<T, void> {
134  void operator()(T& t) const
135  {}
136  };
137 
153  template <typename T>
154  struct HasReferents : boost::mpl::false_
155  {};
156 
174  template <typename T>
175  struct MustConstruct : boost::mpl::false_
176  {};
177 
194  template <typename T>
195  struct MustDestruct : boost::mpl::false_
196  {};
197 
203  template <typename T>
204  struct Serialize {
217  template <class Archive>
218  void operator()(Archive& ar, T& item)
219  {
220  typename ElementTraits::Data<T>::type payload
221  = ElementTraits::data(item);
222  ar & boost::serialization::make_nvp("item", payload);
223  item = payload;
224  }
225  };
226 
237  template <typename T>
238  struct VisitReferents : std::unary_function<T, void> {
245  {}
246 
251  void operator()(const T& t) const
252  {}
253  };
254 
263  template <typename T>
264  struct NAFunc {
276  const T& operator()() const;
277  };
278 
284  template <typename T> struct IsNA {
295  bool operator()(const T& t) const
296  {
297  return t == NAFunc<T>()();
298  }
299  };
300 
307  template <typename T>
308  inline bool hasDistinctNA()
309  {
310  return isNA(NAFunc<T>()());
311  }
312  } // namespace ElementTraits
313 
330  template <typename T> bool isNA(const T& t)
331  {
332  return ElementTraits::IsNA<T>()(t);
333  }
334 
353  template <typename T> const T& NA()
354  {
355  return ElementTraits::NAFunc<T>()();
356  }
357 } // namespace CXXR;
358 
359 #endif // ELEMENTTRAITS_HPP