ADTF
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 #include <stddef.h>
19 
20 namespace ddl {
21 namespace dd {
22 namespace utility {
23 
29 template <typename T>
30 struct Optional {
35  Optional(){};
40  Optional(Optional&&) = default;
45  Optional(const Optional&) = default;
51  Optional& operator=(Optional&&) = default;
57  Optional& operator=(const Optional&) = default;
62  ~Optional() = default;
63 
65  typedef T value_type;
66 
72  Optional(const value_type& value) : _valid(true), _value(value)
73  {
74  }
81  Optional& operator=(const value_type& value)
82  {
83  _valid = true;
84  _value = value;
85  return *this;
86  }
87 
93  Optional(value_type&& value) : _valid(true), _value(value)
94  {
95  }
96 
104  {
105  _valid = true;
106  _value = value;
107  return *this;
108  }
109 
116  operator bool() const
117  {
118  return _valid;
119  }
120 
126  operator value_type() const
127  {
128  return _value;
129  }
130 
137  {
138  return _value;
139  }
140 
146  const value_type operator*() const
147  {
148  return _value;
149  }
150 
155  void reset()
156  {
157  _valid = false;
158  }
164  value_type get() const
165  {
166  return _value;
167  }
175  bool operator==(const Optional& other) const
176  {
177  if (_valid) {
178  return other._valid && (_value == other._value);
179  }
180  else {
181  return !(other._valid);
182  }
183  }
184 
192  bool operator!=(const Optional& other) const
193  {
194  return !operator==(other);
195  }
197  bool _valid = false;
200 };
201 
202 } // namespace utility
203 } // namespace dd
204 namespace utility {
205  using ::ddl::dd::utility::Optional;
209  using OptionalSize = Optional<size_t>;
210 } // namespace utility
211 } // namespace ddl
212 
213 #endif // DD_DDL_ACCESS_OPTIONAL_H_INCLUDED
Optional< size_t > OptionalSize
optional size definition.
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
bool _valid
is valid or not
value_type _value
the value if it is valid