OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_REF_COUNTED_H_ | 5 #ifndef BASE_REF_COUNTED_H_ |
6 #define BASE_REF_COUNTED_H_ | 6 #define BASE_REF_COUNTED_H_ |
7 | 7 |
8 #include "base/atomic_ref_count.h" | 8 #include "base/atomic_ref_count.h" |
9 #include "base/thread_collision_warner.h" | 9 #include "base/thread_collision_warner.h" |
10 | 10 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 | 64 |
65 } // namespace subtle | 65 } // namespace subtle |
66 | 66 |
67 // | 67 // |
68 // A base class for reference counted classes. Otherwise, known as a cheap | 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 | 69 // knock-off of WebKit's RefCounted<T> class. To use this guy just extend your |
70 // class from it like so: | 70 // class from it like so: |
71 // | 71 // |
72 // class MyFoo : public base::RefCounted<MyFoo> { | 72 // class MyFoo : public base::RefCounted<MyFoo> { |
73 // ... | 73 // ... |
| 74 // private: |
| 75 // friend class base::RefCounted<MyFoo>; |
| 76 // ~MyFoo(); |
74 // }; | 77 // }; |
75 // | 78 // |
| 79 // You should always make your destructor private, to avoid any code deleting |
| 80 // the object accidently while there are references to it. |
76 template <class T> | 81 template <class T> |
77 class RefCounted : public subtle::RefCountedBase { | 82 class RefCounted : public subtle::RefCountedBase { |
78 public: | 83 public: |
79 RefCounted() { } | 84 RefCounted() { } |
80 ~RefCounted() { } | 85 ~RefCounted() { } |
81 | 86 |
82 void AddRef() { | 87 void AddRef() { |
83 subtle::RefCountedBase::AddRef(); | 88 subtle::RefCountedBase::AddRef(); |
84 } | 89 } |
85 | 90 |
(...skipping 22 matching lines...) Expand all Loading... |
108 } | 113 } |
109 }; | 114 }; |
110 | 115 |
111 // | 116 // |
112 // A thread-safe variant of RefCounted<T> | 117 // A thread-safe variant of RefCounted<T> |
113 // | 118 // |
114 // class MyFoo : public base::RefCountedThreadSafe<MyFoo> { | 119 // class MyFoo : public base::RefCountedThreadSafe<MyFoo> { |
115 // ... | 120 // ... |
116 // }; | 121 // }; |
117 // | 122 // |
118 // If you're using the default trait, then you may choose to add compile time | 123 // If you're using the default trait, then you should add compile time |
119 // asserts that no one else is deleting your object. i.e. | 124 // asserts that no one else is deleting your object. i.e. |
120 // private: | 125 // private: |
121 // friend struct base::RefCountedThreadSafe<MyFoo>; | 126 // friend class base::RefCountedThreadSafe<MyFoo>; |
122 // ~MyFoo(); | 127 // ~MyFoo(); |
123 template <class T, typename Traits = DefaultRefCountedThreadSafeTraits<T> > | 128 template <class T, typename Traits = DefaultRefCountedThreadSafeTraits<T> > |
124 class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase { | 129 class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase { |
125 public: | 130 public: |
126 RefCountedThreadSafe() { } | 131 RefCountedThreadSafe() { } |
127 ~RefCountedThreadSafe() { } | 132 ~RefCountedThreadSafe() { } |
128 | 133 |
129 void AddRef() { | 134 void AddRef() { |
130 subtle::RefCountedThreadSafeBase::AddRef(); | 135 subtle::RefCountedThreadSafeBase::AddRef(); |
131 } | 136 } |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 | 269 |
265 void swap(scoped_refptr<T>& r) { | 270 void swap(scoped_refptr<T>& r) { |
266 swap(&r.ptr_); | 271 swap(&r.ptr_); |
267 } | 272 } |
268 | 273 |
269 protected: | 274 protected: |
270 T* ptr_; | 275 T* ptr_; |
271 }; | 276 }; |
272 | 277 |
273 #endif // BASE_REF_COUNTED_H_ | 278 #endif // BASE_REF_COUNTED_H_ |
OLD | NEW |