小ネタ。
IUnknownを実装するクラス。
template <typename COMInterface>
struct IUnknownImpl : public COMInterface
{
IUnknownImpl() : ref_(1) {};
virtual ~IUnknownImpl() {};
ULONG __stdcall AddRef()
{
return InterlockedIncrement(&ref_);
}
ULONG __stdcall Release()
{
ULONG ref = InterlockedDecrement(&ref_);
if (0 == ref)
{
delete this;
}
return ref;
}
HRESULT __stdcall QueryInterface(REFIID riid, VOID **ppObj)
{
if (IID_IUnknown == riid)
{
AddRef();
*ppObj = (IUnknown*)this;
}
else if (__uuidof(COMInterface) == riid)
{
AddRef();
*ppObj = (COMInterface*)this;
}
else
{
*ppObj = NULL;
return E_NOINTERFACE;
}
return S_OK;
}
private:
LONG ref_;
};
<使い方>
struct MMNotificationClient : public IUnknownImpl<IMMNotificationClient>
{ ... }