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> |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 36 | struct OptionalStorageBase { |
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. |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 39 | constexpr OptionalStorageBase() : 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 | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 42 | constexpr explicit OptionalStorageBase(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. |
Hidehiko Abe | f1c8789 | 2018-01-19 23:50:24 | [diff] [blame^] | 47 | // Note that this hides the (implicitly declared) move constructor, which |
| 48 | // would be used for constexpr move constructor in OptionalStorage<T>. |
| 49 | // It is needed iff T is trivially move constructible. However, the current |
| 50 | // is_trivially_{copy,move}_constructible implementation requires |
| 51 | // is_trivially_destructible (which looks a bug, cf: |
| 52 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51452 and |
| 53 | // http://cplusplus.github.io/LWG/lwg-active.html#2116), so it is not |
| 54 | // necessary for this case at the moment. Please see also the destructor |
| 55 | // comment in "is_trivially_destructible = true" specialization below. |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 56 | ~OptionalStorageBase() { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 57 | if (!is_null_) |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 58 | value_.~T(); |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 59 | } |
| 60 | |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 61 | template <class... Args> |
| 62 | void Init(Args&&... args) { |
| 63 | DCHECK(is_null_); |
| 64 | ::new (&value_) T(std::forward<Args>(args)...); |
| 65 | is_null_ = false; |
| 66 | } |
| 67 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 68 | bool is_null_ = true; |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 69 | union { |
| 70 | // |empty_| exists so that the union will always be initialized, even when |
alshabalin | a637f25 | 2016-10-26 22:29:28 | [diff] [blame] | 71 | // it doesn't contain a value. Union members must be initialized for the |
| 72 | // constructor to be 'constexpr'. |
| 73 | char empty_; |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 74 | T value_; |
| 75 | }; |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | template <typename T> |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 79 | struct OptionalStorageBase<T, true /* trivially destructible */> { |
alshabalin | a637f25 | 2016-10-26 22:29:28 | [diff] [blame] | 80 | // Initializing |empty_| here instead of using default member initializing |
| 81 | // to avoid errors in g++ 4.8. |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 82 | constexpr OptionalStorageBase() : empty_('\0') {} |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 83 | |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 84 | template <class... Args> |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 85 | constexpr explicit OptionalStorageBase(in_place_t, Args&&... args) |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 86 | : is_null_(false), value_(std::forward<Args>(args)...) {} |
| 87 | |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 88 | // When T is trivially destructible (i.e. its destructor does nothing) there |
Hidehiko Abe | f1c8789 | 2018-01-19 23:50:24 | [diff] [blame^] | 89 | // is no need to call it. Implicitly defined destructor is trivial, because |
| 90 | // both members (bool and union containing only variants which are trivially |
| 91 | // destructible) are trivially destructible. |
| 92 | // Explicitly-defaulted destructor is also trivial, but do not use it here, |
| 93 | // because it hides the implicit move constructor. It is needed to implement |
| 94 | // constexpr move constructor in OptionalStorage iff T is trivially move |
| 95 | // constructible. Note that, if T is trivially move constructible, the move |
| 96 | // constructor of OptionalStorageBase<T> is also implicitly defined and it is |
| 97 | // trivially move constructor. If T is not trivially move constructible, |
| 98 | // "not declaring move constructor without destructor declaration" here means |
| 99 | // "delete move constructor", which works because any move constructor of |
| 100 | // OptionalStorage will not refer to it in that case. |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 101 | |
| 102 | template <class... Args> |
| 103 | void Init(Args&&... args) { |
| 104 | DCHECK(is_null_); |
| 105 | ::new (&value_) T(std::forward<Args>(args)...); |
| 106 | is_null_ = false; |
| 107 | } |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 108 | |
| 109 | bool is_null_ = true; |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 110 | union { |
| 111 | // |empty_| exists so that the union will always be initialized, even when |
alshabalin | a637f25 | 2016-10-26 22:29:28 | [diff] [blame] | 112 | // it doesn't contain a value. Union members must be initialized for the |
| 113 | // constructor to be 'constexpr'. |
| 114 | char empty_; |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 115 | T value_; |
| 116 | }; |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 117 | }; |
| 118 | |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 119 | // Implement conditional constexpr copy and move constructors. These are |
| 120 | // constexpr if is_trivially_{copy,move}_constructible<T>::value is true |
| 121 | // respectively. If each is true, the corresponding constructor is defined as |
| 122 | // "= default;", which generates a constexpr constructor (In this case, |
| 123 | // the condition of constexpr-ness is satisfied because the base class also has |
| 124 | // compiler generated constexpr {copy,move} constructors). Note that |
| 125 | // placement-new is prohibited in constexpr. |
| 126 | template <typename T, |
| 127 | bool = std::is_trivially_copy_constructible<T>::value, |
| 128 | bool = std::is_trivially_move_constructible<T>::value> |
| 129 | struct OptionalStorage : OptionalStorageBase<T> { |
| 130 | // This is no trivially {copy,move} constructible case. Other cases are |
| 131 | // defined below as specializations. |
| 132 | |
| 133 | // Accessing the members of template base class requires explicit |
| 134 | // declaration. |
| 135 | using OptionalStorageBase<T>::is_null_; |
| 136 | using OptionalStorageBase<T>::value_; |
| 137 | using OptionalStorageBase<T>::Init; |
| 138 | |
| 139 | // Inherit constructors (specifically, the in_place constructor). |
| 140 | using OptionalStorageBase<T>::OptionalStorageBase; |
| 141 | |
| 142 | // User defined constructor deletes the default constructor. |
| 143 | // Define it explicitly. |
| 144 | OptionalStorage() = default; |
| 145 | |
| 146 | OptionalStorage(const OptionalStorage& other) { |
| 147 | if (!other.is_null_) |
| 148 | Init(other.value_); |
| 149 | } |
| 150 | |
| 151 | OptionalStorage(OptionalStorage&& other) { |
| 152 | if (!other.is_null_) |
| 153 | Init(std::move(other.value_)); |
| 154 | } |
| 155 | }; |
| 156 | |
| 157 | template <typename T> |
| 158 | struct OptionalStorage<T, |
| 159 | true /* trivially copy constructible */, |
| 160 | false /* trivially move constructible */> |
| 161 | : OptionalStorageBase<T> { |
| 162 | using OptionalStorageBase<T>::is_null_; |
| 163 | using OptionalStorageBase<T>::value_; |
| 164 | using OptionalStorageBase<T>::Init; |
| 165 | using OptionalStorageBase<T>::OptionalStorageBase; |
| 166 | |
| 167 | OptionalStorage() = default; |
| 168 | OptionalStorage(const OptionalStorage& other) = default; |
| 169 | |
| 170 | OptionalStorage(OptionalStorage&& other) { |
| 171 | if (!other.is_null_) |
| 172 | Init(std::move(other.value_)); |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | template <typename T> |
| 177 | struct OptionalStorage<T, |
| 178 | false /* trivially copy constructible */, |
| 179 | true /* trivially move constructible */> |
| 180 | : OptionalStorageBase<T> { |
| 181 | using OptionalStorageBase<T>::is_null_; |
| 182 | using OptionalStorageBase<T>::value_; |
| 183 | using OptionalStorageBase<T>::Init; |
| 184 | using OptionalStorageBase<T>::OptionalStorageBase; |
| 185 | |
| 186 | OptionalStorage() = default; |
| 187 | OptionalStorage(OptionalStorage&& other) = default; |
| 188 | |
| 189 | OptionalStorage(const OptionalStorage& other) { |
| 190 | if (!other.is_null_) |
| 191 | Init(other.value_); |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | template <typename T> |
| 196 | struct OptionalStorage<T, |
| 197 | true /* trivially copy constructible */, |
| 198 | true /* trivially move constructible */> |
| 199 | : OptionalStorageBase<T> { |
| 200 | // If both trivially {copy,move} constructible are true, it is not necessary |
| 201 | // to use user-defined constructors. So, just inheriting constructors |
| 202 | // from the base class works. |
| 203 | using OptionalStorageBase<T>::OptionalStorageBase; |
| 204 | }; |
| 205 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 206 | // Base class to support conditionally usable copy-/move- constructors |
| 207 | // and assign operators. |
| 208 | template <typename T> |
| 209 | class OptionalBase { |
| 210 | // This class provides implementation rather than public API, so everything |
| 211 | // should be hidden. Often we use composition, but we cannot in this case |
| 212 | // because of C++ language restriction. |
| 213 | protected: |
| 214 | constexpr OptionalBase() = default; |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 215 | constexpr OptionalBase(const OptionalBase& other) = default; |
| 216 | constexpr OptionalBase(OptionalBase&& other) = default; |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 217 | |
| 218 | template <class... Args> |
| 219 | constexpr explicit OptionalBase(in_place_t, Args&&... args) |
| 220 | : storage_(in_place, std::forward<Args>(args)...) {} |
| 221 | |
| 222 | ~OptionalBase() = default; |
| 223 | |
| 224 | OptionalBase& operator=(const OptionalBase& other) { |
| 225 | if (other.storage_.is_null_) { |
| 226 | FreeIfNeeded(); |
| 227 | return *this; |
| 228 | } |
| 229 | |
| 230 | InitOrAssign(other.storage_.value_); |
| 231 | return *this; |
| 232 | } |
| 233 | |
| 234 | OptionalBase& operator=(OptionalBase&& other) { |
| 235 | if (other.storage_.is_null_) { |
| 236 | FreeIfNeeded(); |
| 237 | return *this; |
| 238 | } |
| 239 | |
| 240 | InitOrAssign(std::move(other.storage_.value_)); |
| 241 | return *this; |
| 242 | } |
| 243 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 244 | void InitOrAssign(const T& value) { |
| 245 | if (storage_.is_null_) |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 246 | storage_.Init(value); |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 247 | else |
| 248 | storage_.value_ = value; |
| 249 | } |
| 250 | |
| 251 | void InitOrAssign(T&& value) { |
| 252 | if (storage_.is_null_) |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 253 | storage_.Init(std::move(value)); |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 254 | else |
| 255 | storage_.value_ = std::move(value); |
| 256 | } |
| 257 | |
| 258 | void FreeIfNeeded() { |
| 259 | if (storage_.is_null_) |
| 260 | return; |
| 261 | storage_.value_.~T(); |
| 262 | storage_.is_null_ = true; |
| 263 | } |
| 264 | |
| 265 | OptionalStorage<T> storage_; |
| 266 | }; |
| 267 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 268 | } // namespace internal |
| 269 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 270 | // base::Optional is a Chromium version of the C++17 optional class: |
| 271 | // std::optional documentation: |
| 272 | // http://en.cppreference.com/w/cpp/utility/optional |
| 273 | // Chromium documentation: |
| 274 | // https://chromium.googlesource.com/chromium/src/+/master/docs/optional.md |
| 275 | // |
| 276 | // These are the differences between the specification and the implementation: |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 277 | // - Constructors do not use 'constexpr' as it is a C++14 extension. |
| 278 | // - 'constexpr' might be missing in some places for reasons specified locally. |
| 279 | // - No exceptions are thrown, because they are banned from Chromium. |
| 280 | // - All the non-members are in the 'base' namespace instead of 'std'. |
| 281 | template <typename T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 282 | class Optional : public internal::OptionalBase<T> { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 283 | public: |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 284 | using value_type = T; |
| 285 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 286 | // Defer default/copy/move constructor implementation to OptionalBase. |
| 287 | // TODO(hidehiko): Implement conditional enabling. |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 288 | constexpr Optional() = default; |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 289 | constexpr Optional(const Optional& other) = default; |
| 290 | constexpr Optional(Optional&& other) = default; |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 291 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 292 | constexpr Optional(nullopt_t) {} |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 293 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 294 | constexpr Optional(const T& value) |
| 295 | : internal::OptionalBase<T>(in_place, value) {} |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 296 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 297 | constexpr Optional(T&& value) |
| 298 | : internal::OptionalBase<T>(in_place, std::move(value)) {} |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 299 | |
| 300 | template <class... Args> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 301 | constexpr explicit Optional(in_place_t, Args&&... args) |
| 302 | : internal::OptionalBase<T>(in_place, std::forward<Args>(args)...) {} |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 303 | |
jdoerrie | b6e6c75 | 2018-01-03 09:13:45 | [diff] [blame] | 304 | template < |
| 305 | class U, |
| 306 | class... Args, |
| 307 | class = std::enable_if_t<std::is_constructible<value_type, |
| 308 | std::initializer_list<U>&, |
| 309 | Args...>::value>> |
| 310 | constexpr explicit Optional(in_place_t, |
| 311 | std::initializer_list<U> il, |
| 312 | Args&&... args) |
| 313 | : internal::OptionalBase<T>(in_place, il, std::forward<Args>(args)...) {} |
| 314 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 315 | ~Optional() = default; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 316 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 317 | // Defer copy-/move- assign operator implementation to OptionalBase. |
| 318 | // TOOD(hidehiko): Implement conditional enabling. |
| 319 | Optional& operator=(const Optional& other) = default; |
| 320 | Optional& operator=(Optional&& other) = default; |
| 321 | |
| 322 | Optional& operator=(nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 323 | FreeIfNeeded(); |
| 324 | return *this; |
| 325 | } |
| 326 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 327 | template <class U> |
Andrey Kraynov | 976fbbd | 2017-09-13 17:15:44 | [diff] [blame] | 328 | typename std::enable_if<std::is_same<std::decay_t<U>, T>::value, |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 329 | Optional&>::type |
| 330 | operator=(U&& value) { |
| 331 | InitOrAssign(std::forward<U>(value)); |
| 332 | return *this; |
| 333 | } |
| 334 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 335 | constexpr const T* operator->() const { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 336 | DCHECK(!storage_.is_null_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 337 | return &value(); |
| 338 | } |
| 339 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 340 | constexpr T* operator->() { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 341 | DCHECK(!storage_.is_null_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 342 | return &value(); |
| 343 | } |
| 344 | |
| 345 | constexpr const T& operator*() const& { return value(); } |
| 346 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 347 | constexpr T& operator*() & { return value(); } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 348 | |
| 349 | constexpr const T&& operator*() const&& { return std::move(value()); } |
| 350 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 351 | constexpr T&& operator*() && { return std::move(value()); } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 352 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 353 | constexpr explicit operator bool() const { return !storage_.is_null_; } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 354 | |
mlamouri | 2620457 | 2016-08-10 12:24:15 | [diff] [blame] | 355 | constexpr bool has_value() const { return !storage_.is_null_; } |
| 356 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 357 | constexpr T& value() & { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 358 | DCHECK(!storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 359 | return storage_.value_; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 360 | } |
| 361 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 362 | constexpr const T& value() const & { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 363 | DCHECK(!storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 364 | return storage_.value_; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 365 | } |
| 366 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 367 | constexpr T&& value() && { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 368 | DCHECK(!storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 369 | return std::move(storage_.value_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 370 | } |
| 371 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 372 | constexpr const T&& value() const && { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 373 | DCHECK(!storage_.is_null_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 374 | return std::move(storage_.value_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | template <class U> |
| 378 | constexpr T value_or(U&& default_value) const& { |
| 379 | // TODO(mlamouri): add the following assert when possible: |
| 380 | // static_assert(std::is_copy_constructible<T>::value, |
| 381 | // "T must be copy constructible"); |
| 382 | static_assert(std::is_convertible<U, T>::value, |
| 383 | "U must be convertible to T"); |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 384 | return storage_.is_null_ ? static_cast<T>(std::forward<U>(default_value)) |
| 385 | : value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | template <class U> |
| 389 | T value_or(U&& default_value) && { |
| 390 | // TODO(mlamouri): add the following assert when possible: |
| 391 | // static_assert(std::is_move_constructible<T>::value, |
| 392 | // "T must be move constructible"); |
| 393 | static_assert(std::is_convertible<U, T>::value, |
| 394 | "U must be convertible to T"); |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 395 | return storage_.is_null_ ? static_cast<T>(std::forward<U>(default_value)) |
| 396 | : std::move(value()); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void swap(Optional& other) { |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 400 | if (storage_.is_null_ && other.storage_.is_null_) |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 401 | return; |
| 402 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 403 | if (storage_.is_null_ != other.storage_.is_null_) { |
| 404 | if (storage_.is_null_) { |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 405 | storage_.Init(std::move(other.storage_.value_)); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 406 | other.FreeIfNeeded(); |
| 407 | } else { |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 408 | other.storage_.Init(std::move(storage_.value_)); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 409 | FreeIfNeeded(); |
| 410 | } |
| 411 | return; |
| 412 | } |
| 413 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 414 | DCHECK(!storage_.is_null_ && !other.storage_.is_null_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 415 | using std::swap; |
| 416 | swap(**this, *other); |
| 417 | } |
| 418 | |
mlamouri | 2620457 | 2016-08-10 12:24:15 | [diff] [blame] | 419 | void reset() { |
| 420 | FreeIfNeeded(); |
| 421 | } |
| 422 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 423 | template <class... Args> |
| 424 | void emplace(Args&&... args) { |
| 425 | FreeIfNeeded(); |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 426 | storage_.Init(std::forward<Args>(args)...); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 427 | } |
| 428 | |
jdoerrie | b6e6c75 | 2018-01-03 09:13:45 | [diff] [blame] | 429 | template < |
| 430 | class U, |
| 431 | class... Args, |
| 432 | class = std::enable_if_t<std::is_constructible<value_type, |
| 433 | std::initializer_list<U>&, |
| 434 | Args...>::value>> |
| 435 | T& emplace(std::initializer_list<U> il, Args&&... args) { |
| 436 | FreeIfNeeded(); |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 437 | storage_.Init(il, std::forward<Args>(args)...); |
jdoerrie | b6e6c75 | 2018-01-03 09:13:45 | [diff] [blame] | 438 | return storage_.value_; |
| 439 | } |
| 440 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 441 | private: |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 442 | // Accessing template base class's protected member needs explicit |
| 443 | // declaration to do so. |
| 444 | using internal::OptionalBase<T>::FreeIfNeeded; |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 445 | using internal::OptionalBase<T>::InitOrAssign; |
| 446 | using internal::OptionalBase<T>::storage_; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 447 | }; |
| 448 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 449 | // Here after defines comparation operators. The definition follows |
| 450 | // http://en.cppreference.com/w/cpp/utility/optional/operator_cmp |
| 451 | // while bool() casting is replaced by has_value() to meet the chromium |
| 452 | // style guide. |
| 453 | template <class T, class U> |
| 454 | constexpr bool operator==(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 455 | if (lhs.has_value() != rhs.has_value()) |
| 456 | return false; |
| 457 | if (!lhs.has_value()) |
| 458 | return true; |
| 459 | return *lhs == *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 460 | } |
| 461 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 462 | template <class T, class U> |
| 463 | constexpr bool operator!=(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 464 | if (lhs.has_value() != rhs.has_value()) |
| 465 | return true; |
| 466 | if (!lhs.has_value()) |
| 467 | return false; |
| 468 | return *lhs != *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 469 | } |
| 470 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 471 | template <class T, class U> |
| 472 | constexpr bool operator<(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 473 | if (!rhs.has_value()) |
| 474 | return false; |
| 475 | if (!lhs.has_value()) |
| 476 | return true; |
| 477 | return *lhs < *rhs; |
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 Optional<T>& lhs, const Optional<U>& rhs) { |
| 482 | if (!lhs.has_value()) |
| 483 | return true; |
| 484 | if (!rhs.has_value()) |
| 485 | return false; |
| 486 | return *lhs <= *rhs; |
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 Optional<T>& lhs, const Optional<U>& rhs) { |
| 491 | if (!lhs.has_value()) |
| 492 | return false; |
| 493 | if (!rhs.has_value()) |
| 494 | return true; |
| 495 | return *lhs > *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 496 | } |
| 497 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 498 | template <class T, class U> |
| 499 | constexpr bool operator>=(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 500 | if (!rhs.has_value()) |
| 501 | return true; |
| 502 | if (!lhs.has_value()) |
| 503 | return false; |
| 504 | return *lhs >= *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 508 | constexpr bool operator==(const Optional<T>& opt, nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 509 | return !opt; |
| 510 | } |
| 511 | |
| 512 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 513 | constexpr bool operator==(nullopt_t, const Optional<T>& opt) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 514 | return !opt; |
| 515 | } |
| 516 | |
| 517 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 518 | constexpr bool operator!=(const Optional<T>& opt, nullopt_t) { |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 519 | return opt.has_value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 523 | constexpr bool operator!=(nullopt_t, const Optional<T>& opt) { |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 524 | return opt.has_value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 528 | constexpr bool operator<(const Optional<T>& opt, nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 529 | return false; |
| 530 | } |
| 531 | |
| 532 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 533 | constexpr bool operator<(nullopt_t, const Optional<T>& opt) { |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 534 | return opt.has_value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 538 | constexpr bool operator<=(const Optional<T>& opt, nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 539 | return !opt; |
| 540 | } |
| 541 | |
| 542 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 543 | constexpr bool operator<=(nullopt_t, const Optional<T>& opt) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 544 | return true; |
| 545 | } |
| 546 | |
| 547 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 548 | constexpr bool operator>(const Optional<T>& opt, nullopt_t) { |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 549 | return opt.has_value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 553 | constexpr bool operator>(nullopt_t, const Optional<T>& opt) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 554 | return false; |
| 555 | } |
| 556 | |
| 557 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 558 | constexpr bool operator>=(const Optional<T>& opt, nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 559 | return true; |
| 560 | } |
| 561 | |
| 562 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 563 | constexpr bool operator>=(nullopt_t, const Optional<T>& opt) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 564 | return !opt; |
| 565 | } |
| 566 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 567 | template <class T, class U> |
| 568 | constexpr bool operator==(const Optional<T>& opt, const U& value) { |
| 569 | return opt.has_value() ? *opt == value : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 570 | } |
| 571 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 572 | template <class T, class U> |
| 573 | constexpr bool operator==(const U& value, const Optional<T>& opt) { |
| 574 | return opt.has_value() ? value == *opt : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 575 | } |
| 576 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 577 | template <class T, class U> |
| 578 | constexpr bool operator!=(const Optional<T>& opt, const U& value) { |
| 579 | return opt.has_value() ? *opt != value : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 580 | } |
| 581 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 582 | template <class T, class U> |
| 583 | constexpr bool operator!=(const U& value, const Optional<T>& opt) { |
| 584 | return opt.has_value() ? value != *opt : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 585 | } |
| 586 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 587 | template <class T, class U> |
| 588 | constexpr bool operator<(const Optional<T>& opt, const U& value) { |
| 589 | return opt.has_value() ? *opt < value : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 590 | } |
| 591 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 592 | template <class T, class U> |
| 593 | constexpr bool operator<(const U& value, const Optional<T>& opt) { |
| 594 | return opt.has_value() ? value < *opt : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 595 | } |
| 596 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 597 | template <class T, class U> |
| 598 | constexpr bool operator<=(const Optional<T>& opt, const U& value) { |
| 599 | return opt.has_value() ? *opt <= value : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 600 | } |
| 601 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 602 | template <class T, class U> |
| 603 | constexpr bool operator<=(const U& value, const Optional<T>& opt) { |
| 604 | return opt.has_value() ? value <= *opt : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 605 | } |
| 606 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 607 | template <class T, class U> |
| 608 | constexpr bool operator>(const Optional<T>& opt, const U& value) { |
| 609 | return opt.has_value() ? *opt > value : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 610 | } |
| 611 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 612 | template <class T, class U> |
| 613 | constexpr bool operator>(const U& value, const Optional<T>& opt) { |
| 614 | return opt.has_value() ? value > *opt : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 615 | } |
| 616 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 617 | template <class T, class U> |
| 618 | constexpr bool operator>=(const Optional<T>& opt, const U& value) { |
| 619 | return opt.has_value() ? *opt >= value : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 620 | } |
| 621 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 622 | template <class T, class U> |
| 623 | constexpr bool operator>=(const U& value, const Optional<T>& opt) { |
| 624 | return opt.has_value() ? value >= *opt : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | template <class T> |
| 628 | constexpr Optional<typename std::decay<T>::type> make_optional(T&& value) { |
| 629 | return Optional<typename std::decay<T>::type>(std::forward<T>(value)); |
| 630 | } |
| 631 | |
jdoerrie | b6e6c75 | 2018-01-03 09:13:45 | [diff] [blame] | 632 | template <class T, class U, class... Args> |
| 633 | constexpr Optional<T> make_optional(std::initializer_list<U> il, |
| 634 | Args&&... args) { |
| 635 | return Optional<T>(in_place, il, std::forward<Args>(args)...); |
| 636 | } |
| 637 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 638 | template <class T> |
| 639 | void swap(Optional<T>& lhs, Optional<T>& rhs) { |
| 640 | lhs.swap(rhs); |
| 641 | } |
| 642 | |
| 643 | } // namespace base |
| 644 | |
| 645 | namespace std { |
| 646 | |
| 647 | template <class T> |
| 648 | struct hash<base::Optional<T>> { |
| 649 | size_t operator()(const base::Optional<T>& opt) const { |
| 650 | return opt == base::nullopt ? 0 : std::hash<T>()(*opt); |
| 651 | } |
| 652 | }; |
| 653 | |
| 654 | } // namespace std |
| 655 | |
| 656 | #endif // BASE_OPTIONAL_H_ |