ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
Acts::Result< T, E > Class Template Reference

Class which encapsulates either a valid result, or an error. More...

#include <Acts/Utilities/Result.hpp>

Inheritance diagram for Acts::Result< T, E >:
[legend]

Public Types

using ErrorType = E
 Type alias for the error type contained in failed result.
using ValueType = T
 Type alias for the value type contained in successful result.

Public Member Functions

 Result ()=delete
 Default construction is disallowed.
 Result (const Result< T, E > &other)=delete
 Copy construction is disallowed.
 Result (Result< T, E > &&other) noexcept
 Move construction is allowed.
template<typename T2>
requires (!std::same_as<T, E> && !std::constructible_from<T, E> && !std::convertible_to<T, E> && !std::constructible_from<E, T> && !std::convertible_to<E, T> && !(std::convertible_to<T2, T> && std::convertible_to<T2, E>))
 Result (T2 value) noexcept
 Constructor from arbitrary value This constructor allows construction from any value.
template<typename C>
requires std::invocable<C, T&&>
auto and_then (C &&callable) &&
 Bind a function to this result monadically.
template<typename C>
requires std::invocable<C, const T&>
auto and_then (C &&callable) const &
 Bind a function to this result monadically.
error () &&noexcept
 Returns the error by-value.
E & error () &noexcept
 Returns a reference to the error stored in the result.
const E & error () const &noexcept
 Returns a reference to the error stored in the result.
bool ok () const noexcept
 Checks whether this result contains a valid value, and no error.
const T & operator* () const noexcept
 Returns a reference into the variant to the valid value.
T & operator* () noexcept
 Returns a reference into the variant to the valid value.
const T * operator-> () const noexcept
 Allows to access members of the stored object with res->foo similar to std::optional.
T * operator-> () noexcept
 Allows to access members of the stored object with res->foo similar to std::optional.
Result< T, E > & operator= (const Result< T, E > &other)=delete
 Assignment is disallowed.
Result< T, E > & operator= (Result< T, E > &&other) noexcept
 Move assignment is allowed.
template<typename T2>
requires (!std::same_as<T, E> && !std::constructible_from<T, E> && !std::convertible_to<T, E> && !std::constructible_from<E, T> && !std::convertible_to<E, T> && !(std::convertible_to<T2, T> && std::convertible_to<T2, E>))
Result< T, E > & operator= (T2 value) noexcept
 Assignment operator from arbitrary value This operator allows construction from any value.
template<typename C>
requires std::invocable<C, T&&>
auto transform (C &&callable) &&
 Transforms the value contained in this result.
template<typename C>
requires std::invocable<C, const T&>
auto transform (C &&callable) const &
 Transforms the value contained in this result.
T & value () &
 Retrieves the valid value from the result object.
const T & value () const &
 Retrieves the valid value from the result object.
template<typename U>
requires (std::same_as<std::decay_t<U>, T>)
std::conditional_t< std::is_reference_v< U >, const T &, T > value_or (U &&v) const &
 Retrieves the valid value from the result object, or returns a default value if no valid value exists.

Static Public Member Functions

static Result< T, E > failure (E error)
 Static helper factory which forces assignment as an error.
static Result< T, E > success (T value)
 Static helper factory which forces assignment as valid value.

Detailed Description

template<typename T, typename E = std::error_code>
class Acts::Result< T, E >

Class which encapsulates either a valid result, or an error.

Template Parameters
TThe valid result value
EThe error, defaults to std::error_code

Member Typedef Documentation

◆ ErrorType

template<typename T, typename E = std::error_code>
using Acts::Result< T, E >::ErrorType = E

Type alias for the error type contained in failed result.

◆ ValueType

template<typename T, typename E = std::error_code>
using Acts::Result< T, E >::ValueType = T

Type alias for the value type contained in successful result.

Constructor & Destructor Documentation

◆ Result() [1/4]

template<typename T, typename E = std::error_code>
Acts::Result< T, E >::Result ( )
delete

Default construction is disallowed.

◆ Result() [2/4]

template<typename T, typename E = std::error_code>
Acts::Result< T, E >::Result ( const Result< T, E > & other)
delete

Copy construction is disallowed.

◆ Result() [3/4]

template<typename T, typename E = std::error_code>
Acts::Result< T, E >::Result ( Result< T, E > && other)
noexcept

Move construction is allowed.

Parameters
otherThe other result instance to move from

◆ Result() [4/4]

template<typename T, typename E = std::error_code>
template<typename T2>
requires (!std::same_as<T, E> && !std::constructible_from<T, E> && !std::convertible_to<T, E> && !std::constructible_from<E, T> && !std::convertible_to<E, T> && !(std::convertible_to<T2, T> && std::convertible_to<T2, E>))
Acts::Result< T, E >::Result ( T2 value)
noexcept

Constructor from arbitrary value This constructor allows construction from any value.

This constructor is only enabled if T and E are unambiguous, meaning they cannot be implicitly converted and there is T cannot be constructed from E and vice-versa. This means that when this is invoked, the value can be propagated to the underlying variant, and the assignment will be correct, and error will be an error, and a value will be a value.

Note
If T and E are ambiguous, use the success and failure static factory methods.
Template Parameters
T2Type of the potential assignment
Parameters
valueThe potential value, could be an actual valid value or an error.

Member Function Documentation

◆ and_then() [1/2]

template<typename T, typename E = std::error_code>
template<typename C>
requires std::invocable<C, T&&>
auto Acts::Result< T, E >::and_then ( C && callable) &&

Bind a function to this result monadically.

This function takes a function f and, if this result contains a valid value x, returns f(x). If the type of x is T, then f is expected to accept type T and return Result<U>. In this case, transform would return the unhelpful type Result<Result<U>>, so and_then strips away the outer layer to return Result<U>. If the value is invalid, this returns an invalid value in Result<U>.

Parameters
[in]callableThe transformation function to apply.
Note
This is the rvalue version.
This functions is >>= on the functor in A of Result<A, E>.
Returns
The modified valid value if exists, or an error otherwise.

◆ and_then() [2/2]

template<typename T, typename E = std::error_code>
template<typename C>
requires std::invocable<C, const T&>
auto Acts::Result< T, E >::and_then ( C && callable) const &

Bind a function to this result monadically.

This function takes a function f and, if this result contains a valid value x, returns f(x). If the type of x is T, then f is expected to accept type T and return Result<U>. In this case, transform would return the unhelpful type Result<Result<U>>, so and_then strips away the outer layer to return Result<U>. If the value is invalid, this returns an invalid value in Result<U>.

Parameters
[in]callableThe transformation function to apply.
Note
This is the lvalue version.
This functions is >>= on the functor in A of Result<A, E>.
Returns
The modified valid value if exists, or an error otherwise.

◆ error() [1/3]

template<typename T, typename E = std::error_code>
E Acts::Result< T, E >::error ( ) &&
noexcept

Returns the error by-value.

Note
If res.ok() this method will abort (noexcept)
Returns
The error

◆ error() [2/3]

template<typename T, typename E = std::error_code>
E & Acts::Result< T, E >::error ( ) &
noexcept

Returns a reference to the error stored in the result.

Note
If res.ok() this method will abort (noexcept)
Returns
Reference to the error

◆ error() [3/3]

template<typename T, typename E = std::error_code>
const E & Acts::Result< T, E >::error ( ) const &
noexcept

Returns a reference to the error stored in the result.

Note
If res.ok() this method will abort (noexcept)
Returns
Reference to the error

◆ failure()

template<typename T, typename E = std::error_code>
Result< T, E > Acts::Result< T, E >::failure ( E error)
static

Static helper factory which forces assignment as an error.

Parameters
errorThe error to assign. Will not be converted to T.
Returns
Initialized result object

◆ ok()

template<typename T, typename E = std::error_code>
bool Acts::Result< T, E >::ok ( ) const
noexcept

Checks whether this result contains a valid value, and no error.

Returns
bool Whether result contains an error or not.

◆ operator*() [1/2]

template<typename T, typename E = std::error_code>
const T & Acts::Result< T, E >::operator* ( ) const
noexcept

Returns a reference into the variant to the valid value.

Note
If !res.ok(), this method will abort (noexcept)
Returns
Reference to value stored in the variant.

◆ operator*() [2/2]

template<typename T, typename E = std::error_code>
T & Acts::Result< T, E >::operator* ( )
noexcept

Returns a reference into the variant to the valid value.

Note
If !res.ok(), this method will abort (noexcept)
Returns
Reference to value stored in the variant.

◆ operator->() [1/2]

template<typename T, typename E = std::error_code>
const T * Acts::Result< T, E >::operator-> ( ) const
noexcept

Allows to access members of the stored object with res->foo similar to std::optional.

Note
If !res.ok(), this method will abort (noexcept)
Returns
Pointer to value stored in the variant.

◆ operator->() [2/2]

template<typename T, typename E = std::error_code>
T * Acts::Result< T, E >::operator-> ( )
noexcept

Allows to access members of the stored object with res->foo similar to std::optional.

Note
If !res.ok(), this method will abort (noexcept)
Returns
Pointer to value stored in the variant.

◆ operator=() [1/3]

template<typename T, typename E = std::error_code>
Result< T, E > & Acts::Result< T, E >::operator= ( const Result< T, E > & other)
delete

Assignment is disallowed.

◆ operator=() [2/3]

template<typename T, typename E = std::error_code>
Result< T, E > & Acts::Result< T, E >::operator= ( Result< T, E > && other)
noexcept

Move assignment is allowed.

Parameters
otherThe other result instance, rvalue reference
Returns
The assigned instance

◆ operator=() [3/3]

template<typename T, typename E = std::error_code>
template<typename T2>
requires (!std::same_as<T, E> && !std::constructible_from<T, E> && !std::convertible_to<T, E> && !std::constructible_from<E, T> && !std::convertible_to<E, T> && !(std::convertible_to<T2, T> && std::convertible_to<T2, E>))
Result< T, E > & Acts::Result< T, E >::operator= ( T2 value)
noexcept

Assignment operator from arbitrary value This operator allows construction from any value.

The same rules as for the Result(T2 value) constructor apply.

  • Template Parameters
    T2Type of the potential assignment
    Parameters
    valueThe potential value, could be an actual valid value or an error.
    Returns
    The assigned instance

◆ success()

template<typename T, typename E = std::error_code>
Result< T, E > Acts::Result< T, E >::success ( T value)
static

Static helper factory which forces assignment as valid value.

Parameters
valueThe valid value to assign. Will not be converted to E.
Returns
Initialized result object

◆ transform() [1/2]

template<typename T, typename E = std::error_code>
template<typename C>
requires std::invocable<C, T&&>
auto Acts::Result< T, E >::transform ( C && callable) &&

Transforms the value contained in this result.

Applying a function f to a valid value x returns f(x), while applying f to an invalid value returns another invalid value.

Parameters
[in]callableThe transformation function to apply.
Note
This is the rvalue version.
This functions is fmap on the functor in A of Result<A, E>.
Returns
The modified valid value if exists, or an error otherwise.

◆ transform() [2/2]

template<typename T, typename E = std::error_code>
template<typename C>
requires std::invocable<C, const T&>
auto Acts::Result< T, E >::transform ( C && callable) const &

Transforms the value contained in this result.

Applying a function f to a valid value x returns f(x), while applying f to an invalid value returns another invalid value.

Parameters
[in]callableThe transformation function to apply.
Note
This is the lvalue version.
This functions is fmap on the functor in A of Result<A, E>.
Returns
The modified valid value if exists, or an error otherwise.

◆ value() [1/2]

template<typename T, typename E = std::error_code>
T & Acts::Result< T, E >::value ( ) &

Retrieves the valid value from the result object.

Note
This is the lvalue version, returns a reference to the value
Returns
The valid value as a reference

◆ value() [2/2]

template<typename T, typename E = std::error_code>
const T & Acts::Result< T, E >::value ( ) const &

Retrieves the valid value from the result object.

Note
This is the lvalue version, returns a reference to the value
Returns
The valid value as a reference

◆ value_or()

template<typename T, typename E = std::error_code>
template<typename U>
requires (std::same_as<std::decay_t<U>, T>)
std::conditional_t< std::is_reference_v< U >, const T &, T > Acts::Result< T, E >::value_or ( U && v) const &

Retrieves the valid value from the result object, or returns a default value if no valid value exists.

Parameters
[in]vThe default value to use if no valid value exists.
Note
This is the lvalue version.
This function always returns by value.
Returns
Either the valid value, or the given substitute.