| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_MEMORY_REF_COUNTED_H_ | 5 #ifndef BASE_MEMORY_REF_COUNTED_H_ |
| 6 #define BASE_MEMORY_REF_COUNTED_H_ | 6 #define BASE_MEMORY_REF_COUNTED_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <cassert> | 9 #include <cassert> |
| 10 | 10 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 // private: | 78 // private: |
| 79 // friend class base::RefCounted<MyFoo>; | 79 // friend class base::RefCounted<MyFoo>; |
| 80 // ~MyFoo(); | 80 // ~MyFoo(); |
| 81 // }; | 81 // }; |
| 82 // | 82 // |
| 83 // You should always make your destructor private, to avoid any code deleting | 83 // You should always make your destructor private, to avoid any code deleting |
| 84 // the object accidently while there are references to it. | 84 // the object accidently while there are references to it. |
| 85 template <class T> | 85 template <class T> |
| 86 class RefCounted : public subtle::RefCountedBase { | 86 class RefCounted : public subtle::RefCountedBase { |
| 87 public: | 87 public: |
| 88 RefCounted() { } | 88 RefCounted() {} |
| 89 ~RefCounted() { } | |
| 90 | 89 |
| 91 void AddRef() const { | 90 void AddRef() const { |
| 92 subtle::RefCountedBase::AddRef(); | 91 subtle::RefCountedBase::AddRef(); |
| 93 } | 92 } |
| 94 | 93 |
| 95 void Release() const { | 94 void Release() const { |
| 96 if (subtle::RefCountedBase::Release()) { | 95 if (subtle::RefCountedBase::Release()) { |
| 97 delete static_cast<const T*>(this); | 96 delete static_cast<const T*>(this); |
| 98 } | 97 } |
| 99 } | 98 } |
| 100 | 99 |
| 100 protected: |
| 101 ~RefCounted() {} |
| 102 |
| 101 private: | 103 private: |
| 102 DISALLOW_COPY_AND_ASSIGN(RefCounted<T>); | 104 DISALLOW_COPY_AND_ASSIGN(RefCounted<T>); |
| 103 }; | 105 }; |
| 104 | 106 |
| 105 // Forward declaration. | 107 // Forward declaration. |
| 106 template <class T, typename Traits> class RefCountedThreadSafe; | 108 template <class T, typename Traits> class RefCountedThreadSafe; |
| 107 | 109 |
| 108 // Default traits for RefCountedThreadSafe<T>. Deletes the object when its ref | 110 // Default traits for RefCountedThreadSafe<T>. Deletes the object when its ref |
| 109 // count reaches 0. Overload to delete it on a different thread etc. | 111 // count reaches 0. Overload to delete it on a different thread etc. |
| 110 template<typename T> | 112 template<typename T> |
| (...skipping 15 matching lines...) Expand all Loading... |
| 126 // }; | 128 // }; |
| 127 // | 129 // |
| 128 // If you're using the default trait, then you should add compile time | 130 // If you're using the default trait, then you should add compile time |
| 129 // asserts that no one else is deleting your object. i.e. | 131 // asserts that no one else is deleting your object. i.e. |
| 130 // private: | 132 // private: |
| 131 // friend class base::RefCountedThreadSafe<MyFoo>; | 133 // friend class base::RefCountedThreadSafe<MyFoo>; |
| 132 // ~MyFoo(); | 134 // ~MyFoo(); |
| 133 template <class T, typename Traits = DefaultRefCountedThreadSafeTraits<T> > | 135 template <class T, typename Traits = DefaultRefCountedThreadSafeTraits<T> > |
| 134 class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase { | 136 class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase { |
| 135 public: | 137 public: |
| 136 RefCountedThreadSafe() { } | 138 RefCountedThreadSafe() {} |
| 137 ~RefCountedThreadSafe() { } | |
| 138 | 139 |
| 139 void AddRef() const { | 140 void AddRef() const { |
| 140 subtle::RefCountedThreadSafeBase::AddRef(); | 141 subtle::RefCountedThreadSafeBase::AddRef(); |
| 141 } | 142 } |
| 142 | 143 |
| 143 void Release() const { | 144 void Release() const { |
| 144 if (subtle::RefCountedThreadSafeBase::Release()) { | 145 if (subtle::RefCountedThreadSafeBase::Release()) { |
| 145 Traits::Destruct(static_cast<const T*>(this)); | 146 Traits::Destruct(static_cast<const T*>(this)); |
| 146 } | 147 } |
| 147 } | 148 } |
| 148 | 149 |
| 150 protected: |
| 151 ~RefCountedThreadSafe() {} |
| 152 |
| 149 private: | 153 private: |
| 150 friend struct DefaultRefCountedThreadSafeTraits<T>; | 154 friend struct DefaultRefCountedThreadSafeTraits<T>; |
| 151 static void DeleteInternal(const T* x) { delete x; } | 155 static void DeleteInternal(const T* x) { delete x; } |
| 152 | 156 |
| 153 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe); | 157 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe); |
| 154 }; | 158 }; |
| 155 | 159 |
| 156 // | 160 // |
| 157 // A wrapper for some piece of data so we can place other things in | 161 // A wrapper for some piece of data so we can place other things in |
| 158 // scoped_refptrs<>. | 162 // scoped_refptrs<>. |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 }; | 306 }; |
| 303 | 307 |
| 304 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without | 308 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without |
| 305 // having to retype all the template arguments | 309 // having to retype all the template arguments |
| 306 template <typename T> | 310 template <typename T> |
| 307 scoped_refptr<T> make_scoped_refptr(T* t) { | 311 scoped_refptr<T> make_scoped_refptr(T* t) { |
| 308 return scoped_refptr<T>(t); | 312 return scoped_refptr<T>(t); |
| 309 } | 313 } |
| 310 | 314 |
| 311 #endif // BASE_MEMORY_REF_COUNTED_H_ | 315 #endif // BASE_MEMORY_REF_COUNTED_H_ |
| OLD | NEW |