Pecunia 0.9.0-alpha.22
Library using the ISO-4217 currency standard & a fixed monetary unit size
Loading...
Searching...
No Matches
pecunia::Expected< ValueType, ErrorType > Class Template Reference

Represents a result value that may either contain a valid value or an error state. Useful for error handling and propagating error states alongside valid results. Provides a way to work with results without using exceptions or nullable types. Ensures type safety by enforcing the presence of either a value or an error. More...

#include <Expected.hpp>

Collaboration diagram for pecunia::Expected< ValueType, ErrorType >:

Public Member Functions

 Expected ()
 Default constructor that initialises the object to a state with no value present and a default-constructed error object. The default error value depends on the error type's default constructor.
 
template<typename ... Parameters>
 Expected (decltype(unexpectedErr), Parameters &&... params)
 Constructs an instance with an error value.
 
template<typename ... Parameters>
 Expected (std::in_place_t, Parameters &&... params)
 Constructs an instance with a success value.
 
ErrorType & error () &
 Retrieves the stored error value from the object if it contains an error.
 
ErrorType && error () &&
 Retrieves the stored error value from the object if it contains an error.
 
const ErrorType & error () const &
 Retrieves the stored error value from the object if it contains an error.
 
bool hasValue () const noexcept
 Determines whether the object contains a valid value.
 
ValueType & operator* () &
 Provides access to the stored value within the instance if it contains a value.
 
ValueType && operator* () &&
 Provides access to the stored value within the instance if it contains a value.
 
const ValueType & operator* () const &
 Provides access to the stored value within the instance if it contains a value.
 
ValueType * operator-> ()
 Provides access to the contained value if no error is present.
 
const ValueType * operator-> () const
 Provides pointer-like access to the stored value if the operation is successful.
 
template<typename ErrType , typename = std::enable_if_t< ! std::is_same_v<ValueType, ErrType>>>
Expectedoperator= (const ErrType &error)
 Assigns an unexpected error value to the current instance. This is only available when ValueType and ErrorType are different.
 
template<typename ErrType >
Expectedoperator= (const Unexpected< ErrType > &error)
 Assigns an unexpected error value to the current instance. This is only available when ValueType and ErrorType are different.
 
Expectedoperator= (const ValueType &value)
 Assigns a new success value.
 
template<typename ErrType , typename = std::enable_if_t< ! std::is_same_v<ValueType, ErrType>>>
Expectedoperator= (ErrType &&error)
 Assigns an unexpected error value to the current instance. This is only available when ValueType and ErrorType are different.
 
template<typename ErrType >
Expectedoperator= (Unexpected< ErrType > &&error)
 Assigns an unexpected error value to the current instance. This is only available when ValueType and ErrorType are different.
 
Expectedoperator= (ValueType &&value)
 Assigns a new success value.
 
ValueType & value () &
 Provides access to the stored value within the instance if it contains a value.
 
ValueType && value () &&
 Provides access to the stored value within the instance if it contains a value.
 
const ValueType & value () const &
 Provides access to the stored value within the instance if it contains a value.
 
template<typename ElseValueType >
ValueType valueOr (ElseValueType &&elseValue) const
 Retrieves the contained value if it exists; otherwise, returns the provided alternative value.
 

Detailed Description

template<typename ValueType, typename ErrorType = bool>
class pecunia::Expected< ValueType, ErrorType >

Represents a result value that may either contain a valid value or an error state. Useful for error handling and propagating error states alongside valid results. Provides a way to work with results without using exceptions or nullable types. Ensures type safety by enforcing the presence of either a value or an error.

Precondition
A correctly initialised object must contain either a value or an error, but not both.
Neither the value nor the error should be in an uninitialised or invalid state.
Template Parameters
ValueTypeThe type of the success value that can be encapsulated in the object.
ErrorTypeThe type representing an error state for the object, defaulting to bool.

Constructor & Destructor Documentation

◆ Expected() [1/3]

template<typename ValueType , typename ErrorType = bool>
pecunia::Expected< ValueType, ErrorType >::Expected ( )

Default constructor that initialises the object to a state with no value present and a default-constructed error object. The default error value depends on the error type's default constructor.

◆ Expected() [2/3]

template<typename ValueType , typename ErrorType = bool>
template<typename ... Parameters>
pecunia::Expected< ValueType, ErrorType >::Expected ( std::in_place_t ,
Parameters &&... params )
explicit

Constructs an instance with a success value.

Template Parameters
ParametersVariadic template representing the types of parameters for ValueType's constructor.
Parameters
paramsThe parameters to be forwarded to the constructor of ValueType.

◆ Expected() [3/3]

template<typename ValueType , typename ErrorType = bool>
template<typename ... Parameters>
pecunia::Expected< ValueType, ErrorType >::Expected ( decltype(unexpectedErr) ,
Parameters &&... params )
explicit

Constructs an instance with an error value.

Template Parameters
ParametersVariadic template representing the types of parameters for ErrorType's constructor.
Parameters
paramsThe parameters to be forwarded to the constructor of ErrorType.

Member Function Documentation

◆ error() [1/3]

template<typename ValueType , typename ErrorType = bool>
ErrorType & pecunia::Expected< ValueType, ErrorType >::error ( ) &
nodiscard

Retrieves the stored error value from the object if it contains an error.

This function is useful for accessing the error value from an object, allowing the handling of error cases when the object does not contain a valid value. It is intended to be called on objects where the error state is known to be present.

Precondition
The Expected object must be in an error state. If the object is not in an error state, calling this function results in undefined behaviour.
Returns
The stored error value.

◆ error() [2/3]

template<typename ValueType , typename ErrorType = bool>
ErrorType && pecunia::Expected< ValueType, ErrorType >::error ( ) &&
nodiscard

Retrieves the stored error value from the object if it contains an error.

This function is useful for accessing the error value from an object, allowing the handling of error cases when the object does not contain a valid value. It is intended to be called on objects where the error state is known to be present.

Precondition
The Expected object must be in an error state. If the object is not in an error state, calling this function results in undefined behaviour.
Returns
The stored error value.

◆ error() [3/3]

template<typename ValueType , typename ErrorType = bool>
const ErrorType & pecunia::Expected< ValueType, ErrorType >::error ( ) const &
nodiscard

Retrieves the stored error value from the object if it contains an error.

This function is useful for accessing the error value from an object, allowing the handling of error cases when the object does not contain a valid value. It is intended to be called on objects where the error state is known to be present.

Precondition
The Expected object must be in an error state. If the object is not in an error state, calling this function results in undefined behaviour.
Returns
The stored error value.
Here is the caller graph for this function:

◆ hasValue()

template<typename ValueType , typename ErrorType = bool>
bool pecunia::Expected< ValueType, ErrorType >::hasValue ( ) const
nodiscardnoexcept

Determines whether the object contains a valid value.

This method is used to check if the object currently holds a valid value (indicating success), as opposed to an error state. It can be used to verify the success of operations or when a value is required for subsequent processing.

Returns
Gives true if the object contains a value; false if it contains an error.

◆ operator*() [1/3]

template<typename ValueType , typename ErrorType = bool>
ValueType & pecunia::Expected< ValueType, ErrorType >::operator* ( ) &
nodiscard

Provides access to the stored value within the instance if it contains a value.

It is primarily used when you are certain the instance holds a value, and you intend to operate on or retrieve that value.

Precondition
The instance contains a valid value. This must be ensured prior to invoking the method; otherwise, the behaviour is undefined.
Returns
The stored value of the type.

◆ operator*() [2/3]

template<typename ValueType , typename ErrorType = bool>
ValueType && pecunia::Expected< ValueType, ErrorType >::operator* ( ) &&
nodiscard

Provides access to the stored value within the instance if it contains a value.

It is primarily used when you are certain the instance holds a value, and you intend to operate on or retrieve that value.

Precondition
The instance contains a valid value. This must be ensured prior to invoking the method; otherwise, the behaviour is undefined.
Returns
The stored value of the type.

◆ operator*() [3/3]

template<typename ValueType , typename ErrorType = bool>
const ValueType & pecunia::Expected< ValueType, ErrorType >::operator* ( ) const &
nodiscard

Provides access to the stored value within the instance if it contains a value.

It is primarily used when you are certain the instance holds a value, and you intend to operate on or retrieve that value.

Precondition
The instance contains a valid value. This must be ensured prior to invoking the method; otherwise, the behaviour is undefined.
Returns
The stored value of the type.

◆ operator->() [1/2]

template<typename ValueType , typename ErrorType = bool>
ValueType * pecunia::Expected< ValueType, ErrorType >::operator-> ( )
nodiscard

Provides access to the contained value if no error is present.

Precondition
The instance contains a valid value. This must be ensured prior to invoking the method; otherwise, the behaviour is undefined.
Returns
A pointer to the contained value if the object holds a value.

◆ operator->() [2/2]

template<typename ValueType , typename ErrorType = bool>
const ValueType * pecunia::Expected< ValueType, ErrorType >::operator-> ( ) const
nodiscard

Provides pointer-like access to the stored value if the operation is successful.

Precondition
The instance contains a valid value. This must be ensured prior to invoking the method; otherwise, the behaviour is undefined.
Returns
A pointer to the contained value if the object holds a value.

◆ operator=() [1/6]

template<typename ValueType , typename ErrorType = bool>
template<typename ErrType , typename = std::enable_if_t< ! std::is_same_v<ValueType, ErrType>>>
Expected & pecunia::Expected< ValueType, ErrorType >::operator= ( const ErrType & error)
inline

Assigns an unexpected error value to the current instance. This is only available when ValueType and ErrorType are different.

Template Parameters
ErrTypeThe error type, this must be the same as ErrorType.
Parameters
errorThe object containing the error value to be assigned.
Postcondition
The instance will contain an error state with the specified error value. Any previously held value or error will be replaced.
Returns
A reference to the updated instance after the assignment.
Here is the call graph for this function:

◆ operator=() [2/6]

template<typename ValueType , typename ErrorType = bool>
template<typename ErrType >
Expected & pecunia::Expected< ValueType, ErrorType >::operator= ( const Unexpected< ErrType > & error)

Assigns an unexpected error value to the current instance. This is only available when ValueType and ErrorType are different.

Template Parameters
ErrTypeThe error type, this must be the same as ErrorType.
Parameters
errorThe object containing the error value to be assigned.
Postcondition
The instance will contain an error state with the specified error value. Any previously held value or error will be replaced.
Returns
A reference to the updated instance after the assignment.

◆ operator=() [3/6]

template<typename ValueType , typename ErrorType = bool>
Expected & pecunia::Expected< ValueType, ErrorType >::operator= ( const ValueType & value)

Assigns a new success value.

Parameters
valueThe value to be assigned to the object.
Postcondition
The object will hold the newly assigned value, replacing any previously stored value.
The object will no longer contain an error state.
Returns
A reference to the modified object containing the newly assigned value.

◆ operator=() [4/6]

template<typename ValueType , typename ErrorType = bool>
template<typename ErrType , typename = std::enable_if_t< ! std::is_same_v<ValueType, ErrType>>>
Expected & pecunia::Expected< ValueType, ErrorType >::operator= ( ErrType && error)
inline

Assigns an unexpected error value to the current instance. This is only available when ValueType and ErrorType are different.

Template Parameters
ErrTypeThe error type, this must be the same as ErrorType.
Parameters
errorThe object containing the error value to be assigned.
Postcondition
The instance will contain an error state with the specified error value. Any previously held value or error will be replaced.
Returns
A reference to the updated instance after the assignment.
Here is the call graph for this function:

◆ operator=() [5/6]

template<typename ValueType , typename ErrorType = bool>
template<typename ErrType >
Expected & pecunia::Expected< ValueType, ErrorType >::operator= ( Unexpected< ErrType > && error)

Assigns an unexpected error value to the current instance. This is only available when ValueType and ErrorType are different.

Template Parameters
ErrTypeThe error type, this must be the same as ErrorType.
Parameters
errorThe object containing the error value to be assigned.
Postcondition
The instance will contain an error state with the specified error value. Any previously held value or error will be replaced.
Returns
A reference to the updated instance after the assignment.

◆ operator=() [6/6]

template<typename ValueType , typename ErrorType = bool>
Expected & pecunia::Expected< ValueType, ErrorType >::operator= ( ValueType && value)

Assigns a new success value.

Parameters
valueThe value to be assigned to the object.
Postcondition
The object will hold the newly assigned value, replacing any previously stored value.
The object will no longer contain an error state.
Returns
A reference to the modified object containing the newly assigned value.

◆ value() [1/3]

template<typename ValueType , typename ErrorType = bool>
ValueType & pecunia::Expected< ValueType, ErrorType >::value ( ) &
nodiscard

Provides access to the stored value within the instance if it contains a value.

It is primarily used when you are certain that the instance holds a value, and you intend to operate on or retrieve that value.

Precondition
The instance contains a valid value. This must be ensured prior to invoking the method; otherwise, the behaviour is undefined.
Returns
The stored value of the type.

◆ value() [2/3]

template<typename ValueType , typename ErrorType = bool>
ValueType && pecunia::Expected< ValueType, ErrorType >::value ( ) &&
nodiscard

Provides access to the stored value within the instance if it contains a value.

It is primarily used when you are certain that the instance holds a value, and you intend to operate on or retrieve that value.

Precondition
The instance contains a valid value. This must be ensured prior to invoking the method; otherwise, the behaviour is undefined.
Returns
The stored value of the type.

◆ value() [3/3]

template<typename ValueType , typename ErrorType = bool>
const ValueType & pecunia::Expected< ValueType, ErrorType >::value ( ) const &
nodiscard

Provides access to the stored value within the instance if it contains a value.

It is primarily used when you are certain the instance holds a value, and you intend to operate on or retrieve that value.

Precondition
The instance contains a valid value. This must be ensured prior to invoking the method; otherwise, the behaviour is undefined.
Returns
The stored value of the type.

◆ valueOr()

template<typename ValueType , typename ErrorType = bool>
template<typename ElseValueType >
ValueType pecunia::Expected< ValueType, ErrorType >::valueOr ( ElseValueType && elseValue) const
nodiscard

Retrieves the contained value if it exists; otherwise, returns the provided alternative value.

Template Parameters
ElseValueTypeThe type of the alternative value to be returned if no valid value exists. It must be the same as ValueType.
Parameters
elseValueThe alternative value to return if the instance does not contain a valid value.
Returns
The contained value if it exists, or the provided alternative value if no valid value is present.