![]() |
BRICKS
Small, useful blocks of code, to build bigger things.
|
A class to represent the result of an operation. More...
#include <result.hpp>
Public Types | |
using | value_type = T |
The value type of the result. | |
using | error_type = E |
The error type of the result. | |
Public Member Functions | |
result ()=delete | |
result (const result &) noexcept(std::is_nothrow_copy_constructible_v< variant_t >)=default | |
auto | operator= (const result &) noexcept(std::is_nothrow_copy_assignable_v< variant_t >) -> result &=default |
result (result &&) noexcept=default | |
auto | operator= (result &&) noexcept -> result &=default |
~result ()=default | |
template<typename U , typename std::enable_if_t< std::is_same_v< U, ok< T > >||std::is_same_v< U, err< E > >, bool > = true> | |
constexpr | result (U in) noexcept(std::is_nothrow_move_constructible_v< U >) |
Construct a new result object from a value. | |
template<typename U = T, typename std::enable_if_t<!std::is_same_v< U, E >, bool > = true> | |
constexpr | result (value_type in) noexcept(std::is_nothrow_move_constructible_v< ok< T > >) |
template<typename U = E, typename std::enable_if_t<!std::is_same_v< T, U >, bool > = true> | |
constexpr | result (error_type in) noexcept(std::is_nothrow_move_constructible_v< err< E > >) |
template<typename U > | |
constexpr auto | operator= (U in) noexcept -> result & |
Assign a value to the result. | |
constexpr auto | is_value () const noexcept -> bool |
Check if the result is a value. | |
constexpr auto | is_error () const noexcept -> bool |
Check if the result is an error. | |
constexpr auto | expect (const std::string &msg) const -> value_type |
Returns the value of the result. | |
constexpr auto | unwrap () const -> value_type |
Returns the value of the result. | |
constexpr auto | expect_error (const std::string &msg) const -> error_type |
Returns the error of the result. | |
constexpr auto | unwrap_error () const -> error_type |
Returns the error of the result. | |
constexpr auto | unwrap_or (value_type default_value) const noexcept -> value_type |
Returns the value of the result. | |
constexpr auto | unwrap_or_default () const noexcept -> value_type |
Returns the value of the result or a default constructed value. | |
template<typename F > | |
constexpr auto | unwrap_or_else (F &&f) const -> value_type |
Returns the value of the result or the result of a function. | |
template<typename F > | |
constexpr auto | map (F &&f) const -> result< std::invoke_result_t< F, value_type >, error_type > |
Maps a result<T, E> to result<U, E> by applying a function to a contained value. | |
template<typename F > | |
constexpr auto | map_error (F &&f) const -> result< value_type, std::invoke_result_t< F, error_type > > |
Maps a result<T, E> to result<T, F> by applying a function to a contained error. | |
template<typename F > | |
constexpr auto | map_or (std::invoke_result_t< F, value_type > default_value, F &&f) const -> std::invoke_result_t< F, value_type > |
Returns the provided default (if an error) or applies a function to the contained value. | |
template<typename F , typename G > | |
constexpr auto | map_or_else (G &&default_f, F &&f) const -> std::invoke_result_t< F, value_type > |
Maps the result<T, E> to U by applying fallback function default_f to a contained error, or function f to a contained value. | |
template<typename U > | |
constexpr auto | and_instead (const result< U, error_type > &res) const -> result< U, error_type > |
Returns res if the result is a value, otherwise returns the error. | |
template<typename Op > | |
constexpr auto | and_then (Op &&op) const -> result< typename std::invoke_result_t< Op, value_type >::value_type, error_type > |
Calls op if the result is a value, otherwise returns the error. | |
template<typename F > | |
constexpr auto | or_instead (const result< value_type, F > &res) const -> result< value_type, F > |
Returns res if the result is an error, otherwise returns the value. | |
template<typename Op > | |
constexpr auto | or_else (Op &&op) const -> result< value_type, typename std::invoke_result_t< Op, error_type >::error_type > |
Calls op if the result is an error, otherwise returns the value. | |
constexpr auto | operator== (const result &other) const noexcept -> bool |
constexpr auto | operator!= (const result &other) const noexcept -> bool |
Static Public Member Functions | |
template<typename F > | |
static auto | from_try_or (F &&f, error_type error_value) noexcept(std::is_nothrow_constructible_v< err< E >, error_type >) -> result |
Construct a new result object from an operation that might throw. | |
template<typename F > | |
static auto | from_try_or_default (F &&f) noexcept(std::is_nothrow_invocable_v< decltype(result< T, E >::from_try_or< F >)> &&std::is_nothrow_constructible_v< error_type >) -> result |
Construct a new result object from an operation that might throw. | |
template<typename F , typename OnError > | |
static auto | from_try_or_else (F &&f, OnError &&on_error) noexcept(std::is_nothrow_invocable_v< OnError > &&std::is_nothrow_constructible_v< result< T, E >, std::invoke_result_t< OnError > >) -> result |
Construct a new result from an operation that might throw or the result of a function. | |
Friends | |
constexpr auto | std::hash (const result &r) const noexcept -> std::size_t |
A class to represent the result of an operation.
This class is used to represent the result of an operation, which can fail. It can either be a value or an error. It is similar to the std::expected
proposal or the rust Result
type.
The value type and the error type can be different. If they are the same, one can use the ok<T>
and err<E>
types to construct and assign the result.
Example:
using bricks::result< T, E >::error_type = E |
The error type of the result.
using bricks::result< T, E >::value_type = T |
The value type of the result.
|
delete |
|
defaultnoexcept |
|
defaultnoexcept |
|
default |
|
inlineconstexprnoexcept |
Construct a new result object from a value.
If the value and error types are the same, one must use the ok<T>
and err<E>
types to construct the result. Otherwise the value type must be convertible to the value type of the result. And the error type must be convertible to the error type of the result.
Example:
in | The value to construct the result from. |
|
inlineconstexprnoexcept |
|
inlineconstexprnoexcept |
|
inlinenodiscardconstexpr |
Returns res
if the result is a value, otherwise returns the error.
This function can be used for control flow based on result values.
Example:
res | The result to return if the result is a value. |
|
inlinenodiscardconstexpr |
Calls op
if the result is a value, otherwise returns the error.
This function can be used for control flow based on result values.
Example:
op | The operation to perform on the value. |
|
inlinenodiscardconstexpr |
Returns the value of the result.
Throws a bad_result_access
if the result is an error, with the provided message.
Example:
msg | The message to use in the exception. |
|
inlinenodiscardconstexpr |
Returns the error of the result.
Throws a bad_result_access
if the result is a value, with the provided message.
Example:
msg | The message to use in the exception. |
|
inlinestaticnodiscardnoexcept |
Construct a new result object from an operation that might throw.
Returns the result of calling function f
or the provided error value if the operation throws. This function is useful to convert functions that might throw to functions that return a result.
Example:
f | The operation to try. |
error_value | The error value to return if the operation throws. |
|
inlinestaticnodiscardnoexcept |
Construct a new result object from an operation that might throw.
Returns the result of calling function f
or a default constructed error value if f
throws. This function is useful to convert functions that might throw to functions that return a result.
Example:
f | The operation to try. |
|
inlinestaticnodiscardnoexcept |
Construct a new result from an operation that might throw or the result of a function.
Returns the result of calling f
if it doesn't throw. If the f
throws, returns the result of the provided on_error
function.
Example:
f | The operation to try. |
on_error | The function to call if f throws. |
|
inlinenodiscardconstexprnoexcept |
Check if the result is an error.
Example:
|
inlinenodiscardconstexprnoexcept |
Check if the result is a value.
Example:
|
inlinenodiscardconstexpr |
Maps a result<T, E>
to result<U, E>
by applying a function to a contained value.
This function can be used to compose the results of two functions.
Example:
F | The type of the function to apply. |
f | The function to apply. |
|
inlinenodiscardconstexpr |
Maps a result<T, E>
to result<T, F>
by applying a function to a contained error.
This function can be used to pass through a successful result while handling an error.
Example:
F | The type of the function to apply. |
f | The function to apply. |
|
inlinenodiscardconstexpr |
Returns the provided default (if an error) or applies a function to the contained value.
Example:
default_value | The default value to return if the result is an error. |
f | The function to apply if the result is a value. |
|
inlinenodiscardconstexpr |
Maps the result<T, E>
to U
by applying fallback function default_f
to a contained error, or function f
to a contained value.
This function can be used to unpack a successful result while handling an error.
Example:
default_f | The fallback function to apply if the result is an error. |
f | The function to apply if the result is a value. |
|
inlinenodiscardconstexprnoexcept |
|
defaultnoexcept |
|
defaultnoexcept |
|
inlineconstexprnoexcept |
Assign a value to the result.
If the value and error types are the same, one must use the ok<T>
and err<E>
types for the assignment. Otherwise the value type must be convertible to the value type of the result. And the error type must be convertible to the error type of the result.
Example:
|
inlinenodiscardconstexprnoexcept |
|
inlinenodiscardconstexpr |
Calls op
if the result is an error, otherwise returns the value.
This function can be used for control flow based on result values.
Example:
op | The operation to perform on the error. |
|
inlinenodiscardconstexpr |
Returns res
if the result is an error, otherwise returns the value.
This function can be used for control flow based on result values.
Example:
res | The result to return if the result is an error. |
|
inlinenodiscardconstexpr |
Returns the value of the result.
Throws a bad_result_access
if the result is an error.
Example:
|
inlinenodiscardconstexpr |
Returns the error of the result.
Throws a bad_result_access
if the result is a value.
Example:
|
inlinenodiscardconstexprnoexcept |
Returns the value of the result.
If the result is a value, returns the value. If the result is an error, returns the provided default value.
Example:
default_value | The default value to return if the result is an error. |
|
inlinenodiscardconstexprnoexcept |
Returns the value of the result or a default constructed value.
If the result is a value, returns the value. If the result is an error, returns the default constructed value.
Example:
|
inlinenodiscardconstexpr |
Returns the value of the result or the result of a function.
If the result is a value, returns the value. If the result is an error, returns the result of calling the provided function.
Example:
f | The function to call if the result is an error. The function must take a single parameter of type error_type and return a value of type value_type . |
|
friend |