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