1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_MEMORY_REF_COUNTED_H_
6 #define BASE_MEMORY_REF_COUNTED_H_
9 #include "base/atomic_ref_count.h"
10 #include "base/base_export.h"
11 #include "base/threading/thread_collision_warner.h"
17 class BASE_EXPORT RefCountedBase {
19 static bool ImplementsThreadSafeReferenceCounting() { return false; }
21 bool HasOneRef() const { return ref_count_ == 1; }
29 // Returns true if the object should self-delete.
33 mutable int ref_count_;
35 mutable bool in_dtor_;
38 DFAKE_MUTEX(add_release_);
40 DISALLOW_COPY_AND_ASSIGN(RefCountedBase);
43 class BASE_EXPORT RefCountedThreadSafeBase {
45 static bool ImplementsThreadSafeReferenceCounting() { return true; }
47 bool HasOneRef() const;
50 RefCountedThreadSafeBase();
51 ~RefCountedThreadSafeBase();
55 // Returns true if the object should self-delete.
59 mutable AtomicRefCount ref_count_;
61 mutable bool in_dtor_;
64 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase);
70 // A base class for reference counted classes. Otherwise, known as a cheap
71 // knock-off of WebKit's RefCounted<T> class. To use this guy just extend your
72 // class from it like so:
74 // class MyFoo : public base::RefCounted<MyFoo> {
77 // friend class base::RefCounted<MyFoo>;
81 // You should always make your destructor private, to avoid any code deleting
82 // the object accidently while there are references to it.
84 class RefCounted : public subtle::RefCountedBase {
90 subtle::RefCountedBase::AddRef();
93 void Release() const {
94 if (subtle::RefCountedBase::Release()) {
95 delete static_cast<const T*>(this);
100 DISALLOW_COPY_AND_ASSIGN(RefCounted<T>);
103 // Forward declaration.
104 template <class T, typename Traits> class RefCountedThreadSafe;
106 // Default traits for RefCountedThreadSafe<T>. Deletes the object when its ref
107 // count reaches 0. Overload to delete it on a different thread etc.
109 struct DefaultRefCountedThreadSafeTraits {
110 static void Destruct(const T* x) {
111 // Delete through RefCountedThreadSafe to make child classes only need to be
112 // friend with RefCountedThreadSafe instead of this struct, which is an
113 // implementation detail.
114 RefCountedThreadSafe<T,
115 DefaultRefCountedThreadSafeTraits>::DeleteInternal(x);
120 // A thread-safe variant of RefCounted<T>
122 // class MyFoo : public base::RefCountedThreadSafe<MyFoo> {
126 // If you're using the default trait, then you should add compile time
127 // asserts that no one else is deleting your object. i.e.
129 // friend class base::RefCountedThreadSafe<MyFoo>;
131 template <class T, typename Traits = DefaultRefCountedThreadSafeTraits<T> >
132 class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase {
134 RefCountedThreadSafe() { }
135 ~RefCountedThreadSafe() { }
137 void AddRef() const {
138 subtle::RefCountedThreadSafeBase::AddRef();
141 void Release() const {
142 if (subtle::RefCountedThreadSafeBase::Release()) {
143 Traits::Destruct(static_cast<const T*>(this));
148 friend struct DefaultRefCountedThreadSafeTraits<T>;
149 static void DeleteInternal(const T* x) { delete x; }
151 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe);
155 // A wrapper for some piece of data so we can place other things in
159 class RefCountedData : public base::RefCounted< base::RefCountedData<T> > {
161 RefCountedData() : data() {}
162 RefCountedData(const T& in_value) : data(in_value) {}
170 // A smart pointer class for reference counted objects. Use this class instead
171 // of calling AddRef and Release manually on a reference counted object to
172 // avoid common memory leaks caused by forgetting to Release an object
173 // reference. Sample usage:
175 // class MyFoo : public RefCounted<MyFoo> {
179 // void some_function() {
180 // scoped_refptr<MyFoo> foo = new MyFoo();
181 // foo->Method(param);
182 // // |foo| is released when this function returns
185 // void some_other_function() {
186 // scoped_refptr<MyFoo> foo = new MyFoo();
188 // foo = NULL; // explicitly releases |foo|
191 // foo->Method(param);
194 // The above examples show how scoped_refptr<T> acts like a pointer to T.
195 // Given two scoped_refptr<T> classes, it is also possible to exchange
196 // references between the two objects, like so:
199 // scoped_refptr<MyFoo> a = new MyFoo();
200 // scoped_refptr<MyFoo> b;
203 // // now, |b| references the MyFoo object, and |a| references NULL.
206 // To make both |a| and |b| in the above example reference the same MyFoo
207 // object, simply use the assignment operator:
210 // scoped_refptr<MyFoo> a = new MyFoo();
211 // scoped_refptr<MyFoo> b;
214 // // now, |a| and |b| each own a reference to the same MyFoo object.
218 class scoped_refptr {
220 scoped_refptr() : ptr_(NULL) {
223 scoped_refptr(T* p) : ptr_(p) {
228 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) {
233 template <typename U>
234 scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) {
244 T* get() const { return ptr_; }
245 operator T*() const { return ptr_; }
246 T* operator->() const { return ptr_; }
248 // Release a pointer.
249 // The return value is the current pointer held by this object.
250 // If this object holds a NULL pointer, the return value is NULL.
251 // After this operation, this object will hold a NULL pointer,
252 // and will not own the object any more.
259 scoped_refptr<T>& operator=(T* p) {
260 // AddRef first so that self assignment should work
269 scoped_refptr<T>& operator=(const scoped_refptr<T>& r) {
270 return *this = r.ptr_;
273 template <typename U>
274 scoped_refptr<T>& operator=(const scoped_refptr<U>& r) {
275 return *this = r.get();
284 void swap(scoped_refptr<T>& r) {
292 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without
293 // having to retype all the template arguments
294 template <typename T>
295 scoped_refptr<T> make_scoped_refptr(T* t) {
296 return scoped_refptr<T>(t);
299 #endif // BASE_MEMORY_REF_COUNTED_H_