ADTF  3.18.2
dd_access_optional.h
Go to the documentation of this file.
1 
15 #ifndef DD_DDL_ACCESS_OPTIONAL_H_INCLUDED
16 #define DD_DDL_ACCESS_OPTIONAL_H_INCLUDED
17 
18 namespace ddl {
19 namespace dd {
20 namespace utility {
21 
27 template <typename T>
28 struct Optional {
33  Optional(){};
38  Optional(Optional&&) = default;
43  Optional(const Optional&) = default;
49  Optional& operator=(Optional&&) = default;
55  Optional& operator=(const Optional&) = default;
60  ~Optional() = default;
61 
63  typedef T value_type;
64 
70  Optional(const value_type& value) : _valid(true), _value(value)
71  {
72  }
79  Optional& operator=(const value_type& value)
80  {
81  _valid = true;
82  _value = value;
83  return *this;
84  }
85 
91  Optional(value_type&& value) : _valid(true), _value(value)
92  {
93  }
94 
102  {
103  _valid = true;
104  _value = value;
105  return *this;
106  }
107 
114  operator bool() const
115  {
116  return _valid;
117  }
118 
124  operator value_type() const
125  {
126  return _value;
127  }
128 
135  {
136  return _value;
137  }
138 
144  const value_type operator*() const
145  {
146  return _value;
147  }
148 
153  void reset()
154  {
155  _valid = false;
156  }
162  value_type get() const
163  {
164  return _value;
165  }
173  bool operator==(const Optional& other) const
174  {
175  if (_valid) {
176  return other._valid && (_value == other._value);
177  }
178  else {
179  return !(other._valid);
180  }
181  }
182 
190  bool operator!=(const Optional& other) const
191  {
192  return !operator==(other);
193  }
195  bool _valid = false;
198 };
199 
200 } // namespace utility
201 } // namespace dd
202 } // namespace ddl
203 
204 #endif // DD_DDL_ACCESS_OPTIONAL_H_INCLUDED
An optional template as long as the std::optional is not available here.
Optional & operator=(Optional &&)=default
move assignment operator
Optional & operator=(const Optional &)=default
copy assignment operator
Optional & operator=(value_type &&value)
move assignment operator
T value_type
defintion of the value type of this optional
Optional(const value_type &value)
special copy CTOR
Optional(const Optional &)=default
copy CTOR
Optional & operator=(const value_type &value)
copy assignment operator
Optional(value_type &&value)
move CTOR
Optional(Optional &&)=default
move CTOR
value_type & operator*()
referenced access to the value
const value_type operator*() const
dereferenced access of the value
bool operator==(const Optional &other) const
equality operator.
value_type get() const
return the value type as copy
bool operator!=(const Optional &other) const
non equality operator.
void reset()
invalidates the value
value_type _value
the value if it is valid