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 16 matching lines...) Expand all Loading... |
27 constexpr in_place_t in_place = {}; | 27 constexpr in_place_t in_place = {}; |
28 | 28 |
29 // Specification: | 29 // Specification: |
30 // http://en.cppreference.com/w/cpp/utility/optional/nullopt | 30 // http://en.cppreference.com/w/cpp/utility/optional/nullopt |
31 constexpr nullopt_t nullopt(0); | 31 constexpr nullopt_t nullopt(0); |
32 | 32 |
33 namespace internal { | 33 namespace internal { |
34 | 34 |
35 template <typename T, bool = base::is_trivially_destructible<T>::value> | 35 template <typename T, bool = base::is_trivially_destructible<T>::value> |
36 struct OptionalStorage { | 36 struct OptionalStorage { |
37 OptionalStorage() {} | 37 constexpr 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 value_.~T(); | 42 value_.~T(); |
43 } | 43 } |
44 | 44 |
45 bool is_null_ = true; | 45 bool is_null_ = true; |
46 union { | 46 union { |
47 // |empty_| exists so that the union will always be initialized, even when | 47 // |empty_| exists so that the union will always be initialized, even when |
48 // it doesn't contain a value. Not initializing it has been observed to | 48 // it doesn't contain a value. Not initializing it has been observed to |
49 // trigger comiler warnings. | 49 // trigger comiler warnings. |
50 char empty_ = '\0'; | 50 char empty_ = '\0'; |
51 T value_; | 51 T value_; |
52 }; | 52 }; |
53 }; | 53 }; |
54 | 54 |
55 template <typename T> | 55 template <typename T> |
56 struct OptionalStorage<T, true> { | 56 struct OptionalStorage<T, true> { |
57 OptionalStorage() {} | 57 constexpr OptionalStorage() {} |
58 // When T is trivially destructible (i.e. its destructor does nothing) there | 58 // When T is trivially destructible (i.e. its destructor does nothing) there |
59 // is no need to call it. Explicitly defaulting the destructor means it's not | 59 // is no need to call it. Explicitly defaulting the destructor means it's not |
60 // user-provided. Those two together make this destructor trivial. | 60 // user-provided. Those two together make this destructor trivial. |
61 ~OptionalStorage() = default; | 61 ~OptionalStorage() = default; |
62 | 62 |
63 bool is_null_ = true; | 63 bool is_null_ = true; |
64 union { | 64 union { |
65 // |empty_| exists so that the union will always be initialized, even when | 65 // |empty_| exists so that the union will always be initialized, even when |
66 // it doesn't contain a value. Not initializing it has been observed to | 66 // it doesn't contain a value. Not initializing it has been observed to |
67 // trigger comiler warnings. | 67 // trigger comiler warnings. |
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 template <class T> | 465 template <class T> |
466 struct hash<base::Optional<T>> { | 466 struct hash<base::Optional<T>> { |
467 size_t operator()(const base::Optional<T>& opt) const { | 467 size_t operator()(const base::Optional<T>& opt) const { |
468 return opt == base::nullopt ? 0 : std::hash<T>()(*opt); | 468 return opt == base::nullopt ? 0 : std::hash<T>()(*opt); |
469 } | 469 } |
470 }; | 470 }; |
471 | 471 |
472 } // namespace std | 472 } // namespace std |
473 | 473 |
474 #endif // BASE_OPTIONAL_H_ | 474 #endif // BASE_OPTIONAL_H_ |
OLD | NEW |