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