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