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 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 if (has_value_) | 211 if (has_value_) |
212 value_.~T(); | 212 value_.~T(); |
213 else | 213 else |
214 UnpoisonValue(); | 214 UnpoisonValue(); |
215 new (&value_) T(std::forward<Args>(args)...); | 215 new (&value_) T(std::forward<Args>(args)...); |
216 has_value_ = true; | 216 has_value_ = true; |
217 } | 217 } |
218 | 218 |
219 // Conversion to bool to test if we have a value. | 219 // Conversion to bool to test if we have a value. |
220 explicit operator bool() const { return has_value_; } | 220 explicit operator bool() const { return has_value_; } |
| 221 bool has_value() const { return has_value_; } |
221 | 222 |
222 // Dereferencing. Only allowed if we have a value. | 223 // Dereferencing. Only allowed if we have a value. |
223 const T* operator->() const { | 224 const T* operator->() const { |
224 RTC_DCHECK(has_value_); | 225 RTC_DCHECK(has_value_); |
225 return &value_; | 226 return &value_; |
226 } | 227 } |
227 T* operator->() { | 228 T* operator->() { |
228 RTC_DCHECK(has_value_); | 229 RTC_DCHECK(has_value_); |
229 return &value_; | 230 return &value_; |
230 } | 231 } |
231 const T& operator*() const { | 232 const T& operator*() const { |
232 RTC_DCHECK(has_value_); | 233 RTC_DCHECK(has_value_); |
233 return value_; | 234 return value_; |
234 } | 235 } |
235 T& operator*() { | 236 T& operator*() { |
236 RTC_DCHECK(has_value_); | 237 RTC_DCHECK(has_value_); |
237 return value_; | 238 return value_; |
238 } | 239 } |
| 240 const T& value() const { |
| 241 RTC_DCHECK(has_value_); |
| 242 return value_; |
| 243 } |
| 244 T& value() { |
| 245 RTC_DCHECK(has_value_); |
| 246 return value_; |
| 247 } |
239 | 248 |
240 // Dereference with a default value in case we don't have a value. | 249 // Dereference with a default value in case we don't have a value. |
241 const T& value_or(const T& default_val) const { | 250 const T& value_or(const T& default_val) const { |
242 // The no-op call prevents the compiler from generating optimized code that | 251 // The no-op call prevents the compiler from generating optimized code that |
243 // reads value_ even if !has_value_, but only if FunctionThatDoesNothing is | 252 // reads value_ even if !has_value_, but only if FunctionThatDoesNothing is |
244 // not completely inlined; see its declaration.). | 253 // not completely inlined; see its declaration.). |
245 return has_value_ ? *optional_internal::FunctionThatDoesNothing(&value_) | 254 return has_value_ ? *optional_internal::FunctionThatDoesNothing(&value_) |
246 : default_val; | 255 : default_val; |
247 } | 256 } |
248 | 257 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 } else { | 400 } else { |
392 *os << "<empty optional>"; | 401 *os << "<empty optional>"; |
393 } | 402 } |
394 } | 403 } |
395 | 404 |
396 #endif // UNIT_TEST | 405 #endif // UNIT_TEST |
397 | 406 |
398 } // namespace rtc | 407 } // namespace rtc |
399 | 408 |
400 #endif // WEBRTC_BASE_OPTIONAL_H_ | 409 #endif // WEBRTC_BASE_OPTIONAL_H_ |
OLD | NEW |