Chromium Code Reviews| Index: base/memory/ref_counted.h |
| diff --git a/base/memory/ref_counted.h b/base/memory/ref_counted.h |
| index ff46e6d6e5a9c87782b449474fbbebc4e0865454..c8564368b7f42836a4de01ba0699ea1f718a5f8e 100644 |
| --- a/base/memory/ref_counted.h |
| +++ b/base/memory/ref_counted.h |
| @@ -385,6 +385,12 @@ class scoped_refptr { |
| T* ptr_ = nullptr; |
| private: |
| + template <typename U> |
| + friend scoped_refptr<U> AdoptRef(U*); |
| + |
| + 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.
|
| + scoped_refptr(T* p, AdoptRefTag) : ptr_(p) {} |
| + |
| // Friend required for move constructors that set r.ptr_ to null. |
| template <typename U> |
| friend class scoped_refptr; |
| @@ -416,6 +422,16 @@ scoped_refptr<T> make_scoped_refptr(T* t) { |
| return scoped_refptr<T>(t); |
| } |
| +// Creates a scoped_refptr from a raw pointer without incrementing the reference |
| +// count. Use this only for a newly created object whose reference count starts |
| +// from 1 instead of 0. |
| +template <typename T> |
| +scoped_refptr<T> AdoptRef(T* t) { |
| + DCHECK(!t || t->HasOneRef()); |
| + using Tag = typename scoped_refptr<T>::AdoptRefTag; |
| + return scoped_refptr<T>(t, Tag()); |
| +} |
| + |
| template <typename T, typename U> |
| bool operator==(const scoped_refptr<T>& lhs, const U* rhs) { |
| return lhs.get() == rhs; |