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

Description

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

Result containing a value or an error.

Definition at line 315 of file Result.h.

#include <Sawyer/Result.h>

Public Types

using OkValue = T
 
using ErrorValue = E
 
using OkType = Ok< T >
 
using ErrorType = Error< E >
 

Public Member Functions

template<class U = T>
 Result (const Ok< U > &ok)
 
template<class F = E>
 Result (const Error< F > &error)
 
template<class U = T>
Resultoperator= (const Ok< U > &ok)
 Assign an Ok value to this result.
 
template<class F = E>
Resultoperator= (const Error< F > &error)
 Assign an Error value 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 Result< U, F > &other) const
 Test whether this result is equal to the other result.
 
template<class U , class F >
bool operator!= (const Result< U, F > &other) const
 Test whether this result is unequal to the other result.
 
bool isError () const
 Returns true if the result is 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 success value, or thows an exception.
 
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, ErrorValue >::value, constResult >::type orElse (Fn fn) const
 Returns the contained Ok value, or calls a function.
 
template<class F >
const Result< T, F > orElse (const Result< 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 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, constResult >::type andThen (Fn fn) const
 Returns the contained Error value, or calls a function.
 
template<class U >
const Result< U, E > andThen (const Result< U, E > &other) const
 Returns this 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 okay value.
 
template<class F >
bool containsError (const F &error) const
 Returns true if this result contains the specified error value.
 
template<typename F >
auto fmap (F &&f) -> Result< decltype(f(unwrap())), E >
 Conditionally apply a functor f to the 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 success value or throws an exception.
 
const T & operator* () const
 Returns the success value or throws an exception.
 
template<class U >
const ResultassignTo (U &out) const
 Conditionally save a value.
 
template<class U >
ResultassignTo (U &out) const
 Conditionally save a value.
 

Member Typedef Documentation

◆ OkValue

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

Definition at line 317 of file Result.h.

◆ ErrorValue

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

Definition at line 318 of file Result.h.

◆ OkType

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

Definition at line 319 of file Result.h.

◆ ErrorType

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

Definition at line 320 of file Result.h.

Constructor & Destructor Documentation

◆ Result() [1/2]

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

Definition at line 389 of file Result.h.

◆ Result() [2/2]

template<class T , class E >
template<class F = E>
Sawyer::Result< T, E >::Result ( const Error< F > &  error)
inline

Definition at line 393 of file Result.h.

Member Function Documentation

◆ operator=() [1/2]

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

Assign an Ok value to this result.

Definition at line 398 of file Result.h.

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

◆ operator=() [2/2]

template<class T , class E >
template<class F = E>
Result & Sawyer::Result< T, E >::operator= ( const Error< F > &  error)
inline

Assign an Error value to this result.

Definition at line 405 of file Result.h.

References Sawyer::Result< T, E >::error().

◆ operator==() [1/3]

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

Test whether this result has the specified Ok value.

Definition at line 412 of file Result.h.

References Sawyer::Result< T, E >::isOk(), and Sawyer::Result< T, E >::ok().

◆ operator!=() [1/3]

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

Test whether this result does not have the specified Ok value.

Definition at line 418 of file Result.h.

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

◆ operator==() [2/3]

template<class T , class E >
template<class F = E>
bool Sawyer::Result< T, E >::operator== ( const Error< F > &  error) const
inline

Test whether this result has the specified Error value.

Definition at line 424 of file Result.h.

References Sawyer::Result< T, E >::error(), and Sawyer::Result< T, E >::isError().

◆ operator!=() [2/3]

template<class T , class E >
template<class F = E>
bool Sawyer::Result< T, E >::operator!= ( const Error< F > &  error) const
inline

Test whether this result does not have the specified Error value.

Definition at line 430 of file Result.h.

References Sawyer::Result< T, E >::error().

◆ operator==() [3/3]

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

Test whether this result is equal to the other result.

Definition at line 436 of file Result.h.

References Sawyer::Result< T, E >::error(), Sawyer::Result< T, E >::isError(), Sawyer::Result< T, E >::isOk(), and Sawyer::Result< T, E >::ok().

◆ operator!=() [3/3]

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

Test whether this result is unequal to the other result.

Definition at line 443 of file Result.h.

◆ isOk()

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

◆ operator bool()

template<class T , class E >
Sawyer::Result< T, E >::operator bool ( ) const
inlineexplicit

Returns true if the result is okay.

Definition at line 453 of file Result.h.

References Sawyer::Result< T, E >::isOk().

◆ isError()

template<class T , class E >
bool Sawyer::Result< T, E >::isError ( ) const
inline

Returns true if the result is an error.

Definition at line 459 of file Result.h.

References Sawyer::Result< T, E >::isOk().

Referenced by Sawyer::Result< T, E >::operator==(), and Sawyer::Result< T, E >::operator==().

◆ ok()

template<class T , class E >
const Sawyer::Optional< T > Sawyer::Result< T, E >::ok ( ) const
inline

Convert to Optional<T>.

If this result is okay, then return the result, otherwise return nothing.

Definition at line 466 of file Result.h.

References Sawyer::Result< T, E >::isOk().

Referenced by Sawyer::Result< T, E >::andThen(), Sawyer::Result< T, E >::operator!=(), Sawyer::Result< T, E >::operator=(), Sawyer::Result< T, E >::operator==(), and Sawyer::Result< T, E >::operator==().

◆ error()

template<class T , class E >
const Sawyer::Optional< E > Sawyer::Result< T, E >::error ( ) const
inline

◆ expect()

template<class T , class E >
const T & Sawyer::Result< T, E >::expect ( const std::string &  mesg) const
inline

Returns the success value or throws an exception.

If this result is okay, then returns its value, otherwise throws an std::runtime_error with the specified string.

Definition at line 488 of file Result.h.

References Sawyer::Result< T, E >::isOk().

Referenced by Sawyer::Result< T, E >::unwrap().

◆ unwrap()

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

◆ operator*()

template<class T , class E >
const T & Sawyer::Result< T, E >::operator* ( ) const
inline

Returns the success value or throws an exception.

If this result is okay, then returns its value, otherwise throws an std::runtime_error.

Definition at line 504 of file Result.h.

References Sawyer::Result< T, E >::unwrap().

◆ operator->()

template<class T , class E >
const T * Sawyer::Result< T, E >::operator-> ( ) const
inline

Returns a pointer to the success value, or thows an exception.

If this result is okay, then returns a pointer to its value, otherwise throws an std::runtime_error.

Definition at line 512 of file Result.h.

References Sawyer::Result< T, E >::unwrap().

◆ orElse() [1/3]

template<class T , class E >
const T Sawyer::Result< T, E >::orElse ( const T &  dflt) const
inline

Returns the contained Ok value or a provided default.

Definition at line 517 of file Result.h.

References Sawyer::Result< T, E >::isOk(), and Sawyer::Result< T, E >::unwrap().

◆ orElse() [2/3]

template<class T , class E >
template<class Fn >
std::enable_if< is_invocable< Fn, ErrorValue >::value, constResult >::type Sawyer::Result< T, E >::orElse ( Fn  fn) const
inline

Returns the contained Ok value, or calls a function.

If this result is okay, then returns this result, otherwise calls the specified function, fn, with this result's Error value and returns the function's result.

Definition at line 527 of file Result.h.

References Sawyer::Result< T, E >::error(), and Sawyer::Result< T, E >::isOk().

◆ orElse() [3/3]

template<class T , class E >
template<class F >
const Result< T, F > Sawyer::Result< T, E >::orElse ( const Result< T, F > &  other) const
inline

Returns this value or the other result.

If this result is okay then return it, otherwise returns the other result.

Definition at line 539 of file Result.h.

References Sawyer::Result< T, E >::isOk().

◆ orDefault()

template<class T , class E >
const T & Sawyer::Result< T, E >::orDefault ( ) const
inline

Returns the okay value or a default constructed value.

Definition at line 548 of file Result.h.

References Sawyer::Result< T, E >::isOk(), and Sawyer::Result< T, E >::unwrap().

◆ orThrow() [1/2]

template<class T , class E >
template<class Exception = E>
const T & Sawyer::Result< T, E >::orThrow ( ) const
inline

Returns the value or throws an exception.

If the result is okay, 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 558 of file Result.h.

References Sawyer::Result< T, E >::error(), Sawyer::Result< T, E >::isOk(), and Sawyer::Result< T, E >::unwrap().

◆ orThrow() [2/2]

template<class T , class E >
template<class Exception = E>
const T & Sawyer::Result< T, E >::orThrow ( const Exception &  e) const
inline

Returns the value or throws an exception constructed from the specified value.

Definition at line 568 of file Result.h.

References Sawyer::Result< T, E >::isOk(), and Sawyer::Result< T, E >::unwrap().

◆ andThen() [1/2]

template<class T , class E >
template<class Fn >
std::enable_if< is_invocable< Fn, OkValue >::value, constResult >::type Sawyer::Result< 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 582 of file Result.h.

References Sawyer::Result< T, E >::isOk(), and Sawyer::Result< T, E >::ok().

◆ andThen() [2/2]

template<class T , class E >
template<class U >
const Result< U, E > Sawyer::Result< T, E >::andThen ( const Result< U, E > &  other) const
inline

Returns this error or the other result.

If this result is okay, then returns other. Otherwise returns the error value of this result.

Definition at line 594 of file Result.h.

References Sawyer::Result< T, E >::isOk().

◆ expectError()

template<class T , class E >
const E & Sawyer::Result< 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 605 of file Result.h.

References Sawyer::Result< T, E >::isOk().

Referenced by Sawyer::Result< T, E >::unwrapError().

◆ unwrapError()

template<class T , class E >
const E & Sawyer::Result< 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 616 of file Result.h.

References Sawyer::Result< T, E >::expectError().

Referenced by Sawyer::Result< T, E >::containsError(), and Sawyer::Result< T, E >::fmap().

◆ contains()

template<class T , class E >
template<class U >
bool Sawyer::Result< T, E >::contains ( const U &  value) const
inline

Returns true if this result contains the specified okay value.

Definition at line 622 of file Result.h.

References Sawyer::Result< T, E >::isOk(), and Sawyer::Result< T, E >::unwrap().

◆ containsError()

template<class T , class E >
template<class F >
bool Sawyer::Result< T, E >::containsError ( const F &  error) const
inline

Returns true if this result contains the specified error value.

Definition at line 628 of file Result.h.

References Sawyer::Result< T, E >::error(), Sawyer::Result< T, E >::isOk(), and Sawyer::Result< T, E >::unwrapError().

◆ assignTo() [1/2]

template<class T , class E >
template<class U >
const Result & Sawyer::Result< T, E >::assignTo ( U &  out) const
inline

Conditionally save a value.

If this result has a value, then assign it to the argument. Returns the result.

Definition at line 672 of file Result.h.

References Sawyer::Result< T, E >::isOk(), and Sawyer::Result< T, E >::unwrap().

◆ assignTo() [2/2]

template<class T , class E >
template<class U >
Result & Sawyer::Result< T, E >::assignTo ( U &  out) const
inline

Conditionally save a value.

If this result has a value, then assign it to the argument. Returns the result.

Definition at line 679 of file Result.h.

References Sawyer::Result< T, E >::isOk(), and Sawyer::Result< T, E >::unwrap().

◆ fmap()

template<class T , class E >
template<typename F >
auto Sawyer::Result< T, E >::fmap ( F &&  f) -> Result<decltype(f(unwrap())), E>
inline

Conditionally apply a functor f to the contents of a Result.

If this object has a value then fmap builds a new Result using f to construct the value. If it does not have a value, the new Result keeps the error of the input.

Definition at line 693 of file Result.h.

References Sawyer::Result< T, E >::isOk(), Sawyer::makeError(), Sawyer::makeOk(), Sawyer::Result< T, E >::unwrap(), and Sawyer::Result< T, E >::unwrapError().


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