#ifndef RefCounted_h
#define RefCounted_h
#include "Assertions.h"
#include "FastAllocBase.h"
#include "ThreadRestrictionVerifier.h"
#include "Noncopyable.h"
#include "OwnPtr.h"
#include "UnusedParam.h"
namespace WTF {
class RefCountedBase {
public:
void ref()
{
ASSERT(!m_deletionHasBegun);
ASSERT(!m_adoptionIsRequired);
++m_refCount;
}
bool hasOneRef() const
{
ASSERT(!m_deletionHasBegun);
return m_refCount == 1;
}
int refCount() const
{
return m_refCount;
}
void setMutexForVerifier(Mutex&);
#if HAVE(DISPATCH_H)
void setDispatchQueueForVerifier(dispatch_queue_t);
#endif
void deprecatedTurnOffVerifier()
{
}
void relaxAdoptionRequirement()
{
#ifndef NDEBUG
ASSERT(!m_deletionHasBegun);
ASSERT(m_adoptionIsRequired);
m_adoptionIsRequired = false;
#endif
}
const int* addressOfCount() const
{
return &m_refCount;
}
protected:
RefCountedBase()
: m_refCount(1)
#ifndef NDEBUG
, m_deletionHasBegun(false)
, m_adoptionIsRequired(true)
#endif
{
}
~RefCountedBase()
{
ASSERT(m_deletionHasBegun);
ASSERT(!m_adoptionIsRequired);
}
bool derefBase()
{
ASSERT(!m_deletionHasBegun);
ASSERT(!m_adoptionIsRequired);
ASSERT(m_refCount > 0);
if (m_refCount == 1) {
#ifndef NDEBUG
m_deletionHasBegun = true;
#endif
return true;
}
--m_refCount;
return false;
}
#ifndef NDEBUG
bool deletionHasBegun() const
{
return m_deletionHasBegun;
}
#endif
private:
#ifndef NDEBUG
friend void adopted(RefCountedBase*);
#endif
int m_refCount;
#ifndef NDEBUG
bool m_deletionHasBegun;
bool m_adoptionIsRequired;
#endif
};
#ifndef NDEBUG
inline void adopted(RefCountedBase* object)
{
if (!object)
return;
ASSERT(!object->m_deletionHasBegun);
object->m_adoptionIsRequired = false;
}
#endif
template<typename T> class RefCounted : public RefCountedBase {
WTF_MAKE_NONCOPYABLE(RefCounted); WTF_MAKE_FAST_ALLOCATED;
public:
void deref()
{
if (derefBase())
delete static_cast<T*>(this);
}
protected:
RefCounted() { }
~RefCounted()
{
}
};
template<typename T> class RefCountedCustomAllocated : public RefCountedBase {
WTF_MAKE_NONCOPYABLE(RefCountedCustomAllocated);
public:
void deref()
{
if (derefBase())
delete static_cast<T*>(this);
}
protected:
~RefCountedCustomAllocated()
{
}
};
inline void RefCountedBase::setMutexForVerifier(Mutex&) { }
#if HAVE(DISPATCH_H)
inline void RefCountedBase::setDispatchQueueForVerifier(dispatch_queue_t) { }
#endif // HAVE(DISPATCH_H)
}
using WTF::RefCounted;
using WTF::RefCountedCustomAllocated;
#endif // RefCounted_h