| 1 |
// Copyright 2016 Glyn Matthews. |
| 2 |
// Copyright (C) 2011 - 2012 Andrzej Krzemienski. |
| 3 |
// Distributed under the Boost Software License, Version 1.0. |
| 4 |
// (See accompanying file LICENSE_1_0.txt or copy at |
| 5 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
/** |
| 8 |
* \file |
| 9 |
* \brief Contains an implementation of C++17 optional (n3793). |
| 10 |
* |
| 11 |
* \sa https://github.com/akrzemi1/Optional |
| 12 |
* \sa http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3848.html |
| 13 |
*/ |
| 14 |
|
| 15 |
#ifndef NETWORK_OPTIONAL_INC |
| 16 |
#define NETWORK_OPTIONAL_INC |
| 17 |
|
| 18 |
#include <stdexcept> |
| 19 |
#include <type_traits> |
| 20 |
#include <utility> |
| 21 |
#include <memory> |
| 22 |
#include <algorithm> |
| 23 |
|
| 24 |
#if !defined(DOXYGEN_SHOULD_SKIP_THIS) |
| 25 |
#ifdef NDEBUG |
| 26 |
#define NETWORK_ASSERTED_EXPRESSION(CHECK, EXPR) (EXPR) |
| 27 |
#else |
| 28 |
#define NETWORK_ASSERTED_EXPRESSION(CHECK, EXPR) \ |
| 29 |
((CHECK) ? (EXPR) : (fail(#CHECK, __FILE__, __LINE__), (EXPR))) |
| 30 |
inline void fail(const char *, const char *, unsigned) {} |
| 31 |
#endif // NDEBUG |
| 32 |
#endif // !defined(DOXYGEN_SHOULD_SKIP_THIS) |
| 33 |
|
| 34 |
namespace network { |
| 35 |
/** |
| 36 |
* \ingroup optional |
| 37 |
* \class nullopt_t optional.hpp network/uri.hpp |
| 38 |
* \brief Disengaged state indicator. |
| 39 |
* \sa optional |
| 40 |
*/ |
| 41 |
struct nullopt_t { |
| 42 |
#if !defined(DOXYGEN_SHOULD_SKIP_THIS) |
| 43 |
struct init {}; |
| 44 |
constexpr nullopt_t(init) {} |
| 45 |
#endif // !defined(DOXYGEN_SHOULD_SKIP_THIS) |
| 46 |
}; |
| 47 |
|
| 48 |
/** |
| 49 |
* \ingroup optional |
| 50 |
* \brief Used to indicate a *disengaged* state for optional objects. |
| 51 |
*/ |
| 52 |
constexpr nullopt_t nullopt{nullopt_t::init{}}; |
| 53 |
|
| 54 |
/** |
| 55 |
* \ingroup optional |
| 56 |
* \class bad_optional_access optional.hpp network/uri.hpp |
| 57 |
* \brief Exception thrown when the value member function is called when the |
| 58 |
* optional object is disengaged. |
| 59 |
*/ |
| 60 |
class bad_optional_access : public std::logic_error { |
| 61 |
public: |
| 62 |
/** |
| 63 |
* \brief Constructor. |
| 64 |
* \param what_arg The error message. |
| 65 |
*/ |
| 66 |
explicit bad_optional_access(const std::string &what_arg) |
| 67 |
: std::logic_error(what_arg) {} |
| 68 |
|
| 69 |
/** |
| 70 |
* \brief Constructor. |
| 71 |
* \param what_arg The error message. |
| 72 |
*/ |
| 73 |
explicit bad_optional_access(const char *what_arg) |
| 74 |
: std::logic_error(what_arg) {} |
| 75 |
}; |
| 76 |
|
| 77 |
#if !defined(DOXYGEN_SHOULD_SKIP_THIS) |
| 78 |
namespace details { |
| 79 |
struct dummy_t {}; |
| 80 |
|
| 81 |
template <class T> |
| 82 |
union trivially_destructible_optional_storage { |
| 83 |
static_assert(std::is_trivially_destructible<T>::value, ""); |
| 84 |
|
| 85 |
dummy_t dummy_; |
| 86 |
T value_; |
| 87 |
|
| 88 |
constexpr trivially_destructible_optional_storage() : dummy_{} {} |
| 89 |
|
| 90 |
constexpr trivially_destructible_optional_storage(const T &v) : value_{v} {} |
| 91 |
|
| 92 |
~trivially_destructible_optional_storage() = default; |
| 93 |
}; |
| 94 |
|
| 95 |
template <class T> |
| 96 |
union optional_storage { |
| 97 |
dummy_t dummy_; |
| 98 |
T value_; |
| 99 |
|
| 100 |
constexpr optional_storage() : dummy_{} {} |
| 101 |
|
| 102 |
constexpr optional_storage(const T &v) : value_{v} {} |
| 103 |
|
| 104 |
~optional_storage() {} |
| 105 |
}; |
| 106 |
|
| 107 |
template <class T> |
| 108 |
class trivially_destructible_optional_base { |
| 109 |
public: |
| 110 |
typedef T value_type; |
| 111 |
|
| 112 |
constexpr trivially_destructible_optional_base() noexcept |
| 113 |
: init_(false), storage_{} {} |
| 114 |
|
| 115 |
constexpr trivially_destructible_optional_base(const T &v) |
| 116 |
: init_(true), storage_{v} {} |
| 117 |
|
| 118 |
constexpr trivially_destructible_optional_base(T &&v) |
| 119 |
: init_(true), storage_{std::move(v)} {} |
| 120 |
|
| 121 |
~trivially_destructible_optional_base() = default; |
| 122 |
|
| 123 |
protected: |
| 124 |
bool init_; |
| 125 |
optional_storage<T> storage_; |
| 126 |
}; |
| 127 |
|
| 128 |
template <class T> |
| 129 |
class optional_base { |
| 130 |
public: |
| 131 |
typedef T value_type; |
| 132 |
|
| 133 |
constexpr optional_base() noexcept : init_(false), storage_{} {} |
| 134 |
|
| 135 |
constexpr optional_base(const T &v) : init_(true), storage_{v} {} |
| 136 |
|
| 137 |
constexpr optional_base(T &&v) : init_(true), storage_{std::move(v)} {} |
| 138 |
|
| 139 |
~optional_base() { |
| 140 |
if (init_) { |
| 141 |
storage_.value_.T::~T(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
protected: |
| 146 |
bool init_; |
| 147 |
optional_storage<T> storage_; |
| 148 |
}; |
| 149 |
} // namespace details |
| 150 |
#endif // !defined(DOXYGEN_SHOULD_SKIP_THIS) |
| 151 |
|
| 152 |
/** |
| 153 |
* \ingroup optional |
| 154 |
* \class optional optional.hpp network/uri.hpp |
| 155 |
* \brief An implementation of C++17 optional (n3793) |
| 156 |
*/ |
| 157 |
#if !defined(DOXYGEN_SHOULD_SKIP_THIS) |
| 158 |
template <class T> |
| 159 |
using optional_base = |
| 160 |
typename std::conditional<std::is_trivially_destructible<T>::value, |
| 161 |
details::trivially_destructible_optional_base<T>, |
| 162 |
details::optional_base<T>>::type; |
| 163 |
#endif // !defined(DOXYGEN_SHOULD_SKIP_THIS) |
| 164 |
|
| 165 |
template <class T> |
| 166 |
#if !defined(DOXYGEN_SHOULD_SKIP_THIS) |
| 167 |
class optional : optional_base<T> { |
| 168 |
#else |
| 169 |
class optional { |
| 170 |
#endif // !defined(DOXYGEN_SHOULD_SKIP_THIS) |
| 171 |
typedef optional_base<T> base_type; |
| 172 |
|
| 173 |
public: |
| 174 |
/** |
| 175 |
* \brief Optional value type. |
| 176 |
*/ |
| 177 |
typedef T value_type; |
| 178 |
|
| 179 |
/** |
| 180 |
* \brief Constructor. |
| 181 |
* \post *disengaged*. |
| 182 |
*/ |
| 183 |
constexpr optional() : optional_base<T>() {} |
| 184 |
|
| 185 |
/** |
| 186 |
* \brief Constructor. |
| 187 |
* \post *disengaged*. |
| 188 |
*/ |
| 189 |
constexpr optional(nullopt_t) noexcept : optional_base<T>() {} |
| 190 |
|
| 191 |
/** |
| 192 |
* \brief Copy constructor. |
| 193 |
* \param other The other optional object. |
| 194 |
*/ |
| 195 |
optional(const optional &other) { |
| 196 |
if (other) { |
| 197 |
::new (static_cast<void *>(ptr())) T(*other); |
| 198 |
base_type::init_ = true; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* \brief Move constructor. |
| 204 |
* \param other The other optional object. |
| 205 |
*/ |
| 206 |
optional(optional &&other) noexcept { |
| 207 |
if (other) { |
| 208 |
::new (static_cast<void *>(ptr())) T(std::move(other.storage_.value_)); |
| 209 |
base_type::init_ = true; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* \brief Constructor. |
| 215 |
* \param value The value with which to initialize the optional object. |
| 216 |
* \post *engaged* |
| 217 |
*/ |
| 218 |
constexpr optional(const T &value) : optional_base<T>(value) {} |
| 219 |
|
| 220 |
/** |
| 221 |
* \brief Constructor. |
| 222 |
* \param value The value with which to initialize the optional object. |
| 223 |
* \post *engaged* |
| 224 |
*/ |
| 225 |
constexpr optional(T &&value) : optional_base<T>(std::move(value)) {} |
| 226 |
|
| 227 |
/** |
| 228 |
* \brief Assignment operator. |
| 229 |
* \post *disengaged*. |
| 230 |
* \returns \c *this. |
| 231 |
*/ |
| 232 |
optional &operator=(nullopt_t) noexcept { |
| 233 |
if (base_type::init_) { |
| 234 |
ptr()->T::~T(); |
| 235 |
} |
| 236 |
base_type::init_ = false; |
| 237 |
return *this; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* \brief Copy assignment operator. |
| 242 |
* \param other The other optional object. |
| 243 |
* \returns \c *this. |
| 244 |
*/ |
| 245 |
optional &operator=(const optional &other) { |
| 246 |
if (bool(*this) && !other) { |
| 247 |
ptr()->T::~T(); |
| 248 |
base_type::init_ = false; |
| 249 |
} else if (!(*this) && bool(other)) { |
| 250 |
::new (static_cast<void *>(ptr())) T(*other); |
| 251 |
base_type::init_ = true; |
| 252 |
} else if (bool(*this) && bool(other)) { |
| 253 |
base_type::storage_.value_ = *other; |
| 254 |
} |
| 255 |
return *this; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* \brief Move assignment operator. |
| 260 |
* \param other The other optional object. |
| 261 |
* \returns \c *this. |
| 262 |
*/ |
| 263 |
optional &operator=(optional &&other) noexcept { |
| 264 |
if (bool(*this) && !other) { |
| 265 |
ptr()->T::~T(); |
| 266 |
base_type::init_ = false; |
| 267 |
} else if (!(*this) && bool(other)) { |
| 268 |
::new (static_cast<void *>(ptr())) T(std::move(*other)); |
| 269 |
base_type::init_ = true; |
| 270 |
} else if (bool(*this) && bool(other)) { |
| 271 |
base_type::storage_.value_ = std::move(*other); |
| 272 |
} |
| 273 |
return *this; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* \brief Destructor. |
| 278 |
*/ |
| 279 |
~optional() = default; |
| 280 |
|
| 281 |
/** |
| 282 |
* \brief Swap function. |
| 283 |
* \param other The other optional object. |
| 284 |
*/ |
| 285 |
void swap(optional &other) noexcept { |
| 286 |
if (bool(*this) && !other) { |
| 287 |
::new (static_cast<void *>(other.ptr())) T(std::move(**this)); |
| 288 |
ptr()->T::~T(); |
| 289 |
std::swap(base_type::init_, other.base_type::init_); |
| 290 |
} else if (!(*this) && bool(other)) { |
| 291 |
::new (static_cast<void *>(ptr())) T(std::move(*other)); |
| 292 |
other.ptr()->T::~T(); |
| 293 |
std::swap(base_type::init_, other.init_); |
| 294 |
} else if (bool(*this) && bool(other)) { |
| 295 |
std::swap(**this, *other); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* \brief Observer. |
| 301 |
* \pre *engaged* |
| 302 |
* \returns The underlying optional value. |
| 303 |
*/ |
| 304 |
constexpr T const *operator->() const { |
| 305 |
return NETWORK_ASSERTED_EXPRESSION(bool(*this), ptr()); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* \brief Observer. |
| 310 |
* \pre *engaged* |
| 311 |
* \returns The underlying optional value. |
| 312 |
*/ |
| 313 |
T *operator->() { return NETWORK_ASSERTED_EXPRESSION(bool(*this), ptr()); } |
| 314 |
|
| 315 |
/** |
| 316 |
* \brief Observer. |
| 317 |
* \pre *engaged* |
| 318 |
* \returns The underlying optional value. |
| 319 |
*/ |
| 320 |
constexpr T const &operator*() const { |
| 321 |
return NETWORK_ASSERTED_EXPRESSION(bool(*this), base_type::storage_.value_); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* \brief Observer. |
| 326 |
* \pre *engaged* |
| 327 |
* \returns The underlying optional value. |
| 328 |
*/ |
| 329 |
T &operator*() { |
| 330 |
return NETWORK_ASSERTED_EXPRESSION(bool(*this), base_type::storage_.value_); |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* \brief Operator bool overloads. |
| 335 |
* \returns \c true if the optional is *engaged*, \c false if it is |
| 336 |
* *disengaged*. |
| 337 |
*/ |
| 338 |
constexpr explicit operator bool() const noexcept { return base_type::init_; } |
| 339 |
|
| 340 |
/** |
| 341 |
* \returns The underlying optional value, if \c bool(*this). |
| 342 |
* \throws A bad_optional_access if \c !*this. |
| 343 |
*/ |
| 344 |
constexpr T const &value() const { |
| 345 |
return *this ? base_type::storage_.value_ |
| 346 |
: (throw bad_optional_access("Uninitialized optional value"), |
| 347 |
base_type::storage_.value_); |
| 348 |
} |
| 349 |
/** |
| 350 |
* \returns The underlying optional value, if \c bool(*this). |
| 351 |
* \throws A bad_optional_access if \c !*this. |
| 352 |
*/ |
| 353 |
T &value() { |
| 354 |
return *this ? base_type::storage_.value_ |
| 355 |
: (throw bad_optional_access("Uninitialized optional value"), |
| 356 |
base_type::storage_.value_); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* \returns <tt>bool(*this) ? **this : |
| 361 |
* static_cast<T>(std::forward<U>(v))</tt>. \pre \c |
| 362 |
* <tt>std::is_copy_constructible<T>::value</tt> is \c true and |
| 363 |
* <tt>std::is_convertible<U&&, T>::value</tt> is \c true. |
| 364 |
*/ |
| 365 |
template <class U> |
| 366 |
T value_or(U &&other) const & { |
| 367 |
static_assert(std::is_copy_constructible<value_type>::value, |
| 368 |
"Must be copy constructible."); |
| 369 |
static_assert(std::is_convertible<U, value_type>::value, |
| 370 |
"U must be convertible to T."); |
| 371 |
return bool(*this) ? **this : static_cast<T>(std::forward<U>(other)); |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* \returns <tt>bool(*this) ? std::move(**this) : |
| 376 |
* static_cast<T>(std::forward<U>(v))</tt>. \pre |
| 377 |
* <tt>std::is_move_constructible<T>::value</tt> is \c true and |
| 378 |
* <tt>std::is_convertible<U&&, T>::value</tt> is \c true. |
| 379 |
*/ |
| 380 |
template <class U> |
| 381 |
T value_or(U &&other) && { |
| 382 |
static_assert(std::is_copy_constructible<value_type>::value, |
| 383 |
"Must be copy constructible."); |
| 384 |
static_assert(std::is_convertible<U, value_type>::value, |
| 385 |
"U must be convertible to T."); |
| 386 |
return bool(*this) ? std::move(**this) |
| 387 |
: static_cast<T>(std::forward<U>(other)); |
| 388 |
} |
| 389 |
|
| 390 |
private: |
| 391 |
T *ptr() { return std::addressof(base_type::storage_.value_); } |
| 392 |
}; |
| 393 |
|
| 394 |
/** |
| 395 |
* \brief Equality operator. |
| 396 |
*/ |
| 397 |
template <class T> |
| 398 |
bool operator==(const optional<T> &lhs, const optional<T> &rhs) { |
| 399 |
if (bool(lhs) != bool(rhs)) { |
| 400 |
return false; |
| 401 |
} else if (!bool(lhs)) { |
| 402 |
return true; |
| 403 |
} else { |
| 404 |
return *lhs == *rhs; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* \brief Inequality operator. |
| 410 |
*/ |
| 411 |
template <class T> |
| 412 |
bool operator!=(const optional<T> &lhs, const optional<T> &rhs) { |
| 413 |
return !(lhs == rhs); |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* \brief Comparison operator. |
| 418 |
*/ |
| 419 |
template <class T> |
| 420 |
bool operator<(const optional<T> &lhs, const optional<T> &rhs) { |
| 421 |
if (!rhs) { |
| 422 |
return false; |
| 423 |
} else if (!lhs) { |
| 424 |
return true; |
| 425 |
} else { |
| 426 |
return *lhs < *rhs; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
/** |
| 431 |
* \brief Comparison operator. |
| 432 |
* \returns <tt>rhs < lhs</tt>. |
| 433 |
*/ |
| 434 |
template <class T> |
| 435 |
bool operator>(const optional<T> &lhs, const optional<T> &rhs) { |
| 436 |
return rhs < lhs; |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* \brief Comparison operator. |
| 441 |
* \returns <tt>!(rhs < lhs)</tt>. |
| 442 |
*/ |
| 443 |
template <class T> |
| 444 |
bool operator<=(const optional<T> &lhs, const optional<T> &rhs) { |
| 445 |
return !(rhs < lhs); |
| 446 |
} |
| 447 |
|
| 448 |
/** |
| 449 |
* \brief Comparison operator. |
| 450 |
* \returns <tt>!(rhs > lhs)</tt>. |
| 451 |
*/ |
| 452 |
template <class T> |
| 453 |
bool operator>=(const optional<T> &lhs, const optional<T> &rhs) { |
| 454 |
return !(lhs < rhs); |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* \brief Equality operator. |
| 459 |
* \returns \c !x. |
| 460 |
*/ |
| 461 |
template <class T> |
| 462 |
bool operator==(const optional<T> &x, nullopt_t) noexcept { |
| 463 |
return !x; |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* \brief Equality operator. |
| 468 |
* \returns \c !x. |
| 469 |
*/ |
| 470 |
template <class T> |
| 471 |
bool operator==(nullopt_t, const optional<T> &x) noexcept { |
| 472 |
return !x; |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* \brief Inequality operator. |
| 477 |
* \returns \c bool(x). |
| 478 |
*/ |
| 479 |
template <class T> |
| 480 |
bool operator!=(const optional<T> &x, nullopt_t) noexcept { |
| 481 |
return bool(x); |
| 482 |
} |
| 483 |
|
| 484 |
/** |
| 485 |
* \brief Inequality operator. |
| 486 |
* \returns \c bool(x). |
| 487 |
*/ |
| 488 |
template <class T> |
| 489 |
bool operator!=(nullopt_t, const optional<T> &x) noexcept { |
| 490 |
return bool(x); |
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
* \brief Comparison operator. |
| 495 |
* \returns \c false. |
| 496 |
*/ |
| 497 |
template <class T> |
| 498 |
bool operator<(const optional<T> &x, nullopt_t) noexcept { |
| 499 |
return false; |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* \brief Comparison operator. |
| 504 |
* \returns \c bool(x). |
| 505 |
*/ |
| 506 |
template <class T> |
| 507 |
bool operator<(nullopt_t, const optional<T> &x) noexcept { |
| 508 |
return bool(x); |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* \brief Comparison operator. |
| 513 |
* \returns \c !x. |
| 514 |
*/ |
| 515 |
template <class T> |
| 516 |
bool operator<=(const optional<T> &x, nullopt_t) noexcept { |
| 517 |
return !x; |
| 518 |
} |
| 519 |
|
| 520 |
/** |
| 521 |
* \brief Comparison operator. |
| 522 |
* \returns \c true. |
| 523 |
*/ |
| 524 |
template <class T> |
| 525 |
bool operator<=(nullopt_t, const optional<T> &x) noexcept { |
| 526 |
return true; |
| 527 |
} |
| 528 |
|
| 529 |
/** |
| 530 |
* \brief Comparison operator. |
| 531 |
* \returns \c bool(x). |
| 532 |
*/ |
| 533 |
template <class T> |
| 534 |
bool operator>(const optional<T> &x, nullopt_t) noexcept { |
| 535 |
return bool(x); |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* \brief Comparison operator. |
| 540 |
* \returns \c false. |
| 541 |
*/ |
| 542 |
template <class T> |
| 543 |
bool operator>(nullopt_t, const optional<T> &x) noexcept { |
| 544 |
return false; |
| 545 |
} |
| 546 |
|
| 547 |
/** |
| 548 |
* \brief Comparison operator. |
| 549 |
* \returns \c true. |
| 550 |
*/ |
| 551 |
template <class T> |
| 552 |
bool operator>=(const optional<T> &x, nullopt_t) noexcept { |
| 553 |
return true; |
| 554 |
} |
| 555 |
|
| 556 |
/** |
| 557 |
* \brief Comparison operator. |
| 558 |
* \returns \c !x. |
| 559 |
*/ |
| 560 |
template <class T> |
| 561 |
bool operator>=(nullopt_t, const optional<T> &x) noexcept { |
| 562 |
return !x; |
| 563 |
} |
| 564 |
|
| 565 |
/** |
| 566 |
* \brief Equality operator. |
| 567 |
* \returns <tt>bool(x) ? *x == v : false</tt>. |
| 568 |
*/ |
| 569 |
template <class T> |
| 570 |
bool operator==(const optional<T> &x, const T &v) { |
| 571 |
return bool(x) ? *x == v : false; |
| 572 |
} |
| 573 |
|
| 574 |
/** |
| 575 |
* \brief Equality operator. |
| 576 |
* \returns <tt>bool(x) ? v == *x : false</tt>. |
| 577 |
*/ |
| 578 |
template <class T> |
| 579 |
bool operator==(const T &v, const optional<T> &x) { |
| 580 |
return bool(x) ? v == *x : false; |
| 581 |
} |
| 582 |
|
| 583 |
/** |
| 584 |
* \brief Inequality operator. |
| 585 |
* \returns <tt>bool(x) ? !(*x == v) : true</tt>. |
| 586 |
*/ |
| 587 |
template <class T> |
| 588 |
bool operator!=(const optional<T> &x, const T &v) { |
| 589 |
return bool(x) ? !(*x == v) : true; |
| 590 |
} |
| 591 |
|
| 592 |
/** |
| 593 |
* \brief Inequality operator. |
| 594 |
* \returns <tt>bool(x) ? !(v == *x) : true</tt>. |
| 595 |
*/ |
| 596 |
template <class T> |
| 597 |
bool operator!=(const T &v, const optional<T> &x) { |
| 598 |
return bool(x) ? !(v == *x) : true; |
| 599 |
} |
| 600 |
|
| 601 |
/** |
| 602 |
* \brief Comparison operator. |
| 603 |
* \returns <tt>bool(x) ? *x < v : true</tt>. |
| 604 |
*/ |
| 605 |
template <class T> |
| 606 |
bool operator<(const optional<T> &x, const T &v) { |
| 607 |
return bool(x) ? *x < v : true; |
| 608 |
} |
| 609 |
|
| 610 |
/** |
| 611 |
* \brief Comparison operator. |
| 612 |
* \returns <tt>bool(x) ? v < *x : false</tt>. |
| 613 |
*/ |
| 614 |
template <class T> |
| 615 |
bool operator<(const T &v, const optional<T> &x) { |
| 616 |
return bool(x) ? v < *x : false; |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* \brief Comparison operator. |
| 621 |
* \returns <tt>bool(x) ? *x < v : true</tt>. |
| 622 |
*/ |
| 623 |
template <class T> |
| 624 |
bool operator>(const optional<T> &x, const T &v) { |
| 625 |
return bool(x) ? *x < v : true; |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
* \brief Comparison operator. |
| 630 |
* \returns <tt>bool(x) ? v < *x : false</tt>. |
| 631 |
*/ |
| 632 |
template <class T> |
| 633 |
bool operator>(const T &v, const optional<T> &x) { |
| 634 |
return bool(x) ? v < *x : false; |
| 635 |
} |
| 636 |
|
| 637 |
/** |
| 638 |
* \brief Comparison operator. |
| 639 |
* \returns <tt>!(x < v)</tt>. |
| 640 |
*/ |
| 641 |
template <class T> |
| 642 |
bool operator>=(const optional<T> &x, const T &v) { |
| 643 |
return !(x < v); |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* \brief Comparison operator. |
| 648 |
* \returns <tt>!(v < x)</tt>. |
| 649 |
*/ |
| 650 |
template <class T> |
| 651 |
bool operator>=(const T &v, const optional<T> &x) { |
| 652 |
return !(v < x); |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
* \brief Comparison operator. |
| 657 |
* \returns <tt>!(x > v)</tt>. |
| 658 |
*/ |
| 659 |
template <class T> |
| 660 |
bool operator<=(const optional<T> &x, const T &v) { |
| 661 |
return !(x > v); |
| 662 |
} |
| 663 |
|
| 664 |
/** |
| 665 |
* \brief Comparison operator. |
| 666 |
* \returns <tt>!(v > x)</tt>. |
| 667 |
*/ |
| 668 |
template <class T> |
| 669 |
bool operator<=(const T &v, const optional<T> &x) { |
| 670 |
return !(v > x); |
| 671 |
} |
| 672 |
|
| 673 |
/** |
| 674 |
* \ingroup optional |
| 675 |
* \brief Swap function. |
| 676 |
* \param lhs The first optional object. |
| 677 |
* \param rhs The second optional object. |
| 678 |
* |
| 679 |
* Calls: |
| 680 |
* \code{.cpp} |
| 681 |
* lhs.swap(rhs); |
| 682 |
* \endcode |
| 683 |
*/ |
| 684 |
template <class T> |
| 685 |
inline void swap(optional<T> &lhs, |
| 686 |
optional<T> &rhs) noexcept(noexcept(lhs.swap(rhs))) { |
| 687 |
return lhs.swap(rhs); |
| 688 |
} |
| 689 |
|
| 690 |
/** |
| 691 |
* \ingroup optional |
| 692 |
* \brief A helper function to contruct an optional object. |
| 693 |
* \returns <tt>optional<typename |
| 694 |
* std::decay<T>::type>(std::forward(value))</tt>. |
| 695 |
*/ |
| 696 |
template <class T> |
| 697 |
inline constexpr optional<typename std::decay<T>::type> make_optional( |
| 698 |
T &&value) { |
| 699 |
return optional<typename std::decay<T>::type>(std::forward(value)); |
| 700 |
} |
| 701 |
} // namespace network |
| 702 |
|
| 703 |
#endif // NETWORK_OPTIONAL_INC |