Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(672)

Side by Side Diff: base/optional.h

Issue 2198673002: Implement Optional::has_value() and Optional::reset(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/optional_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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_
OLDNEW
« no previous file with comments | « no previous file | base/optional_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698