RPNX::DataStructures
Header-only C++ data structures and supporting utilities.
Loading...
Searching...
No Matches
rpnx::result< T > Class Template Reference

A generic result class that can hold either a value or an exception. More...

#include <result.hpp>

Public Member Functions

 result (T t)
 Constructs a result object with a value.
 
 result ()
 Constructs an empty result object.
 
 result (std::exception_ptr er)
 Constructs a result object with an exception.
 
void test () const
 Tests if the result contains an exception and rethrows it if present.
 
T & get () &
 Returns the stored value from an lvalue result.
 
T const & get () const &
 Returns the stored value from an lvalue result.
 
T && get () &&
 Returns the stored value from an rvalue result.
 
T const && get () const &&
 Returns the stored value from an rvalue result.
 
T & value () &
 Alias for get().
 
T const & value () const &
 Alias for get().
 
T && value () &&
 Alias for rvalue-qualified get().
 
T const && value () const &&
 Alias for const rvalue-qualified get().
 
void set_value (T t)
 Sets the value of the result.
 
void set_error (std::exception_ptr er)
 Sets the exception of the result.
 
void set_exception (std::exception_ptr er)
 Alias for set_error().
 
bool has_result () const
 Checks if the result has a value or an exception.
 
bool has_value () const
 Checks if the result has a value.
 
bool has_error () const
 Checks if the result has an exception.
 
bool has_exception () const
 Alias for has_error().
 
std::exception_ptr get_error () const
 Gets the stored exception.
 
 operator bool () const
 Implicit conversion to bool.
 

Detailed Description

template<typename T>
class rpnx::result< T >

A generic result class that can hold either a value or an exception.

Template Parameters
TThe type of the value stored in the result.

Constructor & Destructor Documentation

◆ result() [1/2]

template<typename T>
rpnx::result< T >::result ( T t)
inline

Constructs a result object with a value.

Parameters
tThe value to store in the result.

◆ result() [2/2]

template<typename T>
rpnx::result< T >::result ( std::exception_ptr er)
inline

Constructs a result object with an exception.

Parameters
erThe exception to store in the result.

Member Function Documentation

◆ get() [1/4]

template<typename T>
T & rpnx::result< T >::get ( ) &
inline

Returns the stored value from an lvalue result.

Returns
A mutable reference to the stored value.
Exceptions
Thestored exception when the result contains an error.
Precondition
The result contains a value or an error.

◆ get() [2/4]

template<typename T>
T && rpnx::result< T >::get ( ) &&
inline

Returns the stored value from an rvalue result.

Returns
An rvalue reference to the stored value.
Exceptions
Thestored exception when the result contains an error.
Precondition
The result contains a value or an error.

◆ get() [3/4]

template<typename T>
T const & rpnx::result< T >::get ( ) const &
inline

Returns the stored value from an lvalue result.

Returns
A mutable reference to the stored value.
Exceptions
Thestored exception when the result contains an error.
Precondition
The result contains a value or an error.

◆ get() [4/4]

template<typename T>
T const && rpnx::result< T >::get ( ) const &&
inline

Returns the stored value from an rvalue result.

Returns
An rvalue reference to the stored value.
Exceptions
Thestored exception when the result contains an error.
Precondition
The result contains a value or an error.

◆ get_error()

template<typename T>
std::exception_ptr rpnx::result< T >::get_error ( ) const
inline

Gets the stored exception.

Returns
The stored exception.

◆ has_error()

template<typename T>
bool rpnx::result< T >::has_error ( ) const
inline

Checks if the result has an exception.

Returns
True if the result has an exception, false otherwise.

◆ has_exception()

template<typename T>
bool rpnx::result< T >::has_exception ( ) const
inline

Alias for has_error().

Returns
true if an exception is stored.

◆ has_result()

template<typename T>
bool rpnx::result< T >::has_result ( ) const
inline

Checks if the result has a value or an exception.

Returns
True if the result has a value or an exception, false otherwise.

◆ has_value()

template<typename T>
bool rpnx::result< T >::has_value ( ) const
inline

Checks if the result has a value.

Returns
True if the result has a value, false otherwise.

◆ operator bool()

template<typename T>
rpnx::result< T >::operator bool ( ) const
inline

Implicit conversion to bool.

Returns
True if the result has a value or an exception, false otherwise.

◆ set_error()

template<typename T>
void rpnx::result< T >::set_error ( std::exception_ptr er)
inline

Sets the exception of the result.

Parameters
erThe exception to set.

◆ set_exception()

template<typename T>
void rpnx::result< T >::set_exception ( std::exception_ptr er)
inline

Alias for set_error().

Parameters
erException to store.

◆ set_value()

template<typename T>
void rpnx::result< T >::set_value ( T t)
inline

Sets the value of the result.

Parameters
tThe value to set.

◆ test()

template<typename T>
void rpnx::result< T >::test ( ) const
inline

Tests if the result contains an exception and rethrows it if present.

Exceptions
Thestored exception if present.

◆ value() [1/4]

template<typename T>
T & rpnx::result< T >::value ( ) &
inline

Alias for get().

Returns
A mutable reference to the stored value.

◆ value() [2/4]

template<typename T>
T && rpnx::result< T >::value ( ) &&
inline

Alias for rvalue-qualified get().

Returns
An rvalue reference to the stored value.

◆ value() [3/4]

template<typename T>
T const & rpnx::result< T >::value ( ) const &
inline

Alias for get().

Returns
An immutable reference to the stored value.

◆ value() [4/4]

template<typename T>
T const && rpnx::result< T >::value ( ) const &&
inline

Alias for const rvalue-qualified get().

Returns
An immutable rvalue reference to the stored value.

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