123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953 |
- #ifndef BASE_OPTIONAL_H_
- #define BASE_OPTIONAL_H_
- #include <functional>
- #include <type_traits>
- #include <utility>
- #include "base/check.h"
- #include "base/template_util.h"
- namespace base {
- struct nullopt_t {
- constexpr explicit nullopt_t(int) {}
- };
- constexpr nullopt_t nullopt(0);
- template <typename T>
- class Optional;
- namespace internal {
- struct DummyUnionMember {};
- template <typename T, bool = std::is_trivially_destructible<T>::value>
- struct OptionalStorageBase {
-
-
- constexpr OptionalStorageBase() : dummy_() {}
- template <class... Args>
- constexpr explicit OptionalStorageBase(in_place_t, Args&&... args)
- : is_populated_(true), value_(std::forward<Args>(args)...) {}
-
-
-
-
-
-
-
-
-
-
-
- ~OptionalStorageBase() {
- if (is_populated_)
- value_.~T();
- }
- template <class... Args>
- void Init(Args&&... args) {
- DCHECK(!is_populated_);
- ::new (std::addressof(value_)) T(std::forward<Args>(args)...);
- is_populated_ = true;
- }
- bool is_populated_ = false;
- union {
-
-
-
-
-
-
-
-
-
-
-
-
- DummyUnionMember dummy_;
- T value_;
- };
- };
- template <typename T>
- struct OptionalStorageBase<T, true > {
-
-
- constexpr OptionalStorageBase() : dummy_() {}
- template <class... Args>
- constexpr explicit OptionalStorageBase(in_place_t, Args&&... args)
- : is_populated_(true), value_(std::forward<Args>(args)...) {}
-
-
-
-
-
-
-
-
-
-
-
-
-
- template <class... Args>
- void Init(Args&&... args) {
- DCHECK(!is_populated_);
- ::new (std::addressof(value_)) T(std::forward<Args>(args)...);
- is_populated_ = true;
- }
- bool is_populated_ = false;
- union {
-
-
-
-
-
-
-
-
-
-
-
-
- DummyUnionMember dummy_;
- T value_;
- };
- };
- template <typename T,
- bool = is_trivially_copy_constructible<T>::value,
- bool = std::is_trivially_move_constructible<T>::value>
- struct OptionalStorage : OptionalStorageBase<T> {
-
-
-
-
- using OptionalStorageBase<T>::is_populated_;
- using OptionalStorageBase<T>::value_;
- using OptionalStorageBase<T>::Init;
-
- using OptionalStorageBase<T>::OptionalStorageBase;
-
-
- OptionalStorage() = default;
- OptionalStorage(const OptionalStorage& other) {
- if (other.is_populated_)
- Init(other.value_);
- }
- OptionalStorage(OptionalStorage&& other) noexcept(
- std::is_nothrow_move_constructible<T>::value) {
- if (other.is_populated_)
- Init(std::move(other.value_));
- }
- };
- template <typename T>
- struct OptionalStorage<T,
- true ,
- false >
- : OptionalStorageBase<T> {
- using OptionalStorageBase<T>::is_populated_;
- using OptionalStorageBase<T>::value_;
- using OptionalStorageBase<T>::Init;
- using OptionalStorageBase<T>::OptionalStorageBase;
- OptionalStorage() = default;
- OptionalStorage(const OptionalStorage& other) = default;
- OptionalStorage(OptionalStorage&& other) noexcept(
- std::is_nothrow_move_constructible<T>::value) {
- if (other.is_populated_)
- Init(std::move(other.value_));
- }
- };
- template <typename T>
- struct OptionalStorage<T,
- false ,
- true >
- : OptionalStorageBase<T> {
- using OptionalStorageBase<T>::is_populated_;
- using OptionalStorageBase<T>::value_;
- using OptionalStorageBase<T>::Init;
- using OptionalStorageBase<T>::OptionalStorageBase;
- OptionalStorage() = default;
- OptionalStorage(OptionalStorage&& other) = default;
- OptionalStorage(const OptionalStorage& other) {
- if (other.is_populated_)
- Init(other.value_);
- }
- };
- template <typename T>
- struct OptionalStorage<T,
- true ,
- true >
- : OptionalStorageBase<T> {
-
-
-
- using OptionalStorageBase<T>::OptionalStorageBase;
- };
- template <typename T>
- class OptionalBase {
-
-
-
- protected:
- constexpr OptionalBase() = default;
- constexpr OptionalBase(const OptionalBase& other) = default;
- constexpr OptionalBase(OptionalBase&& other) = default;
- template <class... Args>
- constexpr explicit OptionalBase(in_place_t, Args&&... args)
- : storage_(in_place, std::forward<Args>(args)...) {}
-
- template <typename U>
- explicit OptionalBase(const OptionalBase<U>& other) {
- if (other.storage_.is_populated_)
- storage_.Init(other.storage_.value_);
- }
- template <typename U>
- explicit OptionalBase(OptionalBase<U>&& other) {
- if (other.storage_.is_populated_)
- storage_.Init(std::move(other.storage_.value_));
- }
- ~OptionalBase() = default;
- OptionalBase& operator=(const OptionalBase& other) {
- CopyAssign(other);
- return *this;
- }
- OptionalBase& operator=(OptionalBase&& other) noexcept(
- std::is_nothrow_move_assignable<T>::value&&
- std::is_nothrow_move_constructible<T>::value) {
- MoveAssign(std::move(other));
- return *this;
- }
- template <typename U>
- void CopyAssign(const OptionalBase<U>& other) {
- if (other.storage_.is_populated_)
- InitOrAssign(other.storage_.value_);
- else
- FreeIfNeeded();
- }
- template <typename U>
- void MoveAssign(OptionalBase<U>&& other) {
- if (other.storage_.is_populated_)
- InitOrAssign(std::move(other.storage_.value_));
- else
- FreeIfNeeded();
- }
- template <typename U>
- void InitOrAssign(U&& value) {
- if (storage_.is_populated_)
- storage_.value_ = std::forward<U>(value);
- else
- storage_.Init(std::forward<U>(value));
- }
- void FreeIfNeeded() {
- if (!storage_.is_populated_)
- return;
- storage_.value_.~T();
- storage_.is_populated_ = false;
- }
-
-
- template <typename U>
- friend class OptionalBase;
- OptionalStorage<T> storage_;
- };
- template <bool is_copy_constructible>
- struct CopyConstructible {};
- template <>
- struct CopyConstructible<false> {
- constexpr CopyConstructible() = default;
- constexpr CopyConstructible(const CopyConstructible&) = delete;
- constexpr CopyConstructible(CopyConstructible&&) = default;
- CopyConstructible& operator=(const CopyConstructible&) = default;
- CopyConstructible& operator=(CopyConstructible&&) = default;
- };
- template <bool is_move_constructible>
- struct MoveConstructible {};
- template <>
- struct MoveConstructible<false> {
- constexpr MoveConstructible() = default;
- constexpr MoveConstructible(const MoveConstructible&) = default;
- constexpr MoveConstructible(MoveConstructible&&) = delete;
- MoveConstructible& operator=(const MoveConstructible&) = default;
- MoveConstructible& operator=(MoveConstructible&&) = default;
- };
- template <bool is_copy_assignable>
- struct CopyAssignable {};
- template <>
- struct CopyAssignable<false> {
- constexpr CopyAssignable() = default;
- constexpr CopyAssignable(const CopyAssignable&) = default;
- constexpr CopyAssignable(CopyAssignable&&) = default;
- CopyAssignable& operator=(const CopyAssignable&) = delete;
- CopyAssignable& operator=(CopyAssignable&&) = default;
- };
- template <bool is_move_assignable>
- struct MoveAssignable {};
- template <>
- struct MoveAssignable<false> {
- constexpr MoveAssignable() = default;
- constexpr MoveAssignable(const MoveAssignable&) = default;
- constexpr MoveAssignable(MoveAssignable&&) = default;
- MoveAssignable& operator=(const MoveAssignable&) = default;
- MoveAssignable& operator=(MoveAssignable&&) = delete;
- };
- template <typename T, typename U>
- using IsConvertibleFromOptional =
- disjunction<std::is_constructible<T, Optional<U>&>,
- std::is_constructible<T, const Optional<U>&>,
- std::is_constructible<T, Optional<U>&&>,
- std::is_constructible<T, const Optional<U>&&>,
- std::is_convertible<Optional<U>&, T>,
- std::is_convertible<const Optional<U>&, T>,
- std::is_convertible<Optional<U>&&, T>,
- std::is_convertible<const Optional<U>&&, T>>;
- template <typename T, typename U>
- using IsAssignableFromOptional =
- disjunction<IsConvertibleFromOptional<T, U>,
- std::is_assignable<T&, Optional<U>&>,
- std::is_assignable<T&, const Optional<U>&>,
- std::is_assignable<T&, Optional<U>&&>,
- std::is_assignable<T&, const Optional<U>&&>>;
- namespace swappable_impl {
- using std::swap;
- struct IsSwappableImpl {
-
-
-
- template <typename T>
- static auto Check(int)
- -> decltype(swap(std::declval<T>(), std::declval<T>()), std::true_type());
- template <typename T>
- static std::false_type Check(...);
- };
- }
- template <typename T>
- struct IsSwappable : decltype(swappable_impl::IsSwappableImpl::Check<T&>(0)) {};
- template <typename T>
- using RemoveCvRefT = std::remove_cv_t<std::remove_reference_t<T>>;
- }
- #ifdef OS_WIN
- #define OPTIONAL_DECLSPEC_EMPTY_BASES __declspec(empty_bases)
- #else
- #define OPTIONAL_DECLSPEC_EMPTY_BASES
- #endif
- template <typename T>
- class OPTIONAL_DECLSPEC_EMPTY_BASES Optional
- : public internal::OptionalBase<T>,
- public internal::CopyConstructible<std::is_copy_constructible<T>::value>,
- public internal::MoveConstructible<std::is_move_constructible<T>::value>,
- public internal::CopyAssignable<std::is_copy_constructible<T>::value &&
- std::is_copy_assignable<T>::value>,
- public internal::MoveAssignable<std::is_move_constructible<T>::value &&
- std::is_move_assignable<T>::value> {
- private:
-
-
- static_assert(
- !std::is_same<internal::RemoveCvRefT<T>, in_place_t>::value,
- "instantiation of base::Optional with in_place_t is ill-formed");
- static_assert(!std::is_same<internal::RemoveCvRefT<T>, nullopt_t>::value,
- "instantiation of base::Optional with nullopt_t is ill-formed");
- static_assert(
- !std::is_reference<T>::value,
- "instantiation of base::Optional with a reference type is ill-formed");
-
- static_assert(std::is_destructible<T>::value,
- "instantiation of base::Optional with a non-destructible type "
- "is ill-formed");
-
-
-
- static_assert(
- !std::is_array<T>::value,
- "instantiation of base::Optional with an array type is ill-formed");
- public:
- #undef OPTIONAL_DECLSPEC_EMPTY_BASES
- using value_type = T;
-
- constexpr Optional() = default;
- constexpr Optional(const Optional& other) = default;
- constexpr Optional(Optional&& other) noexcept(
- std::is_nothrow_move_constructible<T>::value) = default;
- constexpr Optional(nullopt_t) {}
-
-
-
-
- template <
- typename U,
- std::enable_if_t<std::is_constructible<T, const U&>::value &&
- !internal::IsConvertibleFromOptional<T, U>::value &&
- std::is_convertible<const U&, T>::value,
- bool> = false>
- Optional(const Optional<U>& other) : internal::OptionalBase<T>(other) {}
- template <
- typename U,
- std::enable_if_t<std::is_constructible<T, const U&>::value &&
- !internal::IsConvertibleFromOptional<T, U>::value &&
- !std::is_convertible<const U&, T>::value,
- bool> = false>
- explicit Optional(const Optional<U>& other)
- : internal::OptionalBase<T>(other) {}
-
-
- template <
- typename U,
- std::enable_if_t<std::is_constructible<T, U&&>::value &&
- !internal::IsConvertibleFromOptional<T, U>::value &&
- std::is_convertible<U&&, T>::value,
- bool> = false>
- Optional(Optional<U>&& other) : internal::OptionalBase<T>(std::move(other)) {}
- template <
- typename U,
- std::enable_if_t<std::is_constructible<T, U&&>::value &&
- !internal::IsConvertibleFromOptional<T, U>::value &&
- !std::is_convertible<U&&, T>::value,
- bool> = false>
- explicit Optional(Optional<U>&& other)
- : internal::OptionalBase<T>(std::move(other)) {}
- template <class... Args>
- constexpr explicit Optional(in_place_t, Args&&... args)
- : internal::OptionalBase<T>(in_place, std::forward<Args>(args)...) {}
- template <
- class U,
- class... Args,
- class = std::enable_if_t<std::is_constructible<value_type,
- std::initializer_list<U>&,
- Args...>::value>>
- constexpr explicit Optional(in_place_t,
- std::initializer_list<U> il,
- Args&&... args)
- : internal::OptionalBase<T>(in_place, il, std::forward<Args>(args)...) {}
-
-
- template <
- typename U = value_type,
- std::enable_if_t<
- std::is_constructible<T, U&&>::value &&
- !std::is_same<internal::RemoveCvRefT<U>, in_place_t>::value &&
- !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value &&
- std::is_convertible<U&&, T>::value,
- bool> = false>
- constexpr Optional(U&& value)
- : internal::OptionalBase<T>(in_place, std::forward<U>(value)) {}
- template <
- typename U = value_type,
- std::enable_if_t<
- std::is_constructible<T, U&&>::value &&
- !std::is_same<internal::RemoveCvRefT<U>, in_place_t>::value &&
- !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value &&
- !std::is_convertible<U&&, T>::value,
- bool> = false>
- constexpr explicit Optional(U&& value)
- : internal::OptionalBase<T>(in_place, std::forward<U>(value)) {}
- ~Optional() = default;
-
- Optional& operator=(const Optional& other) = default;
- Optional& operator=(Optional&& other) noexcept(
- std::is_nothrow_move_assignable<T>::value&&
- std::is_nothrow_move_constructible<T>::value) = default;
- Optional& operator=(nullopt_t) {
- FreeIfNeeded();
- return *this;
- }
-
- template <typename U>
- std::enable_if_t<
- !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value &&
- std::is_constructible<T, U>::value &&
- std::is_assignable<T&, U>::value &&
- (!std::is_scalar<T>::value ||
- !std::is_same<std::decay_t<U>, T>::value),
- Optional&>
- operator=(U&& value) {
- InitOrAssign(std::forward<U>(value));
- return *this;
- }
-
- template <typename U>
- std::enable_if_t<!internal::IsAssignableFromOptional<T, U>::value &&
- std::is_constructible<T, const U&>::value &&
- std::is_assignable<T&, const U&>::value,
- Optional&>
- operator=(const Optional<U>& other) {
- CopyAssign(other);
- return *this;
- }
-
- template <typename U>
- std::enable_if_t<!internal::IsAssignableFromOptional<T, U>::value &&
- std::is_constructible<T, U>::value &&
- std::is_assignable<T&, U>::value,
- Optional&>
- operator=(Optional<U>&& other) {
- MoveAssign(std::move(other));
- return *this;
- }
- constexpr const T* operator->() const {
- CHECK(storage_.is_populated_);
- return std::addressof(storage_.value_);
- }
- constexpr T* operator->() {
- CHECK(storage_.is_populated_);
- return std::addressof(storage_.value_);
- }
- constexpr const T& operator*() const & {
- CHECK(storage_.is_populated_);
- return storage_.value_;
- }
- constexpr T& operator*() & {
- CHECK(storage_.is_populated_);
- return storage_.value_;
- }
- constexpr const T&& operator*() const && {
- CHECK(storage_.is_populated_);
- return std::move(storage_.value_);
- }
- constexpr T&& operator*() && {
- CHECK(storage_.is_populated_);
- return std::move(storage_.value_);
- }
- constexpr explicit operator bool() const { return storage_.is_populated_; }
- constexpr bool has_value() const { return storage_.is_populated_; }
- constexpr T& value() & {
- CHECK(storage_.is_populated_);
- return storage_.value_;
- }
- constexpr const T& value() const & {
- CHECK(storage_.is_populated_);
- return storage_.value_;
- }
- constexpr T&& value() && {
- CHECK(storage_.is_populated_);
- return std::move(storage_.value_);
- }
- constexpr const T&& value() const && {
- CHECK(storage_.is_populated_);
- return std::move(storage_.value_);
- }
- template <class U>
- constexpr T value_or(U&& default_value) const& {
-
-
-
- static_assert(std::is_convertible<U, T>::value,
- "U must be convertible to T");
- return storage_.is_populated_
- ? storage_.value_
- : static_cast<T>(std::forward<U>(default_value));
- }
- template <class U>
- constexpr T value_or(U&& default_value) && {
-
-
-
- static_assert(std::is_convertible<U, T>::value,
- "U must be convertible to T");
- return storage_.is_populated_
- ? std::move(storage_.value_)
- : static_cast<T>(std::forward<U>(default_value));
- }
- void swap(Optional& other) {
- if (!storage_.is_populated_ && !other.storage_.is_populated_)
- return;
- if (storage_.is_populated_ != other.storage_.is_populated_) {
- if (storage_.is_populated_) {
- other.storage_.Init(std::move(storage_.value_));
- FreeIfNeeded();
- } else {
- storage_.Init(std::move(other.storage_.value_));
- other.FreeIfNeeded();
- }
- return;
- }
- DCHECK(storage_.is_populated_ && other.storage_.is_populated_);
- using std::swap;
- swap(**this, *other);
- }
- void reset() { FreeIfNeeded(); }
- template <class... Args>
- T& emplace(Args&&... args) {
- FreeIfNeeded();
- storage_.Init(std::forward<Args>(args)...);
- return storage_.value_;
- }
- template <class U, class... Args>
- std::enable_if_t<
- std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value,
- T&>
- emplace(std::initializer_list<U> il, Args&&... args) {
- FreeIfNeeded();
- storage_.Init(il, std::forward<Args>(args)...);
- return storage_.value_;
- }
- private:
-
-
- using internal::OptionalBase<T>::CopyAssign;
- using internal::OptionalBase<T>::FreeIfNeeded;
- using internal::OptionalBase<T>::InitOrAssign;
- using internal::OptionalBase<T>::MoveAssign;
- using internal::OptionalBase<T>::storage_;
- };
- template <class T, class U>
- constexpr bool operator==(const Optional<T>& lhs, const Optional<U>& rhs) {
- if (lhs.has_value() != rhs.has_value())
- return false;
- if (!lhs.has_value())
- return true;
- return *lhs == *rhs;
- }
- template <class T, class U>
- constexpr bool operator!=(const Optional<T>& lhs, const Optional<U>& rhs) {
- if (lhs.has_value() != rhs.has_value())
- return true;
- if (!lhs.has_value())
- return false;
- return *lhs != *rhs;
- }
- template <class T, class U>
- constexpr bool operator<(const Optional<T>& lhs, const Optional<U>& rhs) {
- if (!rhs.has_value())
- return false;
- if (!lhs.has_value())
- return true;
- return *lhs < *rhs;
- }
- template <class T, class U>
- constexpr bool operator<=(const Optional<T>& lhs, const Optional<U>& rhs) {
- if (!lhs.has_value())
- return true;
- if (!rhs.has_value())
- return false;
- return *lhs <= *rhs;
- }
- template <class T, class U>
- constexpr bool operator>(const Optional<T>& lhs, const Optional<U>& rhs) {
- if (!lhs.has_value())
- return false;
- if (!rhs.has_value())
- return true;
- return *lhs > *rhs;
- }
- template <class T, class U>
- constexpr bool operator>=(const Optional<T>& lhs, const Optional<U>& rhs) {
- if (!rhs.has_value())
- return true;
- if (!lhs.has_value())
- return false;
- return *lhs >= *rhs;
- }
- template <class T>
- constexpr bool operator==(const Optional<T>& opt, nullopt_t) {
- return !opt;
- }
- template <class T>
- constexpr bool operator==(nullopt_t, const Optional<T>& opt) {
- return !opt;
- }
- template <class T>
- constexpr bool operator!=(const Optional<T>& opt, nullopt_t) {
- return opt.has_value();
- }
- template <class T>
- constexpr bool operator!=(nullopt_t, const Optional<T>& opt) {
- return opt.has_value();
- }
- template <class T>
- constexpr bool operator<(const Optional<T>& opt, nullopt_t) {
- return false;
- }
- template <class T>
- constexpr bool operator<(nullopt_t, const Optional<T>& opt) {
- return opt.has_value();
- }
- template <class T>
- constexpr bool operator<=(const Optional<T>& opt, nullopt_t) {
- return !opt;
- }
- template <class T>
- constexpr bool operator<=(nullopt_t, const Optional<T>& opt) {
- return true;
- }
- template <class T>
- constexpr bool operator>(const Optional<T>& opt, nullopt_t) {
- return opt.has_value();
- }
- template <class T>
- constexpr bool operator>(nullopt_t, const Optional<T>& opt) {
- return false;
- }
- template <class T>
- constexpr bool operator>=(const Optional<T>& opt, nullopt_t) {
- return true;
- }
- template <class T>
- constexpr bool operator>=(nullopt_t, const Optional<T>& opt) {
- return !opt;
- }
- template <class T, class U>
- constexpr bool operator==(const Optional<T>& opt, const U& value) {
- return opt.has_value() ? *opt == value : false;
- }
- template <class T, class U>
- constexpr bool operator==(const U& value, const Optional<T>& opt) {
- return opt.has_value() ? value == *opt : false;
- }
- template <class T, class U>
- constexpr bool operator!=(const Optional<T>& opt, const U& value) {
- return opt.has_value() ? *opt != value : true;
- }
- template <class T, class U>
- constexpr bool operator!=(const U& value, const Optional<T>& opt) {
- return opt.has_value() ? value != *opt : true;
- }
- template <class T, class U>
- constexpr bool operator<(const Optional<T>& opt, const U& value) {
- return opt.has_value() ? *opt < value : true;
- }
- template <class T, class U>
- constexpr bool operator<(const U& value, const Optional<T>& opt) {
- return opt.has_value() ? value < *opt : false;
- }
- template <class T, class U>
- constexpr bool operator<=(const Optional<T>& opt, const U& value) {
- return opt.has_value() ? *opt <= value : true;
- }
- template <class T, class U>
- constexpr bool operator<=(const U& value, const Optional<T>& opt) {
- return opt.has_value() ? value <= *opt : false;
- }
- template <class T, class U>
- constexpr bool operator>(const Optional<T>& opt, const U& value) {
- return opt.has_value() ? *opt > value : false;
- }
- template <class T, class U>
- constexpr bool operator>(const U& value, const Optional<T>& opt) {
- return opt.has_value() ? value > *opt : true;
- }
- template <class T, class U>
- constexpr bool operator>=(const Optional<T>& opt, const U& value) {
- return opt.has_value() ? *opt >= value : false;
- }
- template <class T, class U>
- constexpr bool operator>=(const U& value, const Optional<T>& opt) {
- return opt.has_value() ? value >= *opt : true;
- }
- template <class T>
- constexpr Optional<std::decay_t<T>> make_optional(T&& value) {
- return Optional<std::decay_t<T>>(std::forward<T>(value));
- }
- template <class T, class... Args>
- constexpr Optional<T> make_optional(Args&&... args) {
- return Optional<T>(in_place, std::forward<Args>(args)...);
- }
- template <class T, class U, class... Args>
- constexpr Optional<T> make_optional(std::initializer_list<U> il,
- Args&&... args) {
- return Optional<T>(in_place, il, std::forward<Args>(args)...);
- }
- template <class T>
- std::enable_if_t<std::is_move_constructible<T>::value &&
- internal::IsSwappable<T>::value>
- swap(Optional<T>& lhs, Optional<T>& rhs) {
- lhs.swap(rhs);
- }
- }
- namespace std {
- template <class T>
- struct hash<base::Optional<T>> {
- size_t operator()(const base::Optional<T>& opt) const {
- return opt == base::nullopt ? 0 : std::hash<T>()(*opt);
- }
- };
- }
- #endif
|