Opt< T > Class Template Reference

#include <optional.h>

Detailed Description

template<typename T>
class maxon::Opt< T >

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:

Opt<Int> opt;
// ...
if (opt)
Int v = *opt; // Fast, but undefined behaviour like a pointer if opt has no value.
PyObject PyObject * v
Definition: abstract.h:297
maxon::Int Int
Definition: ge_sys_math.h:60

Alternatively, the value can be retrieved with GetValue():

Opt<Int> opt;
Int v = opt.GetValue() iferr_return; // Returns IllegalStateError if opt has no value.
#define iferr_return
Definition: resultbase.h:1524
Template Parameters
TType 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)
 
Optoperator= (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>
Optoperator= (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>
Optoperator= (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>
Optoperator= (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>
Optoperator= (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>
Optoperator= (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
 
GetValueOr (T &&defaultValue) const
 
const T & GetValueOrDefault () const
 
MAXON_ATTRIBUTE_FORCE_INLINE const T & GetValueOrNull () const
 
Result< T > MoveValue ()
 
MoveValueOr (T &&defaultValue)
 
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
 

Member Typedef Documentation

◆ ValueType

using ValueType = T

◆ ResultFunctions

Constructor & Destructor Documentation

◆ Opt() [1/10]

Opt ( )
default

Default constructor. Creates an Opt with no value.

◆ Opt() [2/10]

Explicit construction with no value. Has the same effect as the default constructor.

◆ Opt() [3/10]

Opt ( const Opt< U > &  src)

Converting copy constructor. For semantics, see non-converting variant. This constructor is enabled only if T is constructible from const U&.

◆ Opt() [4/10]

Opt ( Opt< U > &&  src)

Converting move constructor. For semantics, see non-converting variant. This constructor is enabled only if T is constructible from U&&.

◆ Opt() [5/10]

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).

◆ Opt() [6/10]

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)).

◆ Opt() [7/10]

Opt ( Opt< const U & >  src)

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&.

◆ Opt() [8/10]

Opt ( Opt< U & >  src)

Constructs from optional reference. For semantics, see const-reference variant. This constructor is enabled only if T is constructible from U&.

◆ Opt() [9/10]

Opt ( Opt< U && >  src)

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&&.

◆ Opt() [10/10]

Opt ( IN_PLACE_TYPE  ,
ARGS &&...  args 
)
explicit

Emplace constructor. Constructs the contained value in-place with given arguments, i.e. T thisValue(std::forward<ARGS>(args) ...).

◆ ~Opt()

~Opt ( )
default

If this Opt contains a value, the latter is destroyed.

Member Function Documentation

◆ operator=() [1/6]

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()

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.

◆ operator=() [2/6]

Opt& operator= ( const Opt< U > &  src)

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()

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.

◆ operator=() [3/6]

Opt& operator= ( Opt< U > &&  src)

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&&.

◆ operator=() [4/6]

Opt& operator= ( Opt< const U & >  src)

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&.

◆ operator=() [5/6]

Opt& operator= ( Opt< U & >  src)

Assign from optional reference. For semantics, see const-reference variant. This operator is enabled only if T is assignable from U&.

◆ operator=() [6/6]

Opt& operator= ( Opt< U && >  src)

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&&.

◆ Emplace()

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.

◆ CopyFrom()

Result<void> CopyFrom ( const Opt< T > &  src)

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().

◆ Reset()

void Reset ( )

◆ HasValue()

Bool HasValue ( ) const

Returns true if this Opt contains a value.

◆ operator==()

Bool operator== ( std::nullptr_t  ) const

◆ operator!=()

Bool operator!= ( std::nullptr_t  ) const

◆ operator Bool()

operator Bool ( ) const
explicit

◆ GetValue() [1/2]

Result<T&> GetValue ( )

Returns the contained value, or IllegalState error if Opt contained no value.

◆ GetValue() [2/2]

Result<const T&> GetValue ( ) const

◆ GetValueOr() [1/2]

const T& GetValueOr ( const T &  defaultValue) const

Returns the contained value, or a default value if Opt contained no value.

◆ GetValueOr() [2/2]

T GetValueOr ( T &&  defaultValue) const

Returns the contained value, or a default value if Opt contained no value.

◆ GetValueOrDefault()

const T& GetValueOrDefault ( ) const

Returns the contained value, or a default value if Opt contained no value.

◆ GetValueOrNull()

MAXON_ATTRIBUTE_FORCE_INLINE const T& GetValueOrNull ( ) const

◆ MoveValue()

Result<T> MoveValue ( )

Returns the moved contained value, or IllegalState error if object contained no value.

◆ MoveValueOr()

T MoveValueOr ( T &&  defaultValue)

Returns the moved contained value, or a default value if object contained no value.

◆ MoveValueOrDefault()

T MoveValueOrDefault ( )

Returns the moved contained value, or a default value if Opt contained no value.

◆ operator->() [1/2]

const T* operator-> ( ) const

Returns a pointer to the contained value. Results in undefined behavior if object contained no value.

◆ operator->() [2/2]

T* operator-> ( )

◆ operator*() [1/2]

const T& operator* ( ) const

Returns a reference to the contained value. Results in undefined behavior if object contained no value.

◆ operator*() [2/2]

T& operator* ( )

◆ ToString()

SFINAEHelper<String, T>::type ToString ( const FormatStatement format = nullptr) const

Returns a formated string representation of this optional value.

◆ GetHashCode()

HashInt GetHashCode ( ) const

◆ CheckValue()

MAXON_ATTRIBUTE_FORCE_INLINE void CheckValue ( ) const
private

◆ GetValueRef() [1/2]

T& GetValueRef ( )
private

◆ GetValueRef() [2/2]

const T& GetValueRef ( ) const
private

Friends And Related Function Documentation

◆ Opt

friend class Opt
friend

Member Data Documentation

◆ this

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.

◆ _storage

maxon::details::OptStorageType<T> _storage
private