#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()
{
    EnterCriticalSection(cs);
    ++ref;
    LeaveCriticalSection(cs);
    return this;
}

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