diff src/win32/7zip/7z/CPP/Common/MyCom.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/CPP/Common/MyCom.h	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,225 @@
     1.4 +// MyCom.h
     1.5 +
     1.6 +#ifndef __MYCOM_H
     1.7 +#define __MYCOM_H
     1.8 +
     1.9 +#include "MyWindows.h"
    1.10 +
    1.11 +#ifndef RINOK
    1.12 +#define RINOK(x) { HRESULT __result_ = (x); if (__result_ != S_OK) return __result_; }
    1.13 +#endif
    1.14 +
    1.15 +template <class T>
    1.16 +class CMyComPtr
    1.17 +{
    1.18 +  T* _p;
    1.19 +public:
    1.20 +  // typedef T _PtrClass;
    1.21 +  CMyComPtr() { _p = NULL;}
    1.22 +  CMyComPtr(T* p) {if ((_p = p) != NULL) p->AddRef(); }
    1.23 +  CMyComPtr(const CMyComPtr<T>& lp)
    1.24 +  {
    1.25 +    if ((_p = lp._p) != NULL)
    1.26 +      _p->AddRef();
    1.27 +  }
    1.28 +  ~CMyComPtr() { if (_p) _p->Release(); }
    1.29 +  void Release() { if (_p) { _p->Release(); _p = NULL; } }
    1.30 +  operator T*() const {  return (T*)_p;  }
    1.31 +  // T& operator*() const {  return *_p; }
    1.32 +  T** operator&() { return &_p; }
    1.33 +  T* operator->() const { return _p; }
    1.34 +  T* operator=(T* p)
    1.35 +  {
    1.36 +    if (p != 0)
    1.37 +      p->AddRef();
    1.38 +    if (_p)
    1.39 +      _p->Release();
    1.40 +    _p = p;
    1.41 +    return p;
    1.42 +  }
    1.43 +  T* operator=(const CMyComPtr<T>& lp) { return (*this = lp._p); }
    1.44 +  bool operator!() const { return (_p == NULL); }
    1.45 +  // bool operator==(T* pT) const {  return _p == pT; }
    1.46 +  // Compare two objects for equivalence
    1.47 +  void Attach(T* p2)
    1.48 +  {
    1.49 +    Release();
    1.50 +    _p = p2;
    1.51 +  }
    1.52 +  T* Detach()
    1.53 +  {
    1.54 +    T* pt = _p;
    1.55 +    _p = NULL;
    1.56 +    return pt;
    1.57 +  }
    1.58 +  #ifdef _WIN32
    1.59 +  HRESULT CoCreateInstance(REFCLSID rclsid, REFIID iid, LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL)
    1.60 +  {
    1.61 +    return ::CoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, (void**)&_p);
    1.62 +  }
    1.63 +  #endif
    1.64 +  /*
    1.65 +  HRESULT CoCreateInstance(LPCOLESTR szProgID, LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL)
    1.66 +  {
    1.67 +    CLSID clsid;
    1.68 +    HRESULT hr = CLSIDFromProgID(szProgID, &clsid);
    1.69 +    ATLASSERT(_p == NULL);
    1.70 +    if (SUCCEEDED(hr))
    1.71 +      hr = ::CoCreateInstance(clsid, pUnkOuter, dwClsContext, __uuidof(T), (void**)&_p);
    1.72 +    return hr;
    1.73 +  }
    1.74 +  */
    1.75 +  template <class Q>
    1.76 +  HRESULT QueryInterface(REFGUID iid, Q** pp) const
    1.77 +  {
    1.78 +    return _p->QueryInterface(iid, (void**)pp);
    1.79 +  }
    1.80 +};
    1.81 +
    1.82 +//////////////////////////////////////////////////////////
    1.83 +
    1.84 +inline HRESULT StringToBstr(LPCOLESTR src, BSTR *bstr)
    1.85 +{
    1.86 +  *bstr = ::SysAllocString(src);
    1.87 +  return (*bstr != 0) ? S_OK : E_OUTOFMEMORY;
    1.88 +}
    1.89 +
    1.90 +class CMyComBSTR
    1.91 +{
    1.92 +public:
    1.93 +  BSTR m_str;
    1.94 +  CMyComBSTR(): m_str(NULL) {}
    1.95 +  CMyComBSTR(LPCOLESTR src) { m_str = ::SysAllocString(src); }
    1.96 +  // CMyComBSTR(int nSize) { m_str = ::SysAllocStringLen(NULL, nSize); }
    1.97 +  // CMyComBSTR(int nSize, LPCOLESTR sz) { m_str = ::SysAllocStringLen(sz, nSize);  }
    1.98 +  CMyComBSTR(const CMyComBSTR& src) { m_str = src.MyCopy(); }
    1.99 +  /*
   1.100 +  CMyComBSTR(REFGUID src)
   1.101 +  {
   1.102 +    LPOLESTR szGuid;
   1.103 +    StringFromCLSID(src, &szGuid);
   1.104 +    m_str = ::SysAllocString(szGuid);
   1.105 +    CoTaskMemFree(szGuid);
   1.106 +  }
   1.107 +  */
   1.108 +  ~CMyComBSTR() { ::SysFreeString(m_str); }
   1.109 +  CMyComBSTR& operator=(const CMyComBSTR& src)
   1.110 +  {
   1.111 +    if (m_str != src.m_str)
   1.112 +    {
   1.113 +      if (m_str)
   1.114 +        ::SysFreeString(m_str);
   1.115 +      m_str = src.MyCopy();
   1.116 +    }
   1.117 +    return *this;
   1.118 +  }
   1.119 +  CMyComBSTR& operator=(LPCOLESTR src)
   1.120 +  {
   1.121 +    ::SysFreeString(m_str);
   1.122 +    m_str = ::SysAllocString(src);
   1.123 +    return *this;
   1.124 +  }
   1.125 +  unsigned int Length() const { return ::SysStringLen(m_str); }
   1.126 +  operator BSTR() const { return m_str; }
   1.127 +  BSTR* operator&() { return &m_str; }
   1.128 +  BSTR MyCopy() const
   1.129 +  {
   1.130 +    int byteLen = ::SysStringByteLen(m_str);
   1.131 +    BSTR res = ::SysAllocStringByteLen(NULL, byteLen);
   1.132 +    memcpy(res, m_str, byteLen);
   1.133 +    return res;
   1.134 +  }
   1.135 +  /*
   1.136 +  void Attach(BSTR src) { m_str = src; }
   1.137 +  BSTR Detach()
   1.138 +  {
   1.139 +    BSTR s = m_str;
   1.140 +    m_str = NULL;
   1.141 +    return s;
   1.142 +  }
   1.143 +  */
   1.144 +  void Empty()
   1.145 +  {
   1.146 +    ::SysFreeString(m_str);
   1.147 +    m_str = NULL;
   1.148 +  }
   1.149 +  bool operator!() const {  return (m_str == NULL); }
   1.150 +};
   1.151 +
   1.152 +//////////////////////////////////////////////////////////
   1.153 +
   1.154 +class CMyUnknownImp
   1.155 +{
   1.156 +public:
   1.157 +  ULONG __m_RefCount;
   1.158 +  CMyUnknownImp(): __m_RefCount(0) {}
   1.159 +};
   1.160 +
   1.161 +#define MY_QUERYINTERFACE_BEGIN STDMETHOD(QueryInterface) \
   1.162 +    (REFGUID iid, void **outObject) {
   1.163 +
   1.164 +#define MY_QUERYINTERFACE_ENTRY(i) if (iid == IID_ ## i) \
   1.165 +    { *outObject = (void *)(i *)this; AddRef(); return S_OK; }
   1.166 +
   1.167 +#define MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) if (iid == IID_IUnknown) \
   1.168 +    { *outObject = (void *)(IUnknown *)(i *)this; AddRef(); return S_OK; }
   1.169 +
   1.170 +#define MY_QUERYINTERFACE_BEGIN2(i) MY_QUERYINTERFACE_BEGIN \
   1.171 +    MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) \
   1.172 +    MY_QUERYINTERFACE_ENTRY(i)
   1.173 +
   1.174 +#define MY_QUERYINTERFACE_END return E_NOINTERFACE; }
   1.175 +
   1.176 +#define MY_ADDREF_RELEASE \
   1.177 +STDMETHOD_(ULONG, AddRef)() { return ++__m_RefCount; } \
   1.178 +STDMETHOD_(ULONG, Release)() { if (--__m_RefCount != 0)  \
   1.179 +  return __m_RefCount; delete this; return 0; }
   1.180 +
   1.181 +#define MY_UNKNOWN_IMP_SPEC(i) \
   1.182 +  MY_QUERYINTERFACE_BEGIN \
   1.183 +  i \
   1.184 +  MY_QUERYINTERFACE_END \
   1.185 +  MY_ADDREF_RELEASE
   1.186 +
   1.187 +
   1.188 +#define MY_UNKNOWN_IMP MY_QUERYINTERFACE_BEGIN \
   1.189 +  MY_QUERYINTERFACE_ENTRY_UNKNOWN(IUnknown) \
   1.190 +  MY_QUERYINTERFACE_END \
   1.191 +  MY_ADDREF_RELEASE
   1.192 +
   1.193 +#define MY_UNKNOWN_IMP1(i) MY_UNKNOWN_IMP_SPEC( \
   1.194 +  MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) \
   1.195 +  MY_QUERYINTERFACE_ENTRY(i) \
   1.196 +  )
   1.197 +
   1.198 +#define MY_UNKNOWN_IMP2(i1, i2) MY_UNKNOWN_IMP_SPEC( \
   1.199 +  MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \
   1.200 +  MY_QUERYINTERFACE_ENTRY(i1) \
   1.201 +  MY_QUERYINTERFACE_ENTRY(i2) \
   1.202 +  )
   1.203 +
   1.204 +#define MY_UNKNOWN_IMP3(i1, i2, i3) MY_UNKNOWN_IMP_SPEC( \
   1.205 +  MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \
   1.206 +  MY_QUERYINTERFACE_ENTRY(i1) \
   1.207 +  MY_QUERYINTERFACE_ENTRY(i2) \
   1.208 +  MY_QUERYINTERFACE_ENTRY(i3) \
   1.209 +  )
   1.210 +
   1.211 +#define MY_UNKNOWN_IMP4(i1, i2, i3, i4) MY_UNKNOWN_IMP_SPEC( \
   1.212 +  MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \
   1.213 +  MY_QUERYINTERFACE_ENTRY(i1) \
   1.214 +  MY_QUERYINTERFACE_ENTRY(i2) \
   1.215 +  MY_QUERYINTERFACE_ENTRY(i3) \
   1.216 +  MY_QUERYINTERFACE_ENTRY(i4) \
   1.217 +  )
   1.218 +
   1.219 +#define MY_UNKNOWN_IMP5(i1, i2, i3, i4, i5) MY_UNKNOWN_IMP_SPEC( \
   1.220 +  MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \
   1.221 +  MY_QUERYINTERFACE_ENTRY(i1) \
   1.222 +  MY_QUERYINTERFACE_ENTRY(i2) \
   1.223 +  MY_QUERYINTERFACE_ENTRY(i3) \
   1.224 +  MY_QUERYINTERFACE_ENTRY(i4) \
   1.225 +  MY_QUERYINTERFACE_ENTRY(i5) \
   1.226 +  )
   1.227 +
   1.228 +#endif