| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- //-------------------------------------------------------------------------------------------------------
- // Copyright (C) Microsoft. All rights reserved.
- // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
- //-------------------------------------------------------------------------------------------------------
- #pragma once
- template <typename T, typename HeapAllocatorT = HeapAllocator>
- class AutoPtr : public BasePtr<T>
- {
- public:
- AutoPtr(T * ptr) : BasePtr<T>(ptr) {}
- ~AutoPtr()
- {
- Clear();
- }
- AutoPtr& operator=(T * ptr)
- {
- Clear();
- this->ptr = ptr;
- return *this;
- }
- private:
- void Clear()
- {
- if (this->ptr != nullptr)
- {
- AllocatorDelete(HeapAllocatorT, &HeapAllocatorT::Instance, this->ptr);
- this->ptr = nullptr;
- }
- }
- };
- template <typename T>
- class AutoArrayPtr : public BasePtr<T>
- {
- protected:
- size_t m_elementCount;
- public:
- AutoArrayPtr(T * ptr, size_t elementCount) : BasePtr<T>(ptr), m_elementCount(elementCount) {}
- ~AutoArrayPtr()
- {
- Clear();
- }
- void Set(T* ptr, int elementCount)
- {
- Clear();
- this->ptr = ptr;
- this->m_elementCount = elementCount;
- }
- private:
- void Clear()
- {
- if (this->ptr != nullptr)
- {
- HeapDeleteArray(m_elementCount, this->ptr);
- this->ptr = nullptr;
- }
- }
- };
- template <typename T>
- class AutoArrayAndItemsPtr : public AutoArrayPtr<T>
- {
- public:
- AutoArrayAndItemsPtr(T * ptr, size_t elementCount) : AutoArrayPtr<T>(ptr, elementCount) {}
- ~AutoArrayAndItemsPtr()
- {
- Clear();
- }
- private:
- void Clear()
- {
- if (this->ptr != nullptr){
- for (size_t i = 0; i < this->m_elementCount; i++)
- {
- if (this->ptr[i] != nullptr)
- {
- this->ptr[i]->CleanUp();
- this->ptr[i] = nullptr;
- }
- }
- HeapDeleteArray(this->m_elementCount, this->ptr);
- this->ptr = nullptr;
- }
- }
- };
- template <typename T>
- class AutoReleasePtr : public BasePtr<T>
- {
- using BasePtr<T>::ptr;
- public:
- AutoReleasePtr(T * ptr = nullptr) : BasePtr<T>(ptr) {}
- ~AutoReleasePtr()
- {
- Release();
- }
- void Release()
- {
- if (ptr != nullptr)
- {
- ptr->Release();
- this->ptr = nullptr;
- }
- }
- };
- template <typename T>
- class AutoCOMPtr : public AutoReleasePtr<T>
- {
- using BasePtr<T>::ptr;
- public:
- AutoCOMPtr(T * ptr = nullptr) : AutoReleasePtr<T>(ptr)
- {
- if (ptr != nullptr)
- {
- ptr->AddRef();
- }
- }
- AutoCOMPtr(const AutoCOMPtr<T>& other)
- {
- if ((ptr = other.ptr) != nullptr)
- {
- ptr->AddRef();
- }
- }
- template <class Q>
- HRESULT QueryInterface(Q** pp) const
- {
- return ptr->QueryInterface(__uuidof(Q), (void**)pp);
- }
- void Attach(T* p2)
- {
- if (ptr)
- {
- ptr->Release();
- }
- ptr = p2;
- }
- operator T*() const
- {
- return (T*)ptr;
- }
- T& operator*() const
- {
- Assert(ptr != nullptr);
- return *ptr;
- }
- T* operator=(T* lp)
- {
- return (T*)ComPtrAssign((IUnknown**)&ptr, lp);
- }
- T** operator&()
- {
- return &ptr;
- }
- void SetPtr(T* ptr)
- {
- this->ptr = ptr;
- }
- private:
- static IUnknown* ComPtrAssign(IUnknown** pp, IUnknown* lp)
- {
- if (pp == nullptr)
- {
- return nullptr;
- }
- if (lp != nullptr)
- {
- lp->AddRef();
- }
- if (*pp)
- {
- (*pp)->Release();
- }
-
- *pp = lp;
- return lp;
- }
- };
- template <typename T>
- class AutoDiscardPTR : public BasePtr<T>
- {
- public:
- AutoDiscardPTR(T * ptr) : BasePtr<T>(ptr) {}
- ~AutoDiscardPTR()
- {
- Clear();
- }
- AutoDiscardPTR& operator=(T * ptr)
- {
- Clear();
- this->ptr = ptr;
- return *this;
- }
- private:
- void Clear()
- {
- if (this->ptr != nullptr)
- {
- this->ptr->Discard();
- this->ptr = nullptr;
- }
- }
- };
- template <typename T>
- class AutoCoTaskMemFreePtr : public BasePtr<T>
- {
- public:
- AutoCoTaskMemFreePtr(T* ptr) : BasePtr<T>(ptr) {}
- ~AutoCoTaskMemFreePtr()
- {
- Clear();
- }
- private:
- void Clear()
- {
- if (this->ptr != nullptr)
- {
- CoTaskMemFree(this->ptr);
- this->ptr = nullptr;
- }
- }
- };
|