-
Notifications
You must be signed in to change notification settings - Fork 1
/
compointer.h
251 lines (206 loc) · 4.04 KB
/
compointer.h
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* This file is part of the KDE project.
Copyright (C) 2009 Tim Boundy
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2.1 or 3 of the License.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PHONON_MF_COMPOINTER_H
#define PHONON_MF_COMPOINTER_H
#include <windows.h>
namespace Phonon
{
namespace MF
{
template <typename PtrType, typename SizeType = UINT32>
class ComArray
{
public:
ComArray() : m_array(0), m_count(0)
{
}
~ComArray()
{
Release();
}
void Release()
{
if (m_array)
{
for (SizeType i = 0; i < m_count; i++)
{
m_array[i]->Release();
}
CoTaskMemFree(m_array);
}
m_count = 0;
m_array = 0;
}
PtrType* operator[](int i)
{
return m_array[i];
}
SizeType size()
{
return m_count;
}
PtrType*** pp()
{
Release();
return &m_array;
}
quint32* pc()
{
return &m_count;
}
private:
PtrType** m_array;
SizeType m_count;
// Copying not yet implemented
ComArray(ComArray<PtrType, SizeType>& other);
ComArray& operator =(ComArray<PtrType, SizeType>& other);
};
// Prevents accidental calling of AddRef and Release when encapsulated by ComPointer
template <typename T>
class HideAddRefRelease : public T
{
private:
STDMETHOD_(ULONG, AddRef)() = 0;
STDMETHOD_(ULONG, Release)() = 0;
};
template <typename T>
class ComPointer
{
public:
explicit ComPointer(T* p = 0) : m_p(p)
{
if (m_p)
{
m_p->AddRef();
}
}
ComPointer(const ComPointer<T>& other) : m_p(0)
{
if (other)
{
other->QueryInterface(__uuidof(T), reinterpret_cast<void**>(&m_p));
}
}
// QueryInterface initialising
template <typename U>
explicit ComPointer(U* p) : m_p(0)
{
if (p)
{
p->QueryInterface(__uuidof(T), reinterpret_cast<void**>(&m_p));
}
}
template <typename U>
explicit ComPointer(const ComPointer<U>& other) : m_p(0)
{
if (other)
{
other->QueryInterface(__uuidof(T), reinterpret_cast<void**>(&m_p));
}
}
~ComPointer()
{
if (m_p)
{
m_p->Release();
}
}
T* operator=(T* other)
{
if (m_p != other)
{
if (other)
{
other->AddRef();
}
if (m_p)
{
m_p->Release();
}
m_p = other;
}
return m_p;
}
template <typename U>
T* operator=(U* other)
{
// Operate on a temp object and query before releasing in case the two interfaces refer to the same object
T* temp = 0;
if (other)
{
other->QueryInterface(__uuidof(T), reinterpret_cast<void**>(&temp));
}
if (m_p)
{
m_p->Release();
}
m_p = temp;
return m_p;
}
ComPointer<T>& operator=(const ComPointer<T>& other)
{
operator=((T*)other);
return *this;
}
template <typename U>
ComPointer<T>& operator=(const ComPointer<U>& other)
{
operator=((U*)other);
return *this;
}
operator T*() const
{
return m_p;
}
HideAddRefRelease<T>* operator->() const
{
// Somewhat dirty hack to hide AddRef and Release from interface
return (HideAddRefRelease<T>*)m_p;
}
bool operator!() const
{
return m_p == 0;
}
T** p()
{
Release();
return &m_p;
}
void Release()
{
if (m_p != 0)
{
m_p->Release();
}
m_p = 0;
}
void Attach(T* p)
{
if (m_p)
{
m_p->Release();
}
m_p = p;
}
T* Detach()
{
T* temp = m_p;
m_p = 0;
return temp;
}
private:
T* m_p;
};
}
}
#endif // PHONON_MF_COMPOINTER_H