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;
105 storage_.val_.T(std::forward<Args>(_args)...);
106 storage_.engaged_ =
true;
116 bool initial_engaged =
false)
118 if (storage_.engaged_)
122 storage_.engaged_ = initial_engaged;
123 if (storage_.engaged_)
125 ::new(&storage_.val_)T();
137 if (!storage_.engaged_)
143 return storage_.val_;
154 if (!storage_.engaged_)
160 return storage_.val_;
171 if (!storage_.engaged_)
177 return std::move(storage_.val_);
188 if (!storage_.engaged_)
194 return std::move(storage_.val_);
204 return storage_.engaged_;
212 storage_.engaged_ = opt.storage_.engaged_;
213 if (opt.storage_.engaged_)
215 ::new(&storage_.val_)T(opt.storage_.val_);
225 storage_.engaged_ = opt.storage_.engaged_;
226 if (opt.storage_.engaged_)
228 ::new(&storage_.val_)T(std::move(opt.storage_.val_));
238 ::new(&storage_.val_)T(val);
239 storage_.engaged_ =
true;
248 ::new(&storage_.val_)T(std::move(val));
249 storage_.engaged_ =
true;
265 return opt_val.storage_.engaged_ == storage_.engaged_ &&
266 (storage_.engaged_ ? opt_val.storage_.val_ == storage_.val_ :
true);
285 return storage_.val_;
297 return storage_.val_;
309 return std::move(storage_.val_);
321 return std::move(storage_.val_);
333 return std::addressof(storage_.val_);
345 return std::addressof(storage_.val_);
349 explicit operator bool() const noexcept
351 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
bool operator!=(const optional &opt_val) const
Compares optional values.
Definition optional.hpp:270
void reset(bool initial_engaged=false)
Reset the state of the optional.
Definition optional.hpp:115
optional()=default
Default constructor.
T & value() &
Returns the contained value.
Definition optional.hpp:135
optional(optional< T > &&val) noexcept
Move constructor.
Definition optional.hpp:83
optional(const optional< T > &val) noexcept
Copy constructor.
Definition optional.hpp:72
T && value() &&
Returns the contained value.
Definition optional.hpp:169
bool has_value() const
Checks whether the optional contains a value.
Definition optional.hpp:202
T type
Definition optional.hpp:50
optional(const T &val) noexcept
Copy constructor from an instance of the templated class.
Definition optional.hpp:56
T & operator*() &noexcept
Accesses the contained value.
Definition optional.hpp:283
bool operator==(const optional &opt_val) const
Compares optional values.
Definition optional.hpp:262
T * operator->() noexcept
Accesses the contained value.
Definition optional.hpp:331
const T & value() const &
Returns the contained value.
Definition optional.hpp:152
void emplace(Args &&... _args)
Constructs the contained value in-place.
Definition optional.hpp:101
~optional()=default
Destructor.
const T && value() const &&
Returns the contained value.
Definition optional.hpp:186
optional & operator=(const optional &opt)
Assigns content from an optional.
Definition optional.hpp:208
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 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