ROSE 0.11.145.247
|
Result containing a value and an error.
This type is similar to Result except instead of containing either a value or an error it contains either a value or a value and an error.
Sometimes when a function fails it can still return a useful partial result, in which case it should return one of these types instead of Result.
Definition at line 29 of file PartialResult.h.
#include <Sawyer/PartialResult.h>
Public Types | |
using | OkValue = T |
using | ErrorValue = E |
using | OkType = Ok< T > |
using | ErrorType = Error< E > |
Public Member Functions | |
template<class U = T> | |
PartialResult (const Ok< U > &ok) | |
template<class U = T, class F = E> | |
PartialResult (const U &ok, const F &error) | |
template<class U = T, class F = E> | |
PartialResult (const PartialResult< U, F > &other) | |
template<class U = T> | |
PartialResult & | operator= (const Ok< U > &ok) |
Assign an Ok value to this result. | |
template<class U = T, class F = E> | |
PartialResult & | operator= (const PartialResult< U, F > &other) |
Assign another partial result to this result. | |
template<class U = T> | |
bool | operator== (const Ok< U > &ok) const |
Test whether this result has the specified Ok value. | |
template<class U = T> | |
bool | operator!= (const Ok< U > &ok) const |
Test whether this result does not have the specified Ok value. | |
template<class F = E> | |
bool | operator== (const Error< F > &error) const |
Test whether this result has the specified Error value. | |
template<class F = E> | |
bool | operator!= (const Error< F > &error) const |
Test whether this result does not have the specified Error value. | |
template<class U , class F > | |
bool | operator== (const PartialResult< U, F > &other) const |
Test whether this result is equal to the other result. | |
template<class U , class F > | |
bool | operator!= (const PartialResult< U, F > &other) const |
Test whether this result is unequal to the other result. | |
bool | isError () const |
Returns true if the result has an error. | |
const Sawyer::Optional< T > | ok () const |
Convert to Optional<T>. | |
const Sawyer::Optional< E > | error () const |
Convert to Optional<E>. | |
const T & | expect (const std::string &mesg) const |
Returns the success value or throws an exception. | |
const T * | operator-> () const |
Returns a pointer to the (partial) success value. | |
const T | orElse (const T &dflt) const |
Returns the contained Ok value or a provided default. | |
template<class Fn > | |
std::enable_if< is_invocable< Fn, constPartialResult >::value, constPartialResult >::type | orElse (Fn fn) const |
Returns the contained Ok value, or calls a function. | |
template<class F > | |
const PartialResult< T, F > | orElse (const PartialResult< T, F > &other) const |
Returns this value or the other result. | |
const T & | orDefault () const |
Returns the okay value or a default constructed value. | |
template<class Exception = E> | |
const T & | orThrow () const |
Returns the success value or throws an exception. | |
template<class Exception = E> | |
const T & | orThrow (const Exception &e) const |
Returns the value or throws an exception constructed from the specified value. | |
template<class Fn > | |
std::enable_if< is_invocable< Fn, OkValue >::value, constPartialResult >::type | andThen (Fn fn) const |
Returns the contained Error value, or calls a function. | |
template<class U = T, class F = E> | |
const PartialResult< U, F > | andThen (const PartialResult< U, F > &other) const |
Returns this (if error) or the other result. | |
const E & | expectError (const std::string &mesg) const |
Returns the error value or throws an exception. | |
const E & | unwrapError () const |
Returns the error value or throws an exception. | |
template<class U > | |
bool | contains (const U &value) const |
Returns true if this result contains the specified (partial) okay value. | |
template<class F > | |
bool | containsError (const F &error) const |
Returns true if this result contains the specified error value. | |
template<class F > | |
auto | fmap (F &&f) -> PartialResult< decltype(f(unwrap())), E > |
Conditionally apply a functor f to the okay contents of a result. | |
bool | isOk () const |
Returns true if the result is okay. | |
operator bool () const | |
Returns true if the result is okay. | |
const T & | unwrap () const |
Returns the (partial) success value. | |
const T & | operator* () const |
Returns the (partial) success value. | |
template<class U > | |
const PartialResult & | assignTo (U &out) const |
Conditionally save a value. | |
template<class U > | |
PartialResult & | assignTo (U &out) const |
Conditionally save a value. | |
using Sawyer::PartialResult< T, E >::OkValue = T |
Definition at line 31 of file PartialResult.h.
using Sawyer::PartialResult< T, E >::ErrorValue = E |
Definition at line 32 of file PartialResult.h.
using Sawyer::PartialResult< T, E >::OkType = Ok<T> |
Definition at line 33 of file PartialResult.h.
using Sawyer::PartialResult< T, E >::ErrorType = Error<E> |
Definition at line 34 of file PartialResult.h.
|
inline |
Definition at line 64 of file PartialResult.h.
|
inline |
Definition at line 68 of file PartialResult.h.
|
inline |
Definition at line 72 of file PartialResult.h.
|
inline |
Assign an Ok value to this result.
Definition at line 80 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::ok().
|
inline |
Assign another partial result to this result.
Definition at line 87 of file PartialResult.h.
|
inline |
Test whether this result has the specified Ok value.
Regardless of whether this result is an error, its okay value is compared with the argument. If it is an error then the okay value might be partial, but is still compared.
Definition at line 101 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::ok(), and Sawyer::PartialResult< T, E >::unwrap().
|
inline |
Test whether this result does not have the specified Ok value.
Regardless of whether this result is an error, its okay value is compared with the argument. If it is an error then the okay value might be partial, but is still compared.
Definition at line 110 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::ok().
|
inline |
Test whether this result has the specified Error value.
Definition at line 116 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::error(), and Sawyer::PartialResult< T, E >::isError().
|
inline |
Test whether this result does not have the specified Error value.
Definition at line 122 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::error().
|
inline |
Test whether this result is equal to the other result.
Definition at line 128 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::error(), Sawyer::PartialResult< T, E >::isError(), and Sawyer::PartialResult< T, E >::ok().
|
inline |
Test whether this result is unequal to the other result.
Definition at line 136 of file PartialResult.h.
|
inline |
Returns true if the result is okay.
A result is okay if it has no error.
Definition at line 145 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::isError().
Referenced by Sawyer::PartialResult< T, E >::andThen(), Sawyer::PartialResult< T, E >::andThen(), Sawyer::PartialResult< T, E >::assignTo(), Sawyer::PartialResult< T, E >::expect(), Sawyer::PartialResult< T, E >::fmap(), Sawyer::PartialResult< T, E >::ok(), Sawyer::PartialResult< T, E >::operator bool(), Sawyer::PartialResult< T, E >::orDefault(), Sawyer::PartialResult< T, E >::orElse(), Sawyer::PartialResult< T, E >::orElse(), Sawyer::PartialResult< T, E >::orElse(), Sawyer::PartialResult< T, E >::orThrow(), and Sawyer::PartialResult< T, E >::orThrow().
|
inlineexplicit |
Returns true if the result is okay.
A result is okay if it has no error.
Definition at line 148 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::isOk().
|
inline |
Returns true if the result has an error.
Definition at line 154 of file PartialResult.h.
Referenced by Sawyer::PartialResult< T, E >::isOk(), Sawyer::PartialResult< T, E >::operator==(), and Sawyer::PartialResult< T, E >::operator==().
|
inline |
Convert to Optional<T>.
If this result is okay (has no error), then return the result, otherwise return nothing.
Definition at line 161 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::isOk().
Referenced by Sawyer::PartialResult< T, E >::andThen(), Sawyer::PartialResult< T, E >::operator!=(), Sawyer::PartialResult< T, E >::operator=(), Sawyer::PartialResult< T, E >::operator==(), and Sawyer::PartialResult< T, E >::operator==().
|
inline |
Convert to Optional<E>.
If this result is an error, then return the error, otherwise return nothing.
Definition at line 172 of file PartialResult.h.
Referenced by Sawyer::PartialResult< T, E >::containsError(), Sawyer::PartialResult< T, E >::operator!=(), Sawyer::PartialResult< T, E >::operator==(), Sawyer::PartialResult< T, E >::operator==(), and Sawyer::PartialResult< T, E >::orThrow().
|
inline |
Returns the success value or throws an exception.
If this result is okay (no error), then returns its value, otherwise throws an std::runtime_error
with the specified string.
Definition at line 184 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::isOk().
|
inline |
Returns the (partial) success value.
Returns the success value. If this result has an error then the success value might be incomplete but will still be present.
Definition at line 197 of file PartialResult.h.
Referenced by Sawyer::PartialResult< T, E >::assignTo(), Sawyer::PartialResult< T, E >::contains(), Sawyer::PartialResult< T, E >::fmap(), Sawyer::PartialResult< T, E >::operator*(), Sawyer::PartialResult< T, E >::operator->(), Sawyer::PartialResult< T, E >::operator==(), Sawyer::PartialResult< T, E >::orDefault(), Sawyer::PartialResult< T, E >::orElse(), Sawyer::PartialResult< T, E >::orThrow(), and Sawyer::PartialResult< T, E >::orThrow().
|
inline |
Returns the (partial) success value.
Returns the success value. If this result has an error then the success value might be incomplete but will still be present.
Definition at line 200 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::unwrap().
|
inline |
Returns a pointer to the (partial) success value.
If this result has an error then the success value might be incomplete, but it will still be present.
Definition at line 208 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::unwrap().
|
inline |
Returns the contained Ok value or a provided default.
If this result is an error then the default value is returned, otherwise the (complete) success value is returned.
Definition at line 215 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::isOk(), and Sawyer::PartialResult< T, E >::unwrap().
|
inline |
Returns the contained Ok value, or calls a function.
If this result is okay (no error), then returns this result, otherwise calls the specified function, fn
, with this result and returns the function's result.
Definition at line 225 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::isOk().
|
inline |
Returns this value or the other result.
If this result is okay (no error) then return it, otherwise returns the other
result.
Definition at line 237 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::isOk().
|
inline |
Returns the okay value or a default constructed value.
Definition at line 246 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::isOk(), and Sawyer::PartialResult< T, E >::unwrap().
|
inline |
Returns the success value or throws an exception.
If the result is okay (no error), then its value is returned, otherwise either the error is thrown or it is used to construct the specified Exception
which is then thrown.
Definition at line 256 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::error(), Sawyer::PartialResult< T, E >::isOk(), and Sawyer::PartialResult< T, E >::unwrap().
|
inline |
Returns the value or throws an exception constructed from the specified value.
Definition at line 266 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::isOk(), and Sawyer::PartialResult< T, E >::unwrap().
|
inline |
Returns the contained Error value, or calls a function.
If this result is an error, then it's returned. Otherwise the okay value is passed to the specified function and that function's return value is returned.
Definition at line 280 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::isOk(), and Sawyer::PartialResult< T, E >::ok().
|
inline |
Returns this (if error) or the other result.
If this result is an error it returns this
, otherwise it returns other
.
Definition at line 292 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::isOk().
|
inline |
Returns the error value or throws an exception.
If this result is an error, then returns the error, otherwise throws an std::runtime_error
with the specified string.
Definition at line 303 of file PartialResult.h.
Referenced by Sawyer::PartialResult< T, E >::unwrapError().
|
inline |
Returns the error value or throws an exception.
If this result is an error, then returns the error, otherwise throws an std::runtime_error
.
Definition at line 314 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::expectError().
Referenced by Sawyer::PartialResult< T, E >::containsError(), and Sawyer::PartialResult< T, E >::fmap().
|
inline |
Returns true if this result contains the specified (partial) okay value.
Definition at line 320 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::unwrap().
|
inline |
Returns true if this result contains the specified error value.
Definition at line 326 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::error(), and Sawyer::PartialResult< T, E >::unwrapError().
|
inline |
Conditionally save a value.
If this result is not an error, then assign its okay value to the argument. Returns the result.
Definition at line 336 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::isOk(), and Sawyer::PartialResult< T, E >::unwrap().
|
inline |
Conditionally save a value.
If this result is not an error, then assign its okay value to the argument. Returns the result.
Definition at line 343 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::isOk(), and Sawyer::PartialResult< T, E >::unwrap().
|
inline |
Conditionally apply a functor f
to the okay contents of a result.
The function is applied to the (partial) okay value to produce a new results. Any error is also copied to the result.
Definition at line 355 of file PartialResult.h.
References Sawyer::PartialResult< T, E >::isOk(), Sawyer::PartialResult< T, E >::unwrap(), and Sawyer::PartialResult< T, E >::unwrapError().