#include <optional.h>
An Opt<T> represents an optional value. Either it holds a value of type T, or no value. This is similar to tagging a value with a flag that indicates whether the value is present/valid, e.g. with a Pair<Bool,T>. Opt handles construction/destruction of the contained value, so that constructors and destructors are only called if there is an actual contained value. The value storage is allocated directly within Opt.
The value of an Optional can be accessed like a pointer:
Alternatively, the value can be retrieved with GetValue():
T | Type of the contained value. |
Public Types | |
using | ValueType = T |
template<typename SUPER > | |
using | ResultFunctions = maxon::details::ResultOptFunctions< SUPER > |
Public Member Functions | |
Opt ()=default | |
MAXON_IMPLICIT | Opt (NO_VALUE_TYPE) |
Opt & | operator= (NO_VALUE_TYPE) |
MAXON_DECLARE_CONDITIONAL_COPY_CONSTRUCTOR (Opt, STD_IS_REPLACEMENT(copy_constructible, maxon::details::OptStorageType< T >)) | |
template<typename U , typename = typename std::enable_if<STD_IS_REPLACEMENT(constructible, maxon::details::OptStorageType<T>, const maxon::details::OptStorageType<U>&)>::type> | |
Opt (const Opt< U > &src) | |
template<typename U , typename = typename std::enable_if<STD_IS_REPLACEMENT(assignable, maxon::details::OptStorageType<T>, const maxon::details::OptStorageType<U>&)>::type> | |
Opt & | operator= (const Opt< U > &src) |
MAXON_DECLARE_CONDITIONAL_MOVE_CONSTRUCTOR (Opt, STD_IS_REPLACEMENT(move_constructible, maxon::details::OptStorageType< T >)) | |
template<typename U , typename = typename std::enable_if<STD_IS_REPLACEMENT(constructible, maxon::details::OptStorageType<T>, maxon::details::OptStorageType<U>&&)>::type> | |
Opt (Opt< U > &&src) | |
template<typename U , typename = typename std::enable_if<STD_IS_REPLACEMENT(assignable, maxon::details::OptStorageType<T>, maxon::details::OptStorageType<U>&&)>::type> | |
Opt & | operator= (Opt< U > &&src) |
MAXON_IMPLICIT | Opt (const T &value) |
MAXON_IMPLICIT | Opt (T &&value) |
template<typename U , typename = typename std::enable_if<STD_IS_REPLACEMENT(constructible, maxon::details::OptStorageType<T>, const U&)>::type> | |
Opt (Opt< const U & > src) | |
template<typename U , typename = typename std::enable_if<STD_IS_REPLACEMENT(assignable, maxon::details::OptStorageType<T>, const U&)>::type> | |
Opt & | operator= (Opt< const U & > src) |
template<typename U , typename = typename std::enable_if<STD_IS_REPLACEMENT(constructible, maxon::details::OptStorageType<T>, U&)>::type> | |
Opt (Opt< U & > src) | |
template<typename U , typename = typename std::enable_if<STD_IS_REPLACEMENT(assignable, maxon::details::OptStorageType<T>, U&)>::type> | |
Opt & | operator= (Opt< U & > src) |
template<typename U , typename = typename std::enable_if<STD_IS_REPLACEMENT(constructible, maxon::details::OptStorageType<T>, U&&)>::type> | |
Opt (Opt< U && > src) | |
template<typename U , typename = typename std::enable_if<STD_IS_REPLACEMENT(assignable, maxon::details::OptStorageType<T>, U&&)>::type> | |
Opt & | operator= (Opt< U && > src) |
template<typename ... ARGS> | |
Opt (IN_PLACE_TYPE, ARGS &&... args) | |
template<typename ... ARGS> | |
void | Emplace (ARGS &&... args) |
template<Bool ENABLE = true, typename = typename std::enable_if< ENABLE && TestForCopyFromMember<T>::isSupported>::type> | |
Result< void > | CopyFrom (const Opt &src) |
~Opt ()=default | |
void | Reset () |
Bool | HasValue () const |
Bool | operator== (std::nullptr_t) const |
Bool | operator!= (std::nullptr_t) const |
operator Bool () const | |
Result< T & > | GetValue () |
Result< const T & > | GetValue () const |
const T & | GetValueOr (const T &defaultValue) const |
T | GetValueOr (T &&defaultValue) const |
const T & | GetValueOrDefault () const |
MAXON_ATTRIBUTE_FORCE_INLINE const T & | GetValueOrNull () const |
Result< T > | MoveValue () |
T | MoveValueOr (T &&defaultValue) |
T | MoveValueOrDefault () |
const T * | operator-> () const |
T * | operator-> () |
const T & | operator* () const |
T & | operator* () |
SFINAEHelper< String, T >::type | ToString (const FormatStatement *format=nullptr) const |
HashInt | GetHashCode () const |
Public Attributes | |
return * | this |
Private Member Functions | |
MAXON_ATTRIBUTE_FORCE_INLINE void | CheckValue () const |
T & | GetValueRef () |
const T & | GetValueRef () const |
Private Attributes | |
maxon::details::OptStorageType< T > | _storage |
Friends | |
template<typename U > | |
class | Opt |
using ValueType = T |
using ResultFunctions = maxon::details::ResultOptFunctions<SUPER> |
Explicit construction with no value. Has the same effect as the default constructor.
Converting copy constructor. For semantics, see non-converting variant. This constructor is enabled only if T is constructible from const U&.
Converting move constructor. For semantics, see non-converting variant. This constructor is enabled only if T is constructible from U&&.
MAXON_IMPLICIT Opt | ( | const T & | value | ) |
Copy-from-value constructor. Constructs object as if direct-initializing the contained value with the passed value, i.e. T thisValue(value).
MAXON_IMPLICIT Opt | ( | T && | value | ) |
Move-from-value constructor. Constructs object as if direct-initializing the contained value with the passed value, i.e. T thisValue(std::move(value)).
Constructs from optional const-reference. (1) If src references no value, this object will not contain a value either. (2) If src references a value, the value of this object is copy-constructed with the former, i.e. T thisValue(*src). This constructor is enabled only if T is constructible from const U&.
Constructs from optional reference. For semantics, see const-reference variant. This constructor is enabled only if T is constructible from U&.
Constructs from optional rvalue-reference. For semantics, see const-reference variant, with the following differences: (2) If src references a value, the value of this object is copy-constructed with the former, i.e. T thisValue(*src). This constructor is enabled only if T is constructible from U&&.
|
explicit |
Emplace constructor. Constructs the contained value in-place with given arguments, i.e. T thisValue(std::forward<ARGS>(args) ...).
Opt& operator= | ( | NO_VALUE_TYPE | ) |
Assignment with no value. If this object contains a value, the destructor of the contained value is called. Afterwards, this object no longer contains a value.
MAXON_DECLARE_CONDITIONAL_COPY_CONSTRUCTOR | ( | Opt< T > | , |
STD_IS_REPLACEMENT(copy_constructible, maxon::details::OptStorageType< T >) | |||
) |
Copy constructor. (1) If src contains no value, this object will not contain a value either. (2) If src contains a value, the value of this object is copy-constructed with the former, i.e. T thisValue(srcValue). This constructor is enabled only if T is copy constructible.
Converting copy assignment. For semantics, see non-converting variant. This operator is enabled only if T is assignable from const U&.
MAXON_DECLARE_CONDITIONAL_MOVE_CONSTRUCTOR | ( | Opt< T > | , |
STD_IS_REPLACEMENT(move_constructible, maxon::details::OptStorageType< T >) | |||
) |
Move constructor. (1) If src contains no value, this object will not contain a value either. (2) If src contains a value, the value of this object is move-constructed with the former, i.e. T thisValue(std::move(srcValue)). Note that afterwards, src will still have a value, which has been moved from. This constructor is enabled only if T is move constructible.
Converting move assignment. For semantics, see non-converting variant. (1,2) See copy assignment. (3) If only src contains a value, behavior is identical to move constructor. (4) If both this object and src contain a value, the contained value is assigned src's value, i.e. thisValue = std::move(srcValue). This operator is enabled only if T is assignable from U&&.
Assign from optional const-reference. (1) If this object contains no value and src references no value, this function does nothing. (2) If this object contains a value and src references no value, behavior is identical to assignment with no value. (3) If this object contains no value and src references a value, behavior is identical to copy-from-value constructor. (4) If this object contains a value and src references a value, the value of this object is assigned from *src. This operator is enabled only if T is assignable from const U&.
Assign from optional reference. For semantics, see const-reference variant. This operator is enabled only if T is assignable from U&.
Assign from optional rvalue-reference. For semantics, see const-reference variant, with the following differences: (3) If this object contains no value and src references a value, behavior is identical to move-from-value constructor. (4) If this object contains a value and src references a value, the value of this object is assigned from std::move(*src). This operator is enabled only if T is assignable from U&&.
void Emplace | ( | ARGS &&... | args | ) |
Constructs a new value in-place. Behavior is similar to the in-place constructor, with the difference that if the object already contained a value, the current value is destructed first.
Semantically equivalent to copy assignment, but uses CopyFrom() of the contained value. Use this if copying T may result in an error. This function is enabled only if T implements CopyFrom().
void Reset | ( | ) |
Bool operator== | ( | std::nullptr_t | ) | const |
Bool operator!= | ( | std::nullptr_t | ) | const |
|
explicit |
Result<T&> GetValue | ( | ) |
Returns the contained value, or IllegalState error if Opt contained no value.
Result<const T&> GetValue | ( | ) | const |
const T& GetValueOr | ( | const T & | defaultValue | ) | const |
Returns the contained value, or a default value if Opt contained no value.
T GetValueOr | ( | T && | defaultValue | ) | const |
Returns the contained value, or a default value if Opt contained no value.
const T& GetValueOrDefault | ( | ) | const |
Returns the contained value, or a default value if Opt contained no value.
MAXON_ATTRIBUTE_FORCE_INLINE const T& GetValueOrNull | ( | ) | const |
Result<T> MoveValue | ( | ) |
Returns the moved contained value, or IllegalState error if object contained no value.
T MoveValueOr | ( | T && | defaultValue | ) |
Returns the moved contained value, or a default value if object contained no value.
T MoveValueOrDefault | ( | ) |
Returns the moved contained value, or a default value if Opt contained no value.
const T* operator-> | ( | ) | const |
Returns a pointer to the contained value. Results in undefined behavior if object contained no value.
T* operator-> | ( | ) |
const T& operator* | ( | ) | const |
Returns a reference to the contained value. Results in undefined behavior if object contained no value.
T& operator* | ( | ) |
SFINAEHelper<String, T>::type ToString | ( | const FormatStatement * | format = nullptr | ) | const |
Returns a formated string representation of this optional value.
HashInt GetHashCode | ( | ) | const |
|
private |
|
private |
|
private |
|
friend |
return * this |
Copy assignment. (1) If both this object and src contain no value, this function does nothing. (2) If only this object contains a value, behavior is identical to assignment with no value. (3) If only src contains a value, behavior is identical to copy constructor. (4) If both this object and src contain a value, the contained value is assigned src's value, i.e. thisValue = srcValue. This operator is enabled only if T is copy assignable.
Move assignment. (1,2) See copy assignment. (3) If only src contains a value, behavior is identical to move constructor. (4) If both this object and src contain a value, the contained value is assigned src's value, i.e. thisValue = std::move(srcValue). This operator is enabled only if T is move assignable.
|
private |