aboutsummaryrefslogtreecommitdiff
path: root/CPP/Common/MyCom.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--CPP/Common/MyCom.h370
1 files changed, 286 insertions, 84 deletions
diff --git a/CPP/Common/MyCom.h b/CPP/Common/MyCom.h
index ff54278..65c4330 100644
--- a/CPP/Common/MyCom.h
+++ b/CPP/Common/MyCom.h
@@ -1,15 +1,11 @@
1// MyCom.h 1// MyCom.h
2 2
3#ifndef __MY_COM_H 3#ifndef ZIP7_INC_MY_COM_H
4#define __MY_COM_H 4#define ZIP7_INC_MY_COM_H
5 5
6#include "MyWindows.h" 6#include "MyWindows.h"
7#include "MyTypes.h" 7#include "MyTypes.h"
8 8
9#ifndef RINOK
10#define RINOK(x) { HRESULT __result_ = (x); if (__result_ != S_OK) return __result_; }
11#endif
12
13template <class T> 9template <class T>
14class CMyComPtr 10class CMyComPtr
15{ 11{
@@ -72,6 +68,10 @@ public:
72 } 68 }
73}; 69};
74 70
71#define Z7_DECL_CMyComPtr_QI_FROM(i, v, unk) \
72 CMyComPtr<i> v; (unk)->QueryInterface(IID_ ## i, (void **)&v);
73
74
75////////////////////////////////////////////////////////// 75//////////////////////////////////////////////////////////
76 76
77inline HRESULT StringToBstr(LPCOLESTR src, BSTR *bstr) 77inline HRESULT StringToBstr(LPCOLESTR src, BSTR *bstr)
@@ -83,7 +83,7 @@ inline HRESULT StringToBstr(LPCOLESTR src, BSTR *bstr)
83class CMyComBSTR 83class CMyComBSTR
84{ 84{
85 BSTR m_str; 85 BSTR m_str;
86 CLASS_NO_COPY(CMyComBSTR) 86 Z7_CLASS_NO_COPY(CMyComBSTR)
87public: 87public:
88 CMyComBSTR(): m_str(NULL) {} 88 CMyComBSTR(): m_str(NULL) {}
89 ~CMyComBSTR() { ::SysFreeString(m_str); } 89 ~CMyComBSTR() { ::SysFreeString(m_str); }
@@ -174,7 +174,7 @@ private:
174 174
175class CMyComBSTR_Wipe: public CMyComBSTR 175class CMyComBSTR_Wipe: public CMyComBSTR
176{ 176{
177 CLASS_NO_COPY(CMyComBSTR_Wipe) 177 Z7_CLASS_NO_COPY(CMyComBSTR_Wipe)
178public: 178public:
179 CMyComBSTR_Wipe(): CMyComBSTR() {} 179 CMyComBSTR_Wipe(): CMyComBSTR() {}
180 ~CMyComBSTR_Wipe() { Wipe_and_Free(); } 180 ~CMyComBSTR_Wipe() { Wipe_and_Free(); }
@@ -185,123 +185,325 @@ public:
185/* 185/*
186 If CMyUnknownImp doesn't use virtual destructor, the code size is smaller. 186 If CMyUnknownImp doesn't use virtual destructor, the code size is smaller.
187 But if some class_1 derived from CMyUnknownImp 187 But if some class_1 derived from CMyUnknownImp
188 uses MY_ADDREF_RELEASE and IUnknown::Release() 188 uses Z7_COM_ADDREF_RELEASE and IUnknown::Release()
189 and some another class_2 is derived from class_1, 189 and some another class_2 is derived from class_1,
190 then class_1 must use virtual destructor: 190 then class_1 must use virtual destructor:
191 virtual ~class_1(); 191 virtual ~class_1();
192 In that case, class_1::Release() calls correct destructor of class_2. 192 In that case, class_1::Release() calls correct destructor of class_2.
193 193 We can use virtual ~CMyUnknownImp() to disable warning
194 We use virtual ~CMyUnknownImp() to disable warning
195 "class has virtual functions, but destructor is not virtual". 194 "class has virtual functions, but destructor is not virtual".
196 195 Also we can use virtual ~IUnknown() {} in MyWindows.h
197 also we can use virtual ~IUnknown() {} in MyWindows.h
198*/ 196*/
199 197
200class CMyUnknownImp 198class CMyUnknownImp
201{ 199{
202 CLASS_NO_COPY(CMyUnknownImp) 200 Z7_CLASS_NO_COPY(CMyUnknownImp)
203public: 201protected:
204 ULONG __m_RefCount; 202 ULONG _m_RefCount;
205 CMyUnknownImp(): __m_RefCount(0) {} 203 CMyUnknownImp(): _m_RefCount(0) {}
206 204
207 #ifdef _WIN32 205 #ifdef _WIN32
208 #if defined(__GNUC__) || defined(__clang__) 206 #if defined(__GNUC__) || defined(__clang__)
209 virtual // to disable GCC/CLANG varnings 207 // virtual ~CMyUnknownImp() {} // to disable GCC/CLANG varnings
210 #endif 208 #endif
211 #endif 209 #endif
212 ~CMyUnknownImp() {}
213}; 210};
214 211
215 212
216 213
217#define MY_QUERYINTERFACE_BEGIN STDMETHOD(QueryInterface) \ 214#define Z7_COM_QI_BEGIN \
218(REFGUID iid, void **outObject) throw() { *outObject = NULL; 215 private: STDMETHOD(QueryInterface) (REFGUID iid, void **outObject) throw() Z7_override Z7_final \
216 { *outObject = NULL;
217
218#define Z7_COM_QI_ENTRY(i) \
219 else if (iid == IID_ ## i) \
220 { i *ti = this; *outObject = ti; }
221// { *outObject = (void *)(i *)this; }
219 222
220#define MY_QUERYINTERFACE_ENTRY(i) else if (iid == IID_ ## i) \ 223#define Z7_COM_QI_ENTRY_UNKNOWN_0 \
221 { *outObject = (void *)(i *)this; } 224 if (iid == IID_IUnknown) \
225 { IUnknown *tu = this; *outObject = tu; }
222 226
223#define MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) if (iid == IID_IUnknown) \ 227#define Z7_COM_QI_ENTRY_UNKNOWN(i) \
224 { *outObject = (void *)(IUnknown *)(i *)this; } 228 if (iid == IID_IUnknown) \
229 { i *ti = this; IUnknown *tu = ti; *outObject = tu; }
230// { *outObject = (void *)(IUnknown *)(i *)this; }
225 231
226#define MY_QUERYINTERFACE_BEGIN2(i) MY_QUERYINTERFACE_BEGIN \ 232#define Z7_COM_QI_BEGIN2(i) \
227 MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) \ 233 Z7_COM_QI_BEGIN \
228 MY_QUERYINTERFACE_ENTRY(i) 234 Z7_COM_QI_ENTRY_UNKNOWN(i) \
235 Z7_COM_QI_ENTRY(i)
229 236
230#define MY_QUERYINTERFACE_END else return E_NOINTERFACE; ++__m_RefCount; /* AddRef(); */ return S_OK; } 237#define Z7_COM_QI_END \
238 else return E_NOINTERFACE; \
239 ++_m_RefCount; /* AddRef(); */ return S_OK; }
231 240
232#define MY_ADDREF_RELEASE \ 241#define Z7_COM_ADDREF_RELEASE \
233STDMETHOD_(ULONG, AddRef)() throw() { return ++__m_RefCount; } \ 242 private: \
234STDMETHOD_(ULONG, Release)() { if (--__m_RefCount != 0) return __m_RefCount; \ 243 STDMETHOD_(ULONG, AddRef)() throw() Z7_override Z7_final \
235 delete this; return 0; } 244 { return ++_m_RefCount; } \
245 STDMETHOD_(ULONG, Release)() throw() Z7_override Z7_final \
246 { if (--_m_RefCount != 0) return _m_RefCount; delete this; return 0; } \
236 247
237#define MY_UNKNOWN_IMP_SPEC(i) \ 248#define Z7_COM_UNKNOWN_IMP_SPEC(i) \
238 MY_QUERYINTERFACE_BEGIN \ 249 Z7_COM_QI_BEGIN \
239 i \ 250 i \
240 MY_QUERYINTERFACE_END \ 251 Z7_COM_QI_END \
241 MY_ADDREF_RELEASE 252 Z7_COM_ADDREF_RELEASE
242 253
243 254
244#define MY_UNKNOWN_IMP MY_QUERYINTERFACE_BEGIN \ 255#define Z7_COM_UNKNOWN_IMP_0 \
245 MY_QUERYINTERFACE_ENTRY_UNKNOWN(IUnknown) \ 256 Z7_COM_QI_BEGIN \
246 MY_QUERYINTERFACE_END \ 257 Z7_COM_QI_ENTRY_UNKNOWN_0 \
247 MY_ADDREF_RELEASE 258 Z7_COM_QI_END \
259 Z7_COM_ADDREF_RELEASE
248 260
249#define MY_UNKNOWN_IMP1(i) MY_UNKNOWN_IMP_SPEC( \ 261#define Z7_COM_UNKNOWN_IMP_1(i) \
250 MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) \ 262 Z7_COM_UNKNOWN_IMP_SPEC( \
251 MY_QUERYINTERFACE_ENTRY(i) \ 263 Z7_COM_QI_ENTRY_UNKNOWN(i) \
264 Z7_COM_QI_ENTRY(i) \
252 ) 265 )
253 266
254#define MY_UNKNOWN_IMP2(i1, i2) MY_UNKNOWN_IMP_SPEC( \ 267#define Z7_COM_UNKNOWN_IMP_2(i1, i2) \
255 MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \ 268 Z7_COM_UNKNOWN_IMP_SPEC( \
256 MY_QUERYINTERFACE_ENTRY(i1) \ 269 Z7_COM_QI_ENTRY_UNKNOWN(i1) \
257 MY_QUERYINTERFACE_ENTRY(i2) \ 270 Z7_COM_QI_ENTRY(i1) \
271 Z7_COM_QI_ENTRY(i2) \
258 ) 272 )
259 273
260#define MY_UNKNOWN_IMP3(i1, i2, i3) MY_UNKNOWN_IMP_SPEC( \ 274#define Z7_COM_UNKNOWN_IMP_3(i1, i2, i3) \
261 MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \ 275 Z7_COM_UNKNOWN_IMP_SPEC( \
262 MY_QUERYINTERFACE_ENTRY(i1) \ 276 Z7_COM_QI_ENTRY_UNKNOWN(i1) \
263 MY_QUERYINTERFACE_ENTRY(i2) \ 277 Z7_COM_QI_ENTRY(i1) \
264 MY_QUERYINTERFACE_ENTRY(i3) \ 278 Z7_COM_QI_ENTRY(i2) \
279 Z7_COM_QI_ENTRY(i3) \
265 ) 280 )
266 281
267#define MY_UNKNOWN_IMP4(i1, i2, i3, i4) MY_UNKNOWN_IMP_SPEC( \ 282#define Z7_COM_UNKNOWN_IMP_4(i1, i2, i3, i4) \
268 MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \ 283 Z7_COM_UNKNOWN_IMP_SPEC( \
269 MY_QUERYINTERFACE_ENTRY(i1) \ 284 Z7_COM_QI_ENTRY_UNKNOWN(i1) \
270 MY_QUERYINTERFACE_ENTRY(i2) \ 285 Z7_COM_QI_ENTRY(i1) \
271 MY_QUERYINTERFACE_ENTRY(i3) \ 286 Z7_COM_QI_ENTRY(i2) \
272 MY_QUERYINTERFACE_ENTRY(i4) \ 287 Z7_COM_QI_ENTRY(i3) \
288 Z7_COM_QI_ENTRY(i4) \
273 ) 289 )
274 290
275#define MY_UNKNOWN_IMP5(i1, i2, i3, i4, i5) MY_UNKNOWN_IMP_SPEC( \ 291#define Z7_COM_UNKNOWN_IMP_5(i1, i2, i3, i4, i5) \
276 MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \ 292 Z7_COM_UNKNOWN_IMP_SPEC( \
277 MY_QUERYINTERFACE_ENTRY(i1) \ 293 Z7_COM_QI_ENTRY_UNKNOWN(i1) \
278 MY_QUERYINTERFACE_ENTRY(i2) \ 294 Z7_COM_QI_ENTRY(i1) \
279 MY_QUERYINTERFACE_ENTRY(i3) \ 295 Z7_COM_QI_ENTRY(i2) \
280 MY_QUERYINTERFACE_ENTRY(i4) \ 296 Z7_COM_QI_ENTRY(i3) \
281 MY_QUERYINTERFACE_ENTRY(i5) \ 297 Z7_COM_QI_ENTRY(i4) \
298 Z7_COM_QI_ENTRY(i5) \
282 ) 299 )
283 300
284#define MY_UNKNOWN_IMP6(i1, i2, i3, i4, i5, i6) MY_UNKNOWN_IMP_SPEC( \ 301#define Z7_COM_UNKNOWN_IMP_6(i1, i2, i3, i4, i5, i6) \
285 MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \ 302 Z7_COM_UNKNOWN_IMP_SPEC( \
286 MY_QUERYINTERFACE_ENTRY(i1) \ 303 Z7_COM_QI_ENTRY_UNKNOWN(i1) \
287 MY_QUERYINTERFACE_ENTRY(i2) \ 304 Z7_COM_QI_ENTRY(i1) \
288 MY_QUERYINTERFACE_ENTRY(i3) \ 305 Z7_COM_QI_ENTRY(i2) \
289 MY_QUERYINTERFACE_ENTRY(i4) \ 306 Z7_COM_QI_ENTRY(i3) \
290 MY_QUERYINTERFACE_ENTRY(i5) \ 307 Z7_COM_QI_ENTRY(i4) \
291 MY_QUERYINTERFACE_ENTRY(i6) \ 308 Z7_COM_QI_ENTRY(i5) \
309 Z7_COM_QI_ENTRY(i6) \
292 ) 310 )
293 311
294#define MY_UNKNOWN_IMP7(i1, i2, i3, i4, i5, i6, i7) MY_UNKNOWN_IMP_SPEC( \ 312#define Z7_COM_UNKNOWN_IMP_7(i1, i2, i3, i4, i5, i6, i7) \
295 MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \ 313 Z7_COM_UNKNOWN_IMP_SPEC( \
296 MY_QUERYINTERFACE_ENTRY(i1) \ 314 Z7_COM_QI_ENTRY_UNKNOWN(i1) \
297 MY_QUERYINTERFACE_ENTRY(i2) \ 315 Z7_COM_QI_ENTRY(i1) \
298 MY_QUERYINTERFACE_ENTRY(i3) \ 316 Z7_COM_QI_ENTRY(i2) \
299 MY_QUERYINTERFACE_ENTRY(i4) \ 317 Z7_COM_QI_ENTRY(i3) \
300 MY_QUERYINTERFACE_ENTRY(i5) \ 318 Z7_COM_QI_ENTRY(i4) \
301 MY_QUERYINTERFACE_ENTRY(i6) \ 319 Z7_COM_QI_ENTRY(i5) \
302 MY_QUERYINTERFACE_ENTRY(i7) \ 320 Z7_COM_QI_ENTRY(i6) \
321 Z7_COM_QI_ENTRY(i7) \
303 ) 322 )
304 323
305const HRESULT k_My_HRESULT_WritingWasCut = 0x20000010; 324
325#define Z7_IFACES_IMP_UNK_1(i1) \
326 Z7_COM_UNKNOWN_IMP_1(i1) \
327 Z7_IFACE_COM7_IMP(i1) \
328
329#define Z7_IFACES_IMP_UNK_2(i1, i2) \
330 Z7_COM_UNKNOWN_IMP_2(i1, i2) \
331 Z7_IFACE_COM7_IMP(i1) \
332 Z7_IFACE_COM7_IMP(i2) \
333
334#define Z7_IFACES_IMP_UNK_3(i1, i2, i3) \
335 Z7_COM_UNKNOWN_IMP_3(i1, i2, i3) \
336 Z7_IFACE_COM7_IMP(i1) \
337 Z7_IFACE_COM7_IMP(i2) \
338 Z7_IFACE_COM7_IMP(i3) \
339
340#define Z7_IFACES_IMP_UNK_4(i1, i2, i3, i4) \
341 Z7_COM_UNKNOWN_IMP_4(i1, i2, i3, i4) \
342 Z7_IFACE_COM7_IMP(i1) \
343 Z7_IFACE_COM7_IMP(i2) \
344 Z7_IFACE_COM7_IMP(i3) \
345 Z7_IFACE_COM7_IMP(i4) \
346
347#define Z7_IFACES_IMP_UNK_5(i1, i2, i3, i4, i5) \
348 Z7_COM_UNKNOWN_IMP_5(i1, i2, i3, i4, i5) \
349 Z7_IFACE_COM7_IMP(i1) \
350 Z7_IFACE_COM7_IMP(i2) \
351 Z7_IFACE_COM7_IMP(i3) \
352 Z7_IFACE_COM7_IMP(i4) \
353 Z7_IFACE_COM7_IMP(i5) \
354
355#define Z7_IFACES_IMP_UNK_6(i1, i2, i3, i4, i5, i6) \
356 Z7_COM_UNKNOWN_IMP_6(i1, i2, i3, i4, i5, i6) \
357 Z7_IFACE_COM7_IMP(i1) \
358 Z7_IFACE_COM7_IMP(i2) \
359 Z7_IFACE_COM7_IMP(i3) \
360 Z7_IFACE_COM7_IMP(i4) \
361 Z7_IFACE_COM7_IMP(i5) \
362 Z7_IFACE_COM7_IMP(i6) \
363
364
365#define Z7_CLASS_IMP_COM_0(c) \
366 Z7_class_final(c) : \
367 public IUnknown, \
368 public CMyUnknownImp { \
369 Z7_COM_UNKNOWN_IMP_0 \
370 private:
371
372#define Z7_CLASS_IMP_COM_1(c, i1) \
373 Z7_class_final(c) : \
374 public i1, \
375 public CMyUnknownImp { \
376 Z7_IFACES_IMP_UNK_1(i1) \
377 private:
378
379#define Z7_CLASS_IMP_COM_2(c, i1, i2) \
380 Z7_class_final(c) : \
381 public i1, \
382 public i2, \
383 public CMyUnknownImp { \
384 Z7_IFACES_IMP_UNK_2(i1, i2) \
385 private:
386
387#define Z7_CLASS_IMP_COM_3(c, i1, i2, i3) \
388 Z7_class_final(c) : \
389 public i1, \
390 public i2, \
391 public i3, \
392 public CMyUnknownImp { \
393 Z7_IFACES_IMP_UNK_3(i1, i2, i3) \
394 private:
395
396#define Z7_CLASS_IMP_COM_4(c, i1, i2, i3, i4) \
397 Z7_class_final(c) : \
398 public i1, \
399 public i2, \
400 public i3, \
401 public i4, \
402 public CMyUnknownImp { \
403 Z7_IFACES_IMP_UNK_4(i1, i2, i3, i4) \
404 private:
405
406#define Z7_CLASS_IMP_COM_5(c, i1, i2, i3, i4, i5) \
407 Z7_class_final(c) : \
408 public i1, \
409 public i2, \
410 public i3, \
411 public i4, \
412 public i5, \
413 public CMyUnknownImp { \
414 Z7_IFACES_IMP_UNK_5(i1, i2, i3, i4, i5) \
415 private:
416
417#define Z7_CLASS_IMP_COM_6(c, i1, i2, i3, i4, i5, i6) \
418 Z7_class_final(c) : \
419 public i1, \
420 public i2, \
421 public i3, \
422 public i4, \
423 public i5, \
424 public i6, \
425 public CMyUnknownImp { \
426 Z7_IFACES_IMP_UNK_6(i1, i2, i3, i4, i5, i6) \
427 private:
428
429
430/*
431#define Z7_CLASS_IMP_NOQIB_0(c) \
432 Z7_class_final(c) : \
433 public IUnknown, \
434 public CMyUnknownImp { \
435 Z7_COM_UNKNOWN_IMP_0 \
436 private:
437*/
438
439#define Z7_CLASS_IMP_NOQIB_1(c, i1) \
440 Z7_class_final(c) : \
441 public i1, \
442 public CMyUnknownImp { \
443 Z7_COM_UNKNOWN_IMP_0 \
444 Z7_IFACE_COM7_IMP(i1) \
445 private:
446
447#define Z7_CLASS_IMP_NOQIB_2(c, i1, i2) \
448 Z7_class_final(c) : \
449 public i1, \
450 public i2, \
451 public CMyUnknownImp { \
452 Z7_COM_UNKNOWN_IMP_1(i2) \
453 Z7_IFACE_COM7_IMP(i1) \
454 Z7_IFACE_COM7_IMP(i2) \
455 private:
456
457#define Z7_CLASS_IMP_NOQIB_3(c, i1, i2, i3) \
458 Z7_class_final(c) : \
459 public i1, \
460 public i2, \
461 public i3, \
462 public CMyUnknownImp { \
463 Z7_COM_UNKNOWN_IMP_2(i2, i3) \
464 Z7_IFACE_COM7_IMP(i1) \
465 Z7_IFACE_COM7_IMP(i2) \
466 Z7_IFACE_COM7_IMP(i3) \
467 private:
468
469#define Z7_CLASS_IMP_NOQIB_4(c, i1, i2, i3, i4) \
470 Z7_class_final(c) : \
471 public i1, \
472 public i2, \
473 public i3, \
474 public i4, \
475 public CMyUnknownImp { \
476 Z7_COM_UNKNOWN_IMP_3(i2, i3, i4) \
477 Z7_IFACE_COM7_IMP(i1) \
478 Z7_IFACE_COM7_IMP(i2) \
479 Z7_IFACE_COM7_IMP(i3) \
480 Z7_IFACE_COM7_IMP(i4) \
481
482/*
483#define Z7_CLASS_IMP_NOQIB_5(c, i1, i2, i3, i4, i5) \
484 Z7_class_final(c) : \
485 public i1, \
486 public i2, \
487 public i3, \
488 public i4, \
489 public i5, \
490 public CMyUnknownImp { \
491 Z7_COM_UNKNOWN_IMP_4(i2, i3, i4, i5) \
492 Z7_IFACE_COM7_IMP(i1) \
493 Z7_IFACE_COM7_IMP(i2) \
494 Z7_IFACE_COM7_IMP(i3) \
495 Z7_IFACE_COM7_IMP(i4) \
496 Z7_IFACE_COM7_IMP(i5) \
497*/
498
499
500#define Z7_CLASS_IMP_IInStream(c) \
501 class c Z7_final : \
502 public IInStream, \
503 public CMyUnknownImp { \
504 Z7_IFACES_IMP_UNK_2(ISequentialInStream, IInStream) \
505
506
507#define k_My_HRESULT_WritingWasCut 0x20000010
306 508
307#endif 509#endif