| 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; |
| 345 |
| 346 public: |
| 347 operator Testable() const { return ptr_ ? &scoped_refptr::ptr_ : nullptr; } |
| 348 |
| 336 template <typename U> | 349 template <typename U> |
| 337 bool operator==(const scoped_refptr<U>& rhs) const { | 350 bool operator==(const scoped_refptr<U>& rhs) const { |
| 338 return ptr_ == rhs.get(); | 351 return ptr_ == rhs.get(); |
| 339 } | 352 } |
| 340 | 353 |
| 341 template <typename U> | 354 template <typename U> |
| 342 bool operator!=(const scoped_refptr<U>& rhs) const { | 355 bool operator!=(const scoped_refptr<U>& rhs) const { |
| 343 return !operator==(rhs); | 356 return !operator==(rhs); |
| 344 } | 357 } |
| 345 | 358 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { | 411 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { |
| 399 return !operator==(lhs, rhs); | 412 return !operator==(lhs, rhs); |
| 400 } | 413 } |
| 401 | 414 |
| 402 template <typename T> | 415 template <typename T> |
| 403 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 416 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
| 404 return out << p.get(); | 417 return out << p.get(); |
| 405 } | 418 } |
| 406 | 419 |
| 407 #endif // BASE_MEMORY_REF_COUNTED_H_ | 420 #endif // BASE_MEMORY_REF_COUNTED_H_ |
| OLD | NEW |