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

Side by Side Diff: base/optional.h

Issue 2080003002: base::Optional: Use anonymous union instead of base::AlignedMemory (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 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 | styleguide/c++/c++11.html » ('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"
11 #include "base/memory/aligned_memory.h"
12 #include "base/template_util.h" 11 #include "base/template_util.h"
13 12
14 namespace base { 13 namespace base {
15 14
16 // Specification: 15 // Specification:
17 // http://en.cppreference.com/w/cpp/utility/optional/in_place_t 16 // http://en.cppreference.com/w/cpp/utility/optional/in_place_t
18 struct in_place_t {}; 17 struct in_place_t {};
19 18
20 // Specification: 19 // Specification:
21 // http://en.cppreference.com/w/cpp/utility/optional/nullopt_t 20 // http://en.cppreference.com/w/cpp/utility/optional/nullopt_t
22 struct nullopt_t { 21 struct nullopt_t {
23 constexpr explicit nullopt_t(int) {} 22 constexpr explicit nullopt_t(int) {}
24 }; 23 };
25 24
26 // Specification: 25 // Specification:
27 // http://en.cppreference.com/w/cpp/utility/optional/in_place 26 // http://en.cppreference.com/w/cpp/utility/optional/in_place
28 constexpr in_place_t in_place = {}; 27 constexpr in_place_t in_place = {};
29 28
30 // Specification: 29 // Specification:
31 // http://en.cppreference.com/w/cpp/utility/optional/nullopt 30 // http://en.cppreference.com/w/cpp/utility/optional/nullopt
32 constexpr nullopt_t nullopt(0); 31 constexpr nullopt_t nullopt(0);
33 32
34 namespace internal { 33 namespace internal {
35 34
36 template <typename T, bool = base::is_trivially_destructible<T>::value> 35 template <typename T, bool = base::is_trivially_destructible<T>::value>
37 struct OptionalStorage { 36 struct OptionalStorage {
37 OptionalStorage() {};
38 // When T is not trivially destructible we must call its 38 // When T is not trivially destructible we must call its
39 // destructor before deallocating its memory. 39 // destructor before deallocating its memory.
40 ~OptionalStorage() { 40 ~OptionalStorage() {
41 if (!is_null_) 41 if (!is_null_)
42 buffer_.template data_as<T>()->~T(); 42 value_.~T();
43 } 43 }
44 44
45 bool is_null_ = true; 45 bool is_null_ = true;
46 base::AlignedMemory<sizeof(T), ALIGNOF(T)> buffer_; 46 union {
47 T value_;
danakj 2016/06/22 23:00:03 Why does libcxx put a second field in the union? h
kwiberg-chromium 2016/06/23 02:18:49 It seems like it's required to make constexpr cons
48 };
47 }; 49 };
48 50
49 template <typename T> 51 template <typename T>
50 struct OptionalStorage<T, true> { 52 struct OptionalStorage<T, true> {
51 // When T is trivially destructible (i.e. its destructor does nothing) 53 OptionalStorage() {};
52 // there is no need to call it. 54 // When T is trivially destructible (i.e. its destructor does nothing) there
53 // Since |base::AlignedMemory| is just an array its destructor 55 // is no need to call it. Explicitly defaulting the destructor means it's not
54 // is trivial. Explicitly defaulting the destructor means it's not 56 // user-provided. Those two together make this destructor trivial.
55 // user-provided. All of this together make this destructor trivial.
56 ~OptionalStorage() = default; 57 ~OptionalStorage() = default;
57 58
58 bool is_null_ = true; 59 bool is_null_ = true;
59 base::AlignedMemory<sizeof(T), ALIGNOF(T)> buffer_; 60 union {
61 T value_;
62 };
60 }; 63 };
61 64
62 } // namespace internal 65 } // namespace internal
63 66
64 // base::Optional is a Chromium version of the C++17 optional class: 67 // base::Optional is a Chromium version of the C++17 optional class:
65 // std::optional documentation: 68 // std::optional documentation:
66 // http://en.cppreference.com/w/cpp/utility/optional 69 // http://en.cppreference.com/w/cpp/utility/optional
67 // Chromium documentation: 70 // Chromium documentation:
68 // https://chromium.googlesource.com/chromium/src/+/master/docs/optional.md 71 // https://chromium.googlesource.com/chromium/src/+/master/docs/optional.md
69 // 72 //
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was 163 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was
161 // meant to be 'constexpr const'. 164 // meant to be 'constexpr const'.
162 T&& operator*() && { return std::move(value()); } 165 T&& operator*() && { return std::move(value()); }
163 166
164 constexpr explicit operator bool() const { return !storage_.is_null_; } 167 constexpr explicit operator bool() const { return !storage_.is_null_; }
165 168
166 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was 169 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was
167 // meant to be 'constexpr const'. 170 // meant to be 'constexpr const'.
168 T& value() & { 171 T& value() & {
169 DCHECK(!storage_.is_null_); 172 DCHECK(!storage_.is_null_);
170 return *storage_.buffer_.template data_as<T>(); 173 return storage_.value_;
171 } 174 }
172 175
173 // TODO(mlamouri): can't use 'constexpr' with DCHECK. 176 // TODO(mlamouri): can't use 'constexpr' with DCHECK.
174 const T& value() const& { 177 const T& value() const& {
175 DCHECK(!storage_.is_null_); 178 DCHECK(!storage_.is_null_);
176 return *storage_.buffer_.template data_as<T>(); 179 return storage_.value_;
177 } 180 }
178 181
179 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was 182 // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was
180 // meant to be 'constexpr const'. 183 // meant to be 'constexpr const'.
181 T&& value() && { 184 T&& value() && {
182 DCHECK(!storage_.is_null_); 185 DCHECK(!storage_.is_null_);
183 return std::move(*storage_.buffer_.template data_as<T>()); 186 return std::move(storage_.value_);
184 } 187 }
185 188
186 // TODO(mlamouri): can't use 'constexpr' with DCHECK. 189 // TODO(mlamouri): can't use 'constexpr' with DCHECK.
187 const T&& value() const&& { 190 const T&& value() const&& {
188 DCHECK(!storage_.is_null_); 191 DCHECK(!storage_.is_null_);
189 return std::move(*storage_.buffer_.template data_as<T>()); 192 return std::move(storage_.value_);
190 } 193 }
191 194
192 template <class U> 195 template <class U>
193 constexpr T value_or(U&& default_value) const& { 196 constexpr T value_or(U&& default_value) const& {
194 // TODO(mlamouri): add the following assert when possible: 197 // TODO(mlamouri): add the following assert when possible:
195 // static_assert(std::is_copy_constructible<T>::value, 198 // static_assert(std::is_copy_constructible<T>::value,
196 // "T must be copy constructible"); 199 // "T must be copy constructible");
197 static_assert(std::is_convertible<U, T>::value, 200 static_assert(std::is_convertible<U, T>::value,
198 "U must be convertible to T"); 201 "U must be convertible to T");
199 return storage_.is_null_ ? static_cast<T>(std::forward<U>(default_value)) 202 return storage_.is_null_ ? static_cast<T>(std::forward<U>(default_value))
(...skipping 10 matching lines...) Expand all
210 return storage_.is_null_ ? static_cast<T>(std::forward<U>(default_value)) 213 return storage_.is_null_ ? static_cast<T>(std::forward<U>(default_value))
211 : std::move(value()); 214 : std::move(value());
212 } 215 }
213 216
214 void swap(Optional& other) { 217 void swap(Optional& other) {
215 if (storage_.is_null_ && other.storage_.is_null_) 218 if (storage_.is_null_ && other.storage_.is_null_)
216 return; 219 return;
217 220
218 if (storage_.is_null_ != other.storage_.is_null_) { 221 if (storage_.is_null_ != other.storage_.is_null_) {
219 if (storage_.is_null_) { 222 if (storage_.is_null_) {
220 Init(std::move(*other.storage_.buffer_.template data_as<T>())); 223 Init(std::move(other.storage_.value_));
221 other.FreeIfNeeded(); 224 other.FreeIfNeeded();
222 } else { 225 } else {
223 other.Init(std::move(*storage_.buffer_.template data_as<T>())); 226 other.Init(std::move(storage_.value_));
224 FreeIfNeeded(); 227 FreeIfNeeded();
225 } 228 }
226 return; 229 return;
227 } 230 }
228 231
229 DCHECK(!storage_.is_null_ && !other.storage_.is_null_); 232 DCHECK(!storage_.is_null_ && !other.storage_.is_null_);
230 using std::swap; 233 using std::swap;
231 swap(**this, *other); 234 swap(**this, *other);
232 } 235 }
233 236
234 template <class... Args> 237 template <class... Args>
235 void emplace(Args&&... args) { 238 void emplace(Args&&... args) {
236 FreeIfNeeded(); 239 FreeIfNeeded();
237 Init(std::forward<Args>(args)...); 240 Init(std::forward<Args>(args)...);
238 } 241 }
239 242
240 private: 243 private:
241 void Init(const T& value) { 244 void Init(const T& value) {
242 DCHECK(storage_.is_null_); 245 DCHECK(storage_.is_null_);
243 new (storage_.buffer_.void_data()) T(value); 246 new (&storage_.value_) T(value);
244 storage_.is_null_ = false; 247 storage_.is_null_ = false;
245 } 248 }
246 249
247 void Init(T&& value) { 250 void Init(T&& value) {
248 DCHECK(storage_.is_null_); 251 DCHECK(storage_.is_null_);
249 new (storage_.buffer_.void_data()) T(std::move(value)); 252 new (&storage_.value_) T(std::move(value));
250 storage_.is_null_ = false; 253 storage_.is_null_ = false;
251 } 254 }
252 255
253 template <class... Args> 256 template <class... Args>
254 void Init(Args&&... args) { 257 void Init(Args&&... args) {
255 DCHECK(storage_.is_null_); 258 DCHECK(storage_.is_null_);
256 new (storage_.buffer_.void_data()) T(std::forward<Args>(args)...); 259 new (&storage_.value_) T(std::forward<Args>(args)...);
257 storage_.is_null_ = false; 260 storage_.is_null_ = false;
258 } 261 }
259 262
260 void InitOrAssign(const T& value) { 263 void InitOrAssign(const T& value) {
261 if (storage_.is_null_) 264 if (storage_.is_null_)
262 Init(value); 265 Init(value);
263 else 266 else
264 *storage_.buffer_.template data_as<T>() = value; 267 storage_.value_ = value;
265 } 268 }
266 269
267 void InitOrAssign(T&& value) { 270 void InitOrAssign(T&& value) {
268 if (storage_.is_null_) 271 if (storage_.is_null_)
269 Init(std::move(value)); 272 Init(std::move(value));
270 else 273 else
271 *storage_.buffer_.template data_as<T>() = std::move(value); 274 storage_.value_ = std::move(value);
272 } 275 }
273 276
274 void FreeIfNeeded() { 277 void FreeIfNeeded() {
275 if (storage_.is_null_) 278 if (storage_.is_null_)
276 return; 279 return;
277 storage_.buffer_.template data_as<T>()->~T(); 280 storage_.value_.~T();
278 storage_.is_null_ = true; 281 storage_.is_null_ = true;
279 } 282 }
280 283
281 internal::OptionalStorage<T> storage_; 284 internal::OptionalStorage<T> storage_;
282 }; 285 };
283 286
284 template <class T> 287 template <class T>
285 constexpr bool operator==(const Optional<T>& lhs, const Optional<T>& rhs) { 288 constexpr bool operator==(const Optional<T>& lhs, const Optional<T>& rhs) {
286 return !!lhs != !!rhs ? false : lhs == nullopt || (*lhs == *rhs); 289 return !!lhs != !!rhs ? false : lhs == nullopt || (*lhs == *rhs);
287 } 290 }
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 template <class T> 451 template <class T>
449 struct hash<base::Optional<T>> { 452 struct hash<base::Optional<T>> {
450 size_t operator()(const base::Optional<T>& opt) const { 453 size_t operator()(const base::Optional<T>& opt) const {
451 return opt == base::nullopt ? 0 : std::hash<T>()(*opt); 454 return opt == base::nullopt ? 0 : std::hash<T>()(*opt);
452 } 455 }
453 }; 456 };
454 457
455 } // namespace std 458 } // namespace std
456 459
457 #endif // BASE_OPTIONAL_H_ 460 #endif // BASE_OPTIONAL_H_
OLDNEW
« no previous file with comments | « no previous file | styleguide/c++/c++11.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698