source: Daodan/MSYS2/mingw32/i686-w64-mingw32/include/comutil.h@ 1181

Last change on this file since 1181 was 1166, checked in by rossy, 3 years ago

Daodan: Replace MinGW build env with an up-to-date MSYS2 env

File size: 31.2 KB
RevLine 
[1166]1/**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6#ifndef _INC_COMUTIL
7#define _INC_COMUTIL
8
9#include <ole2.h>
10#include <stdio.h>
11
12#ifndef _COM_ASSERT
13#define _COM_ASSERT(x) ((void)0)
14#endif
15
16#define _COM_MEMCPY_S(dest,destsize,src,count) memcpy(dest,src,count)
17
18/* Use of wsprintf might be impossible, if strsafe.h is included. */
19#ifndef __STDC_SECURE_LIB__
20#define _COM_PRINTF_S_1(dest,destsize,format,arg1) wsprintf(dest,format,arg1)
21#elif defined(UNICODE)
22#define _COM_PRINTF_S_1(dest,destsize,format,arg1) swprintf_s(dest,destsize,format,arg1)
23#else
24#define _COM_PRINTF_S_1(dest,destsize,format,arg1) sprintf_s(dest,destsize,format,arg1)
25#endif
26
27#ifdef __cplusplus
28
29#pragma push_macro("new")
30#undef new
31
32#ifndef WINAPI
33#if defined(_ARM_)
34#define WINAPI
35#else
36#define WINAPI __stdcall
37#endif
38#endif
39
40class _com_error;
41
42void WINAPI _com_issue_error(HRESULT);
43
44class _bstr_t;
45class _variant_t;
46
47namespace _com_util {
48 inline void CheckError(HRESULT hr) {
49 if(FAILED(hr)) { _com_issue_error(hr); }
50 }
51}
52
53namespace _com_util {
54 BSTR WINAPI ConvertStringToBSTR(const char *pSrc);
55 char *WINAPI ConvertBSTRToString(BSTR pSrc);
56}
57
58class _bstr_t {
59public:
60 _bstr_t() throw();
61 _bstr_t(const _bstr_t &s) throw();
62 _bstr_t(const char *s);
63 _bstr_t(const wchar_t *s);
64 _bstr_t(const _variant_t &var);
65 _bstr_t(BSTR bstr,bool fCopy);
66 ~_bstr_t() throw();
67 _bstr_t &operator=(const _bstr_t &s) throw();
68 _bstr_t &operator=(const char *s);
69 _bstr_t &operator=(const wchar_t *s);
70 _bstr_t &operator=(const _variant_t &var);
71 _bstr_t &operator+=(const _bstr_t &s);
72 _bstr_t operator+(const _bstr_t &s) const;
73 friend _bstr_t operator+(const char *s1,const _bstr_t &s2);
74 friend _bstr_t operator+(const wchar_t *s1,const _bstr_t &s2);
75 operator const wchar_t *() const throw();
76 operator wchar_t *() const throw();
77 operator const char *() const;
78 operator char *() const;
79 bool operator!() const throw();
80 bool operator==(const _bstr_t &str) const throw();
81 bool operator!=(const _bstr_t &str) const throw();
82 bool operator<(const _bstr_t &str) const throw();
83 bool operator>(const _bstr_t &str) const throw();
84 bool operator<=(const _bstr_t &str) const throw();
85 bool operator>=(const _bstr_t &str) const throw();
86 BSTR copy(bool fCopy = true) const;
87 unsigned int length() const throw();
88 void Assign(BSTR s);
89 BSTR &GetBSTR();
90 BSTR *GetAddress();
91 void Attach(BSTR s);
92 BSTR Detach() throw();
93private:
94 class Data_t {
95 public:
96 Data_t(const char *s);
97 Data_t(const wchar_t *s);
98 Data_t(BSTR bstr,bool fCopy);
99 Data_t(const _bstr_t &s1,const _bstr_t &s2);
100 unsigned __LONG32 AddRef() throw();
101 unsigned __LONG32 Release() throw();
102 unsigned __LONG32 RefCount() const throw();
103 operator const wchar_t *() const throw();
104 operator const char *() const;
105 const wchar_t *GetWString() const throw();
106 wchar_t *&GetWString() throw();
107 const char *GetString() const;
108 BSTR Copy() const;
109 void Assign(BSTR s);
110 void Attach(BSTR s) throw();
111 unsigned int Length() const throw();
112 int Compare(const Data_t &str) const throw();
113 void *operator new(size_t sz);
114 private:
115 BSTR m_wstr;
116 mutable char *m_str;
117 unsigned __LONG32 m_RefCount;
118 Data_t() throw();
119 Data_t(const Data_t &s) throw();
120 ~Data_t() throw();
121 void _Free() throw();
122 };
123private:
124 Data_t *m_Data;
125private:
126 void _AddRef() throw();
127 void _Free() throw();
128 int _Compare(const _bstr_t &str) const throw();
129};
130
131inline _bstr_t::_bstr_t() throw() : m_Data(NULL) { }
132
133inline _bstr_t::_bstr_t(const _bstr_t &s) throw() : m_Data(s.m_Data) { _AddRef(); }
134
135inline _bstr_t::_bstr_t(const char *s) : m_Data(new Data_t(s)) {
136 if(!m_Data) { _com_issue_error(E_OUTOFMEMORY); }
137}
138
139inline _bstr_t::_bstr_t(const wchar_t *s) : m_Data(new Data_t(s)) {
140 if(!m_Data) { _com_issue_error(E_OUTOFMEMORY); }
141}
142
143inline _bstr_t::_bstr_t(BSTR bstr,bool fCopy) : m_Data(new Data_t(bstr,fCopy)) {
144 if(!m_Data) { _com_issue_error(E_OUTOFMEMORY); }
145}
146
147inline _bstr_t::~_bstr_t() throw() { _Free(); }
148
149inline _bstr_t &_bstr_t::operator=(const _bstr_t &s) throw() {
150 if(this!=&s) {
151 _Free();
152 m_Data = s.m_Data;
153 _AddRef();
154 }
155 return *this;
156}
157
158inline _bstr_t &_bstr_t::operator=(const char *s) {
159 _COM_ASSERT(!s || static_cast<const char *>(*this)!=s);
160 if(!s || static_cast<const char *>(*this)!=s) {
161 _Free();
162 m_Data = new Data_t(s);
163 if(!m_Data) { _com_issue_error(E_OUTOFMEMORY); }
164 }
165 return *this;
166}
167
168inline _bstr_t &_bstr_t::operator=(const wchar_t *s) {
169 _COM_ASSERT(!s || static_cast<const wchar_t *>(*this)!=s);
170 if(!s || static_cast<const wchar_t *>(*this)!=s) {
171 _Free();
172 m_Data = new Data_t(s);
173 if(!m_Data) { _com_issue_error(E_OUTOFMEMORY); }
174 }
175 return *this;
176}
177
178inline _bstr_t &_bstr_t::operator+=(const _bstr_t &s) {
179 Data_t *newData = new Data_t(*this,s);
180 if(!newData) { _com_issue_error(E_OUTOFMEMORY); }
181 else {
182 _Free();
183 m_Data = newData;
184 }
185 return *this;
186}
187
188inline _bstr_t _bstr_t::operator+(const _bstr_t &s) const {
189 _bstr_t b = *this;
190 b += s;
191 return b;
192}
193
194inline _bstr_t operator+(const char *s1,const _bstr_t &s2) {
195 _bstr_t b = s1;
196 b += s2;
197 return b;
198}
199
200inline _bstr_t operator+(const wchar_t *s1,const _bstr_t &s2) {
201 _bstr_t b = s1;
202 b += s2;
203 return b;
204}
205
206inline _bstr_t::operator const wchar_t *() const throw() { return (m_Data!=NULL) ? m_Data->GetWString() : NULL; }
207inline _bstr_t::operator wchar_t *() const throw() { return const_cast<wchar_t *>((m_Data!=NULL) ? m_Data->GetWString() : NULL); }
208inline _bstr_t::operator const char *() const { return (m_Data!=NULL) ? m_Data->GetString() : NULL; }
209inline _bstr_t::operator char *() const { return const_cast<char *>((m_Data!=NULL) ? m_Data->GetString() : NULL); }
210inline bool _bstr_t::operator!() const throw() { return (m_Data!=NULL) ? !m_Data->GetWString() : true; }
211inline bool _bstr_t::operator==(const _bstr_t &str) const throw() { return _Compare(str)==0; }
212inline bool _bstr_t::operator!=(const _bstr_t &str) const throw() { return _Compare(str)!=0; }
213inline bool _bstr_t::operator<(const _bstr_t &str) const throw() { return _Compare(str)<0; }
214inline bool _bstr_t::operator>(const _bstr_t &str) const throw() { return _Compare(str)>0; }
215inline bool _bstr_t::operator<=(const _bstr_t &str) const throw() { return _Compare(str)<=0; }
216inline bool _bstr_t::operator>=(const _bstr_t &str) const throw() { return _Compare(str)>=0; }
217inline BSTR _bstr_t::copy(bool fCopy) const { return (m_Data!=NULL) ? (fCopy ? m_Data->Copy() : m_Data->GetWString()) : NULL; }
218inline unsigned int _bstr_t::length() const throw() { return (m_Data!=NULL) ? m_Data->Length() : 0; }
219inline void _bstr_t::Assign(BSTR s) {
220 _COM_ASSERT(!s || !m_Data || m_Data->GetWString()!=s);
221 if(!s || !m_Data || m_Data->GetWString()!=s) {
222 _Free();
223 m_Data = new Data_t(s,TRUE);
224 if(!m_Data) { _com_issue_error(E_OUTOFMEMORY); }
225 }
226}
227
228inline BSTR &_bstr_t::GetBSTR() {
229 if(!m_Data) {
230 m_Data = new Data_t(0,FALSE);
231 if(!m_Data) { _com_issue_error(E_OUTOFMEMORY); }
232 }
233 return m_Data->GetWString();
234}
235
236inline BSTR *_bstr_t::GetAddress() {
237 Attach(0);
238 return &m_Data->GetWString();
239}
240
241inline void _bstr_t::Attach(BSTR s) {
242 _Free();
243 m_Data = new Data_t(s,FALSE);
244 if(!m_Data) { _com_issue_error(E_OUTOFMEMORY); }
245}
246
247inline BSTR _bstr_t::Detach() throw () {
248 _COM_ASSERT(m_Data!=NULL && m_Data->RefCount()==1);
249 if(m_Data!=NULL && m_Data->RefCount()==1) {
250 BSTR b = m_Data->GetWString();
251 m_Data->GetWString() = NULL;
252 _Free();
253 return b;
254 } else {
255 _com_issue_error(E_POINTER);
256 return NULL;
257 }
258}
259
260inline void _bstr_t::_AddRef() throw() {
261 if(m_Data!=NULL) m_Data->AddRef();
262}
263
264inline void _bstr_t::_Free() throw() {
265 if(m_Data!=NULL) {
266 m_Data->Release();
267 m_Data = NULL;
268 }
269}
270
271inline int _bstr_t::_Compare(const _bstr_t &str) const throw() {
272 if(m_Data==str.m_Data) return 0;
273 if(!m_Data) return -1;
274 if(!str.m_Data) return 1;
275 return m_Data->Compare(*str.m_Data);
276}
277
278inline _bstr_t::Data_t::Data_t(const char *s) : m_str(NULL),m_RefCount(1) {
279 m_wstr = _com_util::ConvertStringToBSTR(s);
280}
281
282inline _bstr_t::Data_t::Data_t(const wchar_t *s) : m_str(NULL),m_RefCount(1) {
283 m_wstr = ::SysAllocString(s);
284 if(!m_wstr && s!=NULL) { _com_issue_error(E_OUTOFMEMORY); }
285}
286
287inline _bstr_t::Data_t::Data_t(BSTR bstr,bool fCopy) : m_str(NULL),m_RefCount(1) {
288 if(fCopy && bstr!=NULL) {
289 m_wstr = ::SysAllocStringByteLen(reinterpret_cast<char *>(bstr),::SysStringByteLen(bstr));
290 if(!m_wstr) { _com_issue_error(E_OUTOFMEMORY); }
291 } else m_wstr = bstr;
292}
293
294inline _bstr_t::Data_t::Data_t(const _bstr_t &s1,const _bstr_t &s2) : m_str(NULL),m_RefCount(1) {
295 const unsigned int l1 = s1.length();
296 const unsigned int l2 = s2.length();
297 m_wstr = ::SysAllocStringByteLen(NULL,(l1 + l2) *sizeof(wchar_t));
298 if(!m_wstr) {
299 _com_issue_error(E_OUTOFMEMORY);
300 return;
301 }
302 const wchar_t *wstr1 = static_cast<const wchar_t *>(s1);
303 if(wstr1!=NULL) {
304 _COM_MEMCPY_S(m_wstr,(l1 + l2 + 1) *sizeof(wchar_t),wstr1,(l1 + 1) *sizeof(wchar_t));
305 }
306 const wchar_t *wstr2 = static_cast<const wchar_t *>(s2);
307 if(wstr2!=NULL) {
308 _COM_MEMCPY_S(m_wstr + l1,(l2 + 1) *sizeof(wchar_t),wstr2,(l2 + 1) *sizeof(wchar_t));
309 }
310}
311
312inline unsigned __LONG32 _bstr_t::Data_t::AddRef() throw() {
313 InterlockedIncrement(reinterpret_cast<LONG*>(&m_RefCount));
314 return m_RefCount;
315}
316
317inline unsigned __LONG32 _bstr_t::Data_t::Release() throw() {
318 unsigned __LONG32 cRef = InterlockedDecrement(reinterpret_cast<LONG*>(&m_RefCount));
319 if(cRef==0) delete this;
320 return cRef;
321}
322
323inline unsigned __LONG32 _bstr_t::Data_t::RefCount() const throw() { return m_RefCount; }
324inline _bstr_t::Data_t::operator const wchar_t *() const throw() { return m_wstr; }
325inline _bstr_t::Data_t::operator const char *() const { return GetString(); }
326inline const wchar_t *_bstr_t::Data_t::GetWString() const throw() { return m_wstr; }
327inline wchar_t *&_bstr_t::Data_t::GetWString() throw() { return m_wstr; }
328inline const char *_bstr_t::Data_t::GetString() const {
329 if(!m_str) m_str = _com_util::ConvertBSTRToString(m_wstr);
330 return m_str;
331}
332inline BSTR _bstr_t::Data_t::Copy() const {
333 if(m_wstr!=NULL) {
334 BSTR bstr = ::SysAllocStringByteLen(reinterpret_cast<char *>(m_wstr),::SysStringByteLen(m_wstr));
335 if(!bstr) { _com_issue_error(E_OUTOFMEMORY); }
336 return bstr;
337 }
338 return NULL;
339}
340inline void _bstr_t::Data_t::Assign(BSTR s) {
341 _Free();
342 if(s!=NULL) {
343 m_wstr = ::SysAllocStringByteLen(reinterpret_cast<char *>(s),::SysStringByteLen(s));
344 m_str = 0;
345 }
346}
347inline void _bstr_t::Data_t::Attach(BSTR s) throw() {
348 _Free();
349 m_wstr = s;
350 m_str = 0;
351 m_RefCount = 1;
352}
353inline unsigned int _bstr_t::Data_t::Length() const throw() { return m_wstr ? ::SysStringLen(m_wstr) : 0; }
354inline int _bstr_t::Data_t::Compare(const _bstr_t::Data_t &str) const throw() {
355 if(!m_wstr) return str.m_wstr ? -1 : 0;
356 if(!str.m_wstr) return 1;
357 const unsigned int l1 = ::SysStringLen(m_wstr);
358 const unsigned int l2 = ::SysStringLen(str.m_wstr);
359 unsigned int len = l1;
360 if(len>l2) len = l2;
361 BSTR bstr1 = m_wstr;
362 BSTR bstr2 = str.m_wstr;
363 while (len-->0) {
364 if(*bstr1++!=*bstr2++) return bstr1[-1] - bstr2[-1];
365 }
366 return (l1<l2) ? -1 : (l1==l2) ? 0 : 1;
367}
368
369#ifdef _COM_OPERATOR_NEW_THROWS
370inline void *_bstr_t::Data_t::operator new(size_t sz) {
371 try {
372 return ::operator new(sz);
373 } catch (...) {
374 return NULL;
375 }
376}
377#else
378inline void *_bstr_t::Data_t::operator new(size_t sz) {
379 return ::operator new(sz);
380}
381#endif
382
383inline _bstr_t::Data_t::~Data_t() throw() { _Free(); }
384inline void _bstr_t::Data_t::_Free() throw() {
385 if(m_wstr!=NULL) ::SysFreeString(m_wstr);
386 if(m_str!=NULL) delete [] m_str;
387}
388
389class _variant_t : public ::tagVARIANT {
390public:
391 _variant_t() throw();
392 _variant_t(const VARIANT &varSrc);
393 _variant_t(const VARIANT *pSrc);
394 _variant_t(const _variant_t &varSrc);
395 _variant_t(VARIANT &varSrc,bool fCopy);
396 _variant_t(short sSrc,VARTYPE vtSrc = VT_I2);
397 _variant_t(__LONG32 lSrc,VARTYPE vtSrc = VT_I4);
398 _variant_t(float fltSrc) throw();
399 _variant_t(double dblSrc,VARTYPE vtSrc = VT_R8);
400 _variant_t(const CY &cySrc) throw();
401 _variant_t(const _bstr_t &bstrSrc);
402 _variant_t(const wchar_t *pSrc);
403 _variant_t(const char *pSrc);
404 _variant_t(IDispatch *pSrc,bool fAddRef = true) throw();
405 _variant_t(bool boolSrc) throw();
406 _variant_t(IUnknown *pSrc,bool fAddRef = true) throw();
407 _variant_t(const DECIMAL &decSrc) throw();
408 _variant_t(BYTE bSrc) throw();
409 _variant_t(char cSrc) throw();
410 _variant_t(unsigned short usSrc) throw();
411 _variant_t(unsigned __LONG32 ulSrc) throw();
412 _variant_t(int iSrc) throw();
413 _variant_t(unsigned int uiSrc) throw();
414 __MINGW_EXTENSION _variant_t(__int64 i8Src) throw();
415 __MINGW_EXTENSION _variant_t(unsigned __int64 ui8Src) throw();
416 ~_variant_t() throw();
417 operator short() const;
418 operator __LONG32() const;
419 operator float() const;
420 operator double() const;
421 operator CY() const;
422 operator _bstr_t() const;
423 operator IDispatch*() const;
424 operator bool() const;
425 operator IUnknown*() const;
426 operator DECIMAL() const;
427 operator BYTE() const;
428 operator VARIANT() const throw();
429 operator char() const;
430 operator unsigned short() const;
431 operator unsigned __LONG32() const;
432 operator int() const;
433 operator unsigned int() const;
434 __MINGW_EXTENSION operator __int64() const;
435 __MINGW_EXTENSION operator unsigned __int64() const;
436 _variant_t &operator=(const VARIANT &varSrc);
437 _variant_t &operator=(const VARIANT *pSrc);
438 _variant_t &operator=(const _variant_t &varSrc);
439 _variant_t &operator=(short sSrc);
440 _variant_t &operator=(__LONG32 lSrc);
441 _variant_t &operator=(float fltSrc);
442 _variant_t &operator=(double dblSrc);
443 _variant_t &operator=(const CY &cySrc);
444 _variant_t &operator=(const _bstr_t &bstrSrc);
445 _variant_t &operator=(const wchar_t *pSrc);
446 _variant_t &operator=(const char *pSrc);
447 _variant_t &operator=(IDispatch *pSrc);
448 _variant_t &operator=(bool boolSrc);
449 _variant_t &operator=(IUnknown *pSrc);
450 _variant_t &operator=(const DECIMAL &decSrc);
451 _variant_t &operator=(BYTE bSrc);
452 _variant_t &operator=(char cSrc);
453 _variant_t &operator=(unsigned short usSrc);
454 _variant_t &operator=(unsigned __LONG32 ulSrc);
455 _variant_t &operator=(int iSrc);
456 _variant_t &operator=(unsigned int uiSrc);
457 __MINGW_EXTENSION _variant_t &operator=(__int64 i8Src);
458 __MINGW_EXTENSION _variant_t &operator=(unsigned __int64 ui8Src);
459 bool operator==(const VARIANT &varSrc) const throw();
460 bool operator==(const VARIANT *pSrc) const throw();
461 bool operator!=(const VARIANT &varSrc) const throw();
462 bool operator!=(const VARIANT *pSrc) const throw();
463 void Clear();
464 void Attach(VARIANT &varSrc);
465 VARIANT Detach();
466 VARIANT &GetVARIANT() throw();
467 VARIANT *GetAddress();
468 void ChangeType(VARTYPE vartype,const _variant_t *pSrc = NULL);
469 void SetString(const char *pSrc);
470};
471
472inline _variant_t::_variant_t() throw() { ::VariantInit(this); }
473inline _variant_t::_variant_t(const VARIANT &varSrc) {
474 ::VariantInit(this);
475 _com_util::CheckError(::VariantCopy(this,const_cast<VARIANT*>(&varSrc)));
476}
477inline _variant_t::_variant_t(const VARIANT *pSrc) {
478 if(!pSrc) { _com_issue_error(E_POINTER); }
479 else {
480 ::VariantInit(this);
481 _com_util::CheckError(::VariantCopy(this,const_cast<VARIANT*>(pSrc)));
482 }
483}
484inline _variant_t::_variant_t(const _variant_t &varSrc) {
485 ::VariantInit(this);
486 _com_util::CheckError(::VariantCopy(this,const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc))));
487}
488inline _variant_t::_variant_t(VARIANT &varSrc,bool fCopy) {
489 if(fCopy) {
490 ::VariantInit(this);
491 _com_util::CheckError(::VariantCopy(this,&varSrc));
492 } else {
493 _COM_MEMCPY_S(this,sizeof(varSrc),&varSrc,sizeof(varSrc));
494 V_VT(&varSrc) = VT_EMPTY;
495 }
496}
497inline _variant_t::_variant_t(short sSrc,VARTYPE vtSrc) {
498 if((vtSrc!=VT_I2) && (vtSrc!=VT_BOOL)) {
499 _com_issue_error(E_INVALIDARG);
500 return;
501 }
502 if(vtSrc==VT_BOOL) {
503 V_VT(this) = VT_BOOL;
504 V_BOOL(this) = (sSrc ? VARIANT_TRUE : VARIANT_FALSE);
505 } else {
506 V_VT(this) = VT_I2;
507 V_I2(this) = sSrc;
508 }
509}
510inline _variant_t::_variant_t(__LONG32 lSrc,VARTYPE vtSrc) {
511 if((vtSrc!=VT_I4) && (vtSrc!=VT_ERROR) && (vtSrc!=VT_BOOL)) {
512 _com_issue_error(E_INVALIDARG);
513 return;
514 }
515 if(vtSrc==VT_ERROR) {
516 V_VT(this) = VT_ERROR;
517 V_ERROR(this) = lSrc;
518 } else if(vtSrc==VT_BOOL) {
519 V_VT(this) = VT_BOOL;
520 V_BOOL(this) = (lSrc ? VARIANT_TRUE : VARIANT_FALSE);
521 } else {
522 V_VT(this) = VT_I4;
523 V_I4(this) = lSrc;
524 }
525}
526inline _variant_t::_variant_t(float fltSrc) throw() {
527 V_VT(this) = VT_R4;
528 V_R4(this) = fltSrc;
529}
530
531inline _variant_t::_variant_t(double dblSrc,VARTYPE vtSrc) {
532 if((vtSrc!=VT_R8) && (vtSrc!=VT_DATE)) {
533 _com_issue_error(E_INVALIDARG);
534 return;
535 }
536 if(vtSrc==VT_DATE) {
537 V_VT(this) = VT_DATE;
538 V_DATE(this) = dblSrc;
539 } else {
540 V_VT(this) = VT_R8;
541 V_R8(this) = dblSrc;
542 }
543}
544inline _variant_t::_variant_t(const CY &cySrc) throw() {
545 V_VT(this) = VT_CY;
546 V_CY(this) = cySrc;
547}
548inline _variant_t::_variant_t(const _bstr_t &bstrSrc) {
549 V_VT(this) = VT_BSTR;
550 BSTR bstr = static_cast<wchar_t *>(bstrSrc);
551 if(!bstr) V_BSTR(this) = NULL;
552 else {
553 V_BSTR(this) = ::SysAllocStringByteLen(reinterpret_cast<char *>(bstr),::SysStringByteLen(bstr));
554 if(!(V_BSTR(this))) { _com_issue_error(E_OUTOFMEMORY); }
555 }
556}
557inline _variant_t::_variant_t(const wchar_t *pSrc) {
558 V_VT(this) = VT_BSTR;
559 V_BSTR(this) = ::SysAllocString(pSrc);
560 if(!(V_BSTR(this)) && pSrc!=NULL) { _com_issue_error(E_OUTOFMEMORY); }
561}
562inline _variant_t::_variant_t(const char *pSrc) {
563 V_VT(this) = VT_BSTR;
564 V_BSTR(this) = _com_util::ConvertStringToBSTR(pSrc);
565}
566inline _variant_t::_variant_t(IDispatch *pSrc,bool fAddRef) throw() {
567 V_VT(this) = VT_DISPATCH;
568 V_DISPATCH(this) = pSrc;
569 if(fAddRef && V_DISPATCH(this)!=NULL) V_DISPATCH(this)->AddRef();
570}
571inline _variant_t::_variant_t(bool boolSrc) throw() {
572 V_VT(this) = VT_BOOL;
573 V_BOOL(this) = (boolSrc ? VARIANT_TRUE : VARIANT_FALSE);
574}
575inline _variant_t::_variant_t(IUnknown *pSrc,bool fAddRef) throw() {
576 V_VT(this) = VT_UNKNOWN;
577 V_UNKNOWN(this) = pSrc;
578 if(fAddRef && V_UNKNOWN(this)!=NULL) V_UNKNOWN(this)->AddRef();
579}
580inline _variant_t::_variant_t(const DECIMAL &decSrc) throw() {
581 V_DECIMAL(this) = decSrc;
582 V_VT(this) = VT_DECIMAL;
583}
584inline _variant_t::_variant_t(BYTE bSrc) throw() {
585 V_VT(this) = VT_UI1;
586 V_UI1(this) = bSrc;
587}
588inline _variant_t::_variant_t(char cSrc) throw() {
589 V_VT(this) = VT_I1;
590 V_I1(this) = cSrc;
591}
592inline _variant_t::_variant_t(unsigned short usSrc) throw() {
593 V_VT(this) = VT_UI2;
594 V_UI2(this) = usSrc;
595}
596inline _variant_t::_variant_t(unsigned __LONG32 ulSrc) throw() {
597 V_VT(this) = VT_UI4;
598 V_UI4(this) = ulSrc;
599}
600inline _variant_t::_variant_t(int iSrc) throw() {
601 V_VT(this) = VT_INT;
602 V_INT(this) = iSrc;
603}
604inline _variant_t::_variant_t(unsigned int uiSrc) throw() {
605 V_VT(this) = VT_UINT;
606 V_UINT(this) = uiSrc;
607}
608__MINGW_EXTENSION inline _variant_t::_variant_t(__int64 i8Src) throw() {
609 V_VT(this) = VT_I8;
610 V_I8(this) = i8Src;
611}
612__MINGW_EXTENSION inline _variant_t::_variant_t(unsigned __int64 ui8Src) throw() {
613 V_VT(this) = VT_UI8;
614 V_UI8(this) = ui8Src;
615}
616inline _variant_t::operator short() const {
617 if(V_VT(this)==VT_I2) return V_I2(this);
618 _variant_t varDest;
619 varDest.ChangeType(VT_I2,this);
620 return V_I2(&varDest);
621}
622inline _variant_t::operator __LONG32() const {
623 if(V_VT(this)==VT_I4) return V_I4(this);
624 _variant_t varDest;
625 varDest.ChangeType(VT_I4,this);
626 return V_I4(&varDest);
627}
628
629inline _variant_t::operator float() const {
630 if(V_VT(this)==VT_R4) return V_R4(this);
631 _variant_t varDest;
632 varDest.ChangeType(VT_R4,this);
633 return V_R4(&varDest);
634}
635
636inline _variant_t::operator double() const {
637 if(V_VT(this)==VT_R8) return V_R8(this);
638 _variant_t varDest;
639 varDest.ChangeType(VT_R8,this);
640 return V_R8(&varDest);
641}
642
643inline _variant_t::operator CY() const {
644 if(V_VT(this)==VT_CY) return V_CY(this);
645 _variant_t varDest;
646 varDest.ChangeType(VT_CY,this);
647 return V_CY(&varDest);
648}
649
650inline _variant_t::operator _bstr_t() const {
651 if(V_VT(this)==VT_BSTR) return V_BSTR(this);
652 _variant_t varDest;
653 varDest.ChangeType(VT_BSTR,this);
654 return V_BSTR(&varDest);
655}
656
657inline _variant_t::operator IDispatch*() const {
658 if(V_VT(this)==VT_DISPATCH) {
659 if(V_DISPATCH(this)!=NULL) V_DISPATCH(this)->AddRef();
660 return V_DISPATCH(this);
661 }
662 _variant_t varDest;
663 varDest.ChangeType(VT_DISPATCH,this);
664 if(V_DISPATCH(&varDest)!=NULL) V_DISPATCH(&varDest)->AddRef();
665 return V_DISPATCH(&varDest);
666}
667inline _variant_t::operator bool() const {
668 if(V_VT(this)==VT_BOOL) return V_BOOL(this) ? true : false;
669 _variant_t varDest;
670 varDest.ChangeType(VT_BOOL,this);
671 return (V_BOOL(&varDest)==VARIANT_TRUE) ? true : false;
672}
673
674inline _variant_t::operator IUnknown*() const {
675 if(V_VT(this)==VT_UNKNOWN) {
676 if(V_UNKNOWN(this)!=NULL) V_UNKNOWN(this)->AddRef();
677 return V_UNKNOWN(this);
678 }
679 _variant_t varDest;
680 varDest.ChangeType(VT_UNKNOWN,this);
681 if(V_UNKNOWN(&varDest)!=NULL) V_UNKNOWN(&varDest)->AddRef();
682 return V_UNKNOWN(&varDest);
683}
684inline _variant_t::operator DECIMAL() const {
685 if(V_VT(this)==VT_DECIMAL) return V_DECIMAL(this);
686 _variant_t varDest;
687 varDest.ChangeType(VT_DECIMAL,this);
688 return V_DECIMAL(&varDest);
689}
690inline _variant_t::operator BYTE() const {
691 if(V_VT(this)==VT_UI1) return V_UI1(this);
692 _variant_t varDest;
693 varDest.ChangeType(VT_UI1,this);
694 return V_UI1(&varDest);
695}
696inline _variant_t::operator VARIANT() const throw() { return *(VARIANT*) this; }
697inline _variant_t::operator char() const {
698 if(V_VT(this)==VT_I1) return V_I1(this);
699 _variant_t varDest;
700 varDest.ChangeType(VT_I1,this);
701 return V_I1(&varDest);
702}
703
704inline _variant_t::operator unsigned short() const {
705 if(V_VT(this)==VT_UI2) return V_UI2(this);
706 _variant_t varDest;
707 varDest.ChangeType(VT_UI2,this);
708 return V_UI2(&varDest);
709}
710
711inline _variant_t::operator unsigned __LONG32() const {
712 if(V_VT(this)==VT_UI4) return V_UI4(this);
713 _variant_t varDest;
714 varDest.ChangeType(VT_UI4,this);
715 return V_UI4(&varDest);
716}
717inline _variant_t::operator int() const {
718 if(V_VT(this)==VT_INT) return V_INT(this);
719 _variant_t varDest;
720 varDest.ChangeType(VT_INT,this);
721 return V_INT(&varDest);
722}
723inline _variant_t::operator unsigned int() const {
724 if(V_VT(this)==VT_UINT) return V_UINT(this);
725 _variant_t varDest;
726 varDest.ChangeType(VT_UINT,this);
727 return V_UINT(&varDest);
728}
729__MINGW_EXTENSION inline _variant_t::operator __int64() const {
730 if(V_VT(this)==VT_I8) return V_I8(this);
731 _variant_t varDest;
732 varDest.ChangeType(VT_I8,this);
733 return V_I8(&varDest);
734}
735__MINGW_EXTENSION inline _variant_t::operator unsigned __int64() const {
736 if(V_VT(this)==VT_UI8) return V_UI8(this);
737 _variant_t varDest;
738 varDest.ChangeType(VT_UI8,this);
739 return V_UI8(&varDest);
740}
741inline _variant_t &_variant_t::operator=(const VARIANT &varSrc) {
742 _com_util::CheckError(::VariantCopy(this,const_cast<VARIANT*>(&varSrc)));
743 return *this;
744}
745inline _variant_t &_variant_t::operator=(const VARIANT *pSrc) {
746 if(!pSrc) { _com_issue_error(E_POINTER); }
747 else { _com_util::CheckError(::VariantCopy(this,const_cast<VARIANT*>(pSrc))); }
748 return *this;
749}
750inline _variant_t &_variant_t::operator=(const _variant_t &varSrc) {
751 _com_util::CheckError(::VariantCopy(this,const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc))));
752 return *this;
753}
754inline _variant_t &_variant_t::operator=(short sSrc) {
755 if(V_VT(this)==VT_I2) V_I2(this) = sSrc;
756 else if(V_VT(this)==VT_BOOL) V_BOOL(this) = (sSrc ? VARIANT_TRUE : VARIANT_FALSE);
757 else {
758 Clear();
759 V_VT(this) = VT_I2;
760 V_I2(this) = sSrc;
761 }
762 return *this;
763}
764inline _variant_t &_variant_t::operator=(__LONG32 lSrc) {
765 if(V_VT(this)==VT_I4) V_I4(this) = lSrc;
766 else if(V_VT(this)==VT_ERROR) V_ERROR(this) = lSrc;
767 else if(V_VT(this)==VT_BOOL) V_BOOL(this) = (lSrc ? VARIANT_TRUE : VARIANT_FALSE);
768 else {
769 Clear();
770 V_VT(this) = VT_I4;
771 V_I4(this) = lSrc;
772 }
773 return *this;
774}
775inline _variant_t &_variant_t::operator=(float fltSrc) {
776 if(V_VT(this)!=VT_R4) {
777 Clear();
778 V_VT(this) = VT_R4;
779 }
780 V_R4(this) = fltSrc;
781 return *this;
782}
783
784inline _variant_t &_variant_t::operator=(double dblSrc)
785{
786 if(V_VT(this)==VT_R8) {
787 V_R8(this) = dblSrc;
788 }
789 else if(V_VT(this)==VT_DATE) {
790 V_DATE(this) = dblSrc;
791 }
792 else {
793
794 Clear();
795
796 V_VT(this) = VT_R8;
797 V_R8(this) = dblSrc;
798 }
799
800 return *this;
801}
802
803inline _variant_t &_variant_t::operator=(const CY &cySrc)
804{
805 if(V_VT(this)!=VT_CY) {
806
807 Clear();
808
809 V_VT(this) = VT_CY;
810 }
811
812 V_CY(this) = cySrc;
813
814 return *this;
815}
816
817inline _variant_t &_variant_t::operator=(const _bstr_t &bstrSrc)
818{
819 _COM_ASSERT(V_VT(this)!=VT_BSTR || !((BSTR) bstrSrc) || V_BSTR(this)!=(BSTR) bstrSrc);
820
821 Clear();
822
823 V_VT(this) = VT_BSTR;
824
825 if(!bstrSrc) {
826 V_BSTR(this) = NULL;
827 }
828 else {
829 BSTR bstr = static_cast<wchar_t *>(bstrSrc);
830 V_BSTR(this) = ::SysAllocStringByteLen(reinterpret_cast<char *>(bstr),::SysStringByteLen(bstr));
831
832 if(!(V_BSTR(this))) {
833 _com_issue_error(E_OUTOFMEMORY);
834 }
835 }
836
837 return *this;
838}
839
840inline _variant_t &_variant_t::operator=(const wchar_t *pSrc)
841{
842 _COM_ASSERT(V_VT(this)!=VT_BSTR || !pSrc || V_BSTR(this)!=pSrc);
843
844 Clear();
845
846 V_VT(this) = VT_BSTR;
847
848 if(!pSrc) {
849 V_BSTR(this) = NULL;
850 }
851 else {
852 V_BSTR(this) = ::SysAllocString(pSrc);
853
854 if(!(V_BSTR(this))) {
855 _com_issue_error(E_OUTOFMEMORY);
856 }
857 }
858
859 return *this;
860}
861
862inline _variant_t &_variant_t::operator=(const char *pSrc)
863{
864 _COM_ASSERT(V_VT(this)!=(VT_I1 | VT_BYREF) || !pSrc || V_I1REF(this)!=pSrc);
865
866 Clear();
867
868 V_VT(this) = VT_BSTR;
869 V_BSTR(this) = _com_util::ConvertStringToBSTR(pSrc);
870
871 return *this;
872}
873
874inline _variant_t &_variant_t::operator=(IDispatch *pSrc)
875{
876 _COM_ASSERT(V_VT(this)!=VT_DISPATCH || pSrc==0 || V_DISPATCH(this)!=pSrc);
877
878 Clear();
879
880 V_VT(this) = VT_DISPATCH;
881 V_DISPATCH(this) = pSrc;
882
883 if(V_DISPATCH(this)!=NULL) {
884
885 V_DISPATCH(this)->AddRef();
886 }
887
888 return *this;
889}
890
891inline _variant_t &_variant_t::operator=(bool boolSrc)
892{
893 if(V_VT(this)!=VT_BOOL) {
894
895 Clear();
896
897 V_VT(this) = VT_BOOL;
898 }
899
900 V_BOOL(this) = (boolSrc ? VARIANT_TRUE : VARIANT_FALSE);
901
902 return *this;
903}
904
905inline _variant_t &_variant_t::operator=(IUnknown *pSrc)
906{
907 _COM_ASSERT(V_VT(this)!=VT_UNKNOWN || !pSrc || V_UNKNOWN(this)!=pSrc);
908
909 Clear();
910
911 V_VT(this) = VT_UNKNOWN;
912 V_UNKNOWN(this) = pSrc;
913
914 if(V_UNKNOWN(this)!=NULL) {
915
916 V_UNKNOWN(this)->AddRef();
917 }
918
919 return *this;
920}
921
922inline _variant_t &_variant_t::operator=(const DECIMAL &decSrc)
923{
924 if(V_VT(this)!=VT_DECIMAL) {
925
926 Clear();
927 }
928
929 V_DECIMAL(this) = decSrc;
930 V_VT(this) = VT_DECIMAL;
931
932 return *this;
933}
934
935inline _variant_t &_variant_t::operator=(BYTE bSrc)
936{
937 if(V_VT(this)!=VT_UI1) {
938
939 Clear();
940
941 V_VT(this) = VT_UI1;
942 }
943
944 V_UI1(this) = bSrc;
945
946 return *this;
947}
948
949inline _variant_t &_variant_t::operator=(char cSrc)
950{
951 if(V_VT(this)!=VT_I1) {
952
953 Clear();
954
955 V_VT(this) = VT_I1;
956 }
957
958 V_I1(this) = cSrc;
959
960 return *this;
961}
962
963inline _variant_t &_variant_t::operator=(unsigned short usSrc)
964{
965 if(V_VT(this)!=VT_UI2) {
966
967 Clear();
968
969 V_VT(this) = VT_UI2;
970 }
971
972 V_UI2(this) = usSrc;
973
974 return *this;
975}
976
977inline _variant_t &_variant_t::operator=(unsigned __LONG32 ulSrc)
978{
979 if(V_VT(this)!=VT_UI4) {
980
981 Clear();
982
983 V_VT(this) = VT_UI4;
984 }
985
986 V_UI4(this) = ulSrc;
987
988 return *this;
989}
990
991inline _variant_t &_variant_t::operator=(int iSrc)
992{
993 if(V_VT(this)!=VT_INT) {
994
995 Clear();
996
997 V_VT(this) = VT_INT;
998 }
999
1000 V_INT(this) = iSrc;
1001
1002 return *this;
1003}
1004
1005inline _variant_t &_variant_t::operator=(unsigned int uiSrc)
1006{
1007 if(V_VT(this)!=VT_UINT) {
1008
1009 Clear();
1010
1011 V_VT(this) = VT_UINT;
1012 }
1013
1014 V_UINT(this) = uiSrc;
1015
1016 return *this;
1017}
1018
1019__MINGW_EXTENSION inline _variant_t &_variant_t::operator=(__int64 i8Src) {
1020 if(V_VT(this)!=VT_I8) {
1021
1022 Clear();
1023
1024 V_VT(this) = VT_I8;
1025 }
1026
1027 V_I8(this) = i8Src;
1028
1029 return *this;
1030}
1031
1032__MINGW_EXTENSION inline _variant_t &_variant_t::operator=(unsigned __int64 ui8Src) {
1033 if(V_VT(this)!=VT_UI8) {
1034
1035 Clear();
1036
1037 V_VT(this) = VT_UI8;
1038 }
1039
1040 V_UI8(this) = ui8Src;
1041
1042 return *this;
1043}
1044
1045inline bool _variant_t::operator==(const VARIANT &varSrc) const throw() {
1046 return *this==&varSrc;
1047}
1048
1049inline bool _variant_t::operator==(const VARIANT *pSrc) const throw()
1050{
1051 if(!pSrc) {
1052 return false;
1053 }
1054
1055 if(this==pSrc) {
1056 return true;
1057 }
1058
1059 if(V_VT(this)!=V_VT(pSrc)) {
1060 return false;
1061 }
1062
1063 switch (V_VT(this)) {
1064case VT_EMPTY:
1065case VT_NULL:
1066 return true;
1067
1068case VT_I2:
1069 return V_I2(this)==V_I2(pSrc);
1070
1071case VT_I4:
1072 return V_I4(this)==V_I4(pSrc);
1073
1074case VT_R4:
1075 return V_R4(this)==V_R4(pSrc);
1076
1077case VT_R8:
1078 return V_R8(this)==V_R8(pSrc);
1079
1080case VT_CY:
1081 return memcmp(&(V_CY(this)),&(V_CY(pSrc)),sizeof(CY))==0;
1082
1083case VT_DATE:
1084 return V_DATE(this)==V_DATE(pSrc);
1085
1086case VT_BSTR:
1087 return (::SysStringByteLen(V_BSTR(this))==::SysStringByteLen(V_BSTR(pSrc))) &&
1088 (memcmp(V_BSTR(this),V_BSTR(pSrc),::SysStringByteLen(V_BSTR(this)))==0);
1089
1090case VT_DISPATCH:
1091 return V_DISPATCH(this)==V_DISPATCH(pSrc);
1092
1093case VT_ERROR:
1094 return V_ERROR(this)==V_ERROR(pSrc);
1095
1096case VT_BOOL:
1097 return V_BOOL(this)==V_BOOL(pSrc);
1098
1099case VT_UNKNOWN:
1100 return V_UNKNOWN(this)==V_UNKNOWN(pSrc);
1101
1102case VT_DECIMAL:
1103 return memcmp(&(V_DECIMAL(this)),&(V_DECIMAL(pSrc)),sizeof(DECIMAL))==0;
1104
1105case VT_UI1:
1106 return V_UI1(this)==V_UI1(pSrc);
1107
1108case VT_I1:
1109 return V_I1(this)==V_I1(pSrc);
1110
1111case VT_UI2:
1112 return V_UI2(this)==V_UI2(pSrc);
1113
1114case VT_UI4:
1115 return V_UI4(this)==V_UI4(pSrc);
1116
1117case VT_INT:
1118 return V_INT(this)==V_INT(pSrc);
1119
1120case VT_UINT:
1121 return V_UINT(this)==V_UINT(pSrc);
1122
1123case VT_I8:
1124 return V_I8(this)==V_I8(pSrc);
1125
1126case VT_UI8:
1127 return V_UI8(this)==V_UI8(pSrc);
1128
1129default:
1130 _com_issue_error(E_INVALIDARG);
1131
1132 }
1133
1134 return false;
1135}
1136
1137inline bool _variant_t::operator!=(const VARIANT &varSrc) const throw()
1138{
1139 return !(*this==&varSrc);
1140}
1141
1142inline bool _variant_t::operator!=(const VARIANT *pSrc) const throw()
1143{
1144 return !(*this==pSrc);
1145}
1146
1147inline void _variant_t::Clear()
1148{
1149 _com_util::CheckError(::VariantClear(this));
1150}
1151
1152inline void _variant_t::Attach(VARIANT &varSrc)
1153{
1154
1155 Clear();
1156
1157 _COM_MEMCPY_S(this,sizeof(varSrc),&varSrc,sizeof(varSrc));
1158 V_VT(&varSrc) = VT_EMPTY;
1159}
1160
1161inline VARIANT _variant_t::Detach()
1162{
1163 VARIANT varResult = *this;
1164 V_VT(this) = VT_EMPTY;
1165
1166 return varResult;
1167}
1168
1169inline VARIANT &_variant_t::GetVARIANT() throw()
1170{
1171 return *(VARIANT*) this;
1172}
1173
1174inline VARIANT *_variant_t::GetAddress() {
1175 Clear();
1176 return (VARIANT*) this;
1177}
1178inline void _variant_t::ChangeType(VARTYPE vartype,const _variant_t *pSrc) {
1179 if(!pSrc) pSrc = this;
1180 if((this!=pSrc) || (vartype!=V_VT(this))) {
1181 _com_util::CheckError(::VariantChangeType(static_cast<VARIANT*>(this),const_cast<VARIANT*>(static_cast<const VARIANT*>(pSrc)),0,vartype));
1182 }
1183}
1184inline void _variant_t::SetString(const char *pSrc) { operator=(pSrc); }
1185inline _variant_t::~_variant_t() throw() { ::VariantClear(this); }
1186inline _bstr_t::_bstr_t(const _variant_t &var) : m_Data(NULL) {
1187 if(V_VT(&var)==VT_BSTR) {
1188 *this = V_BSTR(&var);
1189 return;
1190 }
1191 _variant_t varDest;
1192 varDest.ChangeType(VT_BSTR,&var);
1193 *this = V_BSTR(&varDest);
1194}
1195inline _bstr_t &_bstr_t::operator=(const _variant_t &var) {
1196 if(V_VT(&var)==VT_BSTR) {
1197 *this = V_BSTR(&var);
1198 return *this;
1199 }
1200 _variant_t varDest;
1201 varDest.ChangeType(VT_BSTR,&var);
1202 *this = V_BSTR(&varDest);
1203 return *this;
1204}
1205
1206extern _variant_t vtMissing;
1207
1208#ifndef _USE_RAW
1209#define bstr_t _bstr_t
1210#define variant_t _variant_t
1211#endif
1212
1213#pragma pop_macro("new")
1214
1215/* We use _com_issue_error here, but we only provide its inline version in comdef.h,
1216 * so we need to make sure that it's included as well. */
1217#include <comdef.h>
1218
1219#endif /* __cplusplus */
1220
1221#endif
Note: See TracBrowser for help on using the repository browser.