#include "ReferenceCounter.h"

using namespace Framework;

ReferenceCounter::ReferenceCounter()
{
    cs = new CRITICAL_SECTION();
    InitializeCriticalSection(cs);
    ref = 1;
}

ReferenceCounter::~ReferenceCounter()
{
    DeleteCriticalSection(cs);
    delete cs;
}

int ReferenceCounter::getReferenceCount() const
{
    return ref;
}

ReferenceCounter* ReferenceCounter::getThis() const
{
    EnterCriticalSection(cs);
    ++ref;
    LeaveCriticalSection(cs);
    return (ReferenceCounter*)this;
}

void* ReferenceCounter::release()
{
    EnterCriticalSection(cs);
    --ref;
    LeaveCriticalSection(cs);
    if (!ref) delete this;
    return 0;
}