rsl 1.1.0
ROS Support Library
Loading...
Searching...
No Matches
monad.hpp File Reference
#include <tl_expected/expected.hpp>
#include <optional>

Go to the source code of this file.

Functions

template<typename T , typename Fn >
constexpr auto rsl::mbind (std::optional< T > const &opt, Fn fn) -> std::invoke_result_t< Fn, T >
 Monad optional bind.
 
template<typename T , typename E , typename Fn >
constexpr auto rsl::mbind (tl::expected< T, E > const &exp, Fn fn) -> std::invoke_result_t< Fn, T >
 Monad tl::expected<T,E>
 
template<typename Fn >
auto rsl::mtry (Fn fn) -> tl::expected< std::invoke_result_t< Fn >, std::exception_ptr > try
 Monadic try, used to lift a function that throws an exception into one that returns an tl::expected<T, std::exception_ptr>
 
 rsl::catch (...)
 
template<typename Fn , typename G >
constexpr auto rsl::mcompose (Fn fn, G g)
 Monadic compose two monad functions.
 
template<typename T , typename G , typename... Ts>
constexpr auto rsl::mcompose (T t, G g, Ts... vars)
 Variadic mcompose.
 
template<typename T , typename E >
constexpr auto rsl::has_error (tl::expected< T, E > const &exp)
 Test if expected type is Error.
 
template<typename T , typename E >
constexpr auto rsl::has_value (tl::expected< T, E > const &exp)
 Test if expected type is Value.
 
template<typename E , typename... Args>
constexpr auto rsl::maybe_error (tl::expected< Args, E >... args)
 Tests if any of the expected args passed in has an error.
 
template<typename T , typename Fn , typename = std::enable_if_t<rsl::is_optional<T>>, typename = std::enable_if_t<std::is_invocable_v< Fn, typename std::remove_cv_t<std::remove_reference_t<T>>::value_type>>>
constexpr auto operator| (T &&opt, Fn &&fn)
 Overload of the | operator as bind.
 
template<typename T , typename E , typename Fn >
constexpr auto operator| (tl::expected< T, E > const &exp, Fn fn)
 Overload of the | operator as bind.
 
template<typename T , typename Fn , typename = std::enable_if_t<!rsl::is_optional<T>>>
constexpr auto operator| (T &&val, Fn &&fn) -> typename std::enable_if_t< std::is_invocable_v< Fn, T >, std::invoke_result_t< Fn, T > >
 Overload of the | operator for unary functions.
 

Variables

template<typename >
constexpr bool rsl::is_optional_impl = false
 
template<typename T >
constexpr bool rsl::is_optional = is_optional_impl<std::remove_cv_t<std::remove_reference_t<T>>>
 

Function Documentation

◆ has_error()

template<typename T , typename E >
constexpr auto rsl::has_error ( tl::expected< T, E > const &  exp)
constexpr

Test if expected type is Error.

Parameters
expInput tl::expected<T,E> value
Returns
True if expected paraemter is Error

◆ has_value()

template<typename T , typename E >
constexpr auto rsl::has_value ( tl::expected< T, E > const &  exp)
constexpr

Test if expected type is Value.

Parameters
expInput tl::expected<T,E> value
Returns
True if expected paraemter is Value

◆ maybe_error()

template<typename E , typename... Args>
constexpr auto rsl::maybe_error ( tl::expected< Args, E >...  args)
constexpr

Tests if any of the expected args passed in has an error.

Parameters
argstl::expected<T, E> parameter pack
Template Parameters
EThe error type
ArgsThe value types for the tl::expected<T, E> args
Returns
The first error found or nothing

◆ mbind() [1/2]

template<typename T , typename Fn >
constexpr auto rsl::mbind ( std::optional< T > const &  opt,
Fn  fn 
) -> std::invoke_result_t<Fn, T>
constexpr

Monad optional bind.

Parameters
optInput optional
fnFunction, must return a optional
Template Parameters
TInput type
FnFunction
Returns
Return type of fn

◆ mbind() [2/2]

template<typename T , typename E , typename Fn >
constexpr auto rsl::mbind ( tl::expected< T, E > const &  exp,
Fn  fn 
) -> std::invoke_result_t<Fn, T>
constexpr

Monad tl::expected<T,E>

Parameters
exptl::expected<T,E> input
fnFunction to apply
Template Parameters
TType for the input expected
EError type
FnFunction
Returns
Return type of the function

◆ mcompose() [1/2]

template<typename Fn , typename G >
constexpr auto rsl::mcompose ( Fn  fn,
g 
)
constexpr

Monadic compose two monad functions.

Parameters
fnFirst function
gSecond function
Template Parameters
FnType of the first function
GType of the second function
Returns
A functional composition of two monad functions

◆ mcompose() [2/2]

template<typename T , typename G , typename... Ts>
constexpr auto rsl::mcompose ( t,
g,
Ts...  vars 
)
constexpr

Variadic mcompose.

Parameters
tFirst function
gSecond function
varsRest of the functions
Template Parameters
TType of the first function
GType of the second function
TsTypes of the rest of the functions
Returns
A functional composition of variadic monad functions

◆ mtry()

template<typename Fn >
auto rsl::mtry ( Fn  fn) -> tl::expected<std::invoke_result_t<Fn>, std::exception_ptr> try

Monadic try, used to lift a function that throws an exception into one that returns an tl::expected<T, std::exception_ptr>

Parameters
fnFunction to call
Template Parameters
FnFunction type
Returns
Return value of the function

◆ operator|() [1/3]

template<typename T , typename Fn , typename = std::enable_if_t<rsl::is_optional<T>>, typename = std::enable_if_t<std::is_invocable_v< Fn, typename std::remove_cv_t<std::remove_reference_t<T>>::value_type>>>
constexpr auto operator| ( T &&  opt,
Fn &&  fn 
)
constexpr

Overload of the | operator as bind.

Parameters
optInput optional
fnFunction
Template Parameters
TInput type
FnFunction
Returns
Return type of f

◆ operator|() [2/3]

template<typename T , typename Fn , typename = std::enable_if_t<!rsl::is_optional<T>>>
constexpr auto operator| ( T &&  val,
Fn &&  fn 
) -> typename std::enable_if_t<std::is_invocable_v<Fn, T>, std::invoke_result_t<Fn, T>>
constexpr

Overload of the | operator for unary functions.

Parameters
valInput value
fnFunction to apply on val
Template Parameters
TType for the input
FnFunction
Returns
Return the result of invoking the function on val

◆ operator|() [3/3]

template<typename T , typename E , typename Fn >
constexpr auto operator| ( tl::expected< T, E > const &  exp,
Fn  fn 
)
constexpr

Overload of the | operator as bind.

Parameters
expInput tl::expected<T,E> value
fnFunction to apply
Template Parameters
TType for the input expected
EError type
FnFunction
Returns
Return type of fn