other-licenses/7zstub/src/Common/MyCom.h

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 // MyCom.h
michael@0 2
michael@0 3 #ifndef __MYCOM_H
michael@0 4 #define __MYCOM_H
michael@0 5
michael@0 6 #include "MyWindows.h"
michael@0 7
michael@0 8 #define RINOK(x) { HRESULT __result_ = (x); if(__result_ != S_OK) return __result_; }
michael@0 9
michael@0 10 template <class T>
michael@0 11 class CMyComPtr
michael@0 12 {
michael@0 13 T* _p;
michael@0 14 public:
michael@0 15 // typedef T _PtrClass;
michael@0 16 CMyComPtr() { _p = NULL;}
michael@0 17 CMyComPtr(T* p) {if ((_p = p) != NULL) p->AddRef(); }
michael@0 18 CMyComPtr(const CMyComPtr<T>& lp)
michael@0 19 {
michael@0 20 if ((_p = lp._p) != NULL)
michael@0 21 _p->AddRef();
michael@0 22 }
michael@0 23 ~CMyComPtr() { if (_p) _p->Release(); }
michael@0 24 void Release() { if (_p) { _p->Release(); _p = NULL; } }
michael@0 25 operator T*() const { return (T*)_p; }
michael@0 26 // T& operator*() const { return *_p; }
michael@0 27 T** operator&() { return &_p; }
michael@0 28 T* operator->() const { return _p; }
michael@0 29 T* operator=(T* p)
michael@0 30 {
michael@0 31 if (p != 0)
michael@0 32 p->AddRef();
michael@0 33 if (_p)
michael@0 34 _p->Release();
michael@0 35 _p = p;
michael@0 36 return p;
michael@0 37 }
michael@0 38 T* operator=(const CMyComPtr<T>& lp) { return (*this = lp._p); }
michael@0 39 bool operator!() const { return (_p == NULL); }
michael@0 40 // bool operator==(T* pT) const { return _p == pT; }
michael@0 41 // Compare two objects for equivalence
michael@0 42 void Attach(T* p2)
michael@0 43 {
michael@0 44 Release();
michael@0 45 _p = p2;
michael@0 46 }
michael@0 47 T* Detach()
michael@0 48 {
michael@0 49 T* pt = _p;
michael@0 50 _p = NULL;
michael@0 51 return pt;
michael@0 52 }
michael@0 53 #ifdef _WIN32
michael@0 54 HRESULT CoCreateInstance(REFCLSID rclsid, REFIID iid, LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL)
michael@0 55 {
michael@0 56 return ::CoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, (void**)&_p);
michael@0 57 }
michael@0 58 #endif
michael@0 59 /*
michael@0 60 HRESULT CoCreateInstance(LPCOLESTR szProgID, LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL)
michael@0 61 {
michael@0 62 CLSID clsid;
michael@0 63 HRESULT hr = CLSIDFromProgID(szProgID, &clsid);
michael@0 64 ATLASSERT(_p == NULL);
michael@0 65 if (SUCCEEDED(hr))
michael@0 66 hr = ::CoCreateInstance(clsid, pUnkOuter, dwClsContext, __uuidof(T), (void**)&_p);
michael@0 67 return hr;
michael@0 68 }
michael@0 69 */
michael@0 70 template <class Q>
michael@0 71 HRESULT QueryInterface(REFGUID iid, Q** pp) const
michael@0 72 {
michael@0 73 return _p->QueryInterface(iid, (void**)pp);
michael@0 74 }
michael@0 75 };
michael@0 76
michael@0 77 //////////////////////////////////////////////////////////
michael@0 78
michael@0 79 class CMyComBSTR
michael@0 80 {
michael@0 81 public:
michael@0 82 BSTR m_str;
michael@0 83 CMyComBSTR() { m_str = NULL; }
michael@0 84 CMyComBSTR(LPCOLESTR pSrc) { m_str = ::SysAllocString(pSrc); }
michael@0 85 // CMyComBSTR(int nSize) { m_str = ::SysAllocStringLen(NULL, nSize); }
michael@0 86 // CMyComBSTR(int nSize, LPCOLESTR sz) { m_str = ::SysAllocStringLen(sz, nSize); }
michael@0 87 CMyComBSTR(const CMyComBSTR& src) { m_str = src.MyCopy(); }
michael@0 88 /*
michael@0 89 CMyComBSTR(REFGUID src)
michael@0 90 {
michael@0 91 LPOLESTR szGuid;
michael@0 92 StringFromCLSID(src, &szGuid);
michael@0 93 m_str = ::SysAllocString(szGuid);
michael@0 94 CoTaskMemFree(szGuid);
michael@0 95 }
michael@0 96 */
michael@0 97 ~CMyComBSTR() { ::SysFreeString(m_str); }
michael@0 98 CMyComBSTR& operator=(const CMyComBSTR& src)
michael@0 99 {
michael@0 100 if (m_str != src.m_str)
michael@0 101 {
michael@0 102 if (m_str)
michael@0 103 ::SysFreeString(m_str);
michael@0 104 m_str = src.MyCopy();
michael@0 105 }
michael@0 106 return *this;
michael@0 107 }
michael@0 108 CMyComBSTR& operator=(LPCOLESTR pSrc)
michael@0 109 {
michael@0 110 ::SysFreeString(m_str);
michael@0 111 m_str = ::SysAllocString(pSrc);
michael@0 112 return *this;
michael@0 113 }
michael@0 114 unsigned int Length() const { return ::SysStringLen(m_str); }
michael@0 115 operator BSTR() const { return m_str; }
michael@0 116 BSTR* operator&() { return &m_str; }
michael@0 117 BSTR MyCopy() const
michael@0 118 {
michael@0 119 int byteLen = ::SysStringByteLen(m_str);
michael@0 120 BSTR res = ::SysAllocStringByteLen(NULL, byteLen);
michael@0 121 memmove(res, m_str, byteLen);
michael@0 122 return res;
michael@0 123 }
michael@0 124 void Attach(BSTR src) { m_str = src; }
michael@0 125 BSTR Detach()
michael@0 126 {
michael@0 127 BSTR s = m_str;
michael@0 128 m_str = NULL;
michael@0 129 return s;
michael@0 130 }
michael@0 131 void Empty()
michael@0 132 {
michael@0 133 ::SysFreeString(m_str);
michael@0 134 m_str = NULL;
michael@0 135 }
michael@0 136 bool operator!() const { return (m_str == NULL); }
michael@0 137 };
michael@0 138
michael@0 139
michael@0 140 //////////////////////////////////////////////////////////
michael@0 141
michael@0 142 class CMyUnknownImp
michael@0 143 {
michael@0 144 public:
michael@0 145 ULONG __m_RefCount;
michael@0 146 CMyUnknownImp(): __m_RefCount(0) {}
michael@0 147 };
michael@0 148
michael@0 149 #define MY_QUERYINTERFACE_BEGIN STDMETHOD(QueryInterface) \
michael@0 150 (REFGUID iid, void **outObject) {
michael@0 151
michael@0 152 #define MY_QUERYINTERFACE_ENTRY(i) if (iid == IID_ ## i) \
michael@0 153 { *outObject = (void *)(i *)this; AddRef(); return S_OK; }
michael@0 154
michael@0 155 #define MY_QUERYINTERFACE_END return E_NOINTERFACE; }
michael@0 156
michael@0 157 #define MY_ADDREF_RELEASE \
michael@0 158 STDMETHOD_(ULONG, AddRef)() { return ++__m_RefCount; } \
michael@0 159 STDMETHOD_(ULONG, Release)() { if (--__m_RefCount != 0) \
michael@0 160 return __m_RefCount; delete this; return 0; }
michael@0 161
michael@0 162 #define MY_UNKNOWN_IMP_SPEC(i) \
michael@0 163 MY_QUERYINTERFACE_BEGIN \
michael@0 164 i \
michael@0 165 MY_QUERYINTERFACE_END \
michael@0 166 MY_ADDREF_RELEASE
michael@0 167
michael@0 168
michael@0 169 #define MY_UNKNOWN_IMP STDMETHOD(QueryInterface)(REFGUID, void **) { \
michael@0 170 MY_QUERYINTERFACE_END \
michael@0 171 MY_ADDREF_RELEASE
michael@0 172
michael@0 173 #define MY_UNKNOWN_IMP1(i) MY_UNKNOWN_IMP_SPEC( \
michael@0 174 MY_QUERYINTERFACE_ENTRY(i) \
michael@0 175 )
michael@0 176
michael@0 177 #define MY_UNKNOWN_IMP2(i1, i2) MY_UNKNOWN_IMP_SPEC( \
michael@0 178 MY_QUERYINTERFACE_ENTRY(i1) \
michael@0 179 MY_QUERYINTERFACE_ENTRY(i2) \
michael@0 180 )
michael@0 181
michael@0 182 #define MY_UNKNOWN_IMP3(i1, i2, i3) MY_UNKNOWN_IMP_SPEC( \
michael@0 183 MY_QUERYINTERFACE_ENTRY(i1) \
michael@0 184 MY_QUERYINTERFACE_ENTRY(i2) \
michael@0 185 MY_QUERYINTERFACE_ENTRY(i3) \
michael@0 186 )
michael@0 187
michael@0 188 #define MY_UNKNOWN_IMP4(i1, i2, i3, i4) MY_UNKNOWN_IMP_SPEC( \
michael@0 189 MY_QUERYINTERFACE_ENTRY(i1) \
michael@0 190 MY_QUERYINTERFACE_ENTRY(i2) \
michael@0 191 MY_QUERYINTERFACE_ENTRY(i3) \
michael@0 192 MY_QUERYINTERFACE_ENTRY(i4) \
michael@0 193 )
michael@0 194
michael@0 195 #define MY_UNKNOWN_IMP5(i1, i2, i3, i4, i5) MY_UNKNOWN_IMP_SPEC( \
michael@0 196 MY_QUERYINTERFACE_ENTRY(i1) \
michael@0 197 MY_QUERYINTERFACE_ENTRY(i2) \
michael@0 198 MY_QUERYINTERFACE_ENTRY(i3) \
michael@0 199 MY_QUERYINTERFACE_ENTRY(i4) \
michael@0 200 MY_QUERYINTERFACE_ENTRY(i5) \
michael@0 201 )
michael@0 202
michael@0 203 #endif

mercurial