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