Dev Essential
Loading...
Searching...
No Matches
access_optional.h
Go to the documentation of this file.
1
14
15#ifndef DD_DDL_ACCESS_OPTIONAL_H_INCLUDED
16#define DD_DDL_ACCESS_OPTIONAL_H_INCLUDED
17
18#include <stddef.h>
19#include <utility>
20
21namespace ddl {
22namespace dd {
23namespace utility {
24
30template <typename T>
31struct Optional {
41 Optional(Optional&&) = default;
46 Optional(const Optional&) = default;
58 Optional& operator=(const Optional&) = default;
63 ~Optional() = default;
64
66 typedef T value_type;
67
73 Optional(const value_type& value) : _valid(true), _value(value)
74 {
75 }
76
83 {
84 _valid = true;
85 _value = value;
86 return *this;
87 }
88
94 Optional(value_type&& value) : _valid(true), _value(value)
95 {
96 }
97
105 {
106 _valid = true;
107 _value = value;
108 return *this;
109 }
110
117 operator bool() const
118 {
119 return _valid;
120 }
121
127 operator value_type() const
128 {
129 return _value;
130 }
131
138 {
139 return _value;
140 }
141
148 {
149 return std::move(_value);
150 }
151
157 const value_type& operator*() const&
158 {
159 return _value;
160 }
161
167 const value_type&& operator*() const&&
168 {
169 return std::move(_value);
170 }
171
176 void reset()
177 {
178 _valid = false;
179 }
180
186 {
187 return _value;
188 }
189
195 {
196 return std::move(_value);
197 }
198
204 {
205 return _value;
206 }
207
212 value_type get() const&&
213 {
214 return std::move(_value);
215 }
216
223 bool operator==(const Optional& other) const
224 {
225 if (_valid) {
226 return other._valid && (_value == other._value);
227 }
228 else {
229 return !(other._valid);
230 }
231 }
232
240 bool operator!=(const Optional& other) const
241 {
242 return !operator==(other);
243 }
244
245 bool _valid = false;
248};
249
250} // namespace utility
251} // namespace dd
252namespace utility {
258} // namespace utility
259} // namespace ddl
260
261#endif // DD_DDL_ACCESS_OPTIONAL_H_INCLUDED
definition of the old compatibility utility namespace
Definition datamodel_keyvalue.h:160
definition of the dd namespace
Definition datamodel_base.h:26
Optional< size_t > OptionalSize
Definition access_optional.h:257
definition of the ddl namespace
Definition codec.h:21
An optional template as long as the std::optional is not available here.
Definition access_optional.h:31
Optional()
default CTOR
Definition access_optional.h:36
Optional & operator=(const value_type &value)
copy assignment operator
Definition access_optional.h:82
Optional(const value_type &value)
special copy CTOR
Definition access_optional.h:73
value_type get() const &&
steal value type
Definition access_optional.h:212
void reset()
invalidates the value
Definition access_optional.h:176
Optional(value_type &&value)
move CTOR
Definition access_optional.h:94
Optional & operator=(const Optional &)=default
copy assignment operator
Optional(Optional &&)=default
move CTOR
bool operator!=(const Optional &other) const
non equality operator.
Definition access_optional.h:240
value_type get() const &
return the value type as copy
Definition access_optional.h:203
bool _valid
Definition access_optional.h:245
value_type get() &
return the value type as copy
Definition access_optional.h:185
const value_type & operator*() const &
referenced access of the value
Definition access_optional.h:157
Optional & operator=(Optional &&)=default
move assignment operator
Optional & operator=(value_type &&value)
move assignment operator
Definition access_optional.h:104
const value_type && operator*() const &&
referenced access of the value
Definition access_optional.h:167
bool operator==(const Optional &other) const
equality operator.
Definition access_optional.h:223
value_type & operator*() &
referenced access to the value
Definition access_optional.h:137
value_type && get() &&
steal value type
Definition access_optional.h:194
value_type _value
Definition access_optional.h:247
value_type && operator*() &&
referenced access to the value
Definition access_optional.h:147
size_t value_type
Definition access_optional.h:66
Optional(const Optional &)=default
copy CTOR