diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2008-12-17 18:05:15 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2008-12-17 18:05:15 -0800 |
commit | 1cbdecfa9fc428ac2d8aca0fa91c9580b3d57353 (patch) | |
tree | 4457a7306ea5acb43fe05bfe0973b1f7faf97ba2 /JavaScriptCore/wtf/RefCounted.h | |
parent | 9364f22aed35e1a1e9d07c121510f80be3ab0502 (diff) | |
download | external_webkit-1cbdecfa9fc428ac2d8aca0fa91c9580b3d57353.zip external_webkit-1cbdecfa9fc428ac2d8aca0fa91c9580b3d57353.tar.gz external_webkit-1cbdecfa9fc428ac2d8aca0fa91c9580b3d57353.tar.bz2 |
Code drop from //branches/cupcake/...@124589
Diffstat (limited to 'JavaScriptCore/wtf/RefCounted.h')
-rw-r--r-- | JavaScriptCore/wtf/RefCounted.h | 69 |
1 files changed, 48 insertions, 21 deletions
diff --git a/JavaScriptCore/wtf/RefCounted.h b/JavaScriptCore/wtf/RefCounted.h index dc93b11..2dd5b2a 100644 --- a/JavaScriptCore/wtf/RefCounted.h +++ b/JavaScriptCore/wtf/RefCounted.h @@ -26,9 +26,30 @@ namespace WTF { -template<class T> class RefCounted : Noncopyable { +// This base class holds the non-template methods and attributes. +// The RefCounted class inherits from it reducing the template bloat +// generated by the compiler (technique called template hoisting). +class RefCountedBase : Noncopyable { public: - RefCounted(int initialRefCount = 0) + void ref() + { + ASSERT(!m_deletionHasBegun); + ++m_refCount; + } + + bool hasOneRef() const + { + ASSERT(!m_deletionHasBegun); + return m_refCount == 1; + } + + int refCount() const + { + return m_refCount; + } + +protected: + RefCountedBase(int initialRefCount) : m_refCount(initialRefCount) #ifndef NDEBUG , m_deletionHasBegun(false) @@ -36,13 +57,10 @@ public: { } - void ref() - { - ASSERT(!m_deletionHasBegun); - ++m_refCount; - } + ~RefCountedBase() {} - void deref() + // Returns whether the pointer should be freed or not. + bool derefBase() { ASSERT(!m_deletionHasBegun); ASSERT(m_refCount > 0); @@ -50,20 +68,11 @@ public: #ifndef NDEBUG m_deletionHasBegun = true; #endif - delete static_cast<T*>(this); - } else - --m_refCount; - } + return true; + } - bool hasOneRef() - { - ASSERT(!m_deletionHasBegun); - return m_refCount == 1; - } - - int refCount() const - { - return m_refCount; + --m_refCount; + return false; } private: @@ -73,6 +82,24 @@ private: #endif }; + +template<class T> class RefCounted : public RefCountedBase { +public: + RefCounted(int initialRefCount = 1) + : RefCountedBase(initialRefCount) + { + } + + void deref() + { + if (derefBase()) + delete static_cast<T*>(this); + } + +protected: + ~RefCounted() {} +}; + } // namespace WTF using WTF::RefCounted; |