#ifndef RefCounted_h
#define RefCounted_h
#include <wtf/Assertions.h>
#include <wtf/Noncopyable.h>
namespace WTF {
class RefCountedBase {
public:
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()
: m_refCount(1)
#ifndef NDEBUG
, m_deletionHasBegun(false)
#endif
{
}
~RefCountedBase()
{
}
bool derefBase()
{
ASSERT(!m_deletionHasBegun);
ASSERT(m_refCount > 0);
if (m_refCount == 1) {
#ifndef NDEBUG
m_deletionHasBegun = true;
#endif
return true;
}
--m_refCount;
return false;
}
int* addressOfCount()
{
return &m_refCount;
}
#ifndef NDEBUG
bool deletionHasBegun() const
{
return m_deletionHasBegun;
}
#endif
private:
template<class T>
friend class CrossThreadRefCounted;
int m_refCount;
#ifndef NDEBUG
bool m_deletionHasBegun;
#endif
};
template<class T> class RefCounted : public RefCountedBase, public Noncopyable {
public:
void deref()
{
if (derefBase())
delete static_cast<T*>(this);
}
protected:
~RefCounted()
{
}
};
template<class T> class RefCountedCustomAllocated : public RefCountedBase, public NoncopyableCustomAllocated {
public:
void deref()
{
if (derefBase())
delete static_cast<T*>(this);
}
protected:
~RefCountedCustomAllocated()
{
}
};
}
using WTF::RefCounted;
using WTF::RefCountedCustomAllocated;
#endif // RefCounted_h