File Result.hpp

namespace Acts

Note

This file is foreseen for the Geometry module to replace Extent

template<typename T, typename E = std::error_code>
class Result
#include <Acts/Utilities/Result.hpp>

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

Template Parameters
  • T – The valid result value

  • E – The error, defaults to std::error_code

Public Functions

Result() = delete

Default construction is disallowed.

Result(const Result<T, E> &other) = delete

Copy construction is disallowed.

inline Result(Result<T, E> &&other)

Move construction is allowed.

template<typename T2, typename _E = E, typename _T = T, typename = std::enable_if_t<(!std::is_same_v<_T, _E> && !std::is_constructible_v<_T, _E> && !std::is_convertible_v<_T, _E> && !std::is_constructible_v<_E, _T> && !std::is_convertible_v<_E, _T> && !(std::is_convertible_v<T2, _T> && std::is_convertible_v<T2, _E>))>>
inline 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 the 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 ambigious, use the success and failure static factory methods.

Template Parameters

T2 – Type of the potential assignment

Parameters

value – The potential value, could be an actual valid value or an error.

inline 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

inline E error() && noexcept

Returns the error by-value.

Note

If res.ok() this method will abort (noexcept)

Returns

The error

inline bool ok() const noexcept

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

Returns

bool Whether result contains an error or not.

inline T &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.

inline T *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.

Result<T, E> &operator=(const Result<T, E> &other) = delete

Assignment is disallowed.

inline Result<T, E> &operator=(Result<T, E> &&other)

Move assignment is allowed.

Parameters

other – The other result instance, rvalue reference

Returns

The assigned instance

template<typename T2, typename _E = E, typename _T = T, typename = std::enable_if_t<(!std::is_same_v<_T, _E> && !std::is_constructible_v<_T, _E> && !std::is_convertible_v<_T, _E> && !std::is_constructible_v<_E, _T> && !std::is_convertible_v<_E, _T> && !(std::is_convertible_v<T2, _T> && std::is_convertible_v<T2, _E>))>>
inline 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

T2 – Type of the potential assignment

Parameters

value – The potential value, could be an actual valid value or an error.

Returns

The assigned instance

inline T &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

inline T value() &&

Retrieves the valid value from the result object.

Note

This is the rvalue version, returns the value by-value and moves out of the variant.

Returns

The valid value by value, moved out of the variant.

Public Static Functions

static inline Result<T, E> failure(E error)

Static helper factory which forces assignment as an error.

Parameters

error – The error to assign. Will not be converted to T.

Returns

Initialized result object

static inline Result<T, E> success(T value)

Static helper factory which forces assignment as valid value.

Parameters

value – The valid value to assign. Will not be converted to E.

Returns

Initialized result object

Private Functions

inline Result(std::variant<T, E> &&var)

Private constructor which accepts an external variant.

This is used by the factory static methods to set up the variant unambiguously in all cases.

Private Members

std::variant<T, E> m_var
template<typename E>
class Result<void, E>
#include <Acts/Utilities/Result.hpp>

Template specialization for the void case.

This specialization handles the case where there is no actual return value, but an error might be returned. Returning the error directly would make handling different from other functions using the Result<T, E> mechanism. Result<void, E> does not have the dereference operator, and value methods. The static success factory does not accept a value.

Note

To ease usage, this Result<void, E> is default constructible in the ok state, whereas Result<T, E> is not.

Template Parameters

E – The type of the error

Public Functions

Result() = default

Default constructor which initializes the result in the ok state.

Result(const Result<void, E> &other) = default

The copy constructor is deleted.

inline Result(Result<void, E> &&other)

Move constructor.

Parameters

other – The other result object, rvalue ref

template<typename E2>
inline Result(E2 error) noexcept

Constructor from error.

This implicitly requires E2 to be convertible to E.

Template Parameters

E2 – The type of the actual error

Parameters

error – The instance of the actual error

inline 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

inline E error() && noexcept

Returns the error by-value.

Note

If res.ok() this method will abort (noexcept)

Returns

Reference to the error

inline bool ok() const noexcept

Checks whether this result is in the ok state, and no error.

Returns

bool Whether result contains an error or not.

Result<void, E> &operator=(const Result<void, E> &other) = default

The (self) assignment operator is deleted.

inline Result<void, E> &operator=(Result<void, E> &&other) noexcept

Move assignment operator.

Parameters

other – The other result object, rvalue ref

template<typename E2>
inline Result<void, E> &operator=(E2 error)

Assignment operator from an error.

Template Parameters

E2 – The type of the actual error

Parameters

error – The instance of the actual error

Returns

The assigned instance

Public Static Functions

static inline Result<void, E> failure(E error)

Static factory function to initialize the result in the error state.

Parameters

error – The errorr to initialize with.

Returns

Result object, in error state.

static inline Result<void, E> success()

Static factory function to initialize the result in the ok state.

Returns

Result object, in ok state

Private Members

std::optional<E> m_opt