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