Cluedo Solver v1.0
Cluedo game solver making deductions.
Loading...
Searching...
No Matches
Result< ValueType, ErrorType > Class Template Reference

A class that represents a result that can either be a value or an error. More...

#include <Result.hpp>

Public Member Functions

 Result (ValueType &&value)
 
 Result (ErrorType &&error)
 
 Result (Result const &)=default
 Copies a Result object.
 
 Result (Result &&)=default
 Moves a Result object.
 
 ~Result ()=default
 Destroys a Result object.
 
bool is_value () const
 
bool is_error () const
 
ValueType & value ()
 
ValueType && release_value ()
 
ErrorType & error ()
 
ErrorType && release_error ()
 

Detailed Description

template<typename ValueType, typename ErrorType>
class Result< ValueType, ErrorType >

A class that represents a result that can either be a value or an error.

This class is used in error handling scenarios instead of exceptions. Whenever a function may return an error it will return a Result object that can be checked for the error and handled accordingly.

The advantage of this approach is that it makes errors more explicit than exceptions by specifying them in the return value and forcing them to handle them.

Using this class is also made easier by the TRY and MUST macros.

Template Parameters
ValueTypeThe type of the value that the Result object may contain.
ErrorTypeThe type of the error that the Result object may contain.

Constructor & Destructor Documentation

◆ Result() [1/2]

template<typename ValueType, typename ErrorType>
Result< ValueType, ErrorType >::Result ( ValueType && value)
inline

Construct a Result object with a value.

Parameters
valueThe value that will be stored in the Result object.

◆ Result() [2/2]

template<typename ValueType, typename ErrorType>
Result< ValueType, ErrorType >::Result ( ErrorType && error)
inline

Construct a Result object with an error.

Parameters
errorThe error that will be stored in the Result object.

Member Function Documentation

◆ error()

template<typename ValueType, typename ErrorType>
ErrorType & Result< ValueType, ErrorType >::error ( )
inline

Returns the error stored in the Result object.

Note
This function will fail if the Result object doesn't contain an error.
Returns
The error stored in the Result object

◆ is_error()

template<typename ValueType, typename ErrorType>
bool Result< ValueType, ErrorType >::is_error ( ) const
inline

Checks if the Result object contains an error.

Returns
true if the Result object contains a value, false otherwise.

◆ is_value()

template<typename ValueType, typename ErrorType>
bool Result< ValueType, ErrorType >::is_value ( ) const
inline

Checks if the Result object contains a value.

Returns
true if the Result object contains a value, false otherwise.

◆ release_error()

template<typename ValueType, typename ErrorType>
ErrorType && Result< ValueType, ErrorType >::release_error ( )
inline

Returns the error stored in the Result object and moves it.

Note
This function will fail if the Result object doesn't contain an error.
Returns
The error stored in the Result object

◆ release_value()

template<typename ValueType, typename ErrorType>
ValueType && Result< ValueType, ErrorType >::release_value ( )
inline

Returns the value stored in the Result object and moves it.

Note
This function will fail if the Result object doesn't contain a value.
Returns
The value stored in the Result object

◆ value()

template<typename ValueType, typename ErrorType>
ValueType & Result< ValueType, ErrorType >::value ( )
inline

Returns the value stored in the Result object.

Note
This function will fail if the Result object doesn't contain a value.
Returns
The value stored in the Result object

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