Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Unified Diff: base/memory/ref_counted.h

Issue 2723423002: Start BindStateBase ref count from 1 instead of 0 (Closed)
Patch Set: +comment. +DCHECK. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/callback_internal.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
« no previous file with comments | « base/callback_internal.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698