forked from tapetums/include
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassFactory.hpp
150 lines (113 loc) · 3.31 KB
/
ClassFactory.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#pragma once
//---------------------------------------------------------------------------//
//
// ClassFactory.hpp
//
//---------------------------------------------------------------------------//
#include <windows.h>
//---------------------------------------------------------------------------//
extern ULONG g_cLocks = 0;
//---------------------------------------------------------------------------//
template <class T>
class CClassFactory : public IClassFactory
{
public:
CClassFactory();
~CClassFactory();
public:
HRESULT __stdcall QueryInterface(REFIID riid, void** ppvObject) override;
ULONG __stdcall AddRef() override;
ULONG __stdcall Release() override;
HRESULT __stdcall CreateInstance(IUnknown* pUnkOuter, REFIID riid, void** ppvObject) override;
HRESULT __stdcall LockServer(BOOL fLock) override;
private:
CClassFactory(const CClassFactory&) = delete;
CClassFactory(CClassFactory&&) = delete;
CClassFactory& operator =(const CClassFactory&) = delete;
CClassFactory& operator =(CClassFactory&&) = delete;
};
//---------------------------------------------------------------------------//
template <class T> CClassFactory<T>::CClassFactory()
{
static_assert(std::is_base_of<IUnknown, T>::value, "<T> needs to be IUnknown based");
}
//---------------------------------------------------------------------------//
template <class T> CClassFactory<T>::~CClassFactory()
{
}
//---------------------------------------------------------------------------//
template <class T> STDMETHODIMP CClassFactory<T>::QueryInterface
(
REFIID riid,
void** ppvObject
)
{
if ( nullptr == ppvObject )
{
return E_POINTER;
}
*ppvObject = nullptr;
if ( IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory) )
{
*ppvObject = static_cast<IClassFactory*>(this);
}
else
{
return E_NOINTERFACE;
}
this->AddRef();
return S_OK;
}
//---------------------------------------------------------------------------//
template <class T> STDMETHODIMP_(ULONG) CClassFactory<T>::AddRef()
{
this->LockServer(TRUE);
return 2;
}
//---------------------------------------------------------------------------//
template <class T> STDMETHODIMP_(ULONG) CClassFactory<T>::Release()
{
this->LockServer(FALSE);
return 1;
}
//---------------------------------------------------------------------------//
template<class T> STDMETHODIMP CClassFactory<T>::CreateInstance
(
IUnknown* pUnkOuter,
REFIID riid,
void** ppvObject
)
{
UNREFERENCED_PARAMETER(pUnkOuter);
if ( nullptr == ppvObject )
{
return E_POINTER;
}
*ppvObject = nullptr;
const auto comp = new T;
if ( nullptr == comp )
{
return E_OUTOFMEMORY;
}
const auto hr = comp->QueryInterface(riid, ppvObject);
comp->Release();
return hr;
}
//---------------------------------------------------------------------------//
template <class T> STDMETHODIMP CClassFactory<T>::LockServer
(
BOOL bLock
)
{
if ( bLock )
{
::InterlockedIncrement(&g_cLocks);
}
else
{
::InterlockedDecrement(&g_cLocks);
}
return S_OK;
}
//---------------------------------------------------------------------------//
// ClassFactory.hpp