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());
302 T* operator->() noexcept {
return std::addressof(
get()); }
304 T const* operator->()
const noexcept {
return std::addressof(
get()); }
306 T& operator*() noexcept {
return get(); }
308 T const& operator*()
const noexcept {
return 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
324 return has_error() &&
error() == rhs;
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
340 return has_error() &&
error() == rhs;
381 return error() == rhs.error();
385 bool equal(std::error_code
const& rhs)
const noexcept
387 return has_error() &&
error() == rhs;
391 bool matches(std::error_condition
const& rhs)
const noexcept
393 return has_error() &&
error() == rhs;
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);
*return True if this is storing an error instead of a value bool has_error() const noexcept
Definition expect.h:373
std::error_code error_type
Definition expect.h:351
std::error_code code_
Definition expect.h:347
*Create a successful object expect() noexcept
Definition expect.h:354
expect(std::error_code const &code) noexcept
Definition expect.h:358
expect(expect const &)=default
expect & operator=(expect const &)=default
*return Error alway std::error_code error() const noexcept
Definition expect.h:376
void value_type
Definition expect.h:350
expect & operator=(expect const &src) noexcept(std::is_nothrow_copy_constructible< T >() &&std::is_nothrow_copy_assignable< T >())
Definition expect.h:235
T const & get() const noexcept
Definition expect.h:155
T && value() &&
Definition expect.h:295
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
T value_type
Definition expect.h:175
*return False if otherwise error()
static constexpr bool is_convertible() noexcept
Definition expect.h:138
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
*return pre has_value()`. T *operator->() noexcept
Definition expect.h:301
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
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
std::error_code error_type
Definition expect.h:176
expect(std::error_code const &code) noexcept
Definition expect.h:183
bool success
Definition cold-transaction.cpp:57
bool operator!=(expect< T > const &lhs, expect< U > const &rhs) noexcept(noexcept(lhs.equal(rhs)))
Definition expect.h:423
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
int bool
Definition hash.h:36
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 GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1225
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