33#include <system_error>
40#define MONERO_PRECOND(...) \
43 if (!( __VA_ARGS__ )) \
44 return {::common_error::kInvalidArgument}; \
48#define MONERO_CHECK(...) \
51 const ::expect<void> result = __VA_ARGS__ ; \
53 return result.error(); \
61#define MONERO_UNWRAP(...) \
62 ::detail::expect::unwrap( __VA_ARGS__ , nullptr, __FILE__ , __LINE__ )
67#define MONERO_THROW(code, msg) \
68 ::detail::expect::throw_( code , msg , __FILE__ , __LINE__ )
71template<
typename>
class expect;
82 static void throw_(std::error_code ec,
const char* msg,
const char* file,
unsigned line);
135 static_assert(std::is_nothrow_destructible<T>(),
"T must have a nothrow destructor");
140 return std::is_constructible<T, U>() &&
141 std::is_convertible<U, T>();
146 typename std::aligned_storage<
sizeof(
T),
alignof(
T)>::type
storage_;
152 return *
reinterpret_cast<T*
>(std::addressof(
storage_));
162 void store(U&&
value)
noexcept(std::is_nothrow_constructible<T, U>())
165 code_ = std::error_code{};
191 expect(
T val)
noexcept(std::is_nothrow_move_constructible<T>())
194 store(std::move(val));
197 expect(
expect const& src)
noexcept(std::is_nothrow_copy_constructible<T>())
205 template<typename U, typename = detail::enable_if<is_convertible<U const&>()>>
217 store(std::move(src.get()));
221 template<typename U, typename = detail::enable_if<is_convertible<U>()>>
226 store(std::move(*src));
235 expect&
operator=(
expect const& src)
noexcept(std::is_nothrow_copy_constructible<T>() && std::is_nothrow_copy_assignable<T>())
237 if (
this != std::addressof(src))
243 else if (src.has_value())
252 expect&
operator=(
expect&& src)
noexcept(std::is_nothrow_move_constructible<T>() && std::is_nothrow_move_assignable<T>())
254 if (
this != std::addressof(src))
257 get() = std::move(src.get());
260 else if (src.has_value())
261 store(std::move(src.get()));
298 return std::move(
get());
315 bool equal(
expect<U> const& rhs)
const noexcept(
noexcept(*std::declval<expect<T>>() == *rhs))
318 get() == *rhs :
error() == rhs.error();
322 bool equal(std::error_code
const& rhs)
const noexcept
331 template<typename U, typename = detail::enable_if<!std::is_constructible<std::error_code, U>::value>>
332 bool equal(U
const& rhs)
const noexcept(
noexcept(*std::declval<expect<T>>() == rhs))
338 bool matches(std::error_condition
const& rhs)
const noexcept
381 return error() == rhs.error();
385 bool equal(std::error_code
const& rhs)
const noexcept
391 bool matches(std::error_condition
const& rhs)
const noexcept
400template<
typename T,
typename U>
404 return lhs.equal(rhs);
407template<
typename T,
typename U>
411 return lhs.equal(rhs);
414template<
typename T,
typename U>
418 return rhs.equal(lhs);
421template<
typename T,
typename U>
425 return !lhs.equal(rhs);
428template<
typename T,
typename U>
432 return !lhs.equal(rhs);
435template<
typename T,
typename U>
439 return !rhs.equal(lhs);
std::error_code error_type
Definition expect.h:351
bool matches(std::error_condition const &rhs) const noexcept
Definition expect.h:391
std::error_code code_
Definition expect.h:347
std::error_code error() const noexcept
Definition expect.h:376
expect(std::error_code const &code) noexcept
Definition expect.h:358
expect(expect const &)=default
expect & operator=(expect const &)=default
bool equal(expect const &rhs) const noexcept
Definition expect.h:379
expect() noexcept
Create a successful object.
Definition expect.h:354
bool has_error() const noexcept
Definition expect.h:373
void value_type
Definition expect.h:350
bool equal(std::error_code const &rhs) const noexcept
Definition expect.h:385
expect & operator=(expect const &src) noexcept(std::is_nothrow_copy_constructible< T >() &&std::is_nothrow_copy_assignable< T >())
Definition expect.h:235
T * operator->() noexcept
Definition expect.h:302
bool equal(std::error_code const &rhs) const noexcept
Definition expect.h:322
T const & get() const noexcept
Definition expect.h:155
T && value() &&
Definition expect.h:295
bool equal(U const &rhs) const noexcept(noexcept(*std::declval< expect< T > >()==rhs))
Definition expect.h:332
void maybe_throw() const
Definition expect.h:168
expect(expect< U > &&src) noexcept(std::is_nothrow_constructible< T, U >())
Move conversion from U to T.
Definition expect.h:222
std::aligned_storage< sizeof(T), alignof(T)>::type storage_
Definition expect.h:146
~expect() noexcept
Definition expect.h:229
bool has_value() const noexcept
Definition expect.h:274
T value_type
Definition expect.h:175
bool has_error() const noexcept
Definition expect.h:271
static constexpr bool is_convertible() noexcept
Definition expect.h:138
T const & value() const &
Definition expect.h:287
T & value() &
Definition expect.h:280
std::error_code error() const noexcept
Definition expect.h:277
bool matches(std::error_condition const &rhs) const noexcept
Definition expect.h:338
std::error_code code_
Definition expect.h:145
expect(expect< U > const &src) noexcept(std::is_nothrow_constructible< T, U const & >())
Copy conversion from U to T.
Definition expect.h:206
expect(expect const &src) noexcept(std::is_nothrow_copy_constructible< T >())
Definition expect.h:197
expect & operator=(expect &&src) noexcept(std::is_nothrow_move_constructible< T >() &&std::is_nothrow_move_assignable< T >())
Definition expect.h:252
expect(T val) noexcept(std::is_nothrow_move_constructible< T >())
Store a value, val, in the expect object.
Definition expect.h:191
void store(U &&value) noexcept(std::is_nothrow_constructible< T, U >())
Definition expect.h:162
T const & operator*() const noexcept
Definition expect.h:308
expect(expect &&src) noexcept(std::is_nothrow_move_constructible< T >())
Definition expect.h:213
T & get() noexcept
Definition expect.h:149
bool equal(expect< U > const &rhs) const noexcept(noexcept(*std::declval< expect< T > >()== *rhs))
Definition expect.h:315
T const * operator->() const noexcept
Definition expect.h:304
T & operator*() noexcept
Definition expect.h:306
std::error_code error_type
Definition expect.h:176
expect(std::error_code const &code) noexcept
Definition expect.h:183
bool operator!=(expect< T > const &lhs, expect< U > const &rhs) noexcept(noexcept(lhs.equal(rhs)))
Definition expect.h:423
expect< void > success() noexcept
Definition expect.h:398
bool operator==(expect< T > const &lhs, expect< U > const &rhs) noexcept(noexcept(lhs.equal(rhs)))
Definition expect.h:402
#define const
Definition ipfrdr.c:80
declaration and default definition for the functions used the API
Definition expect.cpp:34
typename std::enable_if< C >::type enable_if
Definition expect.h:77
const portMappingElt code
Definition portlistingparse.c:22
tools::wallet2::message_signature_result_t result
Definition signature.cpp:62
@ kInvalidErrorCode
Default std::error_code given to expect<T>
Definition error.h:37
static T unwrap(::expect< T > &&result, const char *error_msg, const char *file, unsigned line)
If result.has_error() call throw_. Otherwise,.
Definition expect.h:86
static void throw_(std::error_code ec, const char *msg, const char *file, unsigned line)
Definition expect.cpp:64