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 | |
Lei Zhang | 8618bf06 | 2019-03-25 20:37:16 | [diff] [blame] | 8 | #include <functional> |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 9 | #include <type_traits> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 10 | #include <utility> |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 11 | |
| 12 | #include "base/logging.h" |
Hidehiko Abe | 4d8468a | 2018-02-23 09:50:41 | [diff] [blame] | 13 | #include "base/template_util.h" |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 14 | |
| 15 | namespace base { |
| 16 | |
| 17 | // Specification: |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 18 | // http://en.cppreference.com/w/cpp/utility/optional/nullopt_t |
| 19 | struct nullopt_t { |
| 20 | constexpr explicit nullopt_t(int) {} |
| 21 | }; |
| 22 | |
| 23 | // Specification: |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 24 | // http://en.cppreference.com/w/cpp/utility/optional/nullopt |
| 25 | constexpr nullopt_t nullopt(0); |
| 26 | |
Hidehiko Abe | d39417a5 | 2018-01-31 03:22:42 | [diff] [blame] | 27 | // Forward declaration, which is refered by following helpers. |
| 28 | template <typename T> |
| 29 | class Optional; |
| 30 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 31 | namespace internal { |
| 32 | |
danakj | 90acaf829 | 2017-04-05 17:59:27 | [diff] [blame] | 33 | template <typename T, bool = std::is_trivially_destructible<T>::value> |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 34 | struct OptionalStorageBase { |
alshabalin | a637f25 | 2016-10-26 22:29:28 | [diff] [blame] | 35 | // Initializing |empty_| here instead of using default member initializing |
| 36 | // to avoid errors in g++ 4.8. |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 37 | constexpr OptionalStorageBase() : empty_('\0') {} |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 38 | |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 39 | template <class... Args> |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 40 | constexpr explicit OptionalStorageBase(in_place_t, Args&&... args) |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 41 | : is_populated_(true), value_(std::forward<Args>(args)...) {} |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 42 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 43 | // When T is not trivially destructible we must call its |
| 44 | // destructor before deallocating its memory. |
Hidehiko Abe | f1c8789 | 2018-01-19 23:50:24 | [diff] [blame] | 45 | // Note that this hides the (implicitly declared) move constructor, which |
| 46 | // would be used for constexpr move constructor in OptionalStorage<T>. |
| 47 | // It is needed iff T is trivially move constructible. However, the current |
| 48 | // is_trivially_{copy,move}_constructible implementation requires |
| 49 | // is_trivially_destructible (which looks a bug, cf: |
| 50 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51452 and |
| 51 | // http://cplusplus.github.io/LWG/lwg-active.html#2116), so it is not |
| 52 | // necessary for this case at the moment. Please see also the destructor |
| 53 | // comment in "is_trivially_destructible = true" specialization below. |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 54 | ~OptionalStorageBase() { |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 55 | if (is_populated_) |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 56 | value_.~T(); |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 57 | } |
| 58 | |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 59 | template <class... Args> |
| 60 | void Init(Args&&... args) { |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 61 | DCHECK(!is_populated_); |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 62 | ::new (&value_) T(std::forward<Args>(args)...); |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 63 | is_populated_ = true; |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 64 | } |
| 65 | |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 66 | bool is_populated_ = false; |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 67 | union { |
| 68 | // |empty_| exists so that the union will always be initialized, even when |
alshabalin | a637f25 | 2016-10-26 22:29:28 | [diff] [blame] | 69 | // it doesn't contain a value. Union members must be initialized for the |
| 70 | // constructor to be 'constexpr'. |
| 71 | char empty_; |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 72 | T value_; |
| 73 | }; |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | template <typename T> |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 77 | struct OptionalStorageBase<T, true /* trivially destructible */> { |
alshabalin | a637f25 | 2016-10-26 22:29:28 | [diff] [blame] | 78 | // Initializing |empty_| here instead of using default member initializing |
| 79 | // to avoid errors in g++ 4.8. |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 80 | constexpr OptionalStorageBase() : empty_('\0') {} |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 81 | |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 82 | template <class... Args> |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 83 | constexpr explicit OptionalStorageBase(in_place_t, Args&&... args) |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 84 | : is_populated_(true), value_(std::forward<Args>(args)...) {} |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 85 | |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 86 | // When T is trivially destructible (i.e. its destructor does nothing) there |
Hidehiko Abe | f1c8789 | 2018-01-19 23:50:24 | [diff] [blame] | 87 | // is no need to call it. Implicitly defined destructor is trivial, because |
| 88 | // both members (bool and union containing only variants which are trivially |
| 89 | // destructible) are trivially destructible. |
| 90 | // Explicitly-defaulted destructor is also trivial, but do not use it here, |
| 91 | // because it hides the implicit move constructor. It is needed to implement |
| 92 | // constexpr move constructor in OptionalStorage iff T is trivially move |
| 93 | // constructible. Note that, if T is trivially move constructible, the move |
| 94 | // constructor of OptionalStorageBase<T> is also implicitly defined and it is |
| 95 | // trivially move constructor. If T is not trivially move constructible, |
| 96 | // "not declaring move constructor without destructor declaration" here means |
| 97 | // "delete move constructor", which works because any move constructor of |
| 98 | // OptionalStorage will not refer to it in that case. |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 99 | |
| 100 | template <class... Args> |
| 101 | void Init(Args&&... args) { |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 102 | DCHECK(!is_populated_); |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 103 | ::new (&value_) T(std::forward<Args>(args)...); |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 104 | is_populated_ = true; |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 105 | } |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 106 | |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 107 | bool is_populated_ = false; |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 108 | union { |
| 109 | // |empty_| exists so that the union will always be initialized, even when |
alshabalin | a637f25 | 2016-10-26 22:29:28 | [diff] [blame] | 110 | // it doesn't contain a value. Union members must be initialized for the |
| 111 | // constructor to be 'constexpr'. |
| 112 | char empty_; |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 113 | T value_; |
| 114 | }; |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 115 | }; |
| 116 | |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 117 | // Implement conditional constexpr copy and move constructors. These are |
| 118 | // constexpr if is_trivially_{copy,move}_constructible<T>::value is true |
| 119 | // respectively. If each is true, the corresponding constructor is defined as |
| 120 | // "= default;", which generates a constexpr constructor (In this case, |
| 121 | // the condition of constexpr-ness is satisfied because the base class also has |
| 122 | // compiler generated constexpr {copy,move} constructors). Note that |
| 123 | // placement-new is prohibited in constexpr. |
| 124 | template <typename T, |
Hidehiko Abe | 4d8468a | 2018-02-23 09:50:41 | [diff] [blame] | 125 | bool = is_trivially_copy_constructible<T>::value, |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 126 | bool = std::is_trivially_move_constructible<T>::value> |
| 127 | struct OptionalStorage : OptionalStorageBase<T> { |
| 128 | // This is no trivially {copy,move} constructible case. Other cases are |
| 129 | // defined below as specializations. |
| 130 | |
| 131 | // Accessing the members of template base class requires explicit |
| 132 | // declaration. |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 133 | using OptionalStorageBase<T>::is_populated_; |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 134 | using OptionalStorageBase<T>::value_; |
| 135 | using OptionalStorageBase<T>::Init; |
| 136 | |
| 137 | // Inherit constructors (specifically, the in_place constructor). |
| 138 | using OptionalStorageBase<T>::OptionalStorageBase; |
| 139 | |
| 140 | // User defined constructor deletes the default constructor. |
| 141 | // Define it explicitly. |
| 142 | OptionalStorage() = default; |
| 143 | |
| 144 | OptionalStorage(const OptionalStorage& other) { |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 145 | if (other.is_populated_) |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 146 | Init(other.value_); |
| 147 | } |
| 148 | |
Fyodor Gayokho | 65eb2ba | 2018-03-22 18:09:21 | [diff] [blame] | 149 | OptionalStorage(OptionalStorage&& other) noexcept( |
| 150 | std::is_nothrow_move_constructible<T>::value) { |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 151 | if (other.is_populated_) |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 152 | Init(std::move(other.value_)); |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | template <typename T> |
| 157 | struct OptionalStorage<T, |
| 158 | true /* trivially copy constructible */, |
| 159 | false /* trivially move constructible */> |
| 160 | : OptionalStorageBase<T> { |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 161 | using OptionalStorageBase<T>::is_populated_; |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 162 | using OptionalStorageBase<T>::value_; |
| 163 | using OptionalStorageBase<T>::Init; |
| 164 | using OptionalStorageBase<T>::OptionalStorageBase; |
| 165 | |
| 166 | OptionalStorage() = default; |
| 167 | OptionalStorage(const OptionalStorage& other) = default; |
| 168 | |
Fyodor Gayokho | 65eb2ba | 2018-03-22 18:09:21 | [diff] [blame] | 169 | OptionalStorage(OptionalStorage&& other) noexcept( |
| 170 | std::is_nothrow_move_constructible<T>::value) { |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 171 | if (other.is_populated_) |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 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> { |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 181 | using OptionalStorageBase<T>::is_populated_; |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 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) { |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 190 | if (other.is_populated_) |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 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 | |
Hidehiko Abe | d39417a5 | 2018-01-31 03:22:42 | [diff] [blame] | 222 | // Implementation of converting constructors. |
| 223 | template <typename U> |
| 224 | explicit OptionalBase(const OptionalBase<U>& other) { |
| 225 | if (other.storage_.is_populated_) |
| 226 | storage_.Init(other.storage_.value_); |
| 227 | } |
| 228 | |
| 229 | template <typename U> |
| 230 | explicit OptionalBase(OptionalBase<U>&& other) { |
| 231 | if (other.storage_.is_populated_) |
| 232 | storage_.Init(std::move(other.storage_.value_)); |
| 233 | } |
| 234 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 235 | ~OptionalBase() = default; |
| 236 | |
| 237 | OptionalBase& operator=(const OptionalBase& other) { |
Hidehiko Abe | 40ee4ae | 2018-02-23 04:24:12 | [diff] [blame] | 238 | CopyAssign(other); |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 239 | return *this; |
| 240 | } |
| 241 | |
Hidehiko Abe | 3aa61df | 2018-02-24 12:47:07 | [diff] [blame] | 242 | OptionalBase& operator=(OptionalBase&& other) noexcept( |
| 243 | std::is_nothrow_move_assignable<T>::value&& |
| 244 | std::is_nothrow_move_constructible<T>::value) { |
Hidehiko Abe | 40ee4ae | 2018-02-23 04:24:12 | [diff] [blame] | 245 | MoveAssign(std::move(other)); |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 246 | return *this; |
| 247 | } |
| 248 | |
Hidehiko Abe | 40ee4ae | 2018-02-23 04:24:12 | [diff] [blame] | 249 | template <typename U> |
| 250 | void CopyAssign(const OptionalBase<U>& other) { |
| 251 | if (other.storage_.is_populated_) |
| 252 | InitOrAssign(other.storage_.value_); |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 253 | else |
Hidehiko Abe | 40ee4ae | 2018-02-23 04:24:12 | [diff] [blame] | 254 | FreeIfNeeded(); |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 255 | } |
| 256 | |
Hidehiko Abe | 40ee4ae | 2018-02-23 04:24:12 | [diff] [blame] | 257 | template <typename U> |
| 258 | void MoveAssign(OptionalBase<U>&& other) { |
| 259 | if (other.storage_.is_populated_) |
| 260 | InitOrAssign(std::move(other.storage_.value_)); |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 261 | else |
Hidehiko Abe | 40ee4ae | 2018-02-23 04:24:12 | [diff] [blame] | 262 | FreeIfNeeded(); |
| 263 | } |
| 264 | |
| 265 | template <typename U> |
| 266 | void InitOrAssign(U&& value) { |
| 267 | if (storage_.is_populated_) |
| 268 | storage_.value_ = std::forward<U>(value); |
| 269 | else |
| 270 | storage_.Init(std::forward<U>(value)); |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 271 | } |
| 272 | |
Gabriel Charette | d773f12 | 2019-03-19 22:06:38 | [diff] [blame] | 273 | void FreeIfNeeded() { |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 274 | if (!storage_.is_populated_) |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 275 | return; |
| 276 | storage_.value_.~T(); |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 277 | storage_.is_populated_ = false; |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 278 | } |
| 279 | |
Hidehiko Abe | d39417a5 | 2018-01-31 03:22:42 | [diff] [blame] | 280 | // For implementing conversion, allow access to other typed OptionalBase |
| 281 | // class. |
| 282 | template <typename U> |
| 283 | friend class OptionalBase; |
| 284 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 285 | OptionalStorage<T> storage_; |
| 286 | }; |
| 287 | |
Hidehiko Abe | 5cae964 | 2018-01-26 18:01:11 | [diff] [blame] | 288 | // The following {Copy,Move}{Constructible,Assignable} structs are helpers to |
| 289 | // implement constructor/assign-operator overloading. Specifically, if T is |
| 290 | // is not movable but copyable, Optional<T>'s move constructor should not |
| 291 | // participate in overload resolution. This inheritance trick implements that. |
| 292 | template <bool is_copy_constructible> |
| 293 | struct CopyConstructible {}; |
| 294 | |
| 295 | template <> |
| 296 | struct CopyConstructible<false> { |
| 297 | constexpr CopyConstructible() = default; |
| 298 | constexpr CopyConstructible(const CopyConstructible&) = delete; |
| 299 | constexpr CopyConstructible(CopyConstructible&&) = default; |
| 300 | CopyConstructible& operator=(const CopyConstructible&) = default; |
| 301 | CopyConstructible& operator=(CopyConstructible&&) = default; |
| 302 | }; |
| 303 | |
| 304 | template <bool is_move_constructible> |
| 305 | struct MoveConstructible {}; |
| 306 | |
| 307 | template <> |
| 308 | struct MoveConstructible<false> { |
| 309 | constexpr MoveConstructible() = default; |
| 310 | constexpr MoveConstructible(const MoveConstructible&) = default; |
| 311 | constexpr MoveConstructible(MoveConstructible&&) = delete; |
| 312 | MoveConstructible& operator=(const MoveConstructible&) = default; |
| 313 | MoveConstructible& operator=(MoveConstructible&&) = default; |
| 314 | }; |
| 315 | |
| 316 | template <bool is_copy_assignable> |
| 317 | struct CopyAssignable {}; |
| 318 | |
| 319 | template <> |
| 320 | struct CopyAssignable<false> { |
| 321 | constexpr CopyAssignable() = default; |
| 322 | constexpr CopyAssignable(const CopyAssignable&) = default; |
| 323 | constexpr CopyAssignable(CopyAssignable&&) = default; |
| 324 | CopyAssignable& operator=(const CopyAssignable&) = delete; |
| 325 | CopyAssignable& operator=(CopyAssignable&&) = default; |
| 326 | }; |
| 327 | |
| 328 | template <bool is_move_assignable> |
| 329 | struct MoveAssignable {}; |
| 330 | |
| 331 | template <> |
| 332 | struct MoveAssignable<false> { |
| 333 | constexpr MoveAssignable() = default; |
| 334 | constexpr MoveAssignable(const MoveAssignable&) = default; |
| 335 | constexpr MoveAssignable(MoveAssignable&&) = default; |
| 336 | MoveAssignable& operator=(const MoveAssignable&) = default; |
| 337 | MoveAssignable& operator=(MoveAssignable&&) = delete; |
| 338 | }; |
| 339 | |
Hidehiko Abe | 40ee4ae | 2018-02-23 04:24:12 | [diff] [blame] | 340 | // Helper to conditionally enable converting constructors and assign operators. |
Hidehiko Abe | d39417a5 | 2018-01-31 03:22:42 | [diff] [blame] | 341 | template <typename T, typename U> |
| 342 | struct IsConvertibleFromOptional |
| 343 | : std::integral_constant< |
| 344 | bool, |
| 345 | std::is_constructible<T, Optional<U>&>::value || |
| 346 | std::is_constructible<T, const Optional<U>&>::value || |
| 347 | std::is_constructible<T, Optional<U>&&>::value || |
| 348 | std::is_constructible<T, const Optional<U>&&>::value || |
| 349 | std::is_convertible<Optional<U>&, T>::value || |
| 350 | std::is_convertible<const Optional<U>&, T>::value || |
| 351 | std::is_convertible<Optional<U>&&, T>::value || |
| 352 | std::is_convertible<const Optional<U>&&, T>::value> {}; |
| 353 | |
Hidehiko Abe | 40ee4ae | 2018-02-23 04:24:12 | [diff] [blame] | 354 | template <typename T, typename U> |
| 355 | struct IsAssignableFromOptional |
| 356 | : std::integral_constant< |
| 357 | bool, |
| 358 | IsConvertibleFromOptional<T, U>::value || |
| 359 | std::is_assignable<T&, Optional<U>&>::value || |
| 360 | std::is_assignable<T&, const Optional<U>&>::value || |
| 361 | std::is_assignable<T&, Optional<U>&&>::value || |
| 362 | std::is_assignable<T&, const Optional<U>&&>::value> {}; |
| 363 | |
Hidehiko Abe | b467afe | 2018-02-23 07:01:30 | [diff] [blame] | 364 | // Forward compatibility for C++17. |
| 365 | // Introduce one more deeper nested namespace to avoid leaking using std::swap. |
| 366 | namespace swappable_impl { |
| 367 | using std::swap; |
| 368 | |
| 369 | struct IsSwappableImpl { |
| 370 | // Tests if swap can be called. Check<T&>(0) returns true_type iff swap |
| 371 | // is available for T. Otherwise, Check's overload resolution falls back |
| 372 | // to Check(...) declared below thanks to SFINAE, so returns false_type. |
| 373 | template <typename T> |
| 374 | static auto Check(int) |
| 375 | -> decltype(swap(std::declval<T>(), std::declval<T>()), std::true_type()); |
| 376 | |
| 377 | template <typename T> |
| 378 | static std::false_type Check(...); |
| 379 | }; |
| 380 | } // namespace swappable_impl |
| 381 | |
| 382 | template <typename T> |
| 383 | struct IsSwappable : decltype(swappable_impl::IsSwappableImpl::Check<T&>(0)) {}; |
| 384 | |
Hidehiko Abe | 14f92bee | 2018-02-21 06:01:28 | [diff] [blame] | 385 | // Forward compatibility for C++20. |
| 386 | template <typename T> |
| 387 | using RemoveCvRefT = std::remove_cv_t<std::remove_reference_t<T>>; |
| 388 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 389 | } // namespace internal |
| 390 | |
Hidehiko Abe | c4aa16b | 2018-03-07 03:45:37 | [diff] [blame] | 391 | // On Windows, by default, empty-base class optimization does not work, |
| 392 | // which means even if the base class is empty struct, it still consumes one |
| 393 | // byte for its body. __declspec(empty_bases) enables the optimization. |
| 394 | // cf) |
| 395 | // https://blogs.msdn.microsoft.com/vcblog/2016/03/30/optimizing-the-layout-of-empty-base-classes-in-vs2015-update-2-3/ |
| 396 | #ifdef OS_WIN |
| 397 | #define OPTIONAL_DECLSPEC_EMPTY_BASES __declspec(empty_bases) |
| 398 | #else |
| 399 | #define OPTIONAL_DECLSPEC_EMPTY_BASES |
| 400 | #endif |
| 401 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 402 | // base::Optional is a Chromium version of the C++17 optional class: |
| 403 | // std::optional documentation: |
| 404 | // http://en.cppreference.com/w/cpp/utility/optional |
| 405 | // Chromium documentation: |
| 406 | // https://chromium.googlesource.com/chromium/src/+/master/docs/optional.md |
| 407 | // |
| 408 | // These are the differences between the specification and the implementation: |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 409 | // - Constructors do not use 'constexpr' as it is a C++14 extension. |
| 410 | // - 'constexpr' might be missing in some places for reasons specified locally. |
| 411 | // - No exceptions are thrown, because they are banned from Chromium. |
Hidehiko Abe | 3aa61df | 2018-02-24 12:47:07 | [diff] [blame] | 412 | // Marked noexcept for only move constructor and move assign operators. |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 413 | // - All the non-members are in the 'base' namespace instead of 'std'. |
Hidehiko Abe | 14f92bee | 2018-02-21 06:01:28 | [diff] [blame] | 414 | // |
| 415 | // Note that T cannot have a constructor T(Optional<T>) etc. Optional<T> checks |
| 416 | // T's constructor (specifically via IsConvertibleFromOptional), and in the |
| 417 | // check whether T can be constructible from Optional<T>, which is recursive |
| 418 | // so it does not work. As of Feb 2018, std::optional C++17 implementation in |
| 419 | // both clang and gcc has same limitation. MSVC SFINAE looks to have different |
| 420 | // behavior, but anyway it reports an error, too. |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 421 | template <typename T> |
Hidehiko Abe | c4aa16b | 2018-03-07 03:45:37 | [diff] [blame] | 422 | class OPTIONAL_DECLSPEC_EMPTY_BASES Optional |
Hidehiko Abe | 5cae964 | 2018-01-26 18:01:11 | [diff] [blame] | 423 | : public internal::OptionalBase<T>, |
| 424 | public internal::CopyConstructible<std::is_copy_constructible<T>::value>, |
| 425 | public internal::MoveConstructible<std::is_move_constructible<T>::value>, |
| 426 | public internal::CopyAssignable<std::is_copy_constructible<T>::value && |
| 427 | std::is_copy_assignable<T>::value>, |
| 428 | public internal::MoveAssignable<std::is_move_constructible<T>::value && |
| 429 | std::is_move_assignable<T>::value> { |
Mounir Lamouri | 771d3e8 | 2019-06-04 00:47:31 | [diff] [blame] | 430 | private: |
| 431 | // Disable some versions of T that are ill-formed. |
| 432 | // See: https://timsong-cpp.github.io/cppwp/n4659/optional#syn-1 |
| 433 | static_assert( |
| 434 | !std::is_same<internal::RemoveCvRefT<T>, in_place_t>::value, |
| 435 | "instantiation of base::Optional with in_place_t is ill-formed"); |
| 436 | static_assert(!std::is_same<internal::RemoveCvRefT<T>, nullopt_t>::value, |
| 437 | "instantiation of base::Optional with nullopt_t is ill-formed"); |
| 438 | static_assert( |
| 439 | !std::is_reference<T>::value, |
| 440 | "instantiation of base::Optional with a reference type is ill-formed"); |
| 441 | // See: https://timsong-cpp.github.io/cppwp/n4659/optional#optional-3 |
| 442 | static_assert(std::is_destructible<T>::value, |
| 443 | "instantiation of base::Optional with a non-destructible type " |
| 444 | "is ill-formed"); |
| 445 | // Arrays are explicitly disallowed because for arrays of known bound |
| 446 | // is_destructible is of undefined value. |
| 447 | // See: https://en.cppreference.com/w/cpp/types/is_destructible |
| 448 | static_assert( |
| 449 | !std::is_array<T>::value, |
| 450 | "instantiation of base::Optional with an array type is ill-formed"); |
| 451 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 452 | public: |
Hidehiko Abe | c4aa16b | 2018-03-07 03:45:37 | [diff] [blame] | 453 | #undef OPTIONAL_DECLSPEC_EMPTY_BASES |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 454 | using value_type = T; |
| 455 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 456 | // Defer default/copy/move constructor implementation to OptionalBase. |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 457 | constexpr Optional() = default; |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 458 | constexpr Optional(const Optional& other) = default; |
Hidehiko Abe | 3aa61df | 2018-02-24 12:47:07 | [diff] [blame] | 459 | constexpr Optional(Optional&& other) noexcept( |
| 460 | std::is_nothrow_move_constructible<T>::value) = default; |
alshabalin | 9494e454 | 2016-10-25 09:56:41 | [diff] [blame] | 461 | |
Hidehiko Abe | d39417a5 | 2018-01-31 03:22:42 | [diff] [blame] | 462 | constexpr Optional(nullopt_t) {} // NOLINT(runtime/explicit) |
| 463 | |
| 464 | // Converting copy constructor. "explicit" only if |
| 465 | // std::is_convertible<const U&, T>::value is false. It is implemented by |
| 466 | // declaring two almost same constructors, but that condition in enable_if_t |
| 467 | // is different, so that either one is chosen, thanks to SFINAE. |
| 468 | template < |
| 469 | typename U, |
| 470 | std::enable_if_t<std::is_constructible<T, const U&>::value && |
| 471 | !internal::IsConvertibleFromOptional<T, U>::value && |
| 472 | std::is_convertible<const U&, T>::value, |
| 473 | bool> = false> |
| 474 | Optional(const Optional<U>& other) : internal::OptionalBase<T>(other) {} |
| 475 | |
| 476 | template < |
| 477 | typename U, |
| 478 | std::enable_if_t<std::is_constructible<T, const U&>::value && |
| 479 | !internal::IsConvertibleFromOptional<T, U>::value && |
| 480 | !std::is_convertible<const U&, T>::value, |
| 481 | bool> = false> |
| 482 | explicit Optional(const Optional<U>& other) |
| 483 | : internal::OptionalBase<T>(other) {} |
| 484 | |
| 485 | // Converting move constructor. Similar to converting copy constructor, |
| 486 | // declaring two (explicit and non-explicit) constructors. |
| 487 | template < |
| 488 | typename U, |
| 489 | std::enable_if_t<std::is_constructible<T, U&&>::value && |
| 490 | !internal::IsConvertibleFromOptional<T, U>::value && |
| 491 | std::is_convertible<U&&, T>::value, |
| 492 | bool> = false> |
| 493 | Optional(Optional<U>&& other) : internal::OptionalBase<T>(std::move(other)) {} |
| 494 | |
| 495 | template < |
| 496 | typename U, |
| 497 | std::enable_if_t<std::is_constructible<T, U&&>::value && |
| 498 | !internal::IsConvertibleFromOptional<T, U>::value && |
| 499 | !std::is_convertible<U&&, T>::value, |
| 500 | bool> = false> |
| 501 | explicit Optional(Optional<U>&& other) |
| 502 | : internal::OptionalBase<T>(std::move(other)) {} |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 503 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 504 | template <class... Args> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 505 | constexpr explicit Optional(in_place_t, Args&&... args) |
| 506 | : internal::OptionalBase<T>(in_place, std::forward<Args>(args)...) {} |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 507 | |
jdoerrie | b6e6c75 | 2018-01-03 09:13:45 | [diff] [blame] | 508 | template < |
| 509 | class U, |
| 510 | class... Args, |
| 511 | class = std::enable_if_t<std::is_constructible<value_type, |
| 512 | std::initializer_list<U>&, |
| 513 | Args...>::value>> |
| 514 | constexpr explicit Optional(in_place_t, |
| 515 | std::initializer_list<U> il, |
| 516 | Args&&... args) |
| 517 | : internal::OptionalBase<T>(in_place, il, std::forward<Args>(args)...) {} |
| 518 | |
Hidehiko Abe | 14f92bee | 2018-02-21 06:01:28 | [diff] [blame] | 519 | // Forward value constructor. Similar to converting constructors, |
| 520 | // conditionally explicit. |
| 521 | template < |
| 522 | typename U = value_type, |
| 523 | std::enable_if_t< |
| 524 | std::is_constructible<T, U&&>::value && |
| 525 | !std::is_same<internal::RemoveCvRefT<U>, in_place_t>::value && |
| 526 | !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value && |
| 527 | std::is_convertible<U&&, T>::value, |
| 528 | bool> = false> |
| 529 | constexpr Optional(U&& value) |
| 530 | : internal::OptionalBase<T>(in_place, std::forward<U>(value)) {} |
| 531 | |
| 532 | template < |
| 533 | typename U = value_type, |
| 534 | std::enable_if_t< |
| 535 | std::is_constructible<T, U&&>::value && |
| 536 | !std::is_same<internal::RemoveCvRefT<U>, in_place_t>::value && |
| 537 | !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value && |
| 538 | !std::is_convertible<U&&, T>::value, |
| 539 | bool> = false> |
| 540 | constexpr explicit Optional(U&& value) |
| 541 | : internal::OptionalBase<T>(in_place, std::forward<U>(value)) {} |
| 542 | |
alshabalin | f06b07df | 2016-05-27 08:01:31 | [diff] [blame] | 543 | ~Optional() = default; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 544 | |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 545 | // Defer copy-/move- assign operator implementation to OptionalBase. |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 546 | Optional& operator=(const Optional& other) = default; |
Hidehiko Abe | 3aa61df | 2018-02-24 12:47:07 | [diff] [blame] | 547 | Optional& operator=(Optional&& other) noexcept( |
| 548 | std::is_nothrow_move_assignable<T>::value&& |
| 549 | std::is_nothrow_move_constructible<T>::value) = default; |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 550 | |
| 551 | Optional& operator=(nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 552 | FreeIfNeeded(); |
| 553 | return *this; |
| 554 | } |
| 555 | |
Hidehiko Abe | 40ee4ae | 2018-02-23 04:24:12 | [diff] [blame] | 556 | // Perfect-forwarded assignment. |
| 557 | template <typename U> |
| 558 | std::enable_if_t< |
| 559 | !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value && |
| 560 | std::is_constructible<T, U>::value && |
| 561 | std::is_assignable<T&, U>::value && |
| 562 | (!std::is_scalar<T>::value || |
| 563 | !std::is_same<std::decay_t<U>, T>::value), |
| 564 | Optional&> |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 565 | operator=(U&& value) { |
| 566 | InitOrAssign(std::forward<U>(value)); |
| 567 | return *this; |
| 568 | } |
| 569 | |
Hidehiko Abe | 40ee4ae | 2018-02-23 04:24:12 | [diff] [blame] | 570 | // Copy assign the state of other. |
| 571 | template <typename U> |
| 572 | std::enable_if_t<!internal::IsAssignableFromOptional<T, U>::value && |
| 573 | std::is_constructible<T, const U&>::value && |
| 574 | std::is_assignable<T&, const U&>::value, |
| 575 | Optional&> |
| 576 | operator=(const Optional<U>& other) { |
| 577 | CopyAssign(other); |
| 578 | return *this; |
| 579 | } |
| 580 | |
| 581 | // Move assign the state of other. |
| 582 | template <typename U> |
| 583 | std::enable_if_t<!internal::IsAssignableFromOptional<T, U>::value && |
| 584 | std::is_constructible<T, U>::value && |
| 585 | std::is_assignable<T&, U>::value, |
| 586 | Optional&> |
| 587 | operator=(Optional<U>&& other) { |
| 588 | MoveAssign(std::move(other)); |
| 589 | return *this; |
| 590 | } |
| 591 | |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 592 | constexpr const T* operator->() const { |
Daniel Cheng | f24c956 | 2019-04-09 20:40:05 | [diff] [blame] | 593 | CHECK(storage_.is_populated_); |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 594 | return &storage_.value_; |
| 595 | } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 596 | |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 597 | constexpr T* operator->() { |
Daniel Cheng | f24c956 | 2019-04-09 20:40:05 | [diff] [blame] | 598 | CHECK(storage_.is_populated_); |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 599 | return &storage_.value_; |
| 600 | } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 601 | |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 602 | constexpr const T& operator*() const & { |
Daniel Cheng | f24c956 | 2019-04-09 20:40:05 | [diff] [blame] | 603 | CHECK(storage_.is_populated_); |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 604 | return storage_.value_; |
| 605 | } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 606 | |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 607 | constexpr T& operator*() & { |
Daniel Cheng | f24c956 | 2019-04-09 20:40:05 | [diff] [blame] | 608 | CHECK(storage_.is_populated_); |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 609 | return storage_.value_; |
| 610 | } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 611 | |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 612 | constexpr const T&& operator*() const && { |
Daniel Cheng | f24c956 | 2019-04-09 20:40:05 | [diff] [blame] | 613 | CHECK(storage_.is_populated_); |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 614 | return std::move(storage_.value_); |
| 615 | } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 616 | |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 617 | constexpr T&& operator*() && { |
Daniel Cheng | f24c956 | 2019-04-09 20:40:05 | [diff] [blame] | 618 | CHECK(storage_.is_populated_); |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 619 | return std::move(storage_.value_); |
| 620 | } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 621 | |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 622 | constexpr explicit operator bool() const { return storage_.is_populated_; } |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 623 | |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 624 | constexpr bool has_value() const { return storage_.is_populated_; } |
mlamouri | 2620457 | 2016-08-10 12:24:15 | [diff] [blame] | 625 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 626 | constexpr T& value() & { |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 627 | CHECK(storage_.is_populated_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 628 | return storage_.value_; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 629 | } |
| 630 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 631 | constexpr const T& value() const & { |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 632 | CHECK(storage_.is_populated_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 633 | return storage_.value_; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 634 | } |
| 635 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 636 | constexpr T&& value() && { |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 637 | CHECK(storage_.is_populated_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 638 | return std::move(storage_.value_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 639 | } |
| 640 | |
Daniel Cheng | 429dbdc5 | 2017-10-04 15:33:56 | [diff] [blame] | 641 | constexpr const T&& value() const && { |
jdoerrie | c85bfab | 2018-04-12 15:01:30 | [diff] [blame] | 642 | CHECK(storage_.is_populated_); |
kwiberg | 882859a | 2016-08-16 09:42:21 | [diff] [blame] | 643 | return std::move(storage_.value_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | template <class U> |
| 647 | constexpr T value_or(U&& default_value) const& { |
| 648 | // TODO(mlamouri): add the following assert when possible: |
| 649 | // static_assert(std::is_copy_constructible<T>::value, |
| 650 | // "T must be copy constructible"); |
| 651 | static_assert(std::is_convertible<U, T>::value, |
| 652 | "U must be convertible to T"); |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 653 | return storage_.is_populated_ |
Adam Rice | 289c79d | 2018-06-22 10:02:41 | [diff] [blame] | 654 | ? storage_.value_ |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 655 | : static_cast<T>(std::forward<U>(default_value)); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | template <class U> |
Hidehiko Abe | b467afe | 2018-02-23 07:01:30 | [diff] [blame] | 659 | constexpr T value_or(U&& default_value) && { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 660 | // TODO(mlamouri): add the following assert when possible: |
| 661 | // static_assert(std::is_move_constructible<T>::value, |
| 662 | // "T must be move constructible"); |
| 663 | static_assert(std::is_convertible<U, T>::value, |
| 664 | "U must be convertible to T"); |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 665 | return storage_.is_populated_ |
Adam Rice | 289c79d | 2018-06-22 10:02:41 | [diff] [blame] | 666 | ? std::move(storage_.value_) |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 667 | : static_cast<T>(std::forward<U>(default_value)); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | void swap(Optional& other) { |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 671 | if (!storage_.is_populated_ && !other.storage_.is_populated_) |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 672 | return; |
| 673 | |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 674 | if (storage_.is_populated_ != other.storage_.is_populated_) { |
| 675 | if (storage_.is_populated_) { |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 676 | other.storage_.Init(std::move(storage_.value_)); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 677 | FreeIfNeeded(); |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 678 | } else { |
| 679 | storage_.Init(std::move(other.storage_.value_)); |
| 680 | other.FreeIfNeeded(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 681 | } |
| 682 | return; |
| 683 | } |
| 684 | |
Chris Blume | 0a5dd14 | 2018-01-24 22:35:40 | [diff] [blame] | 685 | DCHECK(storage_.is_populated_ && other.storage_.is_populated_); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 686 | using std::swap; |
| 687 | swap(**this, *other); |
| 688 | } |
| 689 | |
Hidehiko Abe | 3aa61df | 2018-02-24 12:47:07 | [diff] [blame] | 690 | void reset() { FreeIfNeeded(); } |
mlamouri | 2620457 | 2016-08-10 12:24:15 | [diff] [blame] | 691 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 692 | template <class... Args> |
Hidehiko Abe | b467afe | 2018-02-23 07:01:30 | [diff] [blame] | 693 | T& emplace(Args&&... args) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 694 | FreeIfNeeded(); |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 695 | storage_.Init(std::forward<Args>(args)...); |
Hidehiko Abe | b467afe | 2018-02-23 07:01:30 | [diff] [blame] | 696 | return storage_.value_; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 697 | } |
| 698 | |
Hidehiko Abe | b467afe | 2018-02-23 07:01:30 | [diff] [blame] | 699 | template <class U, class... Args> |
| 700 | std::enable_if_t< |
| 701 | std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value, |
| 702 | T&> |
| 703 | emplace(std::initializer_list<U> il, Args&&... args) { |
jdoerrie | b6e6c75 | 2018-01-03 09:13:45 | [diff] [blame] | 704 | FreeIfNeeded(); |
Hidehiko Abe | 5134ab38 | 2018-01-08 09:38:16 | [diff] [blame] | 705 | storage_.Init(il, std::forward<Args>(args)...); |
jdoerrie | b6e6c75 | 2018-01-03 09:13:45 | [diff] [blame] | 706 | return storage_.value_; |
| 707 | } |
| 708 | |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 709 | private: |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 710 | // Accessing template base class's protected member needs explicit |
| 711 | // declaration to do so. |
Hidehiko Abe | 40ee4ae | 2018-02-23 04:24:12 | [diff] [blame] | 712 | using internal::OptionalBase<T>::CopyAssign; |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 713 | using internal::OptionalBase<T>::FreeIfNeeded; |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 714 | using internal::OptionalBase<T>::InitOrAssign; |
Hidehiko Abe | 40ee4ae | 2018-02-23 04:24:12 | [diff] [blame] | 715 | using internal::OptionalBase<T>::MoveAssign; |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 716 | using internal::OptionalBase<T>::storage_; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 717 | }; |
| 718 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 719 | // Here after defines comparation operators. The definition follows |
| 720 | // http://en.cppreference.com/w/cpp/utility/optional/operator_cmp |
| 721 | // while bool() casting is replaced by has_value() to meet the chromium |
| 722 | // style guide. |
| 723 | template <class T, class U> |
| 724 | constexpr bool operator==(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 725 | if (lhs.has_value() != rhs.has_value()) |
| 726 | return false; |
| 727 | if (!lhs.has_value()) |
| 728 | return true; |
| 729 | return *lhs == *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 730 | } |
| 731 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 732 | template <class T, class U> |
| 733 | constexpr bool operator!=(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 734 | if (lhs.has_value() != rhs.has_value()) |
| 735 | return true; |
| 736 | if (!lhs.has_value()) |
| 737 | return false; |
| 738 | return *lhs != *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 739 | } |
| 740 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 741 | template <class T, class U> |
| 742 | constexpr bool operator<(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 743 | if (!rhs.has_value()) |
| 744 | return false; |
| 745 | if (!lhs.has_value()) |
| 746 | return true; |
| 747 | return *lhs < *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 748 | } |
| 749 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 750 | template <class T, class U> |
| 751 | constexpr bool operator<=(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 752 | if (!lhs.has_value()) |
| 753 | return true; |
| 754 | if (!rhs.has_value()) |
| 755 | return false; |
| 756 | return *lhs <= *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 757 | } |
| 758 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 759 | template <class T, class U> |
| 760 | constexpr bool operator>(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 761 | if (!lhs.has_value()) |
| 762 | return false; |
| 763 | if (!rhs.has_value()) |
| 764 | return true; |
| 765 | return *lhs > *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 766 | } |
| 767 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 768 | template <class T, class U> |
| 769 | constexpr bool operator>=(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 770 | if (!rhs.has_value()) |
| 771 | return true; |
| 772 | if (!lhs.has_value()) |
| 773 | return false; |
| 774 | return *lhs >= *rhs; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 778 | constexpr bool operator==(const Optional<T>& opt, nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 779 | return !opt; |
| 780 | } |
| 781 | |
| 782 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 783 | constexpr bool operator==(nullopt_t, const Optional<T>& opt) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 784 | return !opt; |
| 785 | } |
| 786 | |
| 787 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 788 | constexpr bool operator!=(const Optional<T>& opt, nullopt_t) { |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 789 | return opt.has_value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 793 | constexpr bool operator!=(nullopt_t, const Optional<T>& opt) { |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 794 | return opt.has_value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 798 | constexpr bool operator<(const Optional<T>& opt, nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 799 | return false; |
| 800 | } |
| 801 | |
| 802 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 803 | constexpr bool operator<(nullopt_t, const Optional<T>& opt) { |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 804 | return opt.has_value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 808 | constexpr bool operator<=(const Optional<T>& opt, nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 809 | return !opt; |
| 810 | } |
| 811 | |
| 812 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 813 | constexpr bool operator<=(nullopt_t, const Optional<T>& opt) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 814 | return true; |
| 815 | } |
| 816 | |
| 817 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 818 | constexpr bool operator>(const Optional<T>& opt, nullopt_t) { |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 819 | return opt.has_value(); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 823 | constexpr bool operator>(nullopt_t, const Optional<T>& opt) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 824 | return false; |
| 825 | } |
| 826 | |
| 827 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 828 | constexpr bool operator>=(const Optional<T>& opt, nullopt_t) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 829 | return true; |
| 830 | } |
| 831 | |
| 832 | template <class T> |
Hidehiko Abe | 25bbd5e | 2017-12-20 04:43:07 | [diff] [blame] | 833 | constexpr bool operator>=(nullopt_t, const Optional<T>& opt) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 834 | return !opt; |
| 835 | } |
| 836 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 837 | template <class T, class U> |
| 838 | constexpr bool operator==(const Optional<T>& opt, const U& value) { |
| 839 | return opt.has_value() ? *opt == value : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 840 | } |
| 841 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 842 | template <class T, class U> |
| 843 | constexpr bool operator==(const U& value, const Optional<T>& opt) { |
| 844 | return opt.has_value() ? value == *opt : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 845 | } |
| 846 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 847 | template <class T, class U> |
| 848 | constexpr bool operator!=(const Optional<T>& opt, const U& value) { |
| 849 | return opt.has_value() ? *opt != value : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 850 | } |
| 851 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 852 | template <class T, class U> |
| 853 | constexpr bool operator!=(const U& value, const Optional<T>& opt) { |
| 854 | return opt.has_value() ? value != *opt : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 855 | } |
| 856 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 857 | template <class T, class U> |
| 858 | constexpr bool operator<(const Optional<T>& opt, const U& value) { |
| 859 | return opt.has_value() ? *opt < value : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 860 | } |
| 861 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 862 | template <class T, class U> |
| 863 | constexpr bool operator<(const U& value, const Optional<T>& opt) { |
| 864 | return opt.has_value() ? value < *opt : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 865 | } |
| 866 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 867 | template <class T, class U> |
| 868 | constexpr bool operator<=(const Optional<T>& opt, const U& value) { |
| 869 | return opt.has_value() ? *opt <= value : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 870 | } |
| 871 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 872 | template <class T, class U> |
| 873 | constexpr bool operator<=(const U& value, const Optional<T>& opt) { |
| 874 | return opt.has_value() ? value <= *opt : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 875 | } |
| 876 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 877 | template <class T, class U> |
| 878 | constexpr bool operator>(const Optional<T>& opt, const U& value) { |
| 879 | return opt.has_value() ? *opt > value : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 880 | } |
| 881 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 882 | template <class T, class U> |
| 883 | constexpr bool operator>(const U& value, const Optional<T>& opt) { |
| 884 | return opt.has_value() ? value > *opt : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 885 | } |
| 886 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 887 | template <class T, class U> |
| 888 | constexpr bool operator>=(const Optional<T>& opt, const U& value) { |
| 889 | return opt.has_value() ? *opt >= value : false; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 890 | } |
| 891 | |
Hidehiko Abe | 7a534c3 | 2017-12-20 10:56:26 | [diff] [blame] | 892 | template <class T, class U> |
| 893 | constexpr bool operator>=(const U& value, const Optional<T>& opt) { |
| 894 | return opt.has_value() ? value >= *opt : true; |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | template <class T> |
Hidehiko Abe | b467afe | 2018-02-23 07:01:30 | [diff] [blame] | 898 | constexpr Optional<std::decay_t<T>> make_optional(T&& value) { |
| 899 | return Optional<std::decay_t<T>>(std::forward<T>(value)); |
| 900 | } |
| 901 | |
| 902 | template <class T, class... Args> |
| 903 | constexpr Optional<T> make_optional(Args&&... args) { |
| 904 | return Optional<T>(in_place, std::forward<Args>(args)...); |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 905 | } |
| 906 | |
jdoerrie | b6e6c75 | 2018-01-03 09:13:45 | [diff] [blame] | 907 | template <class T, class U, class... Args> |
| 908 | constexpr Optional<T> make_optional(std::initializer_list<U> il, |
| 909 | Args&&... args) { |
| 910 | return Optional<T>(in_place, il, std::forward<Args>(args)...); |
| 911 | } |
| 912 | |
Hidehiko Abe | b467afe | 2018-02-23 07:01:30 | [diff] [blame] | 913 | // Partial specialization for a function template is not allowed. Also, it is |
| 914 | // not allowed to add overload function to std namespace, while it is allowed |
| 915 | // to specialize the template in std. Thus, swap() (kind of) overloading is |
| 916 | // defined in base namespace, instead. |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 917 | template <class T> |
Hidehiko Abe | b467afe | 2018-02-23 07:01:30 | [diff] [blame] | 918 | std::enable_if_t<std::is_move_constructible<T>::value && |
| 919 | internal::IsSwappable<T>::value> |
| 920 | swap(Optional<T>& lhs, Optional<T>& rhs) { |
mlamouri | 53f6b25 | 2016-04-19 17:27:01 | [diff] [blame] | 921 | lhs.swap(rhs); |
| 922 | } |
| 923 | |
| 924 | } // namespace base |
| 925 | |
| 926 | namespace std { |
| 927 | |
| 928 | template <class T> |
| 929 | struct hash<base::Optional<T>> { |
| 930 | size_t operator()(const base::Optional<T>& opt) const { |
| 931 | return opt == base::nullopt ? 0 : std::hash<T>()(*opt); |
| 932 | } |
| 933 | }; |
| 934 | |
| 935 | } // namespace std |
| 936 | |
| 937 | #endif // BASE_OPTIONAL_H_ |