13 #ifndef __STOUT_TRY_HPP__ 14 #define __STOUT_TRY_HPP__ 34 template <
typename T,
typename E = Error>
39 std::is_base_of<Error, E>::value,
40 "An error type must be, or be inherited from 'Error'.");
50 typename =
typename std::enable_if<
51 std::is_constructible<T, const U&>::value>
::type>
67 Try(
const Try& that) =
default;
77 bool isSome()
const {
return data.isSome(); }
78 bool isError()
const {
return data.isNone(); }
80 T&
get() & {
return get(*this); }
81 const T&
get()
const & {
return get(*this); }
82 T&&
get() && {
return get(std::move(*
this)); }
83 const T&&
get()
const && {
return get(std::move(*
this)); }
90 const T&&
operator*() const&& {
return std::move(*this).get(); }
91 T&&
operator*() && {
return std::move(*this).get(); }
96 const typename std::conditional<
97 std::is_same<E, Error>::value, std::string, E>
::type&
error()
const 99 assert(data.isNone());
101 return error_impl(error_.
get());
105 static const std::string& error_impl(
const Error& err) {
return err.
message; }
107 template <
typename Self>
108 static auto get(Self&&
self) -> decltype(std::forward<Self>(
self).data.get())
110 if (!
self.data.isSome()) {
111 assert(
self.error_.
isSome());
112 ABORT(
"Try::get() but state == ERROR: " +
self.error_->message);
114 return std::forward<Self>(
self).data.get();
117 template <
typename Err>
118 static const Err& error_impl(
const Err& err) {
return err; }
132 #endif // __STOUT_TRY_HPP__ Definition: errorbase.hpp:36
Definition: option.hpp:29
#define ABORT(...)
Definition: abort.hpp:40
const T && operator*() const &&
Definition: try.hpp:90
const std::conditional< std::is_same< E, Error >::value, std::string, E >::type & error() const
Definition: try.hpp:97
T * operator->()
Definition: try.hpp:86
T & operator*()&
Definition: try.hpp:89
Definition: type_utils.hpp:619
Try(_Some< U > &&some)
Definition: try.hpp:63
bool isSome() const
Definition: option.hpp:116
Try(const E &error)
Definition: try.hpp:54
bool isSome() const
Definition: try.hpp:77
const T & get() const &
Definition: option.hpp:119
static Try error(const E &e)
Definition: try.hpp:43
Try(T &&t)
Definition: try.hpp:56
T && operator*()&&
Definition: try.hpp:91
static Try some(const T &t)
Definition: try.hpp:42
const std::string message
Definition: errorbase.hpp:46
_Some< typename std::decay< T >::type > Some(T &&t)
Definition: some.hpp:42
bool isError() const
Definition: try.hpp:78
const T & operator*() const &
Definition: try.hpp:88
Try(const T &t)
Definition: try.hpp:45
Try< uint32_t > type(const std::string &path)
Try(const _Some< U > &some)
Definition: try.hpp:60
Try(const U &u)
Definition: try.hpp:52
Try & operator=(const Try &that)=default
const T * operator->() const
Definition: try.hpp:85