8#ifndef Sawyer_PartialResult_H
9#define Sawyer_PartialResult_H
11#include <Sawyer/Result.h>
13#include <boost/lexical_cast.hpp>
14#include <boost/variant.hpp>
28template<
class T,
class E>
40#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
42 friend class boost::serialization::access;
45 void serialize(S &s,
const unsigned )
const {
46 s <<BOOST_SERIALIZATION_NVP(ok_);
47 s <<BOOST_SERIALIZATION_NVP(error_);
51#ifdef SAWYER_HAVE_CEREAL
53 friend class cereal::access;
55 template<
class Archive>
56 void CEREAL_SERIALIZE_FUNCTION_NAME(Archive &archive)
const {
57 archive(CEREAL_NVP(ok_));
58 archive(CEREAL_NVP(error_));
67 template<
class U = T,
class F = E>
71 template<
class U = T,
class F = E>
86 template<
class U = T,
class F = E>
100 template<
class U = T>
109 template<
class U = T>
111 return !(*
this ==
ok);
115 template<
class F = E>
121 template<
class F = E>
123 return !(*
this ==
error);
127 template<
class U,
class F>
129 return (*
ok() == *other.
ok() &&
135 template<
class U,
class F>
137 return !(*
this == other);
148 explicit operator bool()
const {
148 explicit operator bool()
const {
…}
184 const T&
expect(
const std::string &mesg)
const {
188 throw std::runtime_error(mesg);
184 const T&
expect(
const std::string &mesg)
const {
…}
224 typename std::enable_if<is_invocable<Fn, const PartialResult>::value,
const PartialResult>::type
255 template<
class Exception = E>
260 throw Exception(*
error());
265 template<
class Exception = E>
279 typename std::enable_if<is_invocable<Fn, OkValue>::value,
const PartialResult>::type
291 template<
class U = T,
class F = E>
307 throw std::runtime_error(mesg);
356 using NewValue =
decltype(f(
unwrap()));
366template<
class T,
class E>
367PartialResult<T, E> makePartialResult(
const T &ok,
const E &error) {
368 return PartialResult<T, E>(ok, error);
Holds a value or nothing.
Result containing a value and an error.
bool isOk() const
Returns true if the result is okay.
std::enable_if< is_invocable< Fn, constPartialResult >::value, constPartialResult >::type orElse(Fn fn) const
Returns the contained Ok value, or calls a function.
bool operator==(const Error< F > &error) const
Test whether this result has the specified Error value.
PartialResult & assignTo(U &out) const
Conditionally save a value.
bool isError() const
Returns true if the result has an error.
const Sawyer::Optional< T > ok() const
Convert to Optional<T>.
PartialResult & operator=(const PartialResult< U, F > &other)
Assign another partial result to this result.
bool containsError(const F &error) const
Returns true if this result contains the specified error value.
const PartialResult< T, F > orElse(const PartialResult< T, F > &other) const
Returns this value or the other result.
const T * operator->() const
Returns a pointer to the (partial) success value.
const T & orDefault() const
Returns the okay value or a default constructed value.
const T & operator*() const
Returns the (partial) success value.
const Sawyer::Optional< E > error() const
Convert to Optional<E>.
PartialResult & operator=(const Ok< U > &ok)
Assign an Ok value to this result.
const T orElse(const T &dflt) const
Returns the contained Ok value or a provided default.
bool operator!=(const Error< F > &error) const
Test whether this result does not have the specified Error value.
const PartialResult< U, F > andThen(const PartialResult< U, F > &other) const
Returns this (if error) or the other result.
auto fmap(F &&f) -> PartialResult< decltype(f(unwrap())), E >
Conditionally apply a functor f to the okay contents of a result.
const T & expect(const std::string &mesg) const
Returns the success value or throws an exception.
std::enable_if< is_invocable< Fn, OkValue >::value, constPartialResult >::type andThen(Fn fn) const
Returns the contained Error value, or calls a function.
const E & unwrapError() const
Returns the error value or throws an exception.
const T & orThrow(const Exception &e) const
Returns the value or throws an exception constructed from the specified value.
const E & expectError(const std::string &mesg) const
Returns the error value or throws an exception.
bool operator==(const Ok< U > &ok) const
Test whether this result has the specified Ok value.
bool operator!=(const Ok< U > &ok) const
Test whether this result does not have the specified Ok value.
const PartialResult & assignTo(U &out) const
Conditionally save a value.
bool contains(const U &value) const
Returns true if this result contains the specified (partial) okay value.
bool operator!=(const PartialResult< U, F > &other) const
Test whether this result is unequal to the other result.
bool operator==(const PartialResult< U, F > &other) const
Test whether this result is equal to the other result.
const T & orThrow() const
Returns the success value or throws an exception.
const T & unwrap() const
Returns the (partial) success value.