nullable.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #ifndef NUKLEI_NULLABLE_H
8 #define NUKLEI_NULLABLE_H
9 
10 #include <nuklei/Definitions.h>
11 
12 //+//////////////////////////////////////////////////////////////////////////+//
13 //+//////// Obsolete -- use boost::optional and boost::none instead /////////+//
14 //+//////////////////////////////////////////////////////////////////////////+//
15 
16 
17 namespace nuklei {
18 
19  struct undefined {};
20 
21  /**
22  * @brief Obsolete -- use boost::optional and boost::none instead
23  */
24  template<typename T>
25  struct nullable
26  {
27  typedef std::pair<bool, T> container_t;
28 
29  nullable() : container_(false, T()) {}
30  nullable(const undefined&) : container_(false, T()) {}
31  nullable(const T& element) : container_(true, element) {}
32  nullable(const nullable& n) : container_(n.container_) {}
33 
34  void assertConsistency() const
35  {
36  NUKLEI_TRACE_BEGIN();
37  if (isDefined()) get().assertConsistency();
38  NUKLEI_TRACE_END();
39  }
40 
41  nullable& operator=(const T& element)
42  { set(element); return *this; }
43 
44  // These are neat, but may make things hard to read.
45  // Operator * is good enough.
46  //operator T& () { return get(); }
47  //operator const T& () const { return get(); }
48 
49  T& operator* () { return get(); }
50  const T& operator* () const { return get(); }
51 
52  T* operator-> () { return &get(); }
53  const T* operator-> () const { return &get(); }
54 
55  bool isDefined() const { return container_.first; }
56 
57  // The following should work, even for POD types (see 8.5).
58  // However this doesn't seem like common practice, and I'm not
59  // sure if it will behave the same in all compilers...
60  //void define() { set(T()); }
61 
62  T& get()
63  { NUKLEI_ASSERT(container_.first); return container_.second; }
64  const T& get() const
65  { NUKLEI_ASSERT(container_.first); return container_.second; }
66 
67  void set(const T& element) { container_ = container_t(true, element); }
68 
69  void clear() { container_ = container_t(false, T()); }
70 
71  private:
72  container_t container_;
73 
74  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
75  template<class Archive>
76  void serialize(Archive &ar, const unsigned int version)
77  {
78  ar & NUKLEI_SERIALIZATION_NVP(container_);
79  }
80  };
81 
82 }
83 
84 #endif
85 
Definition: nullable.h:19
Public namespace.
Definition: Color.cpp:9
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
Obsolete – use boost::optional and boost::none instead.
Definition: nullable.h:25
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.