32 #include <system_error> 33 #include <type_traits> 39 #define MONERO_PRECOND(...) \ 42 if (!( __VA_ARGS__ )) \ 43 return {::common_error::kInvalidArgument}; \ 47 #define MONERO_CHECK(...) \ 50 const ::expect<void> result = __VA_ARGS__ ; \ 52 return result.error(); \ 60 #define MONERO_UNWRAP(...) \ 61 ::detail::expect::unwrap( __VA_ARGS__ , nullptr, __FILE__ , __LINE__ ) 66 #define MONERO_THROW(code, msg) \ 67 ::detail::expect::throw_( code , msg , __FILE__ , __LINE__ ) 81 static void throw_(std::error_code ec,
const char* msg,
const char* file,
unsigned line);
85 static T unwrap(::
expect<T>&& result,
const char* error_msg,
const char* file,
unsigned line)
88 throw_(result.error(), error_msg, file, line);
89 return std::move(*result);
93 static void unwrap(::
expect<void>&& result,
const char* error_msg,
const char* file,
unsigned line);
134 static_assert(std::is_nothrow_destructible<T>(),
"T must have a nothrow destructor");
139 return std::is_constructible<T, U>() &&
140 std::is_convertible<U, T>();
145 typename std::aligned_storage<sizeof(T), alignof(T)>::type
storage_;
151 return *
reinterpret_cast<T*
>(std::addressof(
storage_));
154 T const&
get()
const noexcept
157 return *
reinterpret_cast<T const*
>(std::addressof(
storage_));
161 void store(U&&
value) noexcept(std::is_nothrow_constructible<T, U>())
164 code_ = std::error_code{};
182 expect(std::error_code
const& code) noexcept
186 code_ = ::common_error::kInvalidErrorCode;
190 expect(
T val) noexcept(std::is_nothrow_move_constructible<T>())
193 store(std::move(val));
196 expect(
expect const& src) noexcept(std::is_nothrow_copy_constructible<T>())
204 template<
typename U,
typename = detail::enable_if<is_convertible<U const&>()>>
216 store(std::move(src.get()));
220 template<
typename U,
typename = detail::enable_if<is_convertible<U>()>>
225 store(std::move(*src));
234 expect&
operator=(
expect const& src) noexcept(std::is_nothrow_copy_constructible<T>() && std::is_nothrow_copy_assignable<T>())
236 if (
this != std::addressof(src))
242 else if (src.has_value())
251 expect&
operator=(
expect&& src) noexcept(std::is_nothrow_move_constructible<T>() && std::is_nothrow_move_assignable<T>())
253 if (
this != std::addressof(src))
256 get() = std::move(src.get());
259 else if (src.has_value())
260 store(std::move(src.get()));
297 return std::move(
get());
303 T const*
operator->() const noexcept {
return std::addressof(
get()); }
317 get() == *rhs :
error() == rhs.error();
321 bool equal(std::error_code
const& rhs)
const noexcept
330 template<typename U, typename = detail::enable_if<!std::is_constructible<std::error_code, U>::value>>
331 bool equal(U
const& rhs)
const noexcept(noexcept(*std::declval<
expect<T>>() == rhs))
337 bool matches(std::error_condition
const& rhs)
const noexcept
355 expect(std::error_code
const& code) noexcept
359 code_ = ::common_error::kInvalidErrorCode;
378 return error() == rhs.error();
382 bool equal(std::error_code
const& rhs)
const noexcept
388 bool matches(std::error_condition
const& rhs)
const noexcept
397 template<
typename T,
typename U>
401 return lhs.equal(rhs);
404 template<
typename T,
typename U>
408 return lhs.equal(rhs);
411 template<
typename T,
typename U>
415 return rhs.equal(lhs);
418 template<
typename T,
typename U>
422 return !lhs.equal(rhs);
425 template<
typename T,
typename U>
429 return !lhs.equal(rhs);
432 template<
typename T,
typename U>
436 return !rhs.equal(lhs);
444 throw_(result.error(), error_msg, file, line);
T & value() &
Definition: expect.h:279
bool equal(std::error_code const &rhs) const noexcept
Definition: expect.h:382
expect(expect &&src) noexcept(std::is_nothrow_move_constructible< T >())
Definition: expect.h:212
expect(expect const &src) noexcept(std::is_nothrow_copy_constructible< T >())
Definition: expect.h:196
const uint32_t T[512]
Definition: groestl_tables.h:33
std::error_code code_
Definition: expect.h:346
std::error_code code_
Definition: expect.h:144
bool has_value() const noexcept
Definition: expect.h:273
bool operator!=(expect< T > const &lhs, expect< U > const &rhs) noexcept(noexcept(lhs.equal(rhs)))
Definition: expect.h:420
bool matches(std::error_condition const &rhs) const noexcept
Definition: expect.h:388
T const & value() const &
Definition: expect.h:286
std::aligned_storage< sizeof(T), alignof(T)>::type storage_
Definition: expect.h:145
void maybe_throw() const
Definition: expect.h:167
T * operator->() noexcept
Definition: expect.h:301
expect(expect< U > &&src) noexcept(std::is_nothrow_constructible< T, U >())
Move conversion from U to T.
Definition: expect.h:221
bool equal(U const &rhs) const noexcept(noexcept(*std::declval< expect< T >>()==rhs))
Definition: expect.h:331
std::error_code error_type
Definition: expect.h:350
expect & operator=(expect &&src) noexcept(std::is_nothrow_move_constructible< T >() &&std::is_nothrow_move_assignable< T >())
Definition: expect.h:251
expect & operator=(expect const &src) noexcept(std::is_nothrow_copy_constructible< T >() &&std::is_nothrow_copy_assignable< T >())
Definition: expect.h:234
bool operator==(expect< T > const &lhs, expect< U > const &rhs) noexcept(noexcept(lhs.equal(rhs)))
Definition: expect.h:399
expect(std::error_code const &code) noexcept
Definition: expect.h:182
bool equal(std::error_code const &rhs) const noexcept
Definition: expect.h:321
void store(U &&value) noexcept(std::is_nothrow_constructible< T, U >())
Definition: expect.h:161
std::error_code error() const noexcept
Definition: expect.h:276
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:85
T && value() &&
Definition: expect.h:294
expect(expect< U > const &src) noexcept(std::is_nothrow_constructible< T, U const &>())
Copy conversion from U to T.
Definition: expect.h:205
void value_type
Definition: expect.h:349
static constexpr bool is_convertible() noexcept
Definition: expect.h:137
declaration and default definition for the functions used the API
Definition: expect.cpp:33
T value_type
Definition: expect.h:174
bool equal(expect const &rhs) const noexcept
Definition: expect.h:376
expect(std::error_code const &code) noexcept
Definition: expect.h:355
bool matches(std::error_condition const &rhs) const noexcept
Definition: expect.h:337
T & get() noexcept
Definition: expect.h:148
T const * operator->() const noexcept
Definition: expect.h:303
~expect() noexcept
Definition: expect.h:228
bool has_error() const noexcept
Definition: expect.h:370
expect< void > success() noexcept
Definition: expect.h:395
T & operator*() noexcept
Definition: expect.h:305
bool equal(expect< U > const &rhs) const noexcept(noexcept(*std::declval< expect< T >>()== *rhs))
Definition: expect.h:314
static void throw_(std::error_code ec, const char *msg, const char *file, unsigned line)
Definition: expect.cpp:64
int bool
Definition: stdbool.h:35
std::error_code error_type
Definition: expect.h:175
T const & operator*() const noexcept
Definition: expect.h:307
bool has_error() const noexcept
Definition: expect.h:270
typename std::enable_if< C >::type enable_if
Definition: expect.h:76
expect(T val) noexcept(std::is_nothrow_move_constructible< T >())
Store a value, val, in the expect object.
Definition: expect.h:190
std::error_code error() const noexcept
Definition: expect.h:373