fp
Functional Programming extensions to C++ for ROS projects.
fp Namespace Reference

Classes

struct  Error
 Error type used by Result<T> More...
 
struct  NoDiscard
 
struct  validate_range
 

Typedefs

template<typename T , typename E = Error>
using Result = tl::expected< T, E >
 

Enumerations

enum  ErrorCode : int {
  ErrorCode::UNKNOWN, ErrorCode::CANCELLED, ErrorCode::INVALID_ARGUMENT, ErrorCode::TIMEOUT,
  ErrorCode::NOT_FOUND, ErrorCode::ALREADY_EXISTS, ErrorCode::PERMISSION_DENIED, ErrorCode::RESOURCE_EXHAUSTED,
  ErrorCode::FAILED_PRECONDITION, ErrorCode::ABORTED, ErrorCode::OUT_OF_RANGE, ErrorCode::UNIMPLEMENTED,
  ErrorCode::INTERNAL, ErrorCode::UNAVAILABLE, ErrorCode::DATA_LOSS, ErrorCode::UNAUTHENTICATED,
  ErrorCode::EXCEPTION
}
 Enum for ErrorCodes inspired by absl::StatusCode. More...
 

Functions

template<typename T >
constexpr std::optional< T > make_opt (T value)
 Makes an optional<T> from a T value. More...
 
template<typename T , typename F >
constexpr auto mbind (const std::optional< T > &opt, F f) -> decltype(f(opt.value()))
 Monad optional bind. More...
 
template<typename T , typename E , typename F , typename Ret = typename std::result_of<F(T)>::type>
constexpr Ret mbind (const tl::expected< T, E > &exp, F f)
 Monad tl::expected<T,E> More...
 
template<typename F , typename Ret = typename std::result_of<F()>::type, typename Exp = tl::expected<Ret, std::exception_ptr>>
Exp mtry (F f)
 Monadic try, used to lift a function that throws an exception one that returns an tl::expected<T, std::exception_ptr> More...
 
template<typename F , typename G >
constexpr auto mcompose (F f, G g)
 Monadic compose two monad functions. More...
 
template<typename T , typename G , typename... Types>
constexpr auto mcompose (T t, G g, Types... vars)
 Variadic mcompose. More...
 
constexpr std::string_view toStringView (const ErrorCode &code)
 convert ErrorCode to string_view for easy formatting More...
 
template<typename T , typename E = Error>
constexpr Result< T, E > make_result (T value)
 Makes a Result<T> from a T value. More...
 
template<typename T , typename E >
constexpr bool has_error (const tl::expected< T, E > &exp)
 Filter function for testing if a result has an error. More...
 
template<typename E , typename... Args>
constexpr std::optional< E > maybe_error (tl::expected< Args, E >... args)
 
template<typename F , typename Ret = typename std::result_of<F()>::type, typename Exp = Result<Ret>>
Exp try_to_result (F f)
 Try to Result<T>. Lifts a function that throws an excpetpion to one that returns a Result<T> More...
 
template<typename Rng , typename T >
constexpr Result< T > validate_in (Rng const &valid_values, T const &value, std::string const &name)
 

Variables

constexpr auto Unknown
 
constexpr auto Cancelled
 
constexpr auto InvalidArgument
 
constexpr auto Timeout
 
constexpr auto NotFound
 
constexpr auto AlreadyExists
 
constexpr auto PermissionDenied
 
constexpr auto ResourceExhausted
 
constexpr auto FailedPrecondition
 
constexpr auto Aborted
 
constexpr auto OutOfRange
 
constexpr auto Unimplemented
 
constexpr auto Internal
 
constexpr auto Unavailable
 
constexpr auto DataLoss
 
constexpr auto Unauthenticated
 
constexpr auto Exception
 

Typedef Documentation

◆ Result

template<typename T , typename E = Error>
using fp::Result = typedef tl::expected<T, E>

Definition at line 187 of file result.hpp.

Enumeration Type Documentation

◆ ErrorCode

enum fp::ErrorCode : int
strong

Enum for ErrorCodes inspired by absl::StatusCode.

Enumerator
UNKNOWN 
CANCELLED 
INVALID_ARGUMENT 
TIMEOUT 
NOT_FOUND 
ALREADY_EXISTS 
PERMISSION_DENIED 
RESOURCE_EXHAUSTED 
FAILED_PRECONDITION 
ABORTED 
OUT_OF_RANGE 
UNIMPLEMENTED 
INTERNAL 
UNAVAILABLE 
DATA_LOSS 
UNAUTHENTICATED 
EXCEPTION 

Definition at line 48 of file result.hpp.

Function Documentation

◆ has_error()

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

Filter function for testing if a result has an error.

Parameters
expThe expected type to test
Template Parameters
TThe value type
EThe error type
Returns
if the expected has an error

Definition at line 212 of file result.hpp.

◆ make_opt()

template<typename T >
constexpr std::optional<T> fp::make_opt ( value)
constexpr

Makes an optional<T> from a T value.

Parameters
[in]valueThe value
Template Parameters
TThe type of value
Returns
A optional<T> containing the value

Definition at line 47 of file monad.hpp.

◆ make_result()

template<typename T , typename E = Error>
constexpr Result<T, E> fp::make_result ( value)
constexpr

Makes a Result<T> from a T value.

Parameters
[in]valueThe value
Template Parameters
TThe type of value
Returns
A Result<T> containing value
Examples
maybe_error.cpp, and result.cpp.

Definition at line 199 of file result.hpp.

◆ maybe_error()

template<typename E , typename... Args>
constexpr std::optional<E> fp::maybe_error ( tl::expected< Args, E >...  args)
constexpr
Examples
maybe_error.cpp.

Definition at line 227 of file result.hpp.

◆ mbind() [1/2]

template<typename T , typename F >
constexpr auto fp::mbind ( const std::optional< T > &  opt,
f 
) -> decltype(f(opt.value()))
constexpr

Monad optional bind.

Parameters
[in]optThe input optional
[in]fThe function
Template Parameters
TThe input type
FThe function
Returns
Return type of f

Definition at line 63 of file monad.hpp.

◆ mbind() [2/2]

template<typename T , typename E , typename F , typename Ret = typename std::result_of<F(T)>::type>
constexpr Ret fp::mbind ( const tl::expected< T, E > &  exp,
f 
)
constexpr

Monad tl::expected<T,E>

Parameters
[in]expThe tl::expected<T,E> input
[in]fThe function to apply
Template Parameters
TThe type for the input expected
EThe error type
FThe function
RetThe return type of the function
Returns
The return type of the function

Definition at line 87 of file monad.hpp.

◆ mcompose() [1/2]

template<typename F , typename G >
constexpr auto fp::mcompose ( f,
g 
)
constexpr

Monadic compose two monad functions.

Parameters
[in]fThe first function
[in]gThe second function
Template Parameters
FThe type of the first function
GThe type of the second function
Returns
A functional composition of two monad functions

Definition at line 128 of file monad.hpp.

◆ mcompose() [2/2]

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

Variadic mcompose.

Parameters
[in]tThe first function
[in]gThe second function
[in]varsThe rest of the functions
Template Parameters
TThe type of the first function
GThe type of the second function
TypesThe types of the rest of the functions
Returns
A functional composition of variadic monad functions

Definition at line 146 of file monad.hpp.

◆ mtry()

template<typename F , typename Ret = typename std::result_of<F()>::type, typename Exp = tl::expected<Ret, std::exception_ptr>>
Exp fp::mtry ( f)

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

Parameters
[in]fThe function to call
Template Parameters
FThe function type
RetThe return value of the function
ExpThe expected type
Returns
The return value of the function

Definition at line 108 of file monad.hpp.

◆ toStringView()

constexpr std::string_view fp::toStringView ( const ErrorCode code)
constexpr

convert ErrorCode to string_view for easy formatting

Parameters
[in]codeThe error code

Definition at line 140 of file result.hpp.

◆ try_to_result()

template<typename F , typename Ret = typename std::result_of<F()>::type, typename Exp = Result<Ret>>
Exp fp::try_to_result ( f)

Try to Result<T>. Lifts a function that throws an excpetpion to one that returns a Result<T>

Parameters
[in]fThe function to call
Template Parameters
FThe function type
RetThe return value of the function
ExpThe expected type
Returns
The return value of the function

Definition at line 252 of file result.hpp.

◆ validate_in()

template<typename Rng , typename T >
constexpr Result<T> fp::validate_in ( Rng const &  valid_values,
T const &  value,
std::string const &  name 
)
constexpr
Examples
validate_in.cpp.

Definition at line 95 of file validate.hpp.

Variable Documentation

◆ Aborted

constexpr auto fp::Aborted
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::ABORTED, what};
}

Definition at line 110 of file result.hpp.

◆ AlreadyExists

constexpr auto fp::AlreadyExists
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::ALREADY_EXISTS, what};
}

Definition at line 98 of file result.hpp.

◆ Cancelled

constexpr auto fp::Cancelled
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::CANCELLED, what};
}

Definition at line 86 of file result.hpp.

◆ DataLoss

constexpr auto fp::DataLoss
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::DATA_LOSS, what};
}

Definition at line 125 of file result.hpp.

◆ Exception

constexpr auto fp::Exception
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::EXCEPTION, what};
}

Definition at line 131 of file result.hpp.

◆ FailedPrecondition

constexpr auto fp::FailedPrecondition
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::FAILED_PRECONDITION, what};
}

Definition at line 107 of file result.hpp.

◆ Internal

constexpr auto fp::Internal
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::INTERNAL, what};
}

Definition at line 119 of file result.hpp.

◆ InvalidArgument

constexpr auto fp::InvalidArgument
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::INVALID_ARGUMENT, what};
}
Examples
maybe_error.cpp, and result.cpp.

Definition at line 89 of file result.hpp.

◆ NotFound

constexpr auto fp::NotFound
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::NOT_FOUND, what};
}

Definition at line 95 of file result.hpp.

◆ OutOfRange

constexpr auto fp::OutOfRange
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::OUT_OF_RANGE, what};
}

Definition at line 113 of file result.hpp.

◆ PermissionDenied

constexpr auto fp::PermissionDenied
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::PERMISSION_DENIED, what};
}

Definition at line 101 of file result.hpp.

◆ ResourceExhausted

constexpr auto fp::ResourceExhausted
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::RESOURCE_EXHAUSTED, what};
}

Definition at line 104 of file result.hpp.

◆ Timeout

constexpr auto fp::Timeout
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::TIMEOUT, what};
}

Definition at line 92 of file result.hpp.

◆ Unauthenticated

constexpr auto fp::Unauthenticated
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::UNAUTHENTICATED, what};
}

Definition at line 128 of file result.hpp.

◆ Unavailable

constexpr auto fp::Unavailable
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::UNAVAILABLE, what};
}

Definition at line 122 of file result.hpp.

◆ Unimplemented

constexpr auto fp::Unimplemented
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::UNIMPLEMENTED, what};
}

Definition at line 116 of file result.hpp.

◆ Unknown

constexpr auto fp::Unknown
constexpr
Initial value:
= [](const std::string& what = "") {
return Error{ErrorCode::UNKNOWN, what};
}

Definition at line 83 of file result.hpp.