17 #ifndef Fw_Optional_HPP 18 #define Fw_Optional_HPP 21 #include <type_traits> 44 static_assert(std::is_trivially_copyable<T>::value,
"Fw::Optional only supports trivially copyable types");
55 constexpr
Optional() : m_val(), m_engaged(false) {}
61 constexpr
Optional(
const T& t) : m_val(t), m_engaged(true) {}
66 *
this = other.
value();
99 T
value_or(
const T& default_value)
const {
return m_engaged ? m_val : default_value; }
129 *
this = other.
value();
154 if (m_engaged != other.m_engaged) {
160 return m_val == other.m_val;
bool operator==(const Optional &other) const
Equality comparison with another Optional.
constexpr Optional()
Construct an empty Optional.
T value_or(const T &default_value) const
constexpr None_t NONE
Global constant representing an absent/empty optional value.
~Optional()=default
Destructor (trivial — T must be trivially copyable)
constexpr Optional(const T &t)
Construct an Optional containing a value.
bool operator!=(const None_t &a) const
Compare with NONE sentinel (inequality)
bool operator!=(const Optional &other) const
Inequality comparison with another Optional.
constexpr Optional(const None_t &a)
Construct an empty Optional from NONE sentinel.
Optional & operator=(const Optional &other)
Copy assignment operator.
void reset()
Reset the Optional to an empty state.
Optional & operator=(const None_t &a)
Reset the Optional via NONE assignment.
Optional & operator=(const T &t)
Assign a value into the Optional.
Optional(const Optional &other)
Copy constructor.
Sentinel type representing the absence of a value in an Optional.
bool operator==(const None_t &a) const
Compare with NONE sentinel.
Implementation of malloc based allocator.
A type-safe container for an optional value.