Sen API
Sen Libraries
Loading...
Searching...
No Matches
sen::Result< T, E > Class Template Referencefinal

Result<T, E> is a template type that can be used to return and propagate errors. The intent is to replace exceptions in this context. Result<T, E> is an algebraic data type of Ok(T) that represents success and Err(E) representing an error. More...

#include <result.h>

Public Types

using ValueType = T
using ErrorType = E

Public Member Functions

 Result (impl::Ok< T > &&ok) noexcept
 Construct a Result that indicates success and that carries a valid return value.
 Result (impl::Err< E > &&err) noexcept
 Construct a Result that indicates failure and that carries an error value.
 ~Result ()=default
template<typename U, typename G, std::enable_if_t< std::conjunction_v< std::is_constructible< T, const U & >, std::is_constructible< E, const G & > >, bool > = true>
 Result (const Result< U, G > &other)
 Do a conversion construction from a compatible Result.
template<typename U, typename G, std::enable_if_t< std::conjunction_v< std::is_constructible< T, U >, std::is_constructible< E, G > >, bool > = true>
 Result (Result< U, G > &&other)
 Do a conversion construction from a compatible Result.
bool operator== (const Result &other) const
bool operator!= (const Result &other) const
bool isOk () const noexcept
 Used to determine if the result is not an error.
bool isError () const noexcept
 Used to determine if the result is an error.
 operator bool () const noexcept
 Used to determine if the result is not an error.
template<typename U = T>
const impl::NonVoidT< U > & getValueOr (const U &defaultVal) const noexcept
 Used to determine the return value. If the result indicates an error this method will return defaultValue.
template<typename U = T>
const impl::NonVoidT< U > & getValue () const &
 Used to determine the success value, given that there is no error.
template<typename U = T>
impl::NonVoidT< U > && getValue () &&
 Used to move out the success value, given that there is no error.
const E & getError () const
 Used to determine the error value, given that there is an error.
template<typename U = T>
const impl::NonVoidT< U > & expect (std::string_view errorMsg={}) const noexcept
 Extracts the value of the correct result, or terminates the program with a given error message.

Detailed Description

template<typename T, typename E>
class sen::Result< T, E >

Result<T, E> is a template type that can be used to return and propagate errors. The intent is to replace exceptions in this context. Result<T, E> is an algebraic data type of Ok(T) that represents success and Err(E) representing an error.

Design of this class has been mainly inspired by Rust's std::result. Example:

enum class ErrorCode { too_small, too_large };
Result<int, ErrorCode> doSomething(int a) noexcept
{
if (a < 0)
{
return Err(ErrorCode::too_small);
}
return Ok(a + 100);
}
Result<void, ErrorCode> doSomethingElse(int a) noexcept
{
if (a < 0)
{
return Err(ErrorCode::too_small);
}
return Ok();
}
Result<T, E> is a template type that can be used to return and propagate errors. The intent is to rep...
Definition result.h:135
Result(impl::Ok< T > &&ok) noexcept
Construct a Result that indicates success and that carries a valid return value.
Definition result.h:145
impl::Err< void > Err() noexcept
If E is void, use the void specialization of Err.
Definition result.h:439
impl::Ok< void > Ok() noexcept
If T is void, use the void specialization of Ok.
Definition result.h:434

Use isOk(), isError() or the bool() operator to check if there was an error. Use getError() to get the error data (terminates the program if there was no error). Use getValue() to determine the return value if there is no error (terminates the program otherwise). Use getValueOr(X) as getValue() but returns X if there was an error.

Example:

if (auto result = doSomething()) // result cannot be ignored (would not compile)
{
const auto& val = result.getValue(); // do something with val, if there is any
} // (getValue is not present when T is 'void')
else
{
auto err = result.getError(); // do something with err
}
Note
This class is not thread-aware.
Template Parameters
Tthe type of the result if there is no error (can be void).
Ethe type of the error.

Member Typedef Documentation

◆ ValueType

template<typename T, typename E>
using sen::Result< T, E >::ValueType = T

◆ ErrorType

template<typename T, typename E>
using sen::Result< T, E >::ErrorType = E

Constructor & Destructor Documentation

◆ Result() [1/4]

template<typename T, typename E>
sen::Result< T, E >::Result ( impl::Ok< T > && ok)
inlinenoexcept

Construct a Result that indicates success and that carries a valid return value.

◆ Result() [2/4]

template<typename T, typename E>
sen::Result< T, E >::Result ( impl::Err< E > && err)
inlinenoexcept

Construct a Result that indicates failure and that carries an error value.

◆ ~Result()

template<typename T, typename E>
sen::Result< T, E >::~Result ( )
default

◆ Result() [3/4]

template<typename T, typename E>
template<typename U, typename G, std::enable_if_t< std::conjunction_v< std::is_constructible< T, const U & >, std::is_constructible< E, const G & > >, bool > = true>
sen::Result< T, E >::Result ( const Result< U, G > & other)
inline

Do a conversion construction from a compatible Result.

◆ Result() [4/4]

template<typename T, typename E>
template<typename U, typename G, std::enable_if_t< std::conjunction_v< std::is_constructible< T, U >, std::is_constructible< E, G > >, bool > = true>
sen::Result< T, E >::Result ( Result< U, G > && other)
inline

Do a conversion construction from a compatible Result.

Member Function Documentation

◆ operator==()

template<typename T, typename E>
bool sen::Result< T, E >::operator== ( const Result< T, E > & other) const
inline

◆ operator!=()

template<typename T, typename E>
bool sen::Result< T, E >::operator!= ( const Result< T, E > & other) const
inline

◆ isOk()

template<typename T, typename E>
bool sen::Result< T, E >::isOk ( ) const
inlinenodiscardnoexcept

Used to determine if the result is not an error.

Returns
true - object holds a valid data of type T false - object does not hold valid data of type T

◆ isError()

template<typename T, typename E>
bool sen::Result< T, E >::isError ( ) const
inlinenodiscardnoexcept

Used to determine if the result is an error.

Returns
true - object holds an error of type E false - object does not hold an error of type E

◆ operator bool()

template<typename T, typename E>
sen::Result< T, E >::operator bool ( ) const
inlineexplicitnoexcept

Used to determine if the result is not an error.

Returns
true - object holds a valid data of type T false - object does not hold valid data of type T

◆ getValueOr()

template<typename T, typename E>
template<typename U>
const impl::NonVoidT< U > & sen::Result< T, E >::getValueOr ( const U & defaultVal) const
inlinenodiscardnoexcept

Used to determine the return value. If the result indicates an error this method will return defaultValue.

Parameters
defaultValThe value to be used in case of holding an error.
Returns
defaultVal if this object holds an error, the stored success value otherwise.

◆ getValue() [1/2]

template<typename T, typename E>
template<typename U = T>
const impl::NonVoidT< U > & sen::Result< T, E >::getValue ( ) const &
inlinenodiscard

Used to determine the success value, given that there is no error.

Warning
if this object holds an error, calling this function will cause program to terminate.
Precondition
isOk() || !isError()
Returns
the success value stored in this object.

◆ getValue() [2/2]

template<typename T, typename E>
template<typename U = T>
impl::NonVoidT< U > && sen::Result< T, E >::getValue ( ) &&
inlinenodiscard

Used to move out the success value, given that there is no error.

Warning
if this object holds an error, calling this function will cause program to terminate.
Precondition
isOk() || !isError()
Returns
the success value stored in this object.

◆ getError()

template<typename T, typename E>
const E & sen::Result< T, E >::getError ( ) const
inlinenodiscard

Used to determine the error value, given that there is an error.

Warning
if this object does not hold an error, calling this function will cause program to terminate.
Precondition
isError() || !isOk()
Returns
the error value stored in this object.

◆ expect()

template<typename T, typename E>
template<typename U = T>
const impl::NonVoidT< U > & sen::Result< T, E >::expect ( std::string_view errorMsg = {}) const
inlinenodiscardnoexcept

Extracts the value of the correct result, or terminates the program with a given error message.


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