mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_OPTIONAL_H_ |
| 6 | #define BASE_OPTIONAL_H_ |
| 7 | |
| 8 | #include <type_traits> |
| 9 | |
| 10 | #include "base/logging.h" |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 11 | |
| 12 | namespace base { |
| 13 | |
| 14 | // Specification: |
| 15 | // http://en.cppreference.com/w/cpp/utility/optional/in_place_t |
| 16 | struct in_place_t {}; |
| 17 | |
| 18 | // Specification: |
| 19 | // http://en.cppreference.com/w/cpp/utility/optional/nullopt_t |
| 20 | struct nullopt_t { |
| 21 | constexpr explicit nullopt_t(int) {} |
| 22 | }; |
| 23 | |
| 24 | // Specification: |
| 25 | // http://en.cppreference.com/w/cpp/utility/optional/in_place |
| 26 | constexpr in_place_t in_place = {}; |
| 27 | |
| 28 | // Specification: |
| 29 | // http://en.cppreference.com/w/cpp/utility/optional/nullopt |
| 30 | constexpr nullopt_t nullopt(0); |
| 31 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 32 | namespace internal { |
| 33 | |
danakj | 90acaf829 | 2017-04-05 17:59:27 | [diff] [blame] | 34 | template <typename T, bool = std::is_trivially_destructible<T>::value> |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 35 | struct OptionalStorage { |
alshabalin | a637f25 | 2016-10-26 22:29:28 | [diff] [blame] | 36 | // Initializing |empty_| here instead of using default member initializing |
| 37 | // to avoid errors in g++ 4.8. |
| 38 | constexpr OptionalStorage() : empty_('\0') {} |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 39 | |
| 40 | constexpr explicit OptionalStorage(const T& value) |
| 41 | : is_null_(false), value_(value) {} |
| 42 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 43 | constexpr explicit OptionalStorage(T&& value) |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 44 | : is_null_(false), value_(std::move(value)) {} |
| 45 | |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 46 | template <class... Args> |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 47 | constexpr explicit OptionalStorage(base::in_place_t, Args&&... args) |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 48 | : is_null_(false), value_(std::forward<Args>(args)...) {} |
| 49 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 50 | // When T is not trivially destructible we must call its |
| 51 | // destructor before deallocating its memory. |
| 52 | ~OptionalStorage() { |
| 53 | if (!is_null_) |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 54 | value_.~T(); |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool is_null_ = true; |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 58 | union { |
| 59 | // |empty_| exists so that the union will always be initialized, even when |
alshabalin | a637f25 | 2016-10-26 22:29:28 | [diff] [blame] | 60 | // it doesn't contain a value. Union members must be initialized for the |
| 61 | // constructor to be 'constexpr'. |
| 62 | char empty_; |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 63 | T value_; |
| 64 | }; |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | template <typename T> |
| 68 | struct OptionalStorage<T, true> { |
alshabalin | a637f25 | 2016-10-26 22:29:28 | [diff] [blame] | 69 | // Initializing |empty_| here instead of using default member initializing |
| 70 | // to avoid errors in g++ 4.8. |
| 71 | constexpr OptionalStorage() : empty_('\0') {} |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 72 | |
| 73 | constexpr explicit OptionalStorage(const T& value) |
| 74 | : is_null_(false), value_(value) {} |
| 75 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 76 | constexpr explicit OptionalStorage(T&& value) |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 77 | : is_null_(false), value_(std::move(value)) {} |
| 78 | |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 79 | template <class... Args> |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 80 | constexpr explicit OptionalStorage(base::in_place_t, Args&&... args) |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 81 | : is_null_(false), value_(std::forward<Args>(args)...) {} |
| 82 | |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 83 | // When T is trivially destructible (i.e. its destructor does nothing) there |
| 84 | // is no need to call it. Explicitly defaulting the destructor means it's not |
| 85 | // user-provided. Those two together make this destructor trivial. |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 86 | ~OptionalStorage() = default; |
| 87 | |
| 88 | bool is_null_ = true; |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 89 | union { |
| 90 | // |empty_| exists so that the union will always be initialized, even when |
alshabalin | a637f25 | 2016-10-26 22:29:28 | [diff] [blame] | 91 | // it doesn't contain a value. Union members must be initialized for the |
| 92 | // constructor to be 'constexpr'. |
| 93 | char empty_; |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 94 | T value_; |
| 95 | }; |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | } // namespace internal |
| 99 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 100 | // base::Optional is a Chromium version of the C++17 optional class: |
| 101 | // std::optional documentation: |
| 102 | // http://en.cppreference.com/w/cpp/utility/optional |
| 103 | // Chromium documentation: |
| 104 | // https://chromium.googlesource.com/chromium/src/+/master/docs/optional.md |
| 105 | // |
| 106 | // These are the differences between the specification and the implementation: |
| 107 | // - The constructor and emplace method using initializer_list are not |
| 108 | // implemented because 'initializer_list' is banned from Chromium. |
| 109 | // - Constructors do not use 'constexpr' as it is a C++14 extension. |
| 110 | // - 'constexpr' might be missing in some places for reasons specified locally. |
| 111 | // - No exceptions are thrown, because they are banned from Chromium. |
| 112 | // - All the non-members are in the 'base' namespace instead of 'std'. |
| 113 | template <typename T> |
| 114 | class Optional { |
| 115 | public: |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 116 | using value_type = T; |
| 117 | |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 118 | constexpr Optional() = default; |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 119 | |
| 120 | constexpr Optional(base::nullopt_t) {} |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 121 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 122 | // TODO(dcheng): Make these constexpr iff T is trivially constructible. |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 123 | Optional(const Optional& other) { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 124 | if (!other.storage_.is_null_) |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 125 | Init(other.value()); |
| 126 | } |
| 127 | |
| 128 | Optional(Optional&& other) { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 129 | if (!other.storage_.is_null_) |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 130 | Init(std::move(other.value())); |
| 131 | } |
| 132 | |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 133 | constexpr Optional(const T& value) : storage_(value) {} |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 134 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 135 | constexpr Optional(T&& value) : storage_(std::move(value)) {} |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 136 | |
| 137 | template <class... Args> |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 138 | constexpr explicit Optional(base::in_place_t, Args&&... args) |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 139 | : storage_(base::in_place, std::forward<Args>(args)...) {} |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 140 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 141 | ~Optional() = default; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 142 | |
| 143 | Optional& operator=(base::nullopt_t) { |
| 144 | FreeIfNeeded(); |
| 145 | return *this; |
| 146 | } |
| 147 | |
| 148 | Optional& operator=(const Optional& other) { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 149 | if (other.storage_.is_null_) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 150 | FreeIfNeeded(); |
| 151 | return *this; |
| 152 | } |
| 153 | |
| 154 | InitOrAssign(other.value()); |
| 155 | return *this; |
| 156 | } |
| 157 | |
| 158 | Optional& operator=(Optional&& other) { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 159 | if (other.storage_.is_null_) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 160 | FreeIfNeeded(); |
| 161 | return *this; |
| 162 | } |
| 163 | |
| 164 | InitOrAssign(std::move(other.value())); |
| 165 | return *this; |
| 166 | } |
| 167 | |
| 168 | template <class U> |
Andrey Kraynov | 976fbbd | 2017-09-13 17:15:44 | [diff] [blame] | 169 | typename std::enable_if<std::is_same<std::decay_t<U>, T>::value, |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 170 | Optional&>::type |
| 171 | operator=(U&& value) { |
| 172 | InitOrAssign(std::forward<U>(value)); |
| 173 | return *this; |
| 174 | } |
| 175 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 176 | constexpr const T* operator->() const { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 177 | DCHECK(!storage_.is_null_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 178 | return &value(); |
| 179 | } |
| 180 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 181 | constexpr T* operator->() { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 182 | DCHECK(!storage_.is_null_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 183 | return &value(); |
| 184 | } |
| 185 | |
| 186 | constexpr const T& operator*() const& { return value(); } |
| 187 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 188 | constexpr T& operator*() & { return value(); } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 189 | |
| 190 | constexpr const T&& operator*() const&& { return std::move(value()); } |
| 191 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 192 | constexpr T&& operator*() && { return std::move(value()); } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 193 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 194 | constexpr explicit operator bool() const { return !storage_.is_null_; } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 195 | |
mlamouri | 2620457 | 2016-08-10 12:24:15 | [diff] [blame] | 196 | constexpr bool has_value() const { return !storage_.is_null_; } |
| 197 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 198 | constexpr T& value() & { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 199 | DCHECK(!storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 200 | return storage_.value_; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 201 | } |
| 202 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 203 | constexpr const T& value() const & { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 204 | DCHECK(!storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 205 | return storage_.value_; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 206 | } |
| 207 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 208 | constexpr T&& value() && { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 209 | DCHECK(!storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 210 | return std::move(storage_.value_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 211 | } |
| 212 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 213 | constexpr const T&& value() const && { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 214 | DCHECK(!storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 215 | return std::move(storage_.value_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | template <class U> |
| 219 | constexpr T value_or(U&& default_value) const& { |
| 220 | // TODO(mlamouri): add the following assert when possible: |
| 221 | // static_assert(std::is_copy_constructible<T>::value, |
| 222 | // "T must be copy constructible"); |
| 223 | static_assert(std::is_convertible<U, T>::value, |
| 224 | "U must be convertible to T"); |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 225 | return storage_.is_null_ ? static_cast<T>(std::forward<U>(default_value)) |
| 226 | : value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | template <class U> |
| 230 | T value_or(U&& default_value) && { |
| 231 | // TODO(mlamouri): add the following assert when possible: |
| 232 | // static_assert(std::is_move_constructible<T>::value, |
| 233 | // "T must be move constructible"); |
| 234 | static_assert(std::is_convertible<U, T>::value, |
| 235 | "U must be convertible to T"); |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 236 | return storage_.is_null_ ? static_cast<T>(std::forward<U>(default_value)) |
| 237 | : std::move(value()); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void swap(Optional& other) { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 241 | if (storage_.is_null_ && other.storage_.is_null_) |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 242 | return; |
| 243 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 244 | if (storage_.is_null_ != other.storage_.is_null_) { |
| 245 | if (storage_.is_null_) { |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 246 | Init(std::move(other.storage_.value_)); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 247 | other.FreeIfNeeded(); |
| 248 | } else { |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 249 | other.Init(std::move(storage_.value_)); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 250 | FreeIfNeeded(); |
| 251 | } |
| 252 | return; |
| 253 | } |
| 254 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 255 | DCHECK(!storage_.is_null_ && !other.storage_.is_null_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 256 | using std::swap; |
| 257 | swap(**this, *other); |
| 258 | } |
| 259 | |
mlamouri | 2620457 | 2016-08-10 12:24:15 | [diff] [blame] | 260 | void reset() { |
| 261 | FreeIfNeeded(); |
| 262 | } |
| 263 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 264 | template <class... Args> |
| 265 | void emplace(Args&&... args) { |
| 266 | FreeIfNeeded(); |
| 267 | Init(std::forward<Args>(args)...); |
| 268 | } |
| 269 | |
| 270 | private: |
| 271 | void Init(const T& value) { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 272 | DCHECK(storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 273 | new (&storage_.value_) T(value); |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 274 | storage_.is_null_ = false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void Init(T&& value) { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 278 | DCHECK(storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 279 | new (&storage_.value_) T(std::move(value)); |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 280 | storage_.is_null_ = false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | template <class... Args> |
| 284 | void Init(Args&&... args) { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 285 | DCHECK(storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 286 | new (&storage_.value_) T(std::forward<Args>(args)...); |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 287 | storage_.is_null_ = false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void InitOrAssign(const T& value) { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 291 | if (storage_.is_null_) |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 292 | Init(value); |
| 293 | else |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 294 | storage_.value_ = value; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | void InitOrAssign(T&& value) { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 298 | if (storage_.is_null_) |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 299 | Init(std::move(value)); |
| 300 | else |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 301 | storage_.value_ = std::move(value); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void FreeIfNeeded() { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 305 | if (storage_.is_null_) |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 306 | return; |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 307 | storage_.value_.~T(); |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 308 | storage_.is_null_ = true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 309 | } |
| 310 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 311 | internal::OptionalStorage<T> storage_; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | template <class T> |
| 315 | constexpr bool operator==(const Optional<T>& lhs, const Optional<T>& rhs) { |
| 316 | return !!lhs != !!rhs ? false : lhs == nullopt || (*lhs == *rhs); |
| 317 | } |
| 318 | |
| 319 | template <class T> |
| 320 | constexpr bool operator!=(const Optional<T>& lhs, const Optional<T>& rhs) { |
| 321 | return !(lhs == rhs); |
| 322 | } |
| 323 | |
| 324 | template <class T> |
| 325 | constexpr bool operator<(const Optional<T>& lhs, const Optional<T>& rhs) { |
| 326 | return rhs == nullopt ? false : (lhs == nullopt ? true : *lhs < *rhs); |
| 327 | } |
| 328 | |
| 329 | template <class T> |
| 330 | constexpr bool operator<=(const Optional<T>& lhs, const Optional<T>& rhs) { |
| 331 | return !(rhs < lhs); |
| 332 | } |
| 333 | |
| 334 | template <class T> |
| 335 | constexpr bool operator>(const Optional<T>& lhs, const Optional<T>& rhs) { |
| 336 | return rhs < lhs; |
| 337 | } |
| 338 | |
| 339 | template <class T> |
| 340 | constexpr bool operator>=(const Optional<T>& lhs, const Optional<T>& rhs) { |
| 341 | return !(lhs < rhs); |
| 342 | } |
| 343 | |
| 344 | template <class T> |
| 345 | constexpr bool operator==(const Optional<T>& opt, base::nullopt_t) { |
| 346 | return !opt; |
| 347 | } |
| 348 | |
| 349 | template <class T> |
| 350 | constexpr bool operator==(base::nullopt_t, const Optional<T>& opt) { |
| 351 | return !opt; |
| 352 | } |
| 353 | |
| 354 | template <class T> |
| 355 | constexpr bool operator!=(const Optional<T>& opt, base::nullopt_t) { |
| 356 | return !!opt; |
| 357 | } |
| 358 | |
| 359 | template <class T> |
| 360 | constexpr bool operator!=(base::nullopt_t, const Optional<T>& opt) { |
| 361 | return !!opt; |
| 362 | } |
| 363 | |
| 364 | template <class T> |
| 365 | constexpr bool operator<(const Optional<T>& opt, base::nullopt_t) { |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | template <class T> |
| 370 | constexpr bool operator<(base::nullopt_t, const Optional<T>& opt) { |
| 371 | return !!opt; |
| 372 | } |
| 373 | |
| 374 | template <class T> |
| 375 | constexpr bool operator<=(const Optional<T>& opt, base::nullopt_t) { |
| 376 | return !opt; |
| 377 | } |
| 378 | |
| 379 | template <class T> |
| 380 | constexpr bool operator<=(base::nullopt_t, const Optional<T>& opt) { |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | template <class T> |
| 385 | constexpr bool operator>(const Optional<T>& opt, base::nullopt_t) { |
| 386 | return !!opt; |
| 387 | } |
| 388 | |
| 389 | template <class T> |
| 390 | constexpr bool operator>(base::nullopt_t, const Optional<T>& opt) { |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | template <class T> |
| 395 | constexpr bool operator>=(const Optional<T>& opt, base::nullopt_t) { |
| 396 | return true; |
| 397 | } |
| 398 | |
| 399 | template <class T> |
| 400 | constexpr bool operator>=(base::nullopt_t, const Optional<T>& opt) { |
| 401 | return !opt; |
| 402 | } |
| 403 | |
| 404 | template <class T> |
| 405 | constexpr bool operator==(const Optional<T>& opt, const T& value) { |
| 406 | return opt != nullopt ? *opt == value : false; |
| 407 | } |
| 408 | |
| 409 | template <class T> |
| 410 | constexpr bool operator==(const T& value, const Optional<T>& opt) { |
| 411 | return opt == value; |
| 412 | } |
| 413 | |
| 414 | template <class T> |
| 415 | constexpr bool operator!=(const Optional<T>& opt, const T& value) { |
| 416 | return !(opt == value); |
| 417 | } |
| 418 | |
| 419 | template <class T> |
| 420 | constexpr bool operator!=(const T& value, const Optional<T>& opt) { |
| 421 | return !(opt == value); |
| 422 | } |
| 423 | |
| 424 | template <class T> |
| 425 | constexpr bool operator<(const Optional<T>& opt, const T& value) { |
| 426 | return opt != nullopt ? *opt < value : true; |
| 427 | } |
| 428 | |
| 429 | template <class T> |
| 430 | constexpr bool operator<(const T& value, const Optional<T>& opt) { |
| 431 | return opt != nullopt ? value < *opt : false; |
| 432 | } |
| 433 | |
| 434 | template <class T> |
| 435 | constexpr bool operator<=(const Optional<T>& opt, const T& value) { |
| 436 | return !(opt > value); |
| 437 | } |
| 438 | |
| 439 | template <class T> |
| 440 | constexpr bool operator<=(const T& value, const Optional<T>& opt) { |
| 441 | return !(value > opt); |
| 442 | } |
| 443 | |
| 444 | template <class T> |
| 445 | constexpr bool operator>(const Optional<T>& opt, const T& value) { |
| 446 | return value < opt; |
| 447 | } |
| 448 | |
| 449 | template <class T> |
| 450 | constexpr bool operator>(const T& value, const Optional<T>& opt) { |
| 451 | return opt < value; |
| 452 | } |
| 453 | |
| 454 | template <class T> |
| 455 | constexpr bool operator>=(const Optional<T>& opt, const T& value) { |
| 456 | return !(opt < value); |
| 457 | } |
| 458 | |
| 459 | template <class T> |
| 460 | constexpr bool operator>=(const T& value, const Optional<T>& opt) { |
| 461 | return !(value < opt); |
| 462 | } |
| 463 | |
| 464 | template <class T> |
| 465 | constexpr Optional<typename std::decay<T>::type> make_optional(T&& value) { |
| 466 | return Optional<typename std::decay<T>::type>(std::forward<T>(value)); |
| 467 | } |
| 468 | |
| 469 | template <class T> |
| 470 | void swap(Optional<T>& lhs, Optional<T>& rhs) { |
| 471 | lhs.swap(rhs); |
| 472 | } |
| 473 | |
| 474 | } // namespace base |
| 475 | |
| 476 | namespace std { |
| 477 | |
| 478 | template <class T> |
| 479 | struct hash<base::Optional<T>> { |
| 480 | size_t operator()(const base::Optional<T>& opt) const { |
| 481 | return opt == base::nullopt ? 0 : std::hash<T>()(*opt); |
| 482 | } |
| 483 | }; |
| 484 | |
| 485 | } // namespace std |
| 486 | |
| 487 | #endif // BASE_OPTIONAL_H_ |