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 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 232 |
233 // Dereference with a default value in case we don't have a value. | 233 // Dereference with a default value in case we don't have a value. |
234 const T& value_or(const T& default_val) const { | 234 const T& value_or(const T& default_val) const { |
235 // The no-op call prevents the compiler from generating optimized code that | 235 // The no-op call prevents the compiler from generating optimized code that |
236 // reads value_ even if !has_value_, but only if FunctionThatDoesNothing is | 236 // reads value_ even if !has_value_, but only if FunctionThatDoesNothing is |
237 // not completely inlined; see its declaration.). | 237 // not completely inlined; see its declaration.). |
238 return has_value_ ? *optional_internal::FunctionThatDoesNothing(&value_) | 238 return has_value_ ? *optional_internal::FunctionThatDoesNothing(&value_) |
239 : default_val; | 239 : default_val; |
240 } | 240 } |
241 | 241 |
| 242 // Dereference and move value. |
| 243 T ConsumeValue() { |
| 244 RTC_DCHECK(has_value_); |
| 245 return std::move(value_); |
| 246 } |
| 247 |
242 // Equality tests. Two Optionals are equal if they contain equivalent values, | 248 // Equality tests. Two Optionals are equal if they contain equivalent values, |
243 // or if they're both empty. | 249 // or if they're both empty. |
244 friend bool operator==(const Optional& m1, const Optional& m2) { | 250 friend bool operator==(const Optional& m1, const Optional& m2) { |
245 return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_ | 251 return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_ |
246 : m1.has_value_ == m2.has_value_; | 252 : m1.has_value_ == m2.has_value_; |
247 } | 253 } |
248 friend bool operator==(const Optional& opt, const T& value) { | 254 friend bool operator==(const Optional& opt, const T& value) { |
249 return opt.has_value_ && opt.value_ == value; | 255 return opt.has_value_ && opt.value_ == value; |
250 } | 256 } |
251 friend bool operator==(const T& value, const Optional& opt) { | 257 friend bool operator==(const T& value, const Optional& opt) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 // construct it, and the Optional destructor won't automatically destroy | 292 // construct it, and the Optional destructor won't automatically destroy |
287 // it. Basically, this just allocates a properly sized and aligned block of | 293 // it. Basically, this just allocates a properly sized and aligned block of |
288 // memory in which we can manually put a T with placement new. | 294 // memory in which we can manually put a T with placement new. |
289 T value_; | 295 T value_; |
290 }; | 296 }; |
291 }; | 297 }; |
292 | 298 |
293 } // namespace rtc | 299 } // namespace rtc |
294 | 300 |
295 #endif // WEBRTC_BASE_OPTIONAL_H_ | 301 #endif // WEBRTC_BASE_OPTIONAL_H_ |
OLD | NEW |