[ATL]
[reactos.git] / reactos / lib / atl / atlbase.h
index 02ceb81..2cb7a10 100644 (file)
@@ -22,6 +22,8 @@
 
 #include "atlcore.h"
 #include "statreg.h"
+#include "atlcomcli.h"
+#include "atlalloc.h"
 
 #ifdef _MSC_VER
 // It is common to use this in ATL constructors. They only store this for later use, so the usage is safe.
@@ -232,6 +234,80 @@ public:
     }
 };
 
+
+class CHandle
+{
+public:
+    HANDLE m_handle;
+
+public:
+    CHandle() :
+        m_handle(NULL)
+    {
+    }
+
+    CHandle(_Inout_ CHandle& handle) :
+        m_handle(NULL)
+    {
+        Attach(handle.Detach());
+    }
+
+    explicit CHandle(_In_ HANDLE handle) :
+        m_handle(handle)
+    {
+    }
+
+    ~CHandle()
+    {
+        if (m_handle)
+        {
+            Close();
+        }
+    }
+
+    CHandle& operator=(_Inout_ CHandle& handle)
+    {
+        if (this != &handle)
+        {
+            if (m_handle)
+            {
+                Close();
+            }
+            Attach(handle.Detach());
+        }
+
+        return *this;
+    }
+
+    operator HANDLE() const
+    {
+        return m_handle;
+    }
+
+    void Attach(_In_ HANDLE handle)
+    {
+        ATLASSERT(m_handle == NULL);
+        m_handle = handle;
+    }
+
+    HANDLE Detach()
+    {
+        HANDLE handle = m_handle;
+        m_handle = NULL;
+        return handle;
+    }
+
+    void Close()
+    {
+        if (m_handle)
+        {
+            ::CloseHandle(m_handle);
+            m_handle = NULL;
+        }
+    }
+};
+
+
 inline BOOL WINAPI InlineIsEqualUnknown(REFGUID rguid1)
 {
    return (
@@ -319,9 +395,9 @@ public:
 
 class CAtlModule : public _ATL_MODULE
 {
-protected:
-    static GUID m_libid;
 public:
+    static GUID m_libid;
+
     CAtlModule()
     {
         ATLASSERT(_pAtlModule == NULL);
@@ -742,138 +818,41 @@ public:
 
 extern CAtlWinModule _AtlWinModule;
 
-template<class T>
-class CComPtr
+class CComAllocator
 {
 public:
-    T *p;
-public:
-    CComPtr()
+    static void* Allocate(_In_ size_t size)
     {
-        p = NULL;
+        return ::CoTaskMemAlloc(size);
     }
 
-    CComPtr(T *lp)
+    static void* Reallocate(_In_opt_ void* ptr, _In_ size_t size)
     {
-        p = lp;
-        if (p != NULL)
-            p->AddRef();
+        return ::CoTaskMemRealloc(ptr, size);
     }
 
-    CComPtr(const CComPtr<T> &lp)
+    static void Free(_In_opt_ void* ptr)
     {
-        p = lp.p;
-        if (p != NULL)
-            p->AddRef();
-    }
-
-    ~CComPtr()
-    {
-        if (p != NULL)
-            p->Release();
-    }
-
-    T *operator = (T *lp)
-    {
-        if (p != NULL)
-            p->Release();
-        p = lp;
-        if (p != NULL)
-            p->AddRef();
-        return *this;
-    }
-
-    T *operator = (const CComPtr<T> &lp)
-    {
-        if (p != NULL)
-            p->Release();
-        p = lp.p;
-        if (p != NULL)
-            p->AddRef();
-        return *this;
-    }
-
-    void Release()
-    {
-        if (p != NULL)
-        {
-            p->Release();
-            p = NULL;
-        }
-    }
-
-    void Attach(T *lp)
-    {
-        if (p != NULL)
-            p->Release();
-        p = lp;
-    }
-
-    T *Detach()
-    {
-        T *saveP;
-
-        saveP = p;
-        p = NULL;
-        return saveP;
-    }
-
-    T **operator & ()
-    {
-        ATLASSERT(p == NULL);
-        return &p;
-    }
-
-    operator T * ()
-    {
-        return p;
-    }
-
-    T *operator -> ()
-    {
-        ATLASSERT(p != NULL);
-        return p;
+        ::CoTaskMemFree(ptr);
     }
 };
 
-class CComBSTR
-{
-public:
-    BSTR m_str;
-public:
-    CComBSTR(LPCOLESTR pSrc)
-    {
-        if (pSrc == NULL)
-            m_str = NULL;
-        else
-            m_str = ::SysAllocString(pSrc);
-    }
-    ~CComBSTR()
-    {
-        ::SysFreeString(m_str);
-        m_str = NULL;
-    }
-};
 
-class CComVariant : public tagVARIANT
+template<class T>
+class CComHeapPtr : public CHeapPtr<T, CComAllocator>
 {
 public:
-    CComVariant()
-    {
-        ::VariantInit(this);
-    }
-
-    ~CComVariant()
+    CComHeapPtr()
     {
-        Clear();
     }
 
-    HRESULT Clear()
+    explicit CComHeapPtr(T *lp) :
+        CHeapPtr<T, CComAllocator>(lp)
     {
-        return ::VariantClear(this);
     }
 };
 
+
 inline HRESULT __stdcall AtlAdvise(IUnknown *pUnkCP, IUnknown *pUnk, const IID &iid, LPDWORD pdw)
 {
     CComPtr<IConnectionPointContainer>        container;