ReactOS  0.4.12-dev-708-g95ed44e
atlcomcli.h
Go to the documentation of this file.
1 /*
2  * ReactOS ATL
3  *
4  * Copyright 2009 Andrew Hill <ash77@reactos.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #pragma once
22 
23 #include "atlcore.h"
24 
25 
26 #ifdef _MSC_VER
27 // It is common to use this in ATL constructors. They only store this for later use, so the usage is safe.
28 #pragma warning(disable:4355)
29 #endif
30 
31 #ifndef _ATL_PACKING
32 #define _ATL_PACKING 8
33 #endif
34 
35 #ifndef _ATL_FREE_THREADED
36 #ifndef _ATL_APARTMENT_THREADED
37 #ifndef _ATL_SINGLE_THREADED
38 #define _ATL_FREE_THREADED
39 #endif
40 #endif
41 #endif
42 
43 #ifndef ATLTRY
44 #define ATLTRY(x) x;
45 #endif
46 
47 #ifdef _ATL_DISABLE_NO_VTABLE
48 #define ATL_NO_VTABLE
49 #else
50 #define ATL_NO_VTABLE __declspec(novtable)
51 #endif
52 
53 namespace ATL
54 {
55 
56 
57 template<class T>
58 class CComPtr
59 {
60 public:
61  T *p;
62 public:
64  {
65  p = NULL;
66  }
67 
68  CComPtr(T *lp)
69  {
70  p = lp;
71  if (p != NULL)
72  p->AddRef();
73  }
74 
75  CComPtr(const CComPtr<T> &lp)
76  {
77  p = lp.p;
78  if (p != NULL)
79  p->AddRef();
80  }
81 
83  {
84  if (p != NULL)
85  p->Release();
86  }
87 
88  T *operator = (T *lp)
89  {
90  T* pOld = p;
91 
92  p = lp;
93  if (p != NULL)
94  p->AddRef();
95 
96  if (pOld != NULL)
97  pOld->Release();
98 
99  return *this;
100  }
101 
103  {
104  T* pOld = p;
105 
106  p = lp.p;
107  if (p != NULL)
108  p->AddRef();
109 
110  if (pOld != NULL)
111  pOld->Release();
112 
113  return *this;
114  }
115 
116  // We cannot enable this until gcc starts supporting __uuidof
117  // See CORE-12710
118 #if 0
119  template <typename Q>
120  T* operator=(const CComPtr<Q>& lp)
121  {
122  T* pOld = p;
123 
124  if (!lp.p || FAILED(lp.p->QueryInterface(__uuidof(T), (void**)(IUnknown**)&p)))
125  p = NULL;
126 
127  if (pOld != NULL)
128  pOld->Release();
129 
130  return *this;
131  }
132 #endif
133 
134  void Release()
135  {
136  if (p != NULL)
137  {
138  p->Release();
139  p = NULL;
140  }
141  }
142 
143  void Attach(T *lp)
144  {
145  if (p != NULL)
146  p->Release();
147  p = lp;
148  }
149 
151  {
152  T *saveP;
153 
154  saveP = p;
155  p = NULL;
156  return saveP;
157  }
158 
160  {
161  ATLASSERT(p == NULL);
162  return &p;
163  }
164 
165  operator T * ()
166  {
167  return p;
168  }
169 
171  {
172  ATLASSERT(p != NULL);
173  return p;
174  }
175 };
176 
177 
178 //CComQIIDPtr<I_ID(Itype)> is the gcc compatible version of CComQIPtr<Itype>
179 #define I_ID(Itype) Itype,&IID_##Itype
180 
181 template <class T, const IID* piid>
182 class CComQIIDPtr :
183  public CComPtr<T>
184 {
185 public:
186  // Let's tell GCC how to find a symbol.
187  using CComPtr<T>::p;
188 
190  {
191  }
193  CComPtr<T>(lp)
194  {
195  }
197  CComPtr<T>(lp.p)
198  {
199  }
201  {
202  if (lp != NULL)
203  {
204  if (FAILED(lp->QueryInterface(*piid, (void**)(IUnknown**)&p)))
205  p = NULL;
206  }
207  }
209  {
210  T* pOld = p;
211 
212  p = lp;
213  if (p != NULL)
214  p->AddRef();
215 
216  if (pOld != NULL)
217  pOld->Release();
218 
219  return *this;
220  }
221 
223  {
224  T* pOld = p;
225 
226  p = lp.p;
227  if (p != NULL)
228  p->AddRef();
229 
230  if (pOld != NULL)
231  pOld->Release();
232 
233  return *this;
234  }
235 
237  {
238  T* pOld = p;
239 
240  if (!lp || FAILED(lp->QueryInterface(*piid, (void**)(IUnknown**)&p)))
241  p = NULL;
242 
243  if (pOld != NULL)
244  pOld->Release();
245 
246  return *this;
247  }
248 };
249 
250 
251 class CComBSTR
252 {
253 public:
255 public:
257  m_str(NULL)
258  {
259  }
260 
261  CComBSTR(LPCOLESTR pSrc)
262  {
263  if (pSrc == NULL)
264  m_str = NULL;
265  else
266  m_str = ::SysAllocString(pSrc);
267  }
268 
270  {
271  if (length == 0)
272  m_str = NULL;
273  else
275  }
276 
277  CComBSTR(int length, LPCOLESTR pSrc)
278  {
279  if (length == 0)
280  m_str = NULL;
281  else
283  }
284 
286  {
287  if (pSrc)
288  {
289  int len = MultiByteToWideChar(CP_THREAD_ACP, 0, pSrc, -1, NULL, 0);
291  if (m_str)
292  {
293  int res = MultiByteToWideChar(CP_THREAD_ACP, 0, pSrc, -1, m_str, len);
294  ATLASSERT(res == len);
295  if (res != len)
296  {
298  m_str = NULL;
299  }
300  }
301  }
302  else
303  {
304  m_str = NULL;
305  }
306  }
307 
309  {
310  m_str = other.Copy();
311  }
312 
314  {
315  OLECHAR szGuid[40];
318  }
319 
321  {
323  m_str = NULL;
324  }
325 
326  operator BSTR () const
327  {
328  return m_str;
329  }
330 
332  {
333  return &m_str;
334  }
335 
337  {
339  m_str = other.Copy();
340  return *this;
341  }
342 
343  void Attach(BSTR bstr)
344  {
346  m_str = bstr;
347  }
348 
350  {
351  BSTR str = m_str;
352  m_str = NULL;
353  return str;
354  }
355 
356  BSTR Copy() const
357  {
358  if (!m_str)
359  return NULL;
361  }
362 
364  {
365  if (!other)
366  return E_POINTER;
367  *other = Copy();
368  return S_OK;
369  }
370 
372  {
374  m_str = NULL;
375  const wchar_t *ptr = NULL;
376  int len = ::LoadStringW(module, uID, (PWSTR)&ptr, 0);
377  if (len)
379  return m_str != NULL;
380  }
381 
382  unsigned int Length() const
383  {
385  }
386 
387  unsigned int ByteLength() const
388  {
390  }
391 };
392 
393 
394 class CComVariant : public tagVARIANT
395 {
396 public:
398  {
399  ::VariantInit(this);
400  }
401 
403  {
404  V_VT(this) = VT_EMPTY;
405  Copy(&other);
406  }
407 
409  {
410  Clear();
411  }
412 
413  CComVariant(LPCOLESTR lpStr)
414  {
415  V_VT(this) = VT_BSTR;
416  V_BSTR(this) = ::SysAllocString(lpStr);
417  }
418 
420  {
421  V_VT(this) = VT_BSTR;
422  CComBSTR str(lpStr);
423  V_BSTR(this) = str.Detach();
424  }
425 
427  {
428  V_VT(this) = VT_BOOL;
429  V_BOOL(this) = value ? VARIANT_TRUE : VARIANT_FALSE;
430  }
431 
433  {
434  V_VT(this) = VT_I1;
435  V_I1(this) = value;
436  }
437 
439  {
440  V_VT(this) = VT_UI1;
441  V_UI1(this) = value;
442  }
443 
445  {
446  V_VT(this) = VT_I2;
447  V_I2(this) = value;
448  }
449 
450  CComVariant(unsigned short value)
451  {
452  V_VT(this) = VT_UI2;
453  V_UI2(this) = value;
454  }
455 
457  {
458  if (type == VT_I4 || type == VT_INT)
459  {
460  V_VT(this) = type;
461  V_I4(this) = value;
462  }
463  else
464  {
465  V_VT(this) = VT_ERROR;
466  V_ERROR(this) = E_INVALIDARG;
467  }
468  }
469 
471  {
472  if (type == VT_UI4 || type == VT_UINT)
473  {
474  V_VT(this) = type;
475  V_UI4(this) = value;
476  }
477  else
478  {
479  V_VT(this) = VT_ERROR;
480  V_ERROR(this) = E_INVALIDARG;
481  }
482  }
483 
485  {
486  if (type == VT_I4 || type == VT_ERROR)
487  {
488  V_VT(this) = type;
489  V_I4(this) = value;
490  }
491  else
492  {
493  V_VT(this) = VT_ERROR;
494  V_ERROR(this) = E_INVALIDARG;
495  }
496  }
497 
498  CComVariant(unsigned long value)
499  {
500  V_VT(this) = VT_UI4;
501  V_UI4(this) = value;
502  }
503 
505  {
506  V_VT(this) = VT_R4;
507  V_R4(this) = value;
508  }
509 
511  {
512  if (type == VT_R8 || type == VT_DATE)
513  {
514  V_VT(this) = type;
515  V_R8(this) = value;
516  }
517  else
518  {
519  V_VT(this) = VT_ERROR;
520  V_ERROR(this) = E_INVALIDARG;
521  }
522  }
523 
525  {
526  V_VT(this) = VT_I8;
527  V_I8(this) = value;
528  }
529 
531  {
532  V_VT(this) = VT_UI8;
533  V_UI8(this) = value;
534  }
535 
537  {
538  V_VT(this) = VT_CY;
539  V_I8(this) = value.int64;
540  }
541 
542 
544  {
545  return ::VariantClear(this);
546  }
547 
549  {
550  return ::VariantCopy(this, const_cast<VARIANT*>(src));
551  }
552 
554  {
555  const LPVARIANT lpSrc = src ? src : this;
556  return ::VariantChangeType(this, lpSrc, 0, newType);
557  }
558 };
559 
560 
561 
562 }; // namespace ATL
563 
564 #ifndef _ATL_NO_AUTOMATIC_NAMESPACE
565 using namespace ATL;
566 #endif
567 
WCHAR OLECHAR
Definition: compat.h:1926
CComVariant(const ULONGLONG &value)
Definition: atlcomcli.h:530
CComVariant(float value)
Definition: atlcomcli.h:504
CComBSTR(const CComBSTR &other)
Definition: atlcomcli.h:308
HRESULT WINAPI DECLSPEC_HOTPATCH VariantChangeType(VARIANTARG *pvargDest, VARIANTARG *pvargSrc, USHORT wFlags, VARTYPE vt)
Definition: variant.c:965
CComQIIDPtr(_Inout_ const CComQIIDPtr< T, piid > &lp)
Definition: atlcomcli.h:196
Definition: compat.h:1932
Definition: compat.h:1948
Definition: scsiwmi.h:51
BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
Definition: oleaut.c:342
Definition: compat.h:1944
#define ATLASSERT(x)
Definition: CComVariant.cpp:9
CComVariant(long value, VARENUM type=VT_I4)
Definition: atlcomcli.h:484
uint16_t * PWSTR
Definition: typedefs.h:54
int other
Definition: msacm.c:1364
#define V_R8(A)
Definition: oleauto.h:262
CComBSTR(LPCOLESTR pSrc)
Definition: atlcomcli.h:261
#define V_I2(A)
Definition: oleauto.h:245
CComVariant(bool value)
Definition: atlcomcli.h:426
CComBSTR(int length)
Definition: atlcomcli.h:269
CComVariant(short value)
Definition: atlcomcli.h:444
HRESULT ChangeType(_In_ VARTYPE newType, _In_opt_ const LPVARIANT src=NULL)
Definition: atlcomcli.h:553
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
UINT WINAPI SysStringByteLen(BSTR str)
Definition: oleaut.c:218
CComVariant(char value)
Definition: atlcomcli.h:432
OLECHAR * BSTR
Definition: compat.h:1927
void WINAPI VariantInit(VARIANTARG *pVarg)
Definition: variant.c:571
CComVariant(LPCSTR lpStr)
Definition: atlcomcli.h:419
int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_writes_to_(cchBufferMax, return+1) LPWSTR lpBuffer, _In_ int cchBufferMax)
#define T
Definition: mbstring.h:31
#define V_UI8(A)
Definition: oleauto.h:272
#define _In_opt_
Definition: no_sal2.h:213
#define V_I4(A)
Definition: oleauto.h:247
BSTR Detach()
Definition: atlcomcli.h:349
HRESULT Clear()
Definition: atlcomcli.h:543
const GUID * guid
Definition: compat.h:1889
unsigned int ByteLength() const
Definition: atlcomcli.h:387
CComBSTR(PCSTR pSrc)
Definition: atlcomcli.h:285
CComVariant(const CComVariant &other)
Definition: atlcomcli.h:402
CComVariant(unsigned long value)
Definition: atlcomcli.h:498
static PVOID ptr
Definition: dispmode.c:27
#define E_INVALIDARG
Definition: ddrawi.h:101
const WCHAR * str
smooth NULL
Definition: ftsmooth.c:416
#define V_UI2(A)
Definition: oleauto.h:268
BSTR WINAPI SysAllocString(LPCOLESTR str)
Definition: oleaut.c:241
#define V_I1(A)
Definition: oleauto.h:243
CComQIIDPtr(_Inout_opt_ IUnknown *lp)
Definition: atlcomcli.h:200
const char * LPCSTR
Definition: xmlstorage.h:183
CComVariant(LPCOLESTR lpStr)
Definition: atlcomcli.h:413
CComBSTR(REFGUID guid)
Definition: atlcomcli.h:313
#define V_I8(A)
Definition: oleauto.h:249
int64_t LONGLONG
Definition: typedefs.h:66
CComVariant(BYTE value)
Definition: atlcomcli.h:438
Definition: rosdlgs.h:5
#define V_R4(A)
Definition: oleauto.h:260
CComPtr(const CComPtr< T > &lp)
Definition: atlcomcli.h:75
CComVariant(const CY &value)
Definition: atlcomcli.h:536
BSTR Copy() const
Definition: atlcomcli.h:356
LONG HRESULT
Definition: typedefs.h:77
GLenum GLuint GLenum GLsizei length
Definition: glext.h:5579
CComVariant(const LONGLONG &value)
Definition: atlcomcli.h:524
T * operator ->()
Definition: atlcomcli.h:170
uint64_t ULONGLONG
Definition: typedefs.h:65
CComVariant(unsigned int value, VARENUM type=VT_UI4)
Definition: atlcomcli.h:470
#define V_UI1(A)
Definition: oleauto.h:266
INT WINAPI StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
Definition: compobj.c:2343
#define V_BOOL(A)
Definition: oleauto.h:224
nsrefcnt Release()
unsigned long DWORD
Definition: ntddk_ex.h:95
CComVariant(int value, VARENUM type=VT_I4)
Definition: atlcomcli.h:456
#define _Inout_
Definition: no_sal2.h:244
#define V_UI4(A)
Definition: oleauto.h:270
T ** operator &()
Definition: atlcomcli.h:159
T * operator=(T *lp)
Definition: atlcomcli.h:208
HRESULT WINAPI DECLSPEC_HOTPATCH VariantClear(VARIANTARG *pVarg)
Definition: variant.c:651
Definition: compat.h:1933
CComQIIDPtr(_Inout_opt_ T *lp)
Definition: atlcomcli.h:192
void Attach(BSTR bstr)
Definition: atlcomcli.h:343
#define V_VT(A)
Definition: oleauto.h:211
CComVariant(unsigned short value)
Definition: atlcomcli.h:450
GLenum GLsizei len
Definition: glext.h:6722
unsigned char BYTE
Definition: mem.h:68
UINT WINAPI SysStringLen(BSTR str)
Definition: oleaut.c:199
HRESULT Copy(_In_ const VARIANT *src)
Definition: atlcomcli.h:548
GLenum src
Definition: glext.h:6340
Definition: compat.h:1935
HRESULT CopyTo(BSTR *other) const
Definition: atlcomcli.h:363
GLsizei const GLfloat * value
Definition: glext.h:6069
#define V_BSTR(A)
Definition: oleauto.h:226
CComBSTR(int length, LPCOLESTR pSrc)
Definition: atlcomcli.h:277
#define _In_
Definition: no_sal2.h:204
CComVariant(double value, VARENUM type=VT_R8)
Definition: atlcomcli.h:510
CComPtr(T *lp)
Definition: atlcomcli.h:68
#define S_OK
Definition: intsafe.h:59
VARENUM
Definition: compat.h:1928
bool LoadString(HMODULE module, DWORD uID)
Definition: atlcomcli.h:371
static const WCHAR szGuid[]
Definition: rtlstr.c:1892
void Release()
Definition: atlcomcli.h:134
unsigned short VARTYPE
Definition: compat.h:1888
BSTR * operator &()
Definition: atlcomcli.h:331
void WINAPI DECLSPEC_HOTPATCH SysFreeString(BSTR str)
Definition: oleaut.c:274
#define V_ERROR(A)
Definition: oleauto.h:241
#define MultiByteToWideChar
Definition: compat.h:100
HRESULT QueryInterface([in] REFIID riid, [out, iid_is(riid)] void **ppvObject)
GLuint res
Definition: glext.h:9613
T * operator=(T *lp)
Definition: atlcomcli.h:88
CComBSTR & operator=(const CComBSTR &other)
Definition: atlcomcli.h:336
const char * PCSTR
Definition: typedefs.h:51
T * Detach()
Definition: atlcomcli.h:150
GLfloat GLfloat p
Definition: glext.h:8902
HRESULT WINAPI VariantCopy(VARIANTARG *pvargDest, VARIANTARG *pvargSrc)
Definition: variant.c:751
#define E_POINTER
Definition: winerror.h:2365
#define CP_THREAD_ACP
Definition: winnls.h:230
#define BSTR
Definition: msvc.h:44
unsigned int Length() const
Definition: atlcomcli.h:382
Definition: compat.h:1934
void Attach(T *lp)
Definition: atlcomcli.h:143
Definition: compat.h:1931
T * operator=(IUnknown *lp)
Definition: atlcomcli.h:236
#define _Inout_opt_
Definition: no_sal2.h:258