CXXR (C++ R)
NAAugment.hpp
Go to the documentation of this file.
1 /*CXXR $Id: NAAugment.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 
22 #ifndef NAAUGMENT_HPP
23 #define NAAUGMENT_HPP 1
24 
25 #include "CXXR/ElementTraits.hpp"
26 
27 namespace CXXR {
40  template <typename T> class NAAugment {
41  public:
50  : m_na(true)
51  {}
52 
60  template <typename U>
61  explicit NAAugment(const U& value)
62  : m_value(value), m_na(false)
63  {}
64 
72  template <typename U>
74  {
75  m_value = value;
76  m_na = false;
77  return *this;
78  }
79 
80  operator T&()
81  {
82  return m_value;
83  }
84 
85  operator const T&() const
86  {
87  return m_value;
88  }
89 
95  bool isNA() const
96  {
97  return m_na;
98  }
99 
105  static const NAAugment<T>& NA()
106  {
107  static NAAugment<T> na;
108  return na;
109  }
110 
122  template <class Archive>
123  void serialize(Archive& ar, const unsigned int file_version);
124 
135  T& value()
136  {
137  return m_value;
138  }
139 
150  const T& value() const
151  {
152  return m_value;
153  }
154  private:
155  T m_value;
156  bool m_na;
157  };
158 
159  // ***** Implementation of non-inlined templated members *****
160 
161  template <class T>
162  template <class Archive>
164  const unsigned int file_version)
165  {
166  ar & BOOST_SERIALIZATION_NVP(m_value);
167  ar & BOOST_SERIALIZATION_NVP(m_na);
168  }
169 
170  // ***** Template specializations *****
171  namespace ElementTraits {
172  template <class T>
173  struct Data<NAAugment<T> > {
174  typedef T type;
175 
176  static const type& get(const NAAugment<T>& t)
177  {
178  return t.value();
179  }
180  };
181 
182  template <class T>
183  struct MustConstruct<NAAugment<T> > : boost::mpl::true_
184  {};
185 
186  template <class T>
187  struct MustDestruct<NAAugment<T> > : boost::mpl::true_
188  {};
189 
190  template <class T>
191  struct NAFunc<NAAugment<T> > {
192  const NAAugment<T>& operator()() const
193  {
194  return NAAugment<T>::NA();
195  }
196  };
197 
198  template <class T>
199  struct IsNA<NAAugment<T> > {
200  bool operator()(const NAAugment<T>& t) const
201  {
202  return t.isNA();
203  }
204  };
205  } // namespace ElementTraits
206 } // namespace CXXR
207 
208 #endif // NAAUGMENT_HPP