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
3 // found in the LICENSE file.
5 #ifndef BASE_MEMORY_REF_COUNTED_H_
6 #define BASE_MEMORY_REF_COUNTED_H_
10 #include "base/atomic_ref_count.h"
11 #include "base/base_export.h"
12 #include "base/compiler_specific.h"
13 #include "base/threading/thread_collision_warner.h"
19 class BASE_EXPORT RefCountedBase
{
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 bool HasOneRef() const;
48 RefCountedThreadSafeBase();
49 ~RefCountedThreadSafeBase();
53 // Returns true if the object should self-delete.
57 mutable AtomicRefCount ref_count_
;
59 mutable bool in_dtor_
;
62 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase
);
68 // A base class for reference counted classes. Otherwise, known as a cheap
69 // knock-off of WebKit's RefCounted<T> class. To use this guy just extend your
70 // class from it like so:
72 // class MyFoo : public base::RefCounted<MyFoo> {
75 // friend class base::RefCounted<MyFoo>;
79 // You should always make your destructor private, to avoid any code deleting
80 // the object accidently while there are references to it.
82 class RefCounted
: public subtle::RefCountedBase
{
87 subtle::RefCountedBase::AddRef();
90 void Release() const {
91 if (subtle::RefCountedBase::Release()) {
92 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() {}
136 void AddRef() const {
137 subtle::RefCountedThreadSafeBase::AddRef();
140 void Release() const {
141 if (subtle::RefCountedThreadSafeBase::Release()) {
142 Traits::Destruct(static_cast<const T
*>(this));
147 ~RefCountedThreadSafe() {}
150 friend struct DefaultRefCountedThreadSafeTraits
<T
>;
151 static void DeleteInternal(const T
* x
) { delete x
; }
153 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe
);
157 // A thread-safe wrapper for some piece of data so we can place other
158 // things in scoped_refptrs<>.
162 : public base::RefCountedThreadSafe
< base::RefCountedData
<T
> > {
164 RefCountedData() : data() {}
165 RefCountedData(const T
& in_value
) : data(in_value
) {}
170 friend class base::RefCountedThreadSafe
<base::RefCountedData
<T
> >;
177 // A smart pointer class for reference counted objects. Use this class instead
178 // of calling AddRef and Release manually on a reference counted object to
179 // avoid common memory leaks caused by forgetting to Release an object
180 // reference. Sample usage:
182 // class MyFoo : public RefCounted<MyFoo> {
186 // void some_function() {
187 // scoped_refptr<MyFoo> foo = new MyFoo();
188 // foo->Method(param);
189 // // |foo| is released when this function returns
192 // void some_other_function() {
193 // scoped_refptr<MyFoo> foo = new MyFoo();
195 // foo = NULL; // explicitly releases |foo|
198 // foo->Method(param);
201 // The above examples show how scoped_refptr<T> acts like a pointer to T.
202 // Given two scoped_refptr<T> classes, it is also possible to exchange
203 // references between the two objects, like so:
206 // scoped_refptr<MyFoo> a = new MyFoo();
207 // scoped_refptr<MyFoo> b;
210 // // now, |b| references the MyFoo object, and |a| references NULL.
213 // To make both |a| and |b| in the above example reference the same MyFoo
214 // object, simply use the assignment operator:
217 // scoped_refptr<MyFoo> a = new MyFoo();
218 // scoped_refptr<MyFoo> b;
221 // // now, |a| and |b| each own a reference to the same MyFoo object.
225 class scoped_refptr
{
227 typedef T element_type
;
229 scoped_refptr() : ptr_(NULL
) {
232 scoped_refptr(T
* p
) : ptr_(p
) {
237 scoped_refptr(const scoped_refptr
<T
>& r
) : ptr_(r
.ptr_
) {
242 template <typename U
>
243 scoped_refptr(const scoped_refptr
<U
>& r
) : ptr_(r
.get()) {
253 T
* get() const { return ptr_
; }
254 operator T
*() const { return ptr_
; }
255 T
* operator->() const {
256 assert(ptr_
!= NULL
);
260 scoped_refptr
<T
>& operator=(T
* p
) {
261 // AddRef first so that self assignment should work
271 scoped_refptr
<T
>& operator=(const scoped_refptr
<T
>& r
) {
272 return *this = r
.ptr_
;
275 template <typename U
>
276 scoped_refptr
<T
>& operator=(const scoped_refptr
<U
>& r
) {
277 return *this = r
.get();
286 void swap(scoped_refptr
<T
>& r
) {
294 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without
295 // having to retype all the template arguments
296 template <typename T
>
297 scoped_refptr
<T
> make_scoped_refptr(T
* t
) {
298 return scoped_refptr
<T
>(t
);
301 #endif // BASE_MEMORY_REF_COUNTED_H_