OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/atomic_ref_count.h" | 9 #include "base/atomic_ref_count.h" |
10 #include "base/thread_collision_warner.h" | 10 #include "base/thread_collision_warner.h" |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 DISALLOW_COPY_AND_ASSIGN(RefCounted<T>); | 99 DISALLOW_COPY_AND_ASSIGN(RefCounted<T>); |
100 }; | 100 }; |
101 | 101 |
102 // Forward declaration. | 102 // Forward declaration. |
103 template <class T, typename Traits> class RefCountedThreadSafe; | 103 template <class T, typename Traits> class RefCountedThreadSafe; |
104 | 104 |
105 // Default traits for RefCountedThreadSafe<T>. Deletes the object when its ref | 105 // Default traits for RefCountedThreadSafe<T>. Deletes the object when its ref |
106 // count reaches 0. Overload to delete it on a different thread etc. | 106 // count reaches 0. Overload to delete it on a different thread etc. |
107 template<typename T> | 107 template<typename T> |
108 struct DefaultRefCountedThreadSafeTraits { | 108 struct DefaultRefCountedThreadSafeTraits { |
109 static void Destruct(T* x) { | 109 static void Destruct(const T* x) { |
110 // Delete through RefCountedThreadSafe to make child classes only need to be | 110 // Delete through RefCountedThreadSafe to make child classes only need to be |
111 // friend with RefCountedThreadSafe instead of this struct, which is an | 111 // friend with RefCountedThreadSafe instead of this struct, which is an |
112 // implementation detail. | 112 // implementation detail. |
113 RefCountedThreadSafe<T, | 113 RefCountedThreadSafe<T, |
114 DefaultRefCountedThreadSafeTraits>::DeleteInternal(x); | 114 DefaultRefCountedThreadSafeTraits>::DeleteInternal(x); |
115 } | 115 } |
116 }; | 116 }; |
117 | 117 |
118 // | 118 // |
119 // A thread-safe variant of RefCounted<T> | 119 // A thread-safe variant of RefCounted<T> |
120 // | 120 // |
121 // class MyFoo : public base::RefCountedThreadSafe<MyFoo> { | 121 // class MyFoo : public base::RefCountedThreadSafe<MyFoo> { |
122 // ... | 122 // ... |
123 // }; | 123 // }; |
124 // | 124 // |
125 // If you're using the default trait, then you should add compile time | 125 // If you're using the default trait, then you should add compile time |
126 // asserts that no one else is deleting your object. i.e. | 126 // asserts that no one else is deleting your object. i.e. |
127 // private: | 127 // private: |
128 // friend class base::RefCountedThreadSafe<MyFoo>; | 128 // friend class base::RefCountedThreadSafe<MyFoo>; |
129 // ~MyFoo(); | 129 // ~MyFoo(); |
130 template <class T, typename Traits = DefaultRefCountedThreadSafeTraits<T> > | 130 template <class T, typename Traits = DefaultRefCountedThreadSafeTraits<T> > |
131 class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase { | 131 class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase { |
132 public: | 132 public: |
133 RefCountedThreadSafe() { } | 133 RefCountedThreadSafe() { } |
134 ~RefCountedThreadSafe() { } | 134 ~RefCountedThreadSafe() { } |
135 | 135 |
136 void AddRef() { | 136 void AddRef() const { |
137 subtle::RefCountedThreadSafeBase::AddRef(); | 137 subtle::RefCountedThreadSafeBase::AddRef(); |
138 } | 138 } |
139 | 139 |
140 void Release() { | 140 void Release() const { |
141 if (subtle::RefCountedThreadSafeBase::Release()) { | 141 if (subtle::RefCountedThreadSafeBase::Release()) { |
142 Traits::Destruct(static_cast<T*>(this)); | 142 Traits::Destruct(static_cast<const T*>(this)); |
143 } | 143 } |
144 } | 144 } |
145 | 145 |
146 private: | 146 private: |
147 friend struct DefaultRefCountedThreadSafeTraits<T>; | 147 friend struct DefaultRefCountedThreadSafeTraits<T>; |
148 static void DeleteInternal(T* x) { delete x; } | 148 static void DeleteInternal(const T* x) { delete x; } |
149 | 149 |
150 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe); | 150 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe); |
151 }; | 151 }; |
152 | 152 |
153 // | 153 // |
154 // A wrapper for some piece of data so we can place other things in | 154 // A wrapper for some piece of data so we can place other things in |
155 // scoped_refptrs<>. | 155 // scoped_refptrs<>. |
156 // | 156 // |
157 template<typename T> | 157 template<typename T> |
158 class RefCountedData : public base::RefCounted< base::RefCountedData<T> > { | 158 class RefCountedData : public base::RefCounted< base::RefCountedData<T> > { |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 }; | 289 }; |
290 | 290 |
291 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without | 291 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without |
292 // having to retype all the template arguments | 292 // having to retype all the template arguments |
293 template <typename T> | 293 template <typename T> |
294 scoped_refptr<T> make_scoped_refptr(T* t) { | 294 scoped_refptr<T> make_scoped_refptr(T* t) { |
295 return scoped_refptr<T>(t); | 295 return scoped_refptr<T>(t); |
296 } | 296 } |
297 | 297 |
298 #endif // BASE_REF_COUNTED_H_ | 298 #endif // BASE_REF_COUNTED_H_ |
OLD | NEW |