MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
Results

Results that can hold success (with optional success value) or an error. More...

Classes

struct  mtcore::Error< Underlying >
 A struct representing an error Auto convertible to a Result of any type. More...
 
struct  mtcore::Result< T, ErrType >
 Represents a Result that may have an error (error code) or a success value A type of "void" means there is no Result value, but there may be a failure. More...
 
struct  mtcore::Result< void, ErrType >
 Represents a Result that may have an error (error code) or a success (without a value) More...
 
struct  mtcore::Success< T >
 Represents a success result (auto castable to Result) More...
 
struct  mtcore::Success< void >
 Represents a success value. More...
 

Functions

template<typename T>
Success< T > mtcore::success (const T &v)
 Creates a successful Result.
 
Success< void > mtcore::success ()
 Creates a successful void Result object.
 
template<typename Underlying>
Error< Underlying > mtcore::error (Underlying err)
 Creates an error.
 
template<typename Success, typename Acc, typename F, Iterable I, typename Err = typename decltype(std::declval<F>()( std::declval<std::remove_const_t<typename decltype(std::declval<I>().iter())::IterElem>>()))::Err>
Result< Success, Err > mtcore::results::accumulate (Success initial, const Acc &acc, const F &func, const I &iterable)
 Chains together the execution of an errorable function on a list of arguments If an error is encountered, execution will stop and immediately return No Result value is returned Useful for performing side effects.
 
template<typename F, Iterable I, typename Err = typename decltype(std::declval<F>()( std::declval<std::remove_const_t<typename decltype(std::declval<I>().iter())::IterElem>>()))::Err>
Result< void, Err > mtcore::results::foreach (const F &func, const I &iterable)
 Chains together the execution of an errorable function on a list of arguments If an error is encountered, execution will stop and immediately return No Result value is returned Useful for performing side effects.
 
template<typename F, typename... Args, typename Err = typename decltype(std::declval<F>()(std::declval<typename impl::FirstVariadic<Args...>::Type>()))::Err>
Result< void, Err > mtcore::results::chain (const F &func, const Args &...args)
 Chains together the execution of an errorable function on a list of arguments If an error is encountered, execution will stop and immediately return No Result value is returned Useful for performing side effects.
 
template<typename Success, typename Acc, typename F, typename... Args, typename Err = typename decltype(std::declval<F>()(std::declval<typename impl::FirstVariadic<Args...>::Type>()))::Err>
Result< Success, Err > mtcore::results::reduce (Success initVal, const Acc &acc, const F &func, const Args &...args)
 Reduces arguments with errorable function If an error is encountered, execution will stop and immediately return The accumulated Result will be what's returned on success Useful for reduction/mapping.
 
template<typename F, typename... Args, typename Out = typename decltype(std::declval<F>()(std::declval<typename impl::FirstVariadic<Args...>::Type>()))::Value, typename Err = typename decltype(std::declval<F>()(std::declval<typename impl::FirstVariadic<Args...>::Type>()))::Err>
auto mtcore::results::map (const F &func, const Args &...args)
 Maps values with function that may error.
 

Detailed Description

Results that can hold success (with optional success value) or an error.

Meant to replace exception throwing

Function Documentation

◆ accumulate()

template<typename Success, typename Acc, typename F, Iterable I, typename Err = typename decltype(std::declval<F>()( std::declval<std::remove_const_t<typename decltype(std::declval<I>().iter())::IterElem>>()))::Err>
Result< Success, Err > mtcore::results::accumulate ( Success initial,
const Acc & acc,
const F & func,
const I & iterable )

Chains together the execution of an errorable function on a list of arguments If an error is encountered, execution will stop and immediately return No Result value is returned Useful for performing side effects.

Template Parameters
FType of function to execute
IType Iterable to operate on
ErrType of Result Error
Parameters
funcFunction to execute
iterableIterable to execute function on
Returns
Result of success or error

Definition at line 453 of file result.hpp.

453 {
454 auto iter = iterable.iter();
455 using Elem = std::remove_cv_t<typename decltype(iter)::IterElem>;
456 Elem cur;
457 while (iter.next().move_if_present(cur)) {
458 auto res = func(cur);
459 if (res.is_error()) {
460 return res.error();
461 }
462 initial = acc(initial, res.value());
463 }
464 return success(initial);
465 }
Success< void > success()
Creates a successful void Result object.
Definition result.hpp:398
Generic iterator defaults built on common contracts Does not guarantee performance of iterators Actua...
Definition iter.hpp:91
Here is the call graph for this function:
Here is the caller graph for this function:

◆ chain()

template<typename F, typename... Args, typename Err = typename decltype(std::declval<F>()(std::declval<typename impl::FirstVariadic<Args...>::Type>()))::Err>
Result< void, Err > mtcore::results::chain ( const F & func,
const Args &... args )

Chains together the execution of an errorable function on a list of arguments If an error is encountered, execution will stop and immediately return No Result value is returned Useful for performing side effects.

Template Parameters
FType of function to execute
ArgsType of variadic arguments
ErrType of Result Error
Parameters
funcFunction to execute
argsArguments to execute function on
Returns
Result of success or error

Definition at line 539 of file result.hpp.

539 {
540 return impl::Chain<Err, F, Args...>::run(func, args...);
541 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ error()

template<typename Underlying>
Error< Underlying > mtcore::error ( Underlying err)

Creates an error.

Template Parameters
UnderlyingError underlying type
Parameters
errError data
Returns
An error

Definition at line 425 of file result.hpp.

425 {
426 return Error<Underlying>{err};
427 }
A struct representing an error Auto convertible to a Result of any type.
Definition result.hpp:62

◆ foreach()

template<typename F, Iterable I, typename Err = typename decltype(std::declval<F>()( std::declval<std::remove_const_t<typename decltype(std::declval<I>().iter())::IterElem>>()))::Err>
Result< void, Err > mtcore::results::foreach ( const F & func,
const I & iterable )

Chains together the execution of an errorable function on a list of arguments If an error is encountered, execution will stop and immediately return No Result value is returned Useful for performing side effects.

Template Parameters
FType of function to execute
IType Iterable to operate on
ErrType of Result Error
Parameters
funcFunction to execute
iterableIterable to execute function on
Returns
Result of success or error

Definition at line 483 of file result.hpp.

483 {
484 auto iter = iterable.iter();
485 using Elem = std::remove_cv_t<typename decltype(iter)::IterElem>;
486 Elem cur;
487 while (iter.next().move_if_present(cur)) {
488 auto res = func(cur);
489 if (res.is_error()) {
490 return res.error();
491 }
492 }
493 return success();
494 }
Here is the call graph for this function:

◆ map()

template<typename F, typename... Args, typename Out = typename decltype(std::declval<F>()(std::declval<typename impl::FirstVariadic<Args...>::Type>()))::Value, typename Err = typename decltype(std::declval<F>()(std::declval<typename impl::FirstVariadic<Args...>::Type>()))::Err>
auto mtcore::results::map ( const F & func,
const Args &... args )

Maps values with function that may error.

Results are put into a tuple If an error is encountered, execution will stop and immediately return

Template Parameters
FFunction to map with
ArgsArguments to map
OutType of success output
ErrType of error in Result
Parameters
funcFunction to use to map objects
argsArguments to map
Returns
Tuple of mapped success values on success, or error on failure

Definition at line 585 of file result.hpp.

585 {
586 return impl::Map<Out, Err, F, Args...>::run(func, args...);
587 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduce()

template<typename Success, typename Acc, typename F, typename... Args, typename Err = typename decltype(std::declval<F>()(std::declval<typename impl::FirstVariadic<Args...>::Type>()))::Err>
Result< Success, Err > mtcore::results::reduce ( Success initVal,
const Acc & acc,
const F & func,
const Args &... args )

Reduces arguments with errorable function If an error is encountered, execution will stop and immediately return The accumulated Result will be what's returned on success Useful for reduction/mapping.

Template Parameters
SuccessFinal Result type on success
FErrorable function to map each element through
AccAccumulator which takes current accumulated value and success Result from F (only success value, not full Result object)
ArgsArguments to reduce over
ErrResult error type (auto inferred)
Parameters
initValInitial value to start accumulation with
accFunction to accumulate with
funcFunction that may err
argsArguments to reduce over
Returns
Final Result

Definition at line 564 of file result.hpp.

564 {
565 return impl::Reduce<F, Acc, Args...>::template run<Success, Err>(initVal, func, acc, args...);
566 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ success() [1/2]

Success< void > mtcore::success ( )
inline

Creates a successful void Result object.

Returns
Result

Definition at line 398 of file result.hpp.

398{ return {}; }

◆ success() [2/2]

template<typename T>
Success< T > mtcore::success ( const T & v)

Creates a successful Result.

Template Parameters
TType of successful Result (inferrable)
Parameters
vSuccessful value
Returns
Result

Definition at line 389 of file result.hpp.

389 {
390 return Success<T>{v};
391 }
Represents a success result (auto castable to Result)
Definition result.hpp:374
Here is the call graph for this function:
Here is the caller graph for this function: