AutoPtr.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. template <typename T, typename HeapAllocatorT = HeapAllocator>
  7. class AutoPtr : public BasePtr<T>
  8. {
  9. public:
  10. AutoPtr(T * ptr) : BasePtr<T>(ptr) {}
  11. ~AutoPtr()
  12. {
  13. Clear();
  14. }
  15. AutoPtr& operator=(T * ptr)
  16. {
  17. Clear();
  18. this->ptr = ptr;
  19. return *this;
  20. }
  21. private:
  22. void Clear()
  23. {
  24. if (this->ptr != nullptr)
  25. {
  26. AllocatorDelete(HeapAllocatorT, &HeapAllocatorT::Instance, this->ptr);
  27. this->ptr = nullptr;
  28. }
  29. }
  30. };
  31. template <typename T>
  32. class AutoArrayPtr : public BasePtr<T>
  33. {
  34. protected:
  35. size_t m_elementCount;
  36. public:
  37. AutoArrayPtr(T * ptr, size_t elementCount) : BasePtr<T>(ptr), m_elementCount(elementCount) {}
  38. ~AutoArrayPtr()
  39. {
  40. Clear();
  41. }
  42. void Set(T* ptr, int elementCount)
  43. {
  44. Clear();
  45. this->ptr = ptr;
  46. this->m_elementCount = elementCount;
  47. }
  48. private:
  49. void Clear()
  50. {
  51. if (this->ptr != nullptr)
  52. {
  53. HeapDeleteArray(m_elementCount, this->ptr);
  54. this->ptr = nullptr;
  55. }
  56. }
  57. };
  58. template <typename T>
  59. class AutoArrayAndItemsPtr : public AutoArrayPtr<T>
  60. {
  61. public:
  62. AutoArrayAndItemsPtr(T * ptr, size_t elementCount) : AutoArrayPtr<T>(ptr, elementCount) {}
  63. ~AutoArrayAndItemsPtr()
  64. {
  65. Clear();
  66. }
  67. private:
  68. void Clear()
  69. {
  70. if (this->ptr != nullptr){
  71. for (size_t i = 0; i < this->m_elementCount; i++)
  72. {
  73. if (this->ptr[i] != nullptr)
  74. {
  75. this->ptr[i]->CleanUp();
  76. this->ptr[i] = nullptr;
  77. }
  78. }
  79. HeapDeleteArray(this->m_elementCount, this->ptr);
  80. this->ptr = nullptr;
  81. }
  82. }
  83. };
  84. template <typename T>
  85. class AutoReleasePtr : public BasePtr<T>
  86. {
  87. using BasePtr<T>::ptr;
  88. public:
  89. AutoReleasePtr(T * ptr = nullptr) : BasePtr<T>(ptr) {}
  90. ~AutoReleasePtr()
  91. {
  92. Release();
  93. }
  94. void Release()
  95. {
  96. if (ptr != nullptr)
  97. {
  98. ptr->Release();
  99. this->ptr = nullptr;
  100. }
  101. }
  102. };
  103. template <typename T>
  104. class AutoCOMPtr : public AutoReleasePtr<T>
  105. {
  106. using BasePtr<T>::ptr;
  107. public:
  108. AutoCOMPtr(T * ptr = nullptr) : AutoReleasePtr<T>(ptr)
  109. {
  110. if (ptr != nullptr)
  111. {
  112. ptr->AddRef();
  113. }
  114. }
  115. AutoCOMPtr(const AutoCOMPtr<T>& other)
  116. {
  117. if ((ptr = other.ptr) != nullptr)
  118. {
  119. ptr->AddRef();
  120. }
  121. }
  122. template <class Q>
  123. HRESULT QueryInterface(Q** pp) const
  124. {
  125. return ptr->QueryInterface(__uuidof(Q), (void**)pp);
  126. }
  127. void Attach(T* p2)
  128. {
  129. if (ptr)
  130. {
  131. ptr->Release();
  132. }
  133. ptr = p2;
  134. }
  135. operator T*() const
  136. {
  137. return (T*)ptr;
  138. }
  139. T& operator*() const
  140. {
  141. Assert(ptr != nullptr);
  142. return *ptr;
  143. }
  144. T* operator=(T* lp)
  145. {
  146. return (T*)ComPtrAssign((IUnknown**)&ptr, lp);
  147. }
  148. T** operator&()
  149. {
  150. return &ptr;
  151. }
  152. void SetPtr(T* ptr)
  153. {
  154. this->ptr = ptr;
  155. }
  156. private:
  157. static IUnknown* ComPtrAssign(IUnknown** pp, IUnknown* lp)
  158. {
  159. if (pp == nullptr)
  160. {
  161. return nullptr;
  162. }
  163. if (lp != nullptr)
  164. {
  165. lp->AddRef();
  166. }
  167. if (*pp)
  168. {
  169. (*pp)->Release();
  170. }
  171. *pp = lp;
  172. return lp;
  173. }
  174. };
  175. template <typename T>
  176. class AutoDiscardPTR : public BasePtr<T>
  177. {
  178. public:
  179. AutoDiscardPTR(T * ptr) : BasePtr<T>(ptr) {}
  180. ~AutoDiscardPTR()
  181. {
  182. Clear();
  183. }
  184. AutoDiscardPTR& operator=(T * ptr)
  185. {
  186. Clear();
  187. this->ptr = ptr;
  188. return *this;
  189. }
  190. private:
  191. void Clear()
  192. {
  193. if (this->ptr != nullptr)
  194. {
  195. this->ptr->Discard();
  196. this->ptr = nullptr;
  197. }
  198. }
  199. };
  200. template <typename T>
  201. class AutoCoTaskMemFreePtr : public BasePtr<T>
  202. {
  203. public:
  204. AutoCoTaskMemFreePtr(T* ptr) : BasePtr<T>(ptr) {}
  205. ~AutoCoTaskMemFreePtr()
  206. {
  207. Clear();
  208. }
  209. private:
  210. void Clear()
  211. {
  212. if (this->ptr != nullptr)
  213. {
  214. CoTaskMemFree(this->ptr);
  215. this->ptr = nullptr;
  216. }
  217. }
  218. };