| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#pragma once |
| 31 |
|
| 32 |
#include <cassert> |
| 33 |
#include <system_error> |
| 34 |
#include <type_traits> |
| 35 |
#include <utility> |
| 36 |
|
| 37 |
#include "common/error.h" |
| 38 |
|
| 39 |
|
| 40 |
#define MONERO_PRECOND(...) \ |
| 41 |
do \ |
| 42 |
{ \ |
| 43 |
if (!( __VA_ARGS__ )) \ |
| 44 |
return {::common_error::kInvalidArgument}; \ |
| 45 |
} while (0) |
| 46 |
|
| 47 |
|
| 48 |
#define MONERO_CHECK(...) \ |
| 49 |
do \ |
| 50 |
{ \ |
| 51 |
const ::expect<void> result = __VA_ARGS__ ; \ |
| 52 |
if (!result) \ |
| 53 |
return result.error(); \ |
| 54 |
} while (0) |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#define MONERO_UNWRAP(...) \ |
| 62 |
::detail::expect::unwrap( __VA_ARGS__ , nullptr, __FILE__ , __LINE__ ) |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#define MONERO_THROW(code, msg) \ |
| 68 |
::detail::expect::throw_( code , msg , __FILE__ , __LINE__ ) |
| 69 |
|
| 70 |
|
| 71 |
template<typename> class expect; |
| 72 |
|
| 73 |
namespace detail |
| 74 |
{ |
| 75 |
|
| 76 |
template<bool C> |
| 77 |
using enable_if = typename std::enable_if<C>::type; |
| 78 |
|
| 79 |
struct expect |
| 80 |
{ |
| 81 |
|
| 82 |
static void throw_(std::error_code ec, const char* msg, const char* file, unsigned line); |
| 83 |
|
| 84 |
|
| 85 |
template<typename T> |
| 86 |
static T unwrap(::expect<T>&& result, const char* error_msg, const char* file, unsigned line) |
| 87 |
{ |
| 88 |
if (!result) |
| 89 |
throw_(result.error(), error_msg, file, line); |
| 90 |
return std::move(*result); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
static void unwrap(::expect<void>&& result, const char* error_msg, const char* file, unsigned line); |
| 95 |
}; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
template<typename T> |
| 133 |
class expect |
| 134 |
{ |
| 135 |
static_assert(std::is_nothrow_destructible<T>(), "T must have a nothrow destructor"); |
| 136 |
|
| 137 |
template<typename U> |
| 138 |
static constexpr bool is_convertible() noexcept |
| 139 |
{ |
| 140 |
return std::is_constructible<T, U>() && |
| 141 |
std::is_convertible<U, T>(); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
std::error_code code_; |
| 146 |
typename std::aligned_storage<sizeof(T), alignof(T)>::type storage_; |
| 147 |
|
| 148 |
|
| 149 |
T& get() noexcept |
| 150 |
{ |
| 151 |
assert(has_value()); |
| 152 |
return *reinterpret_cast<T*>(std::addressof(storage_)); |
| 153 |
} |
| 154 |
|
| 155 |
T const& get() const noexcept |
| 156 |
{ |
| 157 |
assert(has_value()); |
| 158 |
return *reinterpret_cast<T const*>(std::addressof(storage_)); |
| 159 |
} |
| 160 |
|
| 161 |
template<typename U> |
| 162 |
void store(U&& value) noexcept(std::is_nothrow_constructible<T, U>()) |
| 163 |
{ |
| 164 |
new (std::addressof(storage_)) T{std::forward<U>(value)}; |
| 165 |
code_ = std::error_code{}; |
| 166 |
} |
| 167 |
|
| 168 |
void maybe_throw() const |
| 169 |
{ |
| 170 |
if (has_error()) |
| 171 |
::detail::expect::throw_(error(), nullptr, nullptr, 0); |
| 172 |
} |
| 173 |
|
| 174 |
public: |
| 175 |
using value_type = T; |
| 176 |
using error_type = std::error_code; |
| 177 |
|
| 178 |
expect() = delete; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
expect(std::error_code const& code) noexcept |
| 184 |
: code_(code), storage_() |
| 185 |
{ |
| 186 |
if (!has_error()) |
| 187 |
code_ = ::common_error::kInvalidErrorCode; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
expect(T val) noexcept(std::is_nothrow_move_constructible<T>()) |
| 192 |
: code_(), storage_() |
| 193 |
{ |
| 194 |
store(std::move(val)); |
| 195 |
} |
| 196 |
|
| 197 |
expect(expect const& src) noexcept(std::is_nothrow_copy_constructible<T>()) |
| 198 |
: code_(src.error()), storage_() |
| 199 |
{ |
| 200 |
if (src.has_value()) |
| 201 |
store(src.get()); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
template<typename U, typename = detail::enable_if<is_convertible<U const&>()>> |
| 206 |
expect(expect<U> const& src) noexcept(std::is_nothrow_constructible<T, U const&>()) |
| 207 |
: code_(src.error()), storage_() |
| 208 |
{ |
| 209 |
if (src.has_value()) |
| 210 |
store(*src); |
| 211 |
} |
| 212 |
|
| 213 |
expect(expect&& src) noexcept(std::is_nothrow_move_constructible<T>()) |
| 214 |
: code_(src.error()), storage_() |
| 215 |
{ |
| 216 |
if (src.has_value()) |
| 217 |
store(std::move(src.get())); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
template<typename U, typename = detail::enable_if<is_convertible<U>()>> |
| 222 |
expect(expect<U>&& src) noexcept(std::is_nothrow_constructible<T, U>()) |
| 223 |
: code_(src.error()), storage_() |
| 224 |
{ |
| 225 |
if (src.has_value()) |
| 226 |
store(std::move(*src)); |
| 227 |
} |
| 228 |
|
| 229 |
~expect() noexcept |
| 230 |
{ |
| 231 |
if (has_value()) |
| 232 |
get().~T(); |
| 233 |
} |
| 234 |
|
| 235 |
expect& operator=(expect const& src) noexcept(std::is_nothrow_copy_constructible<T>() && std::is_nothrow_copy_assignable<T>()) |
| 236 |
{ |
| 237 |
if (this != std::addressof(src)) |
| 238 |
{ |
| 239 |
if (has_value() && src.has_value()) |
| 240 |
get() = src.get(); |
| 241 |
else if (has_value()) |
| 242 |
get().~T(); |
| 243 |
else if (src.has_value()) |
| 244 |
store(src.get()); |
| 245 |
code_ = src.error(); |
| 246 |
} |
| 247 |
return *this; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
expect& operator=(expect&& src) noexcept(std::is_nothrow_move_constructible<T>() && std::is_nothrow_move_assignable<T>()) |
| 253 |
{ |
| 254 |
if (this != std::addressof(src)) |
| 255 |
{ |
| 256 |
if (has_value() && src.has_value()) |
| 257 |
get() = std::move(src.get()); |
| 258 |
else if (has_value()) |
| 259 |
get().~T(); |
| 260 |
else if (src.has_value()) |
| 261 |
store(std::move(src.get())); |
| 262 |
code_ = src.error(); |
| 263 |
} |
| 264 |
return *this; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
explicit operator bool() const noexcept { return has_value(); } |
| 269 |
|
| 270 |
|
| 271 |
bool has_error() const noexcept { return bool(code_); } |
| 272 |
|
| 273 |
|
| 274 |
bool has_value() const noexcept { return !has_error(); } |
| 275 |
|
| 276 |
|
| 277 |
std::error_code error() const noexcept { return code_; } |
| 278 |
|
| 279 |
|
| 280 |
T& value() & |
| 281 |
{ |
| 282 |
maybe_throw(); |
| 283 |
return get(); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
T const& value() const & |
| 288 |
{ |
| 289 |
maybe_throw(); |
| 290 |
return get(); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
T&& value() && |
| 296 |
{ |
| 297 |
maybe_throw(); |
| 298 |
return std::move(get()); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
T* operator->() noexcept { return std::addressof(get()); } |
| 303 |
|
| 304 |
T const* operator->() const noexcept { return std::addressof(get()); } |
| 305 |
|
| 306 |
T& operator*() noexcept { return get(); } |
| 307 |
|
| 308 |
T const& operator*() const noexcept { return get(); } |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
template<typename U> |
| 315 |
bool equal(expect<U> const& rhs) const noexcept(noexcept(*std::declval<expect<T>>() == *rhs)) |
| 316 |
{ |
| 317 |
return has_value() && rhs.has_value() ? |
| 318 |
get() == *rhs : error() == rhs.error(); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
bool equal(std::error_code const& rhs) const noexcept |
| 323 |
{ |
| 324 |
return has_error() && error() == rhs; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 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)) |
| 333 |
{ |
| 334 |
return has_value() && get() == rhs; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
bool matches(std::error_condition const& rhs) const noexcept |
| 339 |
{ |
| 340 |
return has_error() && error() == rhs; |
| 341 |
} |
| 342 |
}; |
| 343 |
|
| 344 |
template<> |
| 345 |
class expect<void> |
| 346 |
{ |
| 347 |
std::error_code code_; |
| 348 |
|
| 349 |
public: |
| 350 |
using value_type = void; |
| 351 |
using error_type = std::error_code; |
| 352 |
|
| 353 |
|
| 354 |
expect() noexcept |
| 355 |
: code_() |
| 356 |
{} |
| 357 |
|
| 358 |
expect(std::error_code const& code) noexcept |
| 359 |
: code_(code) |
| 360 |
{ |
| 361 |
if (!has_error()) |
| 362 |
code_ = ::common_error::kInvalidErrorCode; |
| 363 |
} |
| 364 |
|
| 365 |
expect(expect const&) = default; |
| 366 |
~expect() = default; |
| 367 |
expect& operator=(expect const&) = default; |
| 368 |
|
| 369 |
|
| 370 |
explicit operator bool() const noexcept { return !has_error(); } |
| 371 |
|
| 372 |
|
| 373 |
bool has_error() const noexcept { return bool(code_); } |
| 374 |
|
| 375 |
|
| 376 |
std::error_code error() const noexcept { return code_; } |
| 377 |
|
| 378 |
|
| 379 |
bool equal(expect const& rhs) const noexcept |
| 380 |
{ |
| 381 |
return error() == rhs.error(); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
bool equal(std::error_code const& rhs) const noexcept |
| 386 |
{ |
| 387 |
return has_error() && error() == rhs; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
bool matches(std::error_condition const& rhs) const noexcept |
| 392 |
{ |
| 393 |
return has_error() && error() == rhs; |
| 394 |
} |
| 395 |
}; |
| 396 |
|
| 397 |
|
| 398 |
inline expect<void> success() noexcept { return expect<void>{}; } |
| 399 |
|
| 400 |
template<typename T, typename U> |
| 401 |
inline |
| 402 |
bool operator==(expect<T> const& lhs, expect<U> const& rhs) noexcept(noexcept(lhs.equal(rhs))) |
| 403 |
{ |
| 404 |
return lhs.equal(rhs); |
| 405 |
} |
| 406 |
|
| 407 |
template<typename T, typename U> |
| 408 |
inline |
| 409 |
bool operator==(expect<T> const& lhs, U const& rhs) noexcept(noexcept(lhs.equal(rhs))) |
| 410 |
{ |
| 411 |
return lhs.equal(rhs); |
| 412 |
} |
| 413 |
|
| 414 |
template<typename T, typename U> |
| 415 |
inline |
| 416 |
bool operator==(T const& lhs, expect<U> const& rhs) noexcept(noexcept(rhs.equal(lhs))) |
| 417 |
{ |
| 418 |
return rhs.equal(lhs); |
| 419 |
} |
| 420 |
|
| 421 |
template<typename T, typename U> |
| 422 |
inline |
| 423 |
bool operator!=(expect<T> const& lhs, expect<U> const& rhs) noexcept(noexcept(lhs.equal(rhs))) |
| 424 |
{ |
| 425 |
return !lhs.equal(rhs); |
| 426 |
} |
| 427 |
|
| 428 |
template<typename T, typename U> |
| 429 |
inline |
| 430 |
bool operator!=(expect<T> const& lhs, U const& rhs) noexcept(noexcept(lhs.equal(rhs))) |
| 431 |
{ |
| 432 |
return !lhs.equal(rhs); |
| 433 |
} |
| 434 |
|
| 435 |
template<typename T, typename U> |
| 436 |
inline |
| 437 |
bool operator!=(T const& lhs, expect<U> const& rhs) noexcept(noexcept(rhs.equal(lhs))) |
| 438 |
{ |
| 439 |
return !rhs.equal(lhs); |
| 440 |
} |
| 441 |
|
| 442 |
namespace detail |
| 443 |
{ |
| 444 |
inline void expect::unwrap(::expect<void>&& result, const char* error_msg, const char* file, unsigned line) |
| 445 |
{ |
| 446 |
if (!result) |
| 447 |
throw_(result.error(), error_msg, file, line); |
| 448 |
} |
| 449 |
} |
| 450 |
|