Changeset 36663 in webkit for trunk/JavaScriptCore/wtf/RefCounted.h
- Timestamp:
- Sep 19, 2008, 1:50:13 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/RefCounted.h
r36425 r36663 27 27 namespace WTF { 28 28 29 template<class T> class RefCounted : Noncopyable { 29 // This base class holds the non-template methods and attributes. 30 // The RefCounted class inherits from it reducing the template bloat 31 // generated by the compiler (technique called template hoisting). 32 class RefCountedBase : Noncopyable { 30 33 public: 31 RefCounted(int initialRefCount = 1)32 : m_refCount(initialRefCount)33 #ifndef NDEBUG34 , m_deletionHasBegun(false)35 #endif36 {37 }38 39 34 void ref() 40 35 { 41 36 ASSERT(!m_deletionHasBegun); 42 37 ++m_refCount; 43 }44 45 void deref()46 {47 ASSERT(!m_deletionHasBegun);48 ASSERT(m_refCount > 0);49 if (m_refCount == 1) {50 #ifndef NDEBUG51 m_deletionHasBegun = true;52 #endif53 delete static_cast<T*>(this);54 } else55 --m_refCount;56 38 } 57 39 … … 68 50 69 51 protected: 70 ~RefCounted() {} 52 RefCountedBase(int initialRefCount) 53 : m_refCount(initialRefCount) 54 #ifndef NDEBUG 55 , m_deletionHasBegun(false) 56 #endif 57 { 58 } 59 60 ~RefCountedBase() {} 61 62 // Returns whether the pointer should be freed or not. 63 bool derefBase() 64 { 65 ASSERT(!m_deletionHasBegun); 66 ASSERT(m_refCount > 0); 67 if (m_refCount == 1) { 68 #ifndef NDEBUG 69 m_deletionHasBegun = true; 70 #endif 71 return true; 72 } 73 74 --m_refCount; 75 return false; 76 } 71 77 72 78 private: … … 77 83 }; 78 84 85 86 template<class T> class RefCounted : public RefCountedBase { 87 public: 88 RefCounted(int initialRefCount = 1) 89 : RefCountedBase(initialRefCount) 90 { 91 } 92 93 void deref() 94 { 95 if (derefBase()) 96 delete static_cast<T*>(this); 97 } 98 99 protected: 100 ~RefCounted() {} 101 }; 102 79 103 } // namespace WTF 80 104
Note:
See TracChangeset
for help on using the changeset viewer.