Index: lib/atl/atlbase.h =================================================================== --- lib/atl/atlbase.h (revision 69557) +++ lib/atl/atlbase.h (working copy) @@ -836,6 +836,94 @@ } }; + +// TODO: When someone needs it, make the allocator a template, so you can use it for both +// CoTask* allocations, and CRT-like allocations (malloc, realloc, free) +template +class CComHeapPtr +{ +public: + CComHeapPtr() + :m_Data(NULL) + { + } + + explicit CComHeapPtr(T *lp) + :m_Data(lp) + { + } + + explicit CComHeapPtr(CComHeapPtr &lp) + { + m_Data = lp.Detach(); + } + + ~CComHeapPtr() + { + Release(); + } + + T *operator = (CComHeapPtr &lp) + { + if(lp.m_Data != m_Data) + Attach(lp.Detach()); + return *this; + } + + bool Allocate(size_t nElements = 1) + { + ATLASSERT(m_Data == NULL); + m_Data = (T*)::CoTaskMemAlloc(nElements * sizeof(T)); + return m_Data != NULL; + } + + bool Reallocate(_In_ size_t nElements) + { + T* newData = (T*)::CoTaskMemRealloc(m_Data, nElements * sizeof(T)); + if(newData == NULL) + return false; + m_Data = newData; + return true; + } + + void Release() + { + if (m_Data) + { + ::CoTaskMemFree(m_Data); + m_Data = NULL; + } + } + + void Attach(T *lp) + { + Release(); + m_Data = lp; + } + + T *Detach() + { + T *saveP = m_Data; + m_Data = NULL; + return saveP; + } + + T **operator &() + { + ATLASSERT(m_Data == NULL); + return &m_Data; + } + + operator T* () const + { + return m_Data; + } + +protected: + T *m_Data; +}; + + class CComBSTR { public: