13 #ifndef __STOUT_TRY_HPP__
14 #define __STOUT_TRY_HPP__
33 template <
typename T,
typename E = Error>
38 std::is_base_of<Error, E>::value,
39 "An error type must be, or be inherited from 'Error'.");
49 typename =
typename std::enable_if<
50 std::is_constructible<T, const U&>::value>
::type>
56 : data(
Some(std::move(t))) {}
60 Try(
const Try& that) =
default;
70 bool isSome()
const {
return data.isSome(); }
71 bool isError()
const {
return data.isNone(); }
77 ABORT(
"Try::get() but state == ERROR: " + error_.
get().message);
84 return const_cast<T&
>(
static_cast<const Try&
>(*this).
get());
93 const typename std::conditional<
94 std::is_same<E, Error>::value, std::string, E>
::type&
error()
const
96 assert(data.isNone());
98 return error_impl(error_.
get());
102 static const std::string& error_impl(
const Error& err) {
return err.
message; }
104 template <
typename Err>
105 static const Err& error_impl(
const Err& err) {
return err; }
119 #endif // __STOUT_TRY_HPP__
Definition: errorbase.hpp:35
Definition: option.hpp:28
#define ABORT(...)
Definition: abort.hpp:40
const std::conditional< std::is_same< E, Error >::value, std::string, E >::type & error() const
Definition: try.hpp:94
T * operator->()
Definition: try.hpp:88
bool isSome() const
Definition: option.hpp:115
Try(const E &error)
Definition: try.hpp:53
bool isSome() const
Definition: try.hpp:70
const T & get() const &
Definition: option.hpp:118
static Try error(const E &e)
Definition: try.hpp:42
Try(T &&t)
Definition: try.hpp:55
static Try some(const T &t)
Definition: try.hpp:41
const std::string message
Definition: errorbase.hpp:45
_Some< typename std::decay< T >::type > Some(T &&t)
Definition: some.hpp:42
bool isError() const
Definition: try.hpp:71
Try(const T &t)
Definition: try.hpp:44
Try< uint32_t > type(const std::string &path)
Try(const U &u)
Definition: try.hpp:51
Try & operator=(const Try &that)=default
const T * operator->() const
Definition: try.hpp:87
const T & get() const
Definition: try.hpp:73