Porytiles
Loading...
Searching...
No Matches
porytiles2::ChainableResult< T, E > Class Template Reference

A result type that maintains a chainable sequence of errors for debugging and error reporting. More...

#include <chainable_result.hpp>

Public Member Functions

 ChainableResult (T value)
 Constructs a ChainableResult from a success value.
 
 ChainableResult (const E &error)
 Constructs a ChainableResult from an error value.
 
template<typename CauseT , typename CauseE >
 ChainableResult (const E &error, const ChainableResult< CauseT, CauseE > &cause_result)
 Constructs a ChainableResult by chaining a new error with an existing error chain.
 
 ChainableResult (ChainableResult &&)=default
 
ChainableResultoperator= (ChainableResult &&)=default
 
 ChainableResult (const ChainableResult &)=delete
 
ChainableResultoperator= (const ChainableResult &)=delete
 
template<typename OtherT , typename OtherE >
void add_cause (const ChainableResult< OtherT, OtherE > &cause_result)
 Adds all errors from another ChainableResult's chain to this result's chain.
 
bool has_value () const
 Checks whether the result contains a success value.
 
T & value () &
 Returns a reference to the contained success value.
 
const T & value () const &
 Returns a const reference to the contained success value.
 
T && value () &&
 Returns an rvalue reference to the contained success value.
 
const E & error () const
 Returns a const reference to the immediate error.
 
const std::vector< std::unique_ptr< Error > > & chain () const
 Returns the complete error chain.
 

Protected Member Functions

 ChainableResult ()=default
 

Detailed Description

template<typename T, typename E = FormattableError>
class porytiles2::ChainableResult< T, E >

A result type that maintains a chainable sequence of errors for debugging and error reporting.

ChainableResult extends the concept of std::expected by maintaining a chain of error propagation through multiple layers of the application. Unlike a simple std::expected that only stores the immediate error, ChainableResult maintains a full chain of errors from the originating cause up through each layer that adds context. This is particularly useful for debugging complex failures where understanding the root cause requires knowing the full context of how an error propagated through the system.

The class enforces that error type E must derive from the Error interface, ensuring all errors in the chain can be properly cloned and formatted. The error chain is stored as a vector of unique_ptr<Error> objects, allowing polymorphic error types while maintaining proper ownership semantics.

Template Parameters
TThe type of the expected success value
EThe error type, must be derived from Error interface

Definition at line 31 of file chainable_result.hpp.

Constructor & Destructor Documentation

◆ ChainableResult() [1/6]

template<typename T , typename E = FormattableError>
porytiles2::ChainableResult< T, E >::ChainableResult ( value)
inline

Constructs a ChainableResult from a success value.

This constructor allows implicit conversion from a success value of type T to a ChainableResult. The result is stored as a successful value with no error chain. This provides ergonomic construction for success cases, similar to std::expected's implicit construction from T.

Parameters
valueThe success value to store

Definition at line 44 of file chainable_result.hpp.

◆ ChainableResult() [2/6]

template<typename T , typename E = FormattableError>
porytiles2::ChainableResult< T, E >::ChainableResult ( const E &  error)
inline

Constructs a ChainableResult from an error value.

This constructor allows implicit conversion from an error value of type E to a ChainableResult. The error is stored as the initial error in the chain. This provides ergonomic construction for error cases at the bottom level of an error chain, similar to std::expected's construction from std::unexpected.

Parameters
errorThe error value to store

Definition at line 57 of file chainable_result.hpp.

◆ ChainableResult() [3/6]

template<typename T , typename E = FormattableError>
template<typename CauseT , typename CauseE >
porytiles2::ChainableResult< T, E >::ChainableResult ( const E &  error,
const ChainableResult< CauseT, CauseE > &  cause_result 
)
inlineexplicit

Constructs a ChainableResult by chaining a new error with an existing error chain.

This constructor creates a new error result that includes both a new error message and the complete error chain from a cause result. This is the primary mechanism for building error context as errors propagate up through application layers. The new error is added to the beginning of the chain, followed by all errors from the cause result's chain.

Template Parameters
CauseTThe success type of the cause result (unused but required for template matching)
CauseEThe error type of the cause result, must be derived from Error
Parameters
errorThe new error to add at this level
cause_resultThe ChainableResult containing the error chain to chain

Definition at line 78 of file chainable_result.hpp.

◆ ChainableResult() [4/6]

template<typename T , typename E = FormattableError>
porytiles2::ChainableResult< T, E >::ChainableResult ( ChainableResult< T, E > &&  )
default

◆ ChainableResult() [5/6]

template<typename T , typename E = FormattableError>
porytiles2::ChainableResult< T, E >::ChainableResult ( const ChainableResult< T, E > &  )
delete

◆ ChainableResult() [6/6]

template<typename T , typename E = FormattableError>
porytiles2::ChainableResult< T, E >::ChainableResult ( )
protecteddefault

Member Function Documentation

◆ add_cause()

template<typename T , typename E = FormattableError>
template<typename OtherT , typename OtherE >
void porytiles2::ChainableResult< T, E >::add_cause ( const ChainableResult< OtherT, OtherE > &  cause_result)
inline

Adds all errors from another ChainableResult's chain to this result's chain.

This method appends the complete error chain from a cause result to the current error chain. Each error in the cause's chain is cloned to maintain proper ownership semantics. The method will panic if the cause_result contains a success value rather than an error, as this would indicate a programming error.

Template Parameters
OtherTThe success type of the cause result
OtherEThe error type of the cause result
Parameters
cause_resultThe ChainableResult whose error chain should be appended

Definition at line 107 of file chainable_result.hpp.

◆ chain()

template<typename T , typename E = FormattableError>
const std::vector< std::unique_ptr< Error > > & porytiles2::ChainableResult< T, E >::chain ( ) const
inline

Returns the complete error chain.

The error chain contains all errors in the chain, starting with the most recent error (added at this level) and proceeding through to the original root cause. Each error in the chain is owned by this ChainableResult through unique_ptr.

Returns
A const reference to the vector of error pointers representing the full error chain

Definition at line 194 of file chainable_result.hpp.

◆ error()

template<typename T , typename E = FormattableError>
const E & porytiles2::ChainableResult< T, E >::error ( ) const
inline

Returns a const reference to the immediate error.

This returns only the immediate error at this level, not the full error chain. To access the complete error history, use the chain() method instead.

Returns
A const reference to the error value

Definition at line 179 of file chainable_result.hpp.

◆ has_value()

template<typename T , typename E = FormattableError>
bool porytiles2::ChainableResult< T, E >::has_value ( ) const
inline

Checks whether the result contains a success value.

Returns
True if the result contains a success value, false if it contains an error

Definition at line 122 of file chainable_result.hpp.

◆ operator=() [1/2]

template<typename T , typename E = FormattableError>
ChainableResult & porytiles2::ChainableResult< T, E >::operator= ( ChainableResult< T, E > &&  )
default

◆ operator=() [2/2]

template<typename T , typename E = FormattableError>
ChainableResult & porytiles2::ChainableResult< T, E >::operator= ( const ChainableResult< T, E > &  )
delete

◆ value() [1/3]

template<typename T , typename E = FormattableError>
T & porytiles2::ChainableResult< T, E >::value ( ) &
inline

Returns a reference to the contained success value.

This method provides mutable access to the success value. It will throw std::bad_expected_access if called when the result contains an error rather than a success value.

Returns
A mutable reference to the success value

Definition at line 136 of file chainable_result.hpp.

◆ value() [2/3]

template<typename T , typename E = FormattableError>
T && porytiles2::ChainableResult< T, E >::value ( ) &&
inline

Returns an rvalue reference to the contained success value.

This method provides move access to the success value when called on an rvalue ChainableResult. It enables efficient extraction of the value when the ChainableResult itself is a temporary or has been moved from. It will throw std::bad_expected_access if called when the result contains an error rather than a success value.

Returns
An rvalue reference to the success value

Definition at line 165 of file chainable_result.hpp.

◆ value() [3/3]

template<typename T , typename E = FormattableError>
const T & porytiles2::ChainableResult< T, E >::value ( ) const &
inline

Returns a const reference to the contained success value.

This method provides immutable access to the success value. It will throw std::bad_expected_access if called when the result contains an error rather than a success value.

Returns
A const reference to the success value

Definition at line 150 of file chainable_result.hpp.


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