Chromium Code Reviews| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <cassert> | 10 #include <cassert> |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 378 | 378 |
| 379 template <typename U> | 379 template <typename U> |
| 380 bool operator<(const scoped_refptr<U>& rhs) const { | 380 bool operator<(const scoped_refptr<U>& rhs) const { |
| 381 return ptr_ < rhs.get(); | 381 return ptr_ < rhs.get(); |
| 382 } | 382 } |
| 383 | 383 |
| 384 protected: | 384 protected: |
| 385 T* ptr_ = nullptr; | 385 T* ptr_ = nullptr; |
| 386 | 386 |
| 387 private: | 387 private: |
| 388 template <typename U> | |
| 389 friend scoped_refptr<U> AdoptRef(U*); | |
| 390 | |
| 391 struct AdoptRefTag {}; | |
|
dcheng
2017/03/06 22:09:18
FWIW, the usual trick I've seen in the past is to
tzik
2017/03/07 13:39:42
Done.
| |
| 392 scoped_refptr(T* p, AdoptRefTag) : ptr_(p) {} | |
| 393 | |
| 388 // Friend required for move constructors that set r.ptr_ to null. | 394 // Friend required for move constructors that set r.ptr_ to null. |
| 389 template <typename U> | 395 template <typename U> |
| 390 friend class scoped_refptr; | 396 friend class scoped_refptr; |
| 391 | 397 |
| 392 // Non-inline helpers to allow: | 398 // Non-inline helpers to allow: |
| 393 // class Opaque; | 399 // class Opaque; |
| 394 // extern template class scoped_refptr<Opaque>; | 400 // extern template class scoped_refptr<Opaque>; |
| 395 // Otherwise the compiler will complain that Opaque is an incomplete type. | 401 // Otherwise the compiler will complain that Opaque is an incomplete type. |
| 396 static void AddRef(T* ptr); | 402 static void AddRef(T* ptr); |
| 397 static void Release(T* ptr); | 403 static void Release(T* ptr); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 409 ptr->Release(); | 415 ptr->Release(); |
| 410 } | 416 } |
| 411 | 417 |
| 412 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without | 418 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without |
| 413 // having to retype all the template arguments | 419 // having to retype all the template arguments |
| 414 template <typename T> | 420 template <typename T> |
| 415 scoped_refptr<T> make_scoped_refptr(T* t) { | 421 scoped_refptr<T> make_scoped_refptr(T* t) { |
| 416 return scoped_refptr<T>(t); | 422 return scoped_refptr<T>(t); |
| 417 } | 423 } |
| 418 | 424 |
| 425 // Creates a scoped_refptr from a raw pointer without incrementing the reference | |
| 426 // count. Use this only for a newly created object whose reference count starts | |
| 427 // from 1 instead of 0. | |
| 428 template <typename T> | |
| 429 scoped_refptr<T> AdoptRef(T* t) { | |
| 430 DCHECK(!t || t->HasOneRef()); | |
| 431 using Tag = typename scoped_refptr<T>::AdoptRefTag; | |
| 432 return scoped_refptr<T>(t, Tag()); | |
| 433 } | |
| 434 | |
| 419 template <typename T, typename U> | 435 template <typename T, typename U> |
| 420 bool operator==(const scoped_refptr<T>& lhs, const U* rhs) { | 436 bool operator==(const scoped_refptr<T>& lhs, const U* rhs) { |
| 421 return lhs.get() == rhs; | 437 return lhs.get() == rhs; |
| 422 } | 438 } |
| 423 | 439 |
| 424 template <typename T, typename U> | 440 template <typename T, typename U> |
| 425 bool operator==(const T* lhs, const scoped_refptr<U>& rhs) { | 441 bool operator==(const T* lhs, const scoped_refptr<U>& rhs) { |
| 426 return lhs == rhs.get(); | 442 return lhs == rhs.get(); |
| 427 } | 443 } |
| 428 | 444 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 455 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { | 471 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { |
| 456 return !operator==(null, rhs); | 472 return !operator==(null, rhs); |
| 457 } | 473 } |
| 458 | 474 |
| 459 template <typename T> | 475 template <typename T> |
| 460 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 476 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
| 461 return out << p.get(); | 477 return out << p.get(); |
| 462 } | 478 } |
| 463 | 479 |
| 464 #endif // BASE_MEMORY_REF_COUNTED_H_ | 480 #endif // BASE_MEMORY_REF_COUNTED_H_ |
| OLD | NEW |