Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
4.2 KiB

  1. #ifndef _INC_DSKQUOTA_AUTOPTR_H
  2. #define _INC_DSKQUOTA_AUTOPTR_H
  3. ///////////////////////////////////////////////////////////////////////////////
  4. /* File: autoptr.h
  5. Description: Template auto pointer classes to support normal C++ pointers
  6. as well as shell and COM object pointers.
  7. This code was created by DavePl for the Entertainment Center project.
  8. It worked very well so I've "borrowed" it (thanks Dave). I think his
  9. original implementation borrowed from the STL implementation.
  10. Revision History:
  11. Date Description Programmer
  12. -------- --------------------------------------------------- ----------
  13. 07/01/97 Initial creation. BrianAu
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////
  16. // a_ptr
  17. //
  18. // Safe pointer class that knows to delete the referrent object when
  19. // the pointer goes out of scope or is replaced, etc.
  20. #if _MSC_VER >= 1200
  21. #pragma warning(push)
  22. #endif
  23. #pragma warning(disable:4284)
  24. template<class _TYPE> class a_ptr
  25. {
  26. public:
  27. typedef _TYPE element_type;
  28. a_ptr(_TYPE *_P = 0)
  29. : _Owns(_P != 0), _Ptr(_P)
  30. {}
  31. typedef _TYPE _U;
  32. a_ptr(const a_ptr<_U>& _Y) : _Owns(_Y._Owns), _Ptr((_TYPE *)_Y.disown())
  33. {}
  34. virtual void nukeit() = 0
  35. {
  36. }
  37. a_ptr<_TYPE>& operator=(const a_ptr<_U>& _Y)
  38. {
  39. if ((void *)this != (void *)&_Y)
  40. {
  41. if (_Owns)
  42. nukeit();
  43. _Owns = _Y._Owns;
  44. _Ptr = (_TYPE *)_Y.disown();
  45. // ASSERT( !_Owns || _Ptr );
  46. }
  47. return (*this);
  48. }
  49. a_ptr<_TYPE>& replace(const a_ptr<_U>& _Y)
  50. {
  51. return *this = _Y;
  52. }
  53. virtual ~a_ptr()
  54. {
  55. }
  56. operator _TYPE*()
  57. {
  58. return get();
  59. }
  60. operator const _TYPE*() const
  61. {
  62. return get();
  63. }
  64. _TYPE& operator*() const
  65. {
  66. return (*get());
  67. }
  68. _TYPE *operator->() const
  69. {
  70. return (get());
  71. }
  72. _TYPE *get() const
  73. {
  74. return (_Ptr);
  75. }
  76. _TYPE *disown() const
  77. {
  78. ((a_ptr<_TYPE> *)this)->_Owns = FALSE;
  79. return (_Ptr);
  80. }
  81. _TYPE ** getaddr()
  82. {
  83. *this = (_TYPE *) NULL;
  84. _Owns = TRUE;
  85. return (&_Ptr);
  86. }
  87. protected:
  88. BOOL _Owns;
  89. _TYPE *_Ptr;
  90. };
  91. #if _MSC_VER >= 1200
  92. #pragma warning(pop)
  93. #else
  94. #pragma warning(default:4284)
  95. #endif
  96. // autoptr
  97. //
  98. template<class _TYPE>
  99. class autoptr : public a_ptr<_TYPE>
  100. {
  101. virtual void nukeit()
  102. {
  103. delete _Ptr;
  104. }
  105. public:
  106. ~autoptr()
  107. {
  108. if (_Owns)
  109. this->nukeit();
  110. }
  111. autoptr(_TYPE *_P = 0)
  112. : a_ptr<_TYPE>(_P)
  113. {
  114. }
  115. };
  116. template<class _TYPE>
  117. class array_autoptr : public a_ptr<_TYPE>
  118. {
  119. virtual void nukeit()
  120. {
  121. if (_Ptr)
  122. delete[] _Ptr;
  123. }
  124. public:
  125. ~array_autoptr()
  126. {
  127. if (_Owns)
  128. this->nukeit();
  129. }
  130. array_autoptr(_TYPE *_P = 0)
  131. : a_ptr<_TYPE>(_P)
  132. {
  133. }
  134. };
  135. // sh_autoptr
  136. //
  137. // Smart pointer that manually runs the referent's destructor and then
  138. // calls the shell's task allocator to free the object's memory footprint
  139. template<class _TYPE>
  140. class sh_autoptr : virtual public a_ptr<_TYPE>
  141. {
  142. virtual void nukeit()
  143. {
  144. if (_Ptr)
  145. {
  146. IMalloc *pMalloc;
  147. _Ptr->~_TYPE();
  148. if (SUCCEEDED(SHGetMalloc(&pMalloc)))
  149. {
  150. pMalloc->Free(_Ptr);
  151. pMalloc->Release();
  152. }
  153. }
  154. }
  155. public:
  156. ~sh_autoptr()
  157. {
  158. if (_Owns)
  159. this->nukeit();
  160. }
  161. sh_autoptr(_TYPE *_P = 0)
  162. : a_ptr<_TYPE>(_P)
  163. {
  164. }
  165. };
  166. // com_autoptr (nothing to do with ole automation... its an automatic ole ptr)
  167. //
  168. // Smart pointer that calls disown() on the referent when the pointer itself
  169. // goes out of scope
  170. template<class _TYPE>
  171. class com_autoptr : public a_ptr<_TYPE>
  172. {
  173. virtual void nukeit()
  174. {
  175. if (_Ptr)
  176. _Ptr->Release();
  177. }
  178. public:
  179. ~com_autoptr()
  180. {
  181. if (_Owns)
  182. this->nukeit();
  183. }
  184. com_autoptr(_TYPE *_P = 0)
  185. : a_ptr<_TYPE>(_P)
  186. {
  187. }
  188. };
  189. #endif // _INC_DSKQUOTA_AUTOPTR_H