| 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" |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 T& operator*() & { return value(); } | 156 T& operator*() & { return value(); } |
| 157 | 157 |
| 158 constexpr const T&& operator*() const&& { return std::move(value()); } | 158 constexpr const T&& operator*() const&& { return std::move(value()); } |
| 159 | 159 |
| 160 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was | 160 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was |
| 161 // meant to be 'constexpr const'. | 161 // meant to be 'constexpr const'. |
| 162 T&& operator*() && { return std::move(value()); } | 162 T&& operator*() && { return std::move(value()); } |
| 163 | 163 |
| 164 constexpr explicit operator bool() const { return !storage_.is_null_; } | 164 constexpr explicit operator bool() const { return !storage_.is_null_; } |
| 165 | 165 |
| 166 constexpr bool has_value() const { return !storage_.is_null_; } |
| 167 |
| 166 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was | 168 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was |
| 167 // meant to be 'constexpr const'. | 169 // meant to be 'constexpr const'. |
| 168 T& value() & { | 170 T& value() & { |
| 169 DCHECK(!storage_.is_null_); | 171 DCHECK(!storage_.is_null_); |
| 170 return *storage_.buffer_.template data_as<T>(); | 172 return *storage_.buffer_.template data_as<T>(); |
| 171 } | 173 } |
| 172 | 174 |
| 173 // TODO(mlamouri): can't use 'constexpr' with DCHECK. | 175 // TODO(mlamouri): can't use 'constexpr' with DCHECK. |
| 174 const T& value() const& { | 176 const T& value() const& { |
| 175 DCHECK(!storage_.is_null_); | 177 DCHECK(!storage_.is_null_); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 FreeIfNeeded(); | 226 FreeIfNeeded(); |
| 225 } | 227 } |
| 226 return; | 228 return; |
| 227 } | 229 } |
| 228 | 230 |
| 229 DCHECK(!storage_.is_null_ && !other.storage_.is_null_); | 231 DCHECK(!storage_.is_null_ && !other.storage_.is_null_); |
| 230 using std::swap; | 232 using std::swap; |
| 231 swap(**this, *other); | 233 swap(**this, *other); |
| 232 } | 234 } |
| 233 | 235 |
| 236 void reset() { |
| 237 FreeIfNeeded(); |
| 238 } |
| 239 |
| 234 template <class... Args> | 240 template <class... Args> |
| 235 void emplace(Args&&... args) { | 241 void emplace(Args&&... args) { |
| 236 FreeIfNeeded(); | 242 FreeIfNeeded(); |
| 237 Init(std::forward<Args>(args)...); | 243 Init(std::forward<Args>(args)...); |
| 238 } | 244 } |
| 239 | 245 |
| 240 private: | 246 private: |
| 241 void Init(const T& value) { | 247 void Init(const T& value) { |
| 242 DCHECK(storage_.is_null_); | 248 DCHECK(storage_.is_null_); |
| 243 new (storage_.buffer_.void_data()) T(value); | 249 new (storage_.buffer_.void_data()) T(value); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 template <class T> | 454 template <class T> |
| 449 struct hash<base::Optional<T>> { | 455 struct hash<base::Optional<T>> { |
| 450 size_t operator()(const base::Optional<T>& opt) const { | 456 size_t operator()(const base::Optional<T>& opt) const { |
| 451 return opt == base::nullopt ? 0 : std::hash<T>()(*opt); | 457 return opt == base::nullopt ? 0 : std::hash<T>()(*opt); |
| 452 } | 458 } |
| 453 }; | 459 }; |
| 454 | 460 |
| 455 } // namespace std | 461 } // namespace std |
| 456 | 462 |
| 457 #endif // BASE_OPTIONAL_H_ | 463 #endif // BASE_OPTIONAL_H_ |
| OLD | NEW |