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