SPH
Public Member Functions | List of all members
Optional< Type > Class Template Reference

Wrapper of type value of which may or may not be present. More...

#include <Optional.h>

Public Member Functions

 Optional ()=default
 Creates an uninitialized value. More...
 
template<typename T , typename = std::enable_if_t<std::is_copy_assignable<T>::value>>
 Optional (const T &t)
 Copy constuctor from stored value. More...
 
 Optional (Type &&t)
 Move constuctor from stored value. More...
 
 Optional (const Optional &other)
 Copy constructor from other optional. More...
 
 Optional (Optional &&other)
 Move constructor from other optional. More...
 
 Optional (const NothingType &)
 Construct uninitialized value. More...
 
 ~Optional ()
 
template<typename... TArgs>
void emplace (TArgs &&... args)
 Constructs the uninitialized object from a list of arguments. More...
 
template<typename T , typename = std::enable_if_t<std::is_assignable<Type, T>::value>>
Optionaloperator= (const T &t)
 Copies the value on right-hand side, initializing the optional if necessary. More...
 
Optionaloperator= (Type &&t)
 Moves the value on right-hand side, initializing the optional if necessary. More...
 
Optionaloperator= (const Optional &other)
 Copies the value of another optional object. More...
 
Optionaloperator= (Optional &&other)
 Moves the value of another optional object. More...
 
Optionaloperator= (const NothingType &)
 Destroys the stored value, provided the object has been initialized. More...
 
INLINE Type & value ()
 Returns the reference to the stored value. More...
 
INLINE const Type & value () const
 Returns the const reference to the stored value. More...
 
template<typename TOther >
INLINE Type valueOr (const TOther &other) const
 Returns the stored value if the object has been initialized, otherwise returns provided parameter. More...
 
template<typename TException , typename... TArgs>
INLINE Type valueOrThrow (TArgs &&... args)
 Returns the stored value if the object has been initialized, otherwise throws an exception, constructed with the provided list of arguments. More...
 
INLINE const RawType * operator-> () const
 Used to access members of the stored value. More...
 
INLINE RawType * operator-> ()
 Used to access members of the stored value. More...
 
INLINE operator bool () const
 Checks if the object has been initialized. More...
 
INLINE bool operator! () const
 Returns true if the object is uninitialized. More...
 

Detailed Description

template<typename Type>
class Optional< Type >

Wrapper of type value of which may or may not be present.

Similar to std::optional introduced in c++17. See http://en.cppreference.com/w/cpp/utility/optional

Definition at line 23 of file Optional.h.

Constructor & Destructor Documentation

◆ Optional() [1/6]

template<typename Type >
Optional< Type >::Optional ( )
default

Creates an uninitialized value.

◆ Optional() [2/6]

template<typename Type >
template<typename T , typename = std::enable_if_t<std::is_copy_assignable<T>::value>>
Optional< Type >::Optional ( const T &  t)
inline

Copy constuctor from stored value.

Initializes the optional value.

Definition at line 46 of file Optional.h.

◆ Optional() [3/6]

template<typename Type >
Optional< Type >::Optional ( Type &&  t)
inline

Move constuctor from stored value.

Initializes the optional value.

Definition at line 54 of file Optional.h.

◆ Optional() [4/6]

template<typename Type >
Optional< Type >::Optional ( const Optional< Type > &  other)
inline

Copy constructor from other optional.

Copies the state and if the passed optional is initialized, copies the value as well.

Definition at line 63 of file Optional.h.

◆ Optional() [5/6]

template<typename Type >
Optional< Type >::Optional ( Optional< Type > &&  other)
inline

Move constructor from other optional.

Copies the state and if the passed optional is initialized, copies the value as well. Also un-initializes moved optional.

Definition at line 74 of file Optional.h.

◆ Optional() [6/6]

template<typename Type >
Optional< Type >::Optional ( const NothingType )
inline

Construct uninitialized value.

Definition at line 83 of file Optional.h.

◆ ~Optional()

template<typename Type >
Optional< Type >::~Optional ( )
inline

Definition at line 87 of file Optional.h.

Member Function Documentation

◆ emplace()

template<typename Type >
template<typename... TArgs>
void Optional< Type >::emplace ( TArgs &&...  args)
inline

Constructs the uninitialized object from a list of arguments.

If the object was previously initialized, the stored value is destroyed.

Definition at line 95 of file Optional.h.

◆ operator bool()

template<typename Type >
INLINE Optional< Type >::operator bool ( ) const
inlineexplicit

Checks if the object has been initialized.

Definition at line 224 of file Optional.h.

◆ operator!()

template<typename Type >
INLINE bool Optional< Type >::operator! ( ) const
inline

Returns true if the object is uninitialized.

Definition at line 229 of file Optional.h.

◆ operator->() [1/2]

template<typename Type >
INLINE RawType* Optional< Type >::operator-> ( )
inline

Used to access members of the stored value.

The value must be initialized.

Definition at line 218 of file Optional.h.

◆ operator->() [2/2]

template<typename Type >
INLINE const RawType* Optional< Type >::operator-> ( ) const
inline

Used to access members of the stored value.

The value must be initialized.

Definition at line 210 of file Optional.h.

◆ operator=() [1/5]

template<typename Type >
Optional& Optional< Type >::operator= ( const NothingType )
inline

Destroys the stored value, provided the object has been initialized.

Definition at line 161 of file Optional.h.

◆ operator=() [2/5]

template<typename Type >
Optional& Optional< Type >::operator= ( const Optional< Type > &  other)
inline

Copies the value of another optional object.

If the right-hand side object was previously uninitialized, the value in this object is destroyed.

Definition at line 129 of file Optional.h.

◆ operator=() [3/5]

template<typename Type >
template<typename T , typename = std::enable_if_t<std::is_assignable<Type, T>::value>>
Optional& Optional< Type >::operator= ( const T &  t)
inline

Copies the value on right-hand side, initializing the optional if necessary.

Definition at line 105 of file Optional.h.

◆ operator=() [4/5]

template<typename Type >
Optional& Optional< Type >::operator= ( Optional< Type > &&  other)
inline

Moves the value of another optional object.

If the right-hand side object was previously uninitialized, the value in this object is destroyed.

Definition at line 146 of file Optional.h.

◆ operator=() [5/5]

template<typename Type >
Optional& Optional< Type >::operator= ( Type &&  t)
inline

Moves the value on right-hand side, initializing the optional if necessary.

Definition at line 116 of file Optional.h.

◆ value() [1/2]

template<typename Type >
INLINE Type& Optional< Type >::value ( )
inline

Returns the reference to the stored value.

Object must be already initialized.

Definition at line 172 of file Optional.h.

◆ value() [2/2]

template<typename Type >
INLINE const Type& Optional< Type >::value ( ) const
inline

Returns the const reference to the stored value.

Object must be already initialized.

Definition at line 180 of file Optional.h.

◆ valueOr()

template<typename Type >
template<typename TOther >
INLINE Type Optional< Type >::valueOr ( const TOther &  other) const
inline

Returns the stored value if the object has been initialized, otherwise returns provided parameter.

Definition at line 188 of file Optional.h.

◆ valueOrThrow()

template<typename Type >
template<typename TException , typename... TArgs>
INLINE Type Optional< Type >::valueOrThrow ( TArgs &&...  args)
inline

Returns the stored value if the object has been initialized, otherwise throws an exception, constructed with the provided list of arguments.

Definition at line 199 of file Optional.h.


The documentation for this class was generated from the following files: