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...
|
| 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>>> |
Expected & | operator= (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 > |
Expected & | operator= (const Unexpected< ErrType > &error) |
| Assigns an unexpected error value to the current instance. This is only available when ValueType and ErrorType are different.
|
|
Expected & | operator= (const ValueType &value) |
| Assigns a new success value.
|
|
template<typename ErrType , typename = std::enable_if_t< ! std::is_same_v<ValueType, ErrType>>> |
Expected & | operator= (ErrType &&error) |
| Assigns an unexpected error value to the current instance. This is only available when ValueType and ErrorType are different.
|
|
template<typename ErrType > |
Expected & | operator= (Unexpected< ErrType > &&error) |
| Assigns an unexpected error value to the current instance. This is only available when ValueType and ErrorType are different.
|
|
Expected & | operator= (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.
|
|
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
-
ValueType | The type of the success value that can be encapsulated in the object. |
ErrorType | The type representing an error state for the object, defaulting to bool. |
template<typename ValueType , typename ErrorType = bool>
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.
template<typename ValueType , typename ErrorType = bool>
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.
template<typename ValueType , typename ErrorType = bool>
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.
template<typename ValueType , typename ErrorType = bool>
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.
template<typename ValueType , typename ErrorType = bool>
template<typename ErrType , typename = std::enable_if_t< ! std::is_same_v<ValueType, ErrType>>>
Assigns an unexpected error value to the current instance. This is only available when ValueType
and ErrorType
are different.
- Template Parameters
-
ErrType | The error type, this must be the same as ErrorType . |
- Parameters
-
error | The 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.
template<typename ValueType , typename ErrorType = bool>
template<typename ErrType , typename = std::enable_if_t< ! std::is_same_v<ValueType, ErrType>>>
Assigns an unexpected error value to the current instance. This is only available when ValueType
and ErrorType
are different.
- Template Parameters
-
ErrType | The error type, this must be the same as ErrorType . |
- Parameters
-
error | The 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.