SPH
Classes | Public Member Functions | List of all members
Expected< Type, Error > Class Template Reference

Wrapper of type that either contains a value of given type, or an error message. More...

#include <Expected.h>

Public Member Functions

 Expected ()
 Construct the expected value using default constructor. More...
 
template<typename T , typename = std::enable_if_t<std::is_constructible<Type, T>::value>>
 Expected (T &&value)
 Constructs an expected value. More...
 
template<typename TError , typename = std::enable_if_t<std::is_constructible<Error, TError>::value>>
 Expected (UnexpectedTag, TError &&error)
 Constructs an unexpected value. More...
 
 operator bool () const
 Conversion to bool, checking whether object constains expected value. More...
 
bool operator! () const
 Negation operator, returns true if object does NOT contain expected value. More...
 
Type & value ()
 Returns the reference to expected value. More...
 
const Type & value () const
 Returns the const reference to expected value. More...
 
Type valueOr (const Type &other) const
 Returns the expected value or given alternative if the object contains unexpected value. More...
 
const Error & error () const
 Returns the error message. More...
 
Type * operator-> ()
 Operator -> for convenient access to member variables and functions of expected value. More...
 
const Type * operator-> () const
 operator->() More...
 

Detailed Description

template<typename Type, typename Error = std::string>
class Expected< Type, Error >

Wrapper of type that either contains a value of given type, or an error message.

Expected is designed as a return type. When talking about 'expected' value, it means no error has been encounter and Expected contains value of given type; 'unexpected' value means that Expected contains an error message.

Inspired by Andrei Alexandrescu - Systematic Error Handling in C++ https://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C

Definition at line 25 of file Expected.h.

Constructor & Destructor Documentation

◆ Expected() [1/3]

template<typename Type , typename Error = std::string>
Expected< Type, Error >::Expected ( )
inline

Construct the expected value using default constructor.

Should be avoided if possible as Expected is mainly designed as a value returned from function, but nevertheless the default constructor is defined for convenience.

Definition at line 40 of file Expected.h.

◆ Expected() [2/3]

template<typename Type , typename Error = std::string>
template<typename T , typename = std::enable_if_t<std::is_constructible<Type, T>::value>>
Expected< Type, Error >::Expected ( T &&  value)
inline

Constructs an expected value.

Definition at line 46 of file Expected.h.

◆ Expected() [3/3]

template<typename Type , typename Error = std::string>
template<typename TError , typename = std::enable_if_t<std::is_constructible<Error, TError>::value>>
Expected< Type, Error >::Expected ( UnexpectedTag  ,
TError &&  error 
)
inline

Constructs an unexpected value.

Definition at line 52 of file Expected.h.

Member Function Documentation

◆ error()

template<typename Type , typename Error = std::string>
const Error& Expected< Type, Error >::error ( ) const
inline

Returns the error message.

Object must contain unexpected value, checked by assert.

Definition at line 94 of file Expected.h.

◆ operator bool()

template<typename Type , typename Error = std::string>
Expected< Type, Error >::operator bool ( ) const
inlineexplicit

Conversion to bool, checking whether object constains expected value.

Definition at line 57 of file Expected.h.

◆ operator!()

template<typename Type , typename Error = std::string>
bool Expected< Type, Error >::operator! ( ) const
inline

Negation operator, returns true if object does NOT contain expected value.

Definition at line 62 of file Expected.h.

◆ operator->() [1/2]

template<typename Type , typename Error = std::string>
Type* Expected< Type, Error >::operator-> ( )
inline

Operator -> for convenient access to member variables and functions of expected value.

If the object contains unexpected, throws an assert.

Definition at line 102 of file Expected.h.

◆ operator->() [2/2]

template<typename Type , typename Error = std::string>
const Type* Expected< Type, Error >::operator-> ( ) const
inline

operator->()

operator->()

Definition at line 108 of file Expected.h.

◆ value() [1/2]

template<typename Type , typename Error = std::string>
Type& Expected< Type, Error >::value ( )
inline

Returns the reference to expected value.

Object must not contain unexpected value, checked by assert.

Definition at line 69 of file Expected.h.

◆ value() [2/2]

template<typename Type , typename Error = std::string>
const Type& Expected< Type, Error >::value ( ) const
inline

Returns the const reference to expected value.

Object must not contain unexpected value, checked by assert.

Definition at line 77 of file Expected.h.

◆ valueOr()

template<typename Type , typename Error = std::string>
Type Expected< Type, Error >::valueOr ( const Type &  other) const
inline

Returns the expected value or given alternative if the object contains unexpected value.

Definition at line 83 of file Expected.h.


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