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