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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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) : value_(std::move(val)), has_value_(true) {} | 69 explicit Optional(T&& val) : value_(std::move(val)), has_value_(true) {} |
70 | 70 |
71 // Copy and move constructors. | 71 // Copy and move constructors. |
72 // TODO(kwiberg): =default the move constructor when MSVC supports it. | |
73 Optional(const Optional&) = default; | 72 Optional(const Optional&) = default; |
74 Optional(Optional&& m) | 73 Optional(Optional&&) = default; |
75 : value_(std::move(m.value_)), has_value_(m.has_value_) {} | |
76 | 74 |
77 // Assignment. | 75 // Assignment. |
78 // TODO(kwiberg): =default the move assignment op when MSVC supports it. | |
79 Optional& operator=(const Optional&) = default; | 76 Optional& operator=(const Optional&) = default; |
80 Optional& operator=(Optional&& m) { | 77 Optional& operator=(Optional&&) = default; |
81 value_ = std::move(m.value_); | |
82 has_value_ = m.has_value_; | |
83 return *this; | |
84 } | |
85 | 78 |
86 friend void swap(Optional& m1, Optional& m2) { | 79 friend void swap(Optional& m1, Optional& m2) { |
87 using std::swap; | 80 using std::swap; |
88 swap(m1.value_, m2.value_); | 81 swap(m1.value_, m2.value_); |
89 swap(m1.has_value_, m2.has_value_); | 82 swap(m1.has_value_, m2.has_value_); |
90 } | 83 } |
91 | 84 |
92 // Conversion to bool to test if we have a value. | 85 // Conversion to bool to test if we have a value. |
93 explicit operator bool() const { return has_value_; } | 86 explicit operator bool() const { return has_value_; } |
94 | 87 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 private: | 123 private: |
131 // Invariant: Unless *this has been moved from, value_ is default-initialized | 124 // Invariant: Unless *this has been moved from, value_ is default-initialized |
132 // (or copied or moved from a default-initialized T) if !has_value_. | 125 // (or copied or moved from a default-initialized T) if !has_value_. |
133 T value_; | 126 T value_; |
134 bool has_value_; | 127 bool has_value_; |
135 }; | 128 }; |
136 | 129 |
137 } // namespace rtc | 130 } // namespace rtc |
138 | 131 |
139 #endif // WEBRTC_BASE_OPTIONAL_H_ | 132 #endif // WEBRTC_BASE_OPTIONAL_H_ |
OLD | NEW |