OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_MEMORY_REF_COUNTED_H_ | 5 #ifndef BASE_MEMORY_REF_COUNTED_H_ |
6 #define BASE_MEMORY_REF_COUNTED_H_ | 6 #define BASE_MEMORY_REF_COUNTED_H_ |
7 | 7 |
8 #include <cassert> | 8 #include <cassert> |
9 #include <iosfwd> | 9 #include <iosfwd> |
10 | 10 |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
326 void swap(T** pp) { | 326 void swap(T** pp) { |
327 T* p = ptr_; | 327 T* p = ptr_; |
328 ptr_ = *pp; | 328 ptr_ = *pp; |
329 *pp = p; | 329 *pp = p; |
330 } | 330 } |
331 | 331 |
332 void swap(scoped_refptr<T>& r) { | 332 void swap(scoped_refptr<T>& r) { |
333 swap(&r.ptr_); | 333 swap(&r.ptr_); |
334 } | 334 } |
335 | 335 |
336 private: | |
337 // Allow scoped_refptr<T> to be used in boolean expressions, but not | |
338 // implicitly convertible to a real bool (which is dangerous). | |
339 // | |
340 // Note that this trick is only safe when the == and != operators | |
341 // are declared explicitly, as otherwise "refptr1 == refptr2" | |
342 // will compile but do the wrong thing (i.e., convert to Testable | |
343 // and then do the comparison). | |
344 typedef T* scoped_refptr::*Testable; | |
Lei Zhang
2014/11/26 00:44:18
nit: blank line after.
dcheng
2014/11/26 00:45:28
Done.
| |
345 public: | |
346 operator Testable() const { return ptr_ ? &scoped_refptr::ptr_ : nullptr; } | |
347 | |
336 template <typename U> | 348 template <typename U> |
337 bool operator==(const scoped_refptr<U>& rhs) const { | 349 bool operator==(const scoped_refptr<U>& rhs) const { |
338 return ptr_ == rhs.get(); | 350 return ptr_ == rhs.get(); |
339 } | 351 } |
340 | 352 |
341 template <typename U> | 353 template <typename U> |
342 bool operator!=(const scoped_refptr<U>& rhs) const { | 354 bool operator!=(const scoped_refptr<U>& rhs) const { |
343 return !operator==(rhs); | 355 return !operator==(rhs); |
344 } | 356 } |
345 | 357 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { | 410 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { |
399 return !operator==(lhs, rhs); | 411 return !operator==(lhs, rhs); |
400 } | 412 } |
401 | 413 |
402 template <typename T> | 414 template <typename T> |
403 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 415 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
404 return out << p.get(); | 416 return out << p.get(); |
405 } | 417 } |
406 | 418 |
407 #endif // BASE_MEMORY_REF_COUNTED_H_ | 419 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |