OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // TODO(kwiberg): Get rid of this class when the standard library has | 59 // TODO(kwiberg): Get rid of this class when the standard library has |
60 // std::optional (and we're allowed to use it). | 60 // std::optional (and we're allowed to use it). |
61 template <typename T> | 61 template <typename T> |
62 class Optional final { | 62 class Optional final { |
63 public: | 63 public: |
64 // Construct an empty Optional. | 64 // Construct an empty Optional. |
65 Optional() : has_value_(false) {} | 65 Optional() : has_value_(false) {} |
66 | 66 |
67 // Construct an Optional that contains a value. | 67 // Construct an Optional that contains a value. |
68 explicit Optional(const T& val) : value_(val), has_value_(true) {} | 68 explicit Optional(const T& val) : value_(val), has_value_(true) {} |
69 explicit Optional(T&& val) | 69 explicit Optional(T&& val) : value_(std::move(val)), has_value_(true) {} |
70 : value_(static_cast<T&&>(val)), has_value_(true) {} | |
71 | 70 |
72 // Copy and move constructors. | 71 // Copy and move constructors. |
73 // TODO(kwiberg): =default the move constructor when MSVC supports it. | 72 // TODO(kwiberg): =default the move constructor when MSVC supports it. |
74 Optional(const Optional&) = default; | 73 Optional(const Optional&) = default; |
75 Optional(Optional&& m) | 74 Optional(Optional&& m) |
76 : value_(static_cast<T&&>(m.value_)), has_value_(m.has_value_) {} | 75 : value_(std::move(m.value_)), has_value_(m.has_value_) {} |
77 | 76 |
78 // Assignment. | 77 // Assignment. |
79 // TODO(kwiberg): =default the move assignment op when MSVC supports it. | 78 // TODO(kwiberg): =default the move assignment op when MSVC supports it. |
80 Optional& operator=(const Optional&) = default; | 79 Optional& operator=(const Optional&) = default; |
81 Optional& operator=(Optional&& m) { | 80 Optional& operator=(Optional&& m) { |
82 value_ = static_cast<T&&>(m.value_); | 81 value_ = std::move(m.value_); |
83 has_value_ = m.has_value_; | 82 has_value_ = m.has_value_; |
84 return *this; | 83 return *this; |
85 } | 84 } |
86 | 85 |
87 friend void swap(Optional& m1, Optional& m2) { | 86 friend void swap(Optional& m1, Optional& m2) { |
88 using std::swap; | 87 using std::swap; |
89 swap(m1.value_, m2.value_); | 88 swap(m1.value_, m2.value_); |
90 swap(m1.has_value_, m2.has_value_); | 89 swap(m1.has_value_, m2.has_value_); |
91 } | 90 } |
92 | 91 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 private: | 130 private: |
132 // Invariant: Unless *this has been moved from, value_ is default-initialized | 131 // Invariant: Unless *this has been moved from, value_ is default-initialized |
133 // (or copied or moved from a default-initialized T) if !has_value_. | 132 // (or copied or moved from a default-initialized T) if !has_value_. |
134 T value_; | 133 T value_; |
135 bool has_value_; | 134 bool has_value_; |
136 }; | 135 }; |
137 | 136 |
138 } // namespace rtc | 137 } // namespace rtc |
139 | 138 |
140 #endif // WEBRTC_BASE_OPTIONAL_H_ | 139 #endif // WEBRTC_BASE_OPTIONAL_H_ |
OLD | NEW |