#pragma once

#include <apiTypes.h>


//! Helper implements IUnknown interface.
template <typename T>
class IUnknownImpl : public T
{
public:

    IUnknownImpl(): counter(0) {}

    virtual ~IUnknownImpl() {}

    virtual BOOL isOurRIID(REFIID riid) 
    {
        return false;        
    }

    virtual HRESULT WINAPI QueryInterface(REFIID riid, LPVOID* ppvObj) 
    {
        if (!ppvObj)
            return E_POINTER;

        if (EqualGUID(IID_IUnknown, riid) || isOurRIID(riid))
        {
            *ppvObj = this;
            AddRef();
            return S_OK;
        }

        return E_NOINTERFACE;
    }

    virtual ULONG WINAPI AddRef(void)
    { 
        return ++counter; 
    }

    virtual ULONG WINAPI Release(void) 
    {
        ULONG reference_count = --counter;
        if (reference_count == 0)
            delete this;
        return reference_count;
    }

private:

    ULONG counter;
};
