21 constexpr Optional() noexcept : mValuePtr{
nullptr}
27 mValuePtr =
new T{value};
32 mValuePtr =
new T{std::move(value)};
37 mValuePtr = other.
HasValue() ?
new T{*other.mValuePtr} :
nullptr;
41 std::is_nothrow_move_assignable<T>::value)
45 mValuePtr = other.mValuePtr;
46 other.mValuePtr =
nullptr;
65 mValuePtr =
new T{*other.mValuePtr};
72 std::is_nothrow_move_assignable<T>::value)
78 mValuePtr = other.mValuePtr;
79 other.mValuePtr =
nullptr;
85 template <
typename U = T>
86 constexpr Optional &operator=(
const U &value)
89 mValuePtr =
new T{
static_cast<const T &
>(value)};
94 template <
typename U = T>
95 constexpr Optional &operator=(U &&value)
98 mValuePtr =
new T{
static_cast<T &&
>(value)};
105 template <
typename... Args>
109 mValuePtr =
new T{args...};
115 std::is_nothrow_move_assignable<T>::value)
119 std::swap(mValuePtr, other.mValuePtr);
123 other.mValuePtr = mValuePtr;
128 mValuePtr = other.mValuePtr;
129 other.mValuePtr =
nullptr;
147 return mValuePtr !=
nullptr;
151 constexpr explicit operator bool() const noexcept
166 throw std::runtime_error(
"Optional contains no value.");
176 return std::move(*mValuePtr);
180 throw std::runtime_error(
"Optional contains no value.");
194 throw std::runtime_error(
"Optional contains no value.");
209 throw std::runtime_error(
"Optional contains no value.");
220 T *_result{mValuePtr};
223 return std::move(*_result);
227 throw std::runtime_error(
"Optional contains no value.");
235 template <
typename U>
244 return static_cast<T
>(defaultValue);
252 template <
typename U>
257 return std::move(*mValuePtr);
261 return static_cast<T
>(defaultValue);
267 template <
typename T>
272 if (lhs.HasValue() && rhs.HasValue())
274 _result = lhs.Value() == rhs.Value();
276 else if (!lhs.HasValue() && !rhs.HasValue())
289 template <
typename T>
294 if (lhs.HasValue() && rhs.HasValue())
296 _result = lhs.Value() != rhs.Value();
298 else if (!lhs.HasValue() && !rhs.HasValue())
A wrapper around a possible value.
Definition: optional.h:16
T && Value() &&
Get instance possible value.
Definition: optional.h:216
void Reset() noexcept
Reset the instance value.
Definition: optional.h:134
const T & operator*() const &
Definition: optional.h:158
constexpr bool HasValue() const noexcept
Indicate whether the instance has a value or not.
Definition: optional.h:145
T && operator*() &&
Definition: optional.h:172
const T * operator->() const
Definition: optional.h:186
T ValueOr(U &&defaultValue) const &
Get the instance value or the default value.
Definition: optional.h:236
void Emplace(Args &&...args)
Construct a new value from the give argument(s) and assign it to the instance value.
Definition: optional.h:106
const T & Value() const &
Get instance possible value.
Definition: optional.h:201
void Swap(Optional &other) noexcept(std::is_nothrow_move_assignable< T >::value)
Swap the current instance with another one.
Definition: optional.h:114
T ValueOr(U &&defaultValue) &&
Get the instance value or the default value.
Definition: optional.h:253