Avionics
Dropship Simulator
Nullable.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 template <typename T>
6 class Nullable
7 {
8 private:
9  T m_value;
10  bool m_isSet;
11 
12  void set(T value)
13  {
14  m_value = value;
15  m_isSet = true;
16  }
17 
18 public:
19  Nullable() : m_value(), m_isSet(false)
20  {
21  }
22 
23  Nullable(T value) : m_value(value), m_isSet(true)
24  {
25  }
26 
27  Nullable(const Nullable& other) : m_value(other.m_value), m_isSet(other.m_isSet)
28  {
29  }
30 
32  /*friend void swap(Nullable& a, Nullable& b)
33  {
34  std::swap(a.m_isSet, b.m_isSet);
35  std::swap(a.m_value, b.m_value);
36  }
37 
39  Nullable& operator=(Nullable other)
40  {
41  swap(*this, other);
42  return *this;
43  }*/
44 
45  T operator=(T value)
46  {
47  set(value);
48  return m_value;
49  }
50 
51  bool operator==(const T value)
52  {
53  return m_isSet && value == m_value;
54  }
55 
56  bool operator==(const Nullable& other)
57  {
58  if (other.is_set() != m_isSet ||
59  (other.is_set() && m_isSet && other.get() != m_value))
60  return false;
61 
62  return true;
63  }
64 
65  bool operator!=(const Nullable& other)
66  {
67  if (other.is_set() != m_isSet ||
68  (other.is_set() && m_isSet && other.get() != m_value))
69  return true;
70 
71  return false;
72  }
73 
74  operator T() const
75  {
76  return get();
77  }
78 
79  T get() const
80  {
81  if (m_isSet) return m_value;
82 
83  throw std::logic_error("Value of Nullable is not set.");
84  }
85 
86  bool is_set() const
87  {
88  return m_isSet;
89  }
90 
91  void reset()
92  {
93  m_isSet = false;
94  }
95 };
Nullable(T value)
Definition: Nullable.h:23
Nullable(const Nullable &other)
Definition: Nullable.h:27
T operator=(T value)
Used in copy-constructor below, see comment on copy constructor.
Definition: Nullable.h:45
bool operator!=(const Nullable &other)
Definition: Nullable.h:65
Nullable()
Definition: Nullable.h:19
void reset()
Definition: Nullable.h:91
bool operator==(const Nullable &other)
Definition: Nullable.h:56
T m_value
Definition: Nullable.h:9
bool is_set() const
Definition: Nullable.h:86
bool operator==(const T value)
Definition: Nullable.h:51
bool m_isSet
Definition: Nullable.h:10
T get() const
Definition: Nullable.h:79