WTF
RefCounted.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef RefCounted_h
00022 #define RefCounted_h
00023
00024 #include <wtf/Assertions.h>
00025 #include <wtf/Noncopyable.h>
00026
00027 namespace WTF {
00028
00029 template<class T> class RefCounted : Noncopyable {
00030 public:
00031 RefCounted(int initialRefCount = 1)
00032 : m_refCount(initialRefCount)
00033 #ifndef NDEBUG
00034 , m_deletionHasBegun(false)
00035 #endif
00036 {
00037 }
00038
00039 void ref()
00040 {
00041 ASSERT(!m_deletionHasBegun);
00042 ++m_refCount;
00043 }
00044
00045 void deref()
00046 {
00047 ASSERT(!m_deletionHasBegun);
00048 ASSERT(m_refCount > 0);
00049 if (m_refCount == 1) {
00050 #ifndef NDEBUG
00051 m_deletionHasBegun = true;
00052 #endif
00053 delete static_cast<T*>(this);
00054 } else
00055 --m_refCount;
00056 }
00057
00058 bool hasOneRef()
00059 {
00060 ASSERT(!m_deletionHasBegun);
00061 return m_refCount == 1;
00062 }
00063
00064 int refCount() const
00065 {
00066 return m_refCount;
00067 }
00068
00069 private:
00070 int m_refCount;
00071 #ifndef NDEBUG
00072 bool m_deletionHasBegun;
00073 #endif
00074 };
00075
00076 }
00077
00078 using WTF::RefCounted;
00079
00080 #endif // RefCounted_h