Resilient
Public Types | Public Member Functions | Friends | Related Functions | List of all members
resilient::Failable< Value, Failure > Class Template Reference

A class to represent the result of an operation which might fail. More...

#include <failable.hpp>

Inherits resilient::detail::Variant< Types >.

Public Types

using failure_type = Failure
 Alias for the type of the failure.
 
using value_type = Value
 Alias for the type of the value.
 

Public Member Functions

template<typename Other , if_is_failable< Other > = nullptr>
 Failable (Other &&other)
 Create a Failable from another Failable. More...
 
template<typename Other , if_is_not_failable< Other > = nullptr>
 Failable (Other &&other)
 Create a Failable from a either a Failure or a Value. More...
 
template<typename Other , if_is_failable< Other > = nullptr>
Failableoperator= (Other &&other)
 Assign the value of another Failable to this Failable. More...
 
template<typename Other , if_is_not_failable< Other > = nullptr>
Failableoperator= (Other &&other)
 Assign an object to this Failable. More...
 

Friends

template<typename Failable >
struct detail::as_variant
 
template<typename Failable_ >
detail::as_variant_t< Failable_ > detail::get_variant (Failable_ &&f)
 

Related Functions

(Note that these are not member functions.)

template<typename Value , typename Failure >
bool holds_failure (const Failable< Value, Failure > &failable)
 Check whether the Failable contains a failure. More...
 
template<typename Value , typename Failure >
bool holds_value (const Failable< Value, Failure > &failable)
 Check whether the Failable contains a value. More...
 
template<typename Failable , if_is_failable< Failable > = nullptr>
auto get_failure (Failable &&failable) -> detail::same_const_ref_as_t< Failable, detail::failable_failure_type_t< Failable >>
 Get the failure in the Failable. More...
 
template<typename Failable , if_is_failable< Failable > = nullptr>
auto get_value (Failable &&failable) -> detail::same_const_ref_as_t< Failable, detail::failable_value_type_t< Failable >>
 Get the value in the Failable. More...
 
template<typename Failable , if_is_failable< Failable > = nullptr>
auto get_value_or (Failable &&failable, detail::get_value_return_type< Failable > default_value) -> detail::get_value_return_type< Failable >
 Get the value in the Failable or default_value if not present. More...
 
template<typename Failable , typename OtherFailable , if_is_failable< Failable > = nullptr, if_is_failable< OtherFailable > = nullptr>
auto get_value_or (Failable &&failable, OtherFailable &&other) -> std::remove_const_t< std::remove_reference_t< OtherFailable >>
 Get the combination of two Failables sharing the same value_type. More...
 
template<typename Failable , typename Invocable , typename InvocableResult = detail::invoke_result_t<Invocable>, if_is_failable< Failable > = nullptr, detail::if_is_convertible_to_get_value_return_type_of< Failable, InvocableResult > = nullptr>
auto get_value_or_invoke (Failable &&failable, Invocable &&invocable) -> detail::get_value_return_type< Failable >
 Get the value in failable if it holds a value, the returned value of invoking invocable otherwise. More...
 
template<typename Failable , typename Invocable , typename InvocableResult = detail::invoke_result_t<Invocable>, if_is_failable< Failable > = nullptr, if_is_failable< InvocableResult > = nullptr>
auto get_value_or_invoke (Failable &&failable, Invocable &&invocable) -> std::remove_const_t< std::remove_reference_t< InvocableResult >>
 Get the combination of failable and the result of invoking invocable. More...
 
template<typename Visitor , typename Failable , if_is_failable< Failable > = nullptr>
 decltype (auto) resilient::visit(Visitor &&visitor, Failable &&failable)
 Apply a visitor to the Failable. More...
 
template<typename Failable , typename Failure >
Failable from_failure (Failure &&failure)
 Create a Failable with a failure type. More...
 
template<typename Failable , typename OtherFailable >
Failable from_narrower_failable (OtherFailable &&failable)
 Construct a Failable from another Failable with the same value_type and a narrower failure_type. More...
 

Detailed Description

template<typename Value, typename Failure>
class resilient::Failable< Value, Failure >

A class to represent the result of an operation which might fail.

A Failable is either a value or a failure. The value is the return value of the invoked function, the failure is an object representing the failure which happened.

Template Parameters
ValueThe value.
FailureThe type of the failure.

Constructor & Destructor Documentation

template<typename Value , typename Failure >
template<typename Other , if_is_failable< Other > = nullptr>
resilient::Failable< Value, Failure >::Failable ( Other &&  other)
inline

Create a Failable from another Failable.

Parameters
otherThe other Failable.
template<typename Value , typename Failure >
template<typename Other , if_is_not_failable< Other > = nullptr>
resilient::Failable< Value, Failure >::Failable ( Other &&  other)
inline

Create a Failable from a either a Failure or a Value.

Parameters
otherThe object to instantiate the Failable with.

Member Function Documentation

template<typename Value , typename Failure >
template<typename Other , if_is_failable< Other > = nullptr>
Failable& resilient::Failable< Value, Failure >::operator= ( Other &&  other)
inline

Assign the value of another Failable to this Failable.

Parameters
otherThe other Failable.
Returns
This Failable.
template<typename Value , typename Failure >
template<typename Other , if_is_not_failable< Other > = nullptr>
Failable& resilient::Failable< Value, Failure >::operator= ( Other &&  other)
inline

Assign an object to this Failable.

Parameters
otherThe object to assign.
Returns
This Failable.

Friends And Related Function Documentation

template<typename Visitor , typename Failable , if_is_failable< Failable > = nullptr>
decltype ( Visitor &&  visitor,
Failable< Value, Failure > &&  failable 
)
related

Apply a visitor to the Failable.

Invoke the visitor with either the failure or the value, depending on what the Failable is currently holding.

Parameters
visitorThe visitor to use.
failableThe Failable to visit.
Returns
The value returned by the visitor.
template<typename Failable , typename Failure >
Failable from_failure ( Failure &&  failure)
related

Create a Failable with a failure type.

Template Parameters
FailableThe type of the Failable.
Parameters
failureThe failure to use.
Returns
Failable The Failable initialized with the failure.
template<typename Failable , typename OtherFailable >
Failable from_narrower_failable ( OtherFailable< Value, Failure > &&  failable)
related

Construct a Failable from another Failable with the same value_type and a narrower failure_type.

A failure_type A is narrower than another type B if the variant which represents it can hold a subset of the types B can hold. For example in using B = resilient::add_failure_to_failable_t<A, NewFailure>, A has a narrower failure_type than B since A can not represent the failure NewFailure.

Template Parameters
FailableThe type of the Failable to create.
Parameters
failableThe failable to use when creatint the new Failable.
Returns
Failable The Failable initialized from the narrower failable.
template<typename Failable , if_is_failable< Failable > = nullptr>
auto get_failure ( Failable< Value, Failure > &&  failable) -> detail::same_const_ref_as_t<Failable, detail::failable_failure_type_t<Failable>>
related

Get the failure in the Failable.

Precondition
The Failable is currently holding a failure.
Parameters
failableThe Failable to get the failure from.
Returns
The failure.
template<typename Failable , if_is_failable< Failable > = nullptr>
auto get_value ( Failable< Value, Failure > &&  failable) -> detail::same_const_ref_as_t<Failable, detail::failable_value_type_t<Failable>>
related

Get the value in the Failable.

Precondition
The Failable is currently holding a value.
Parameters
failableThe Failable to get the value from.
Returns
The value.
template<typename Failable , if_is_failable< Failable > = nullptr>
auto get_value_or ( Failable< Value, Failure > &&  failable,
detail::get_value_return_type< Failable< Value, Failure > >  default_value 
) -> detail::get_value_return_type<Failable>
related

Get the value in the Failable or default_value if not present.

Parameters
failableThe Failable to get the value from if present.
default_valueThe default value if a value is not present in Failable
Returns
The value in Failable if present, default_value otherwise
template<typename Failable , typename OtherFailable , if_is_failable< Failable > = nullptr, if_is_failable< OtherFailable > = nullptr>
auto get_value_or ( Failable< Value, Failure > &&  failable,
OtherFailable< Value, Failure > &&  other 
) -> std::remove_const_t<std::remove_reference_t<OtherFailable>>
related

Get the combination of two Failables sharing the same value_type.

Get the value combining two Failables. The value of the first Failable is preferred. The failure of the second Failable is preferred.

Parameters
failableA failable
otherA failable with a compatible value_type of failable.
Returns
A Failable. It holds the value of failable if it contains a value. It holds the value of other if it contains a value. It holds the failure of other if both failables hold a failure. The returned type is always a value (not a reference).
template<typename Failable , typename Invocable , typename InvocableResult = detail::invoke_result_t<Invocable>, if_is_failable< Failable > = nullptr, detail::if_is_convertible_to_get_value_return_type_of< Failable, InvocableResult > = nullptr>
auto get_value_or_invoke ( Failable< Value, Failure > &&  failable,
Invocable &&  invocable 
) -> detail::get_value_return_type<Failable>
related

Get the value in failable if it holds a value, the returned value of invoking invocable otherwise.

See also
get_value_or()

The function is similar to get_value_or() but allows to compute the default value lazily.

Parameters
failableThe Failable to get the value from if present.
invocableThe object to call if failable does not hold a value.
Returns
The value stored in failable or the value returned by invocable.
template<typename Failable , typename Invocable , typename InvocableResult = detail::invoke_result_t<Invocable>, if_is_failable< Failable > = nullptr, if_is_failable< InvocableResult > = nullptr>
auto get_value_or_invoke ( Failable< Value, Failure > &&  failable,
Invocable &&  invocable 
) -> std::remove_const_t<std::remove_reference_t<InvocableResult>>
related

Get the combination of failable and the result of invoking invocable.

See also
get_value_or

Equivalent to get_value_or() with a Failable which is provided by lazily calling invocable. invocable is only called if failable does not contain a value.

Parameters
failableA Failable.
invocableA function which returns a Failable with value_type compatible to the one of failable.
Returns
The combination of failable and the result of invocable. See get_value_or() for what the result contains.
template<typename Value , typename Failure >
bool holds_failure ( const Failable< Value, Failure > &  failable)
related

Check whether the Failable contains a failure.

Parameters
failableThe Failable to check.
Returns
true If the Failable contains the failure.
false If the Failable contains the value.
template<typename Value , typename Failure >
bool holds_value ( const Failable< Value, Failure > &  failable)
related

Check whether the Failable contains a value.

Parameters
failableThe Failable to check.
Returns
true If the Failable contains the value.
false If the Failable contains the failure.

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