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: |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 174 | // - Constructors do not use 'constexpr' as it is a C++14 extension. |
| 175 | // - 'constexpr' might be missing in some places for reasons specified locally. |
| 176 | // - No exceptions are thrown, because they are banned from Chromium. |
| 177 | // - All the non-members are in the 'base' namespace instead of 'std'. |
| 178 | template <typename T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 179 | class Optional : public internal::OptionalBase<T> { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 180 | public: |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 181 | using value_type = T; |
| 182 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 183 | // Defer default/copy/move constructor implementation to OptionalBase. |
| 184 | // TODO(hidehiko): Implement conditional enabling. |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 185 | constexpr Optional() = default; |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 186 | Optional(const Optional& other) = default; |
| 187 | Optional(Optional&& other) = default; |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 188 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 189 | constexpr Optional(nullopt_t) {} |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 190 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 191 | constexpr Optional(const T& value) |
| 192 | : internal::OptionalBase<T>(in_place, value) {} |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 193 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 194 | constexpr Optional(T&& value) |
| 195 | : internal::OptionalBase<T>(in_place, std::move(value)) {} |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 196 | |
| 197 | template <class... Args> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 198 | constexpr explicit Optional(in_place_t, Args&&... args) |
| 199 | : internal::OptionalBase<T>(in_place, std::forward<Args>(args)...) {} |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 200 | |
jdoerrie | b6e6c75 | 2018-01-03 09:13:45 | [diff] [blame^] | 201 | template < |
| 202 | class U, |
| 203 | class... Args, |
| 204 | class = std::enable_if_t<std::is_constructible<value_type, |
| 205 | std::initializer_list<U>&, |
| 206 | Args...>::value>> |
| 207 | constexpr explicit Optional(in_place_t, |
| 208 | std::initializer_list<U> il, |
| 209 | Args&&... args) |
| 210 | : internal::OptionalBase<T>(in_place, il, std::forward<Args>(args)...) {} |
| 211 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 212 | ~Optional() = default; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 213 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 214 | // Defer copy-/move- assign operator implementation to OptionalBase. |
| 215 | // TOOD(hidehiko): Implement conditional enabling. |
| 216 | Optional& operator=(const Optional& other) = default; |
| 217 | Optional& operator=(Optional&& other) = default; |
| 218 | |
| 219 | Optional& operator=(nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 220 | FreeIfNeeded(); |
| 221 | return *this; |
| 222 | } |
| 223 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 224 | template <class U> |
Andrey Kraynov | 976fbbd | 2017-09-13 17:15:44 | [diff] [blame] | 225 | typename std::enable_if<std::is_same<std::decay_t<U>, T>::value, |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 226 | Optional&>::type |
| 227 | operator=(U&& value) { |
| 228 | InitOrAssign(std::forward<U>(value)); |
| 229 | return *this; |
| 230 | } |
| 231 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 232 | constexpr const T* operator->() const { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 233 | DCHECK(!storage_.is_null_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 234 | return &value(); |
| 235 | } |
| 236 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 237 | constexpr T* operator->() { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 238 | DCHECK(!storage_.is_null_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 239 | return &value(); |
| 240 | } |
| 241 | |
| 242 | constexpr const T& operator*() const& { return value(); } |
| 243 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 244 | constexpr T& operator*() & { return value(); } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 245 | |
| 246 | constexpr const T&& operator*() const&& { return std::move(value()); } |
| 247 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 248 | constexpr T&& operator*() && { return std::move(value()); } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 249 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 250 | constexpr explicit operator bool() const { return !storage_.is_null_; } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 251 | |
mlamouri | 2620457 | 2016-08-10 12:24:15 | [diff] [blame] | 252 | constexpr bool has_value() const { return !storage_.is_null_; } |
| 253 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 254 | constexpr T& value() & { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 255 | DCHECK(!storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 256 | return storage_.value_; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 257 | } |
| 258 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 259 | constexpr const T& value() const & { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 260 | DCHECK(!storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 261 | return storage_.value_; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 262 | } |
| 263 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 264 | constexpr T&& value() && { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 265 | DCHECK(!storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 266 | return std::move(storage_.value_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 267 | } |
| 268 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 269 | constexpr const T&& value() const && { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 270 | DCHECK(!storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 271 | return std::move(storage_.value_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | template <class U> |
| 275 | constexpr T value_or(U&& default_value) const& { |
| 276 | // TODO(mlamouri): add the following assert when possible: |
| 277 | // static_assert(std::is_copy_constructible<T>::value, |
| 278 | // "T must be copy constructible"); |
| 279 | static_assert(std::is_convertible<U, T>::value, |
| 280 | "U must be convertible to T"); |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 281 | return storage_.is_null_ ? static_cast<T>(std::forward<U>(default_value)) |
| 282 | : value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | template <class U> |
| 286 | T value_or(U&& default_value) && { |
| 287 | // TODO(mlamouri): add the following assert when possible: |
| 288 | // static_assert(std::is_move_constructible<T>::value, |
| 289 | // "T must be move constructible"); |
| 290 | static_assert(std::is_convertible<U, T>::value, |
| 291 | "U must be convertible to T"); |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 292 | return storage_.is_null_ ? static_cast<T>(std::forward<U>(default_value)) |
| 293 | : std::move(value()); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void swap(Optional& other) { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 297 | if (storage_.is_null_ && other.storage_.is_null_) |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 298 | return; |
| 299 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 300 | if (storage_.is_null_ != other.storage_.is_null_) { |
| 301 | if (storage_.is_null_) { |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 302 | Init(std::move(other.storage_.value_)); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 303 | other.FreeIfNeeded(); |
| 304 | } else { |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 305 | other.Init(std::move(storage_.value_)); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 306 | FreeIfNeeded(); |
| 307 | } |
| 308 | return; |
| 309 | } |
| 310 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 311 | DCHECK(!storage_.is_null_ && !other.storage_.is_null_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 312 | using std::swap; |
| 313 | swap(**this, *other); |
| 314 | } |
| 315 | |
mlamouri | 2620457 | 2016-08-10 12:24:15 | [diff] [blame] | 316 | void reset() { |
| 317 | FreeIfNeeded(); |
| 318 | } |
| 319 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 320 | template <class... Args> |
| 321 | void emplace(Args&&... args) { |
| 322 | FreeIfNeeded(); |
| 323 | Init(std::forward<Args>(args)...); |
| 324 | } |
| 325 | |
jdoerrie | b6e6c75 | 2018-01-03 09:13:45 | [diff] [blame^] | 326 | template < |
| 327 | class U, |
| 328 | class... Args, |
| 329 | class = std::enable_if_t<std::is_constructible<value_type, |
| 330 | std::initializer_list<U>&, |
| 331 | Args...>::value>> |
| 332 | T& emplace(std::initializer_list<U> il, Args&&... args) { |
| 333 | FreeIfNeeded(); |
| 334 | Init(il, std::forward<Args>(args)...); |
| 335 | return storage_.value_; |
| 336 | } |
| 337 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 338 | private: |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 339 | // Accessing template base class's protected member needs explicit |
| 340 | // declaration to do so. |
| 341 | using internal::OptionalBase<T>::FreeIfNeeded; |
| 342 | using internal::OptionalBase<T>::Init; |
| 343 | using internal::OptionalBase<T>::InitOrAssign; |
| 344 | using internal::OptionalBase<T>::storage_; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 345 | }; |
| 346 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 347 | // Here after defines comparation operators. The definition follows |
| 348 | // http://en.cppreference.com/w/cpp/utility/optional/operator_cmp |
| 349 | // while bool() casting is replaced by has_value() to meet the chromium |
| 350 | // style guide. |
| 351 | template <class T, class U> |
| 352 | constexpr bool operator==(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 353 | if (lhs.has_value() != rhs.has_value()) |
| 354 | return false; |
| 355 | if (!lhs.has_value()) |
| 356 | return true; |
| 357 | return *lhs == *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 358 | } |
| 359 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 360 | template <class T, class U> |
| 361 | constexpr bool operator!=(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 362 | if (lhs.has_value() != rhs.has_value()) |
| 363 | return true; |
| 364 | if (!lhs.has_value()) |
| 365 | return false; |
| 366 | return *lhs != *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 367 | } |
| 368 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 369 | template <class T, class U> |
| 370 | constexpr bool operator<(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 371 | if (!rhs.has_value()) |
| 372 | return false; |
| 373 | if (!lhs.has_value()) |
| 374 | return true; |
| 375 | return *lhs < *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 376 | } |
| 377 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 378 | template <class T, class U> |
| 379 | constexpr bool operator<=(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 380 | if (!lhs.has_value()) |
| 381 | return true; |
| 382 | if (!rhs.has_value()) |
| 383 | return false; |
| 384 | return *lhs <= *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 385 | } |
| 386 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 387 | template <class T, class U> |
| 388 | constexpr bool operator>(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 389 | if (!lhs.has_value()) |
| 390 | return false; |
| 391 | if (!rhs.has_value()) |
| 392 | return true; |
| 393 | return *lhs > *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 394 | } |
| 395 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 396 | template <class T, class U> |
| 397 | constexpr bool operator>=(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 398 | if (!rhs.has_value()) |
| 399 | return true; |
| 400 | if (!lhs.has_value()) |
| 401 | return false; |
| 402 | return *lhs >= *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 406 | constexpr bool operator==(const Optional<T>& opt, nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 407 | return !opt; |
| 408 | } |
| 409 | |
| 410 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 411 | constexpr bool operator==(nullopt_t, const Optional<T>& opt) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 412 | return !opt; |
| 413 | } |
| 414 | |
| 415 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 416 | constexpr bool operator!=(const Optional<T>& opt, nullopt_t) { |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 417 | return opt.has_value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 421 | constexpr bool operator!=(nullopt_t, const Optional<T>& opt) { |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 422 | return opt.has_value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 426 | constexpr bool operator<(const Optional<T>& opt, nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 427 | return false; |
| 428 | } |
| 429 | |
| 430 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 431 | constexpr bool operator<(nullopt_t, const Optional<T>& opt) { |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 432 | return opt.has_value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 436 | constexpr bool operator<=(const Optional<T>& opt, nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 437 | return !opt; |
| 438 | } |
| 439 | |
| 440 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 441 | constexpr bool operator<=(nullopt_t, const Optional<T>& opt) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 442 | return true; |
| 443 | } |
| 444 | |
| 445 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 446 | constexpr bool operator>(const Optional<T>& opt, nullopt_t) { |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 447 | return opt.has_value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 451 | constexpr bool operator>(nullopt_t, const Optional<T>& opt) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 452 | return false; |
| 453 | } |
| 454 | |
| 455 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 456 | constexpr bool operator>=(const Optional<T>& opt, nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 457 | return true; |
| 458 | } |
| 459 | |
| 460 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 461 | constexpr bool operator>=(nullopt_t, const Optional<T>& opt) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 462 | return !opt; |
| 463 | } |
| 464 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 465 | template <class T, class U> |
| 466 | constexpr bool operator==(const Optional<T>& opt, const U& value) { |
| 467 | return opt.has_value() ? *opt == value : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 468 | } |
| 469 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 470 | template <class T, class U> |
| 471 | constexpr bool operator==(const U& value, const Optional<T>& opt) { |
| 472 | return opt.has_value() ? value == *opt : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 473 | } |
| 474 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 475 | template <class T, class U> |
| 476 | constexpr bool operator!=(const Optional<T>& opt, const U& value) { |
| 477 | return opt.has_value() ? *opt != value : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 478 | } |
| 479 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 480 | template <class T, class U> |
| 481 | constexpr bool operator!=(const U& value, const Optional<T>& opt) { |
| 482 | return opt.has_value() ? value != *opt : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 483 | } |
| 484 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 485 | template <class T, class U> |
| 486 | constexpr bool operator<(const Optional<T>& opt, const U& value) { |
| 487 | return opt.has_value() ? *opt < value : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 488 | } |
| 489 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 490 | template <class T, class U> |
| 491 | constexpr bool operator<(const U& value, const Optional<T>& opt) { |
| 492 | return opt.has_value() ? value < *opt : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 493 | } |
| 494 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 495 | template <class T, class U> |
| 496 | constexpr bool operator<=(const Optional<T>& opt, const U& value) { |
| 497 | return opt.has_value() ? *opt <= value : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 498 | } |
| 499 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 500 | template <class T, class U> |
| 501 | constexpr bool operator<=(const U& value, const Optional<T>& opt) { |
| 502 | return opt.has_value() ? value <= *opt : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 503 | } |
| 504 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 505 | template <class T, class U> |
| 506 | constexpr bool operator>(const Optional<T>& opt, const U& value) { |
| 507 | return opt.has_value() ? *opt > value : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 508 | } |
| 509 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 510 | template <class T, class U> |
| 511 | constexpr bool operator>(const U& value, const Optional<T>& opt) { |
| 512 | return opt.has_value() ? value > *opt : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 513 | } |
| 514 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 515 | template <class T, class U> |
| 516 | constexpr bool operator>=(const Optional<T>& opt, const U& value) { |
| 517 | return opt.has_value() ? *opt >= value : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 518 | } |
| 519 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 520 | template <class T, class U> |
| 521 | constexpr bool operator>=(const U& value, const Optional<T>& opt) { |
| 522 | return opt.has_value() ? value >= *opt : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | template <class T> |
| 526 | constexpr Optional<typename std::decay<T>::type> make_optional(T&& value) { |
| 527 | return Optional<typename std::decay<T>::type>(std::forward<T>(value)); |
| 528 | } |
| 529 | |
jdoerrie | b6e6c75 | 2018-01-03 09:13:45 | [diff] [blame^] | 530 | template <class T, class U, class... Args> |
| 531 | constexpr Optional<T> make_optional(std::initializer_list<U> il, |
| 532 | Args&&... args) { |
| 533 | return Optional<T>(in_place, il, std::forward<Args>(args)...); |
| 534 | } |
| 535 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 536 | template <class T> |
| 537 | void swap(Optional<T>& lhs, Optional<T>& rhs) { |
| 538 | lhs.swap(rhs); |
| 539 | } |
| 540 | |
| 541 | } // namespace base |
| 542 | |
| 543 | namespace std { |
| 544 | |
| 545 | template <class T> |
| 546 | struct hash<base::Optional<T>> { |
| 547 | size_t operator()(const base::Optional<T>& opt) const { |
| 548 | return opt == base::nullopt ? 0 : std::hash<T>()(*opt); |
| 549 | } |
| 550 | }; |
| 551 | |
| 552 | } // namespace std |
| 553 | |
| 554 | #endif // BASE_OPTIONAL_H_ |