15 #ifndef _FASTCDR_XCDR_OPTIONAL_HPP_
16 #define _FASTCDR_XCDR_OPTIONAL_HPP_
21 #include "detail/optional.hpp"
22 #include "../exceptions/BadOptionalAccessException.hpp"
57 const T& val) noexcept
59 ::new(&storage_.val_)T(val);
60 storage_.engaged_ =
true;
67 ::new(&storage_.val_)T(std::move(val));
68 storage_.engaged_ =
true;
75 if (val.storage_.engaged_)
77 ::new(&storage_.val_)T(val.storage_.val_);
78 storage_.engaged_ =
true;
86 if (val.storage_.engaged_)
88 ::new(&storage_.val_)T(std::move(val.storage_.val_));
89 storage_.engaged_ =
true;
95 #if defined(__GNUC__) && __GNUC__ >= 12
110 storage_.val_.T(std::forward<Args>(_args)...);
111 storage_.engaged_ =
true;
121 bool initial_engaged =
false)
123 if (storage_.engaged_)
127 storage_.engaged_ = initial_engaged;
128 if (storage_.engaged_)
130 ::new(&storage_.val_)T();
142 if (!storage_.engaged_)
148 return storage_.val_;
159 if (!storage_.engaged_)
165 return storage_.val_;
176 if (!storage_.engaged_)
182 return std::move(storage_.val_);
193 if (!storage_.engaged_)
199 return std::move(storage_.val_);
209 return storage_.engaged_;
215 #if defined(__GNUC__) && __GNUC__ >= 12
221 storage_.engaged_ = opt.storage_.engaged_;
222 if (opt.storage_.engaged_)
224 ::new(&storage_.val_)T(opt.storage_.val_);
232 #if defined(__GNUC__) && __GNUC__ >= 12
238 storage_.engaged_ = opt.storage_.engaged_;
239 if (opt.storage_.engaged_)
241 ::new(&storage_.val_)T(std::move(opt.storage_.val_));
251 ::new(&storage_.val_)T(val);
252 storage_.engaged_ =
true;
261 ::new(&storage_.val_)T(std::move(val));
262 storage_.engaged_ =
true;
278 return opt_val.storage_.engaged_ == storage_.engaged_ &&
279 (storage_.engaged_ ? opt_val.storage_.val_ == storage_.val_ :
true);
298 return storage_.val_;
310 return storage_.val_;
322 return std::move(storage_.val_);
334 return std::move(storage_.val_);
346 return std::addressof(storage_.val_);
358 return std::addressof(storage_.val_);
362 explicit operator bool() const noexcept
364 return storage_.engaged_;
This class is thrown as an exception when accessing the value of a null optional.
Definition: BadOptionalAccessException.hpp:28
static const char *const BAD_OPTIONAL_ACCESS_MESSAGE_DEFAULT
Default message used in the library.
Definition: BadOptionalAccessException.hpp:78
This class template manages an optional contained value, i.e.
Definition: optional.hpp:47
bool operator!=(const optional &opt_val) const
Compares optional values.
Definition: optional.hpp:283
void reset(bool initial_engaged=false)
Reset the state of the optional.
Definition: optional.hpp:120
optional()=default
Default constructor.
const T && value() const &&
Returns the contained value.
Definition: optional.hpp:191
optional(optional< T > &&val) noexcept
Move constructor.
Definition: optional.hpp:83
optional & operator=(const optional &opt)
Assigns content from an optional.
Definition: optional.hpp:213
optional(const optional< T > &val) noexcept
Copy constructor.
Definition: optional.hpp:72
const T && operator*() const &&noexcept
Accesses the contained value.
Definition: optional.hpp:332
bool has_value() const
Checks whether the optional contains a value.
Definition: optional.hpp:207
T && value() &&
Returns the contained value.
Definition: optional.hpp:174
T type
Definition: optional.hpp:50
optional(const T &val) noexcept
Copy constructor from an instance of the templated class.
Definition: optional.hpp:56
const T & value() const &
Returns the contained value.
Definition: optional.hpp:157
T & operator*() &noexcept
Accesses the contained value.
Definition: optional.hpp:296
bool operator==(const optional &opt_val) const
Compares optional values.
Definition: optional.hpp:275
T & value() &
Returns the contained value.
Definition: optional.hpp:140
T * operator->() noexcept
Accesses the contained value.
Definition: optional.hpp:344
void emplace(Args &&... _args)
Constructs the contained value in-place.
Definition: optional.hpp:106
~optional()=default
Destructor.
optional(T &&val) noexcept
Move constructor from an instance of the templated class.
Definition: optional.hpp:64
static constexpr nullopt_t nullopt
nullopt is a constant of type nullopt_t that is used to indicate optional type with uninitialized sta...
Definition: optional.hpp:40
Definition: fixed_size_string.hpp:32
Definition: optional.hpp:25
An empty class type used to indicate optional type with uninitialized state.
Definition: optional.hpp:29
constexpr nullopt_t(int)
Definition: optional.hpp:30