Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_OPTIONAL_H_ | 5 #ifndef BASE_OPTIONAL_H_ |
| 6 #define BASE_OPTIONAL_H_ | 6 #define BASE_OPTIONAL_H_ |
| 7 | 7 |
| 8 #include <type_traits> | 8 #include <type_traits> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/aligned_memory.h" | 11 #include "base/memory/aligned_memory.h" |
| 12 #include "base/template_util.h" | |
| 12 | 13 |
| 13 namespace base { | 14 namespace base { |
| 14 | 15 |
| 15 // Specification: | 16 // Specification: |
| 16 // http://en.cppreference.com/w/cpp/utility/optional/in_place_t | 17 // http://en.cppreference.com/w/cpp/utility/optional/in_place_t |
| 17 struct in_place_t {}; | 18 struct in_place_t {}; |
| 18 | 19 |
| 19 // Specification: | 20 // Specification: |
| 20 // http://en.cppreference.com/w/cpp/utility/optional/nullopt_t | 21 // http://en.cppreference.com/w/cpp/utility/optional/nullopt_t |
| 21 struct nullopt_t { | 22 struct nullopt_t { |
| 22 constexpr explicit nullopt_t(int) {} | 23 constexpr explicit nullopt_t(int) {} |
| 23 }; | 24 }; |
| 24 | 25 |
| 25 // Specification: | 26 // Specification: |
| 26 // http://en.cppreference.com/w/cpp/utility/optional/in_place | 27 // http://en.cppreference.com/w/cpp/utility/optional/in_place |
| 27 constexpr in_place_t in_place = {}; | 28 constexpr in_place_t in_place = {}; |
| 28 | 29 |
| 29 // Specification: | 30 // Specification: |
| 30 // http://en.cppreference.com/w/cpp/utility/optional/nullopt | 31 // http://en.cppreference.com/w/cpp/utility/optional/nullopt |
| 31 constexpr nullopt_t nullopt(0); | 32 constexpr nullopt_t nullopt(0); |
| 32 | 33 |
| 34 namespace internal { | |
| 35 | |
| 36 template <typename T, bool = base::is_trivially_destructible<T>::value> | |
| 37 struct OptionalStorage { | |
| 38 ~OptionalStorage() { | |
| 39 if (!is_null_) | |
| 40 buffer_.template data_as<T>()->~T(); | |
| 41 } | |
| 42 | |
| 43 bool is_null_ = true; | |
| 44 base::AlignedMemory<sizeof(T), ALIGNOF(T)> buffer_; | |
| 45 }; | |
| 46 | |
| 47 template <typename T> | |
| 48 struct OptionalStorage<T, true> { | |
| 49 ~OptionalStorage() = default; | |
|
danakj
2016/05/25 20:02:08
Can you leave a comment here explaining that this
alshabalin
2016/05/26 09:18:45
Done.
| |
| 50 | |
| 51 bool is_null_ = true; | |
| 52 base::AlignedMemory<sizeof(T), ALIGNOF(T)> buffer_; | |
| 53 }; | |
| 54 | |
| 55 } // namespace internal | |
| 56 | |
| 33 // base::Optional is a Chromium version of the C++17 optional class: | 57 // base::Optional is a Chromium version of the C++17 optional class: |
| 34 // std::optional documentation: | 58 // std::optional documentation: |
| 35 // http://en.cppreference.com/w/cpp/utility/optional | 59 // http://en.cppreference.com/w/cpp/utility/optional |
| 36 // Chromium documentation: | 60 // Chromium documentation: |
| 37 // https://chromium.googlesource.com/chromium/src/+/master/docs/optional.md | 61 // https://chromium.googlesource.com/chromium/src/+/master/docs/optional.md |
| 38 // | 62 // |
| 39 // These are the differences between the specification and the implementation: | 63 // These are the differences between the specification and the implementation: |
| 40 // - The constructor and emplace method using initializer_list are not | 64 // - The constructor and emplace method using initializer_list are not |
| 41 // implemented because 'initializer_list' is banned from Chromium. | 65 // implemented because 'initializer_list' is banned from Chromium. |
| 42 // - Constructors do not use 'constexpr' as it is a C++14 extension. | 66 // - Constructors do not use 'constexpr' as it is a C++14 extension. |
| 43 // - 'constexpr' might be missing in some places for reasons specified locally. | 67 // - 'constexpr' might be missing in some places for reasons specified locally. |
| 44 // - No exceptions are thrown, because they are banned from Chromium. | 68 // - No exceptions are thrown, because they are banned from Chromium. |
| 45 // - All the non-members are in the 'base' namespace instead of 'std'. | 69 // - All the non-members are in the 'base' namespace instead of 'std'. |
| 46 template <typename T> | 70 template <typename T> |
| 47 class Optional { | 71 class Optional { |
| 48 public: | 72 public: |
| 73 using value_type = T; | |
| 74 | |
| 49 constexpr Optional() = default; | 75 constexpr Optional() = default; |
| 50 Optional(base::nullopt_t) : Optional() {} | 76 Optional(base::nullopt_t) : Optional() {} |
| 51 | 77 |
| 52 Optional(const Optional& other) { | 78 Optional(const Optional& other) { |
| 53 if (!other.is_null_) | 79 if (!other.storage_.is_null_) |
| 54 Init(other.value()); | 80 Init(other.value()); |
| 55 } | 81 } |
| 56 | 82 |
| 57 Optional(Optional&& other) { | 83 Optional(Optional&& other) { |
| 58 if (!other.is_null_) | 84 if (!other.storage_.is_null_) |
| 59 Init(std::move(other.value())); | 85 Init(std::move(other.value())); |
| 60 } | 86 } |
| 61 | 87 |
| 62 Optional(const T& value) { Init(value); } | 88 Optional(const T& value) { Init(value); } |
| 63 | 89 |
| 64 Optional(T&& value) { Init(std::move(value)); } | 90 Optional(T&& value) { Init(std::move(value)); } |
| 65 | 91 |
| 66 template <class... Args> | 92 template <class... Args> |
| 67 explicit Optional(base::in_place_t, Args&&... args) { | 93 explicit Optional(base::in_place_t, Args&&... args) { |
| 68 emplace(std::forward<Args>(args)...); | 94 emplace(std::forward<Args>(args)...); |
| 69 } | 95 } |
| 70 | 96 |
| 71 ~Optional() { | 97 ~Optional() = default; |
| 72 // TODO(mlamouri): use is_trivially_destructible<T>::value when possible. | |
| 73 FreeIfNeeded(); | |
| 74 } | |
| 75 | 98 |
| 76 Optional& operator=(base::nullopt_t) { | 99 Optional& operator=(base::nullopt_t) { |
| 77 FreeIfNeeded(); | 100 FreeIfNeeded(); |
| 78 return *this; | 101 return *this; |
| 79 } | 102 } |
| 80 | 103 |
| 81 Optional& operator=(const Optional& other) { | 104 Optional& operator=(const Optional& other) { |
| 82 if (other.is_null_) { | 105 if (other.storage_.is_null_) { |
| 83 FreeIfNeeded(); | 106 FreeIfNeeded(); |
| 84 return *this; | 107 return *this; |
| 85 } | 108 } |
| 86 | 109 |
| 87 InitOrAssign(other.value()); | 110 InitOrAssign(other.value()); |
| 88 return *this; | 111 return *this; |
| 89 } | 112 } |
| 90 | 113 |
| 91 Optional& operator=(Optional&& other) { | 114 Optional& operator=(Optional&& other) { |
| 92 if (other.is_null_) { | 115 if (other.storage_.is_null_) { |
| 93 FreeIfNeeded(); | 116 FreeIfNeeded(); |
| 94 return *this; | 117 return *this; |
| 95 } | 118 } |
| 96 | 119 |
| 97 InitOrAssign(std::move(other.value())); | 120 InitOrAssign(std::move(other.value())); |
| 98 return *this; | 121 return *this; |
| 99 } | 122 } |
| 100 | 123 |
| 101 template <class U> | 124 template <class U> |
| 102 typename std::enable_if<std::is_same<std::decay<U>, T>::value, | 125 typename std::enable_if<std::is_same<std::decay<U>, T>::value, |
| 103 Optional&>::type | 126 Optional&>::type |
| 104 operator=(U&& value) { | 127 operator=(U&& value) { |
| 105 InitOrAssign(std::forward<U>(value)); | 128 InitOrAssign(std::forward<U>(value)); |
| 106 return *this; | 129 return *this; |
| 107 } | 130 } |
| 108 | 131 |
| 109 // TODO(mlamouri): can't use 'constexpr' with DCHECK. | 132 // TODO(mlamouri): can't use 'constexpr' with DCHECK. |
| 110 const T* operator->() const { | 133 const T* operator->() const { |
| 111 DCHECK(!is_null_); | 134 DCHECK(!storage_.is_null_); |
| 112 return &value(); | 135 return &value(); |
| 113 } | 136 } |
| 114 | 137 |
| 115 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was | 138 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was |
| 116 // meant to be 'constexpr const'. | 139 // meant to be 'constexpr const'. |
| 117 T* operator->() { | 140 T* operator->() { |
| 118 DCHECK(!is_null_); | 141 DCHECK(!storage_.is_null_); |
| 119 return &value(); | 142 return &value(); |
| 120 } | 143 } |
| 121 | 144 |
| 122 constexpr const T& operator*() const& { return value(); } | 145 constexpr const T& operator*() const& { return value(); } |
| 123 | 146 |
| 124 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was | 147 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was |
| 125 // meant to be 'constexpr const'. | 148 // meant to be 'constexpr const'. |
| 126 T& operator*() & { return value(); } | 149 T& operator*() & { return value(); } |
| 127 | 150 |
| 128 constexpr const T&& operator*() const&& { return std::move(value()); } | 151 constexpr const T&& operator*() const&& { return std::move(value()); } |
| 129 | 152 |
| 130 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was | 153 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was |
| 131 // meant to be 'constexpr const'. | 154 // meant to be 'constexpr const'. |
| 132 T&& operator*() && { return std::move(value()); } | 155 T&& operator*() && { return std::move(value()); } |
| 133 | 156 |
| 134 constexpr explicit operator bool() const { return !is_null_; } | 157 constexpr explicit operator bool() const { return !storage_.is_null_; } |
| 135 | 158 |
| 136 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was | 159 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was |
| 137 // meant to be 'constexpr const'. | 160 // meant to be 'constexpr const'. |
| 138 T& value() & { | 161 T& value() & { |
| 139 DCHECK(!is_null_); | 162 DCHECK(!storage_.is_null_); |
| 140 return *buffer_.template data_as<T>(); | 163 return *storage_.buffer_.template data_as<T>(); |
| 141 } | 164 } |
| 142 | 165 |
| 143 // TODO(mlamouri): can't use 'constexpr' with DCHECK. | 166 // TODO(mlamouri): can't use 'constexpr' with DCHECK. |
| 144 const T& value() const& { | 167 const T& value() const& { |
| 145 DCHECK(!is_null_); | 168 DCHECK(!storage_.is_null_); |
| 146 return *buffer_.template data_as<T>(); | 169 return *storage_.buffer_.template data_as<T>(); |
| 147 } | 170 } |
| 148 | 171 |
| 149 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was | 172 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was |
| 150 // meant to be 'constexpr const'. | 173 // meant to be 'constexpr const'. |
| 151 T&& value() && { | 174 T&& value() && { |
| 152 DCHECK(!is_null_); | 175 DCHECK(!storage_.is_null_); |
| 153 return std::move(*buffer_.template data_as<T>()); | 176 return std::move(*storage_.buffer_.template data_as<T>()); |
| 154 } | 177 } |
| 155 | 178 |
| 156 // TODO(mlamouri): can't use 'constexpr' with DCHECK. | 179 // TODO(mlamouri): can't use 'constexpr' with DCHECK. |
| 157 const T&& value() const&& { | 180 const T&& value() const&& { |
| 158 DCHECK(!is_null_); | 181 DCHECK(!storage_.is_null_); |
| 159 return std::move(*buffer_.template data_as<T>()); | 182 return std::move(*storage_.buffer_.template data_as<T>()); |
| 160 } | 183 } |
| 161 | 184 |
| 162 template <class U> | 185 template <class U> |
| 163 constexpr T value_or(U&& default_value) const& { | 186 constexpr T value_or(U&& default_value) const& { |
| 164 // TODO(mlamouri): add the following assert when possible: | 187 // TODO(mlamouri): add the following assert when possible: |
| 165 // static_assert(std::is_copy_constructible<T>::value, | 188 // static_assert(std::is_copy_constructible<T>::value, |
| 166 // "T must be copy constructible"); | 189 // "T must be copy constructible"); |
| 167 static_assert(std::is_convertible<U, T>::value, | 190 static_assert(std::is_convertible<U, T>::value, |
| 168 "U must be convertible to T"); | 191 "U must be convertible to T"); |
| 169 return is_null_ ? static_cast<T>(std::forward<U>(default_value)) : value(); | 192 return storage_.is_null_ ? static_cast<T>(std::forward<U>(default_value)) |
| 193 : value(); | |
| 170 } | 194 } |
| 171 | 195 |
| 172 template <class U> | 196 template <class U> |
| 173 T value_or(U&& default_value) && { | 197 T value_or(U&& default_value) && { |
| 174 // TODO(mlamouri): add the following assert when possible: | 198 // TODO(mlamouri): add the following assert when possible: |
| 175 // static_assert(std::is_move_constructible<T>::value, | 199 // static_assert(std::is_move_constructible<T>::value, |
| 176 // "T must be move constructible"); | 200 // "T must be move constructible"); |
| 177 static_assert(std::is_convertible<U, T>::value, | 201 static_assert(std::is_convertible<U, T>::value, |
| 178 "U must be convertible to T"); | 202 "U must be convertible to T"); |
| 179 return is_null_ ? static_cast<T>(std::forward<U>(default_value)) | 203 return storage_.is_null_ ? static_cast<T>(std::forward<U>(default_value)) |
| 180 : std::move(value()); | 204 : std::move(value()); |
| 181 } | 205 } |
| 182 | 206 |
| 183 void swap(Optional& other) { | 207 void swap(Optional& other) { |
| 184 if (is_null_ && other.is_null_) | 208 if (storage_.is_null_ && other.storage_.is_null_) |
| 185 return; | 209 return; |
| 186 | 210 |
| 187 if (is_null_ != other.is_null_) { | 211 if (storage_.is_null_ != other.storage_.is_null_) { |
| 188 if (is_null_) { | 212 if (storage_.is_null_) { |
| 189 Init(std::move(*other.buffer_.template data_as<T>())); | 213 Init(std::move(*other.storage_.buffer_.template data_as<T>())); |
| 190 other.FreeIfNeeded(); | 214 other.FreeIfNeeded(); |
| 191 } else { | 215 } else { |
| 192 other.Init(std::move(*buffer_.template data_as<T>())); | 216 other.Init(std::move(*storage_.buffer_.template data_as<T>())); |
| 193 FreeIfNeeded(); | 217 FreeIfNeeded(); |
| 194 } | 218 } |
| 195 return; | 219 return; |
| 196 } | 220 } |
| 197 | 221 |
| 198 DCHECK(!is_null_ && !other.is_null_); | 222 DCHECK(!storage_.is_null_ && !other.storage_.is_null_); |
| 199 using std::swap; | 223 using std::swap; |
| 200 swap(**this, *other); | 224 swap(**this, *other); |
| 201 } | 225 } |
| 202 | 226 |
| 203 template <class... Args> | 227 template <class... Args> |
| 204 void emplace(Args&&... args) { | 228 void emplace(Args&&... args) { |
| 205 FreeIfNeeded(); | 229 FreeIfNeeded(); |
| 206 Init(std::forward<Args>(args)...); | 230 Init(std::forward<Args>(args)...); |
| 207 } | 231 } |
| 208 | 232 |
| 209 private: | 233 private: |
| 210 void Init(const T& value) { | 234 void Init(const T& value) { |
| 211 DCHECK(is_null_); | 235 DCHECK(storage_.is_null_); |
| 212 new (buffer_.template data_as<T>()) T(value); | 236 new (storage_.buffer_.template data_as<T>()) T(value); |
| 213 is_null_ = false; | 237 storage_.is_null_ = false; |
| 214 } | 238 } |
| 215 | 239 |
| 216 void Init(T&& value) { | 240 void Init(T&& value) { |
| 217 DCHECK(is_null_); | 241 DCHECK(storage_.is_null_); |
| 218 new (buffer_.template data_as<T>()) T(std::move(value)); | 242 new (storage_.buffer_.template data_as<T>()) T(std::move(value)); |
| 219 is_null_ = false; | 243 storage_.is_null_ = false; |
| 220 } | 244 } |
| 221 | 245 |
| 222 template <class... Args> | 246 template <class... Args> |
| 223 void Init(Args&&... args) { | 247 void Init(Args&&... args) { |
| 224 DCHECK(is_null_); | 248 DCHECK(storage_.is_null_); |
| 225 new (buffer_.template data_as<T>()) T(std::forward<Args>(args)...); | 249 new (storage_.buffer_.template data_as<T>()) T(std::forward<Args>(args)...); |
| 226 is_null_ = false; | 250 storage_.is_null_ = false; |
| 227 } | 251 } |
| 228 | 252 |
| 229 void InitOrAssign(const T& value) { | 253 void InitOrAssign(const T& value) { |
| 230 if (is_null_) | 254 if (storage_.is_null_) |
| 231 Init(value); | 255 Init(value); |
| 232 else | 256 else |
| 233 *buffer_.template data_as<T>() = value; | 257 *storage_.buffer_.template data_as<T>() = value; |
| 234 } | 258 } |
| 235 | 259 |
| 236 void InitOrAssign(T&& value) { | 260 void InitOrAssign(T&& value) { |
| 237 if (is_null_) | 261 if (storage_.is_null_) |
| 238 Init(std::move(value)); | 262 Init(std::move(value)); |
| 239 else | 263 else |
| 240 *buffer_.template data_as<T>() = std::move(value); | 264 *storage_.buffer_.template data_as<T>() = std::move(value); |
| 241 } | 265 } |
| 242 | 266 |
| 243 void FreeIfNeeded() { | 267 void FreeIfNeeded() { |
| 244 if (is_null_) | 268 if (storage_.is_null_) |
| 245 return; | 269 return; |
| 246 buffer_.template data_as<T>()->~T(); | 270 storage_.buffer_.template data_as<T>()->~T(); |
| 247 is_null_ = true; | 271 storage_.is_null_ = true; |
| 248 } | 272 } |
| 249 | 273 |
| 250 bool is_null_ = true; | 274 internal::OptionalStorage<T> storage_; |
| 251 base::AlignedMemory<sizeof(T), ALIGNOF(T)> buffer_; | |
| 252 }; | 275 }; |
| 253 | 276 |
| 254 template <class T> | 277 template <class T> |
| 255 constexpr bool operator==(const Optional<T>& lhs, const Optional<T>& rhs) { | 278 constexpr bool operator==(const Optional<T>& lhs, const Optional<T>& rhs) { |
| 256 return !!lhs != !!rhs ? false : lhs == nullopt || (*lhs == *rhs); | 279 return !!lhs != !!rhs ? false : lhs == nullopt || (*lhs == *rhs); |
| 257 } | 280 } |
| 258 | 281 |
| 259 template <class T> | 282 template <class T> |
| 260 constexpr bool operator!=(const Optional<T>& lhs, const Optional<T>& rhs) { | 283 constexpr bool operator!=(const Optional<T>& lhs, const Optional<T>& rhs) { |
| 261 return !(lhs == rhs); | 284 return !(lhs == rhs); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 418 template <class T> | 441 template <class T> |
| 419 struct hash<base::Optional<T>> { | 442 struct hash<base::Optional<T>> { |
| 420 size_t operator()(const base::Optional<T>& opt) const { | 443 size_t operator()(const base::Optional<T>& opt) const { |
| 421 return opt == base::nullopt ? 0 : std::hash<T>()(*opt); | 444 return opt == base::nullopt ? 0 : std::hash<T>()(*opt); |
| 422 } | 445 } |
| 423 }; | 446 }; |
| 424 | 447 |
| 425 } // namespace std | 448 } // namespace std |
| 426 | 449 |
| 427 #endif // BASE_OPTIONAL_H_ | 450 #endif // BASE_OPTIONAL_H_ |
| OLD | NEW |