ROSE 0.11.145.247
Public Types | Public Member Functions | List of all members
Sawyer::PartialResult< T, E > Class Template Reference

Description

template<class T, class E>
class Sawyer::PartialResult< T, E >

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>
PartialResultoperator= (const Ok< U > &ok)
 Assign an Ok value to this result.
 
template<class U = T, class F = E>
PartialResultoperator= (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 PartialResultassignTo (U &out) const
 Conditionally save a value.
 
template<class U >
PartialResultassignTo (U &out) const
 Conditionally save a value.
 

Member Typedef Documentation

◆ OkValue

template<class T , class E >
using Sawyer::PartialResult< T, E >::OkValue = T

Definition at line 31 of file PartialResult.h.

◆ ErrorValue

template<class T , class E >
using Sawyer::PartialResult< T, E >::ErrorValue = E

Definition at line 32 of file PartialResult.h.

◆ OkType

template<class T , class E >
using Sawyer::PartialResult< T, E >::OkType = Ok<T>

Definition at line 33 of file PartialResult.h.

◆ ErrorType

template<class T , class E >
using Sawyer::PartialResult< T, E >::ErrorType = Error<E>

Definition at line 34 of file PartialResult.h.

Constructor & Destructor Documentation

◆ PartialResult() [1/3]

template<class T , class E >
template<class U = T>
Sawyer::PartialResult< T, E >::PartialResult ( const Ok< U > &  ok)
inline

Definition at line 64 of file PartialResult.h.

◆ PartialResult() [2/3]

template<class T , class E >
template<class U = T, class F = E>
Sawyer::PartialResult< T, E >::PartialResult ( const U &  ok,
const F &  error 
)
inline

Definition at line 68 of file PartialResult.h.

◆ PartialResult() [3/3]

template<class T , class E >
template<class U = T, class F = E>
Sawyer::PartialResult< T, E >::PartialResult ( const PartialResult< U, F > &  other)
inline

Definition at line 72 of file PartialResult.h.

Member Function Documentation

◆ operator=() [1/2]

template<class T , class E >
template<class U = T>
PartialResult & Sawyer::PartialResult< T, E >::operator= ( const Ok< U > &  ok)
inline

Assign an Ok value to this result.

Definition at line 80 of file PartialResult.h.

References Sawyer::PartialResult< T, E >::ok().

◆ operator=() [2/2]

template<class T , class E >
template<class U = T, class F = E>
PartialResult & Sawyer::PartialResult< T, E >::operator= ( const PartialResult< U, F > &  other)
inline

Assign another partial result to this result.

Definition at line 87 of file PartialResult.h.

◆ operator==() [1/3]

template<class T , class E >
template<class U = T>
bool Sawyer::PartialResult< T, E >::operator== ( const Ok< U > &  ok) const
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().

◆ operator!=() [1/3]

template<class T , class E >
template<class U = T>
bool Sawyer::PartialResult< T, E >::operator!= ( const Ok< U > &  ok) const
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().

◆ operator==() [2/3]

template<class T , class E >
template<class F = E>
bool Sawyer::PartialResult< T, E >::operator== ( const Error< F > &  error) const
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().

◆ operator!=() [2/3]

template<class T , class E >
template<class F = E>
bool Sawyer::PartialResult< T, E >::operator!= ( const Error< F > &  error) const
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().

◆ operator==() [3/3]

template<class T , class E >
template<class U , class F >
bool Sawyer::PartialResult< T, E >::operator== ( const PartialResult< U, F > &  other) const
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().

◆ operator!=() [3/3]

template<class T , class E >
template<class U , class F >
bool Sawyer::PartialResult< T, E >::operator!= ( const PartialResult< U, F > &  other) const
inline

Test whether this result is unequal to the other result.

Definition at line 136 of file PartialResult.h.

◆ isOk()

template<class T , class E >
bool Sawyer::PartialResult< T, E >::isOk ( ) const
inline

◆ operator bool()

template<class T , class E >
Sawyer::PartialResult< T, E >::operator bool ( ) const
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().

◆ isError()

template<class T , class E >
bool Sawyer::PartialResult< T, E >::isError ( ) const
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==().

◆ ok()

template<class T , class E >
const Sawyer::Optional< T > Sawyer::PartialResult< T, E >::ok ( ) const
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==().

◆ error()

template<class T , class E >
const Sawyer::Optional< E > Sawyer::PartialResult< T, E >::error ( ) const
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().

◆ expect()

template<class T , class E >
const T & Sawyer::PartialResult< T, E >::expect ( const std::string &  mesg) const
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().

◆ unwrap()

template<class T , class E >
const T & Sawyer::PartialResult< T, E >::unwrap ( ) const
inline

◆ operator*()

template<class T , class E >
const T & Sawyer::PartialResult< T, E >::operator* ( ) const
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().

◆ operator->()

template<class T , class E >
const T * Sawyer::PartialResult< T, E >::operator-> ( ) const
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().

◆ orElse() [1/3]

template<class T , class E >
const T Sawyer::PartialResult< T, E >::orElse ( const T &  dflt) const
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().

◆ orElse() [2/3]

template<class T , class E >
template<class Fn >
std::enable_if< is_invocable< Fn, constPartialResult >::value, constPartialResult >::type Sawyer::PartialResult< T, E >::orElse ( Fn  fn) const
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().

◆ orElse() [3/3]

template<class T , class E >
template<class F >
const PartialResult< T, F > Sawyer::PartialResult< T, E >::orElse ( const PartialResult< T, F > &  other) const
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().

◆ orDefault()

template<class T , class E >
const T & Sawyer::PartialResult< T, E >::orDefault ( ) const
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().

◆ orThrow() [1/2]

template<class T , class E >
template<class Exception = E>
const T & Sawyer::PartialResult< T, E >::orThrow ( ) const
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().

◆ orThrow() [2/2]

template<class T , class E >
template<class Exception = E>
const T & Sawyer::PartialResult< T, E >::orThrow ( const Exception &  e) const
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().

◆ andThen() [1/2]

template<class T , class E >
template<class Fn >
std::enable_if< is_invocable< Fn, OkValue >::value, constPartialResult >::type Sawyer::PartialResult< T, E >::andThen ( Fn  fn) const
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().

◆ andThen() [2/2]

template<class T , class E >
template<class U = T, class F = E>
const PartialResult< U, F > Sawyer::PartialResult< T, E >::andThen ( const PartialResult< U, F > &  other) const
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().

◆ expectError()

template<class T , class E >
const E & Sawyer::PartialResult< T, E >::expectError ( const std::string &  mesg) const
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().

◆ unwrapError()

template<class T , class E >
const E & Sawyer::PartialResult< T, E >::unwrapError ( ) const
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().

◆ contains()

template<class T , class E >
template<class U >
bool Sawyer::PartialResult< T, E >::contains ( const U &  value) const
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().

◆ containsError()

template<class T , class E >
template<class F >
bool Sawyer::PartialResult< T, E >::containsError ( const F &  error) const
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().

◆ assignTo() [1/2]

template<class T , class E >
template<class U >
const PartialResult & Sawyer::PartialResult< T, E >::assignTo ( U &  out) const
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().

◆ assignTo() [2/2]

template<class T , class E >
template<class U >
PartialResult & Sawyer::PartialResult< T, E >::assignTo ( U &  out) const
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().

◆ fmap()

template<class T , class E >
template<class F >
auto Sawyer::PartialResult< T, E >::fmap ( F &&  f) -> PartialResult<decltype(f(unwrap())), E>
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().


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