Leaked source code of windows server 2003
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.

369 lines
10 KiB

  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Filename : AutoPtr.h
  4. // Purpose : To supply auto pointers of different kinds.
  5. // CAutoPointer<T, Deletor> Generic pointer needs a deletor
  6. // class to instaciate.
  7. // CAutoMallocPonter<T> A malloc allocated pointer.
  8. // CAutoClassPointer<T> A "new" allocated pointer
  9. // CAutoArrayPointer<T> A "new[]" allocated pointer
  10. // CAutoOlePointer<T> A pointer that should be
  11. // "Release()".
  12. //
  13. // Project : PersistentQuery
  14. // Component: Common
  15. //
  16. // Author : urib
  17. //
  18. // Log:
  19. //
  20. // Jan 15 1997 urib Creation
  21. // Jan 19 1997 urib Fix Ole pointer. Enable instanciation without
  22. // ownership
  23. // Jun 9 1997 urib Better OlePointer. Some safety fixes.
  24. // Nov 17 1997 urib Add ole task pointer.
  25. // Jun 30 1998 dovh Add Assert to CAutoPointer operator=
  26. // Feb 25 1999 urib Add smart pointer typedef macro.
  27. // Jun 23 1999 urib Add equality op.
  28. // Aug 5 1999 urib Fix a memory leak bug in the assignment operator
  29. // of CAutoPointer. Add assignment operation
  30. // creation macro.
  31. // Dec 1 1999 urib Change return type from int to bool in IsValid.
  32. //
  33. ////////////////////////////////////////////////////////////////////////////////
  34. #ifndef AUTOPTR_H
  35. #define AUTOPTR_H
  36. #include "tracer.h"
  37. #include <comdef.h>
  38. #pragma once
  39. ////////////////////////////////////////////////////////////////////////////////
  40. //
  41. // return type for 'identifier::operator ->'is not a UDT or reference to a UDT.
  42. // Will produce errors if applied using infix notation
  43. //
  44. ////////////////////////////////////////////////////////////////////////////////
  45. #pragma warning(disable: 4284 4786)
  46. template<class T, class Deletor>
  47. class CAutoPointer
  48. {
  49. protected:
  50. typedef Deletor m_Deletor;
  51. public:
  52. typedef T m_PointerType;
  53. // Constructors
  54. CAutoPointer(T* pt = NULL, BOOL fOwnMemory = TRUE)
  55. :m_fIOwnTheMemory(fOwnMemory && (pt != NULL))
  56. ,m_ptThePointer(pt) {}
  57. CAutoPointer(const CAutoPointer<T, Deletor>& acp)
  58. {
  59. m_fIOwnTheMemory = acp.m_fIOwnTheMemory;
  60. m_ptThePointer = acp.Detach();
  61. }
  62. // Assignemnt operation.
  63. CAutoPointer<T, Deletor>&
  64. operator=(const CAutoPointer<T, Deletor>& acp)
  65. {
  66. if (m_ptThePointer != acp.m_ptThePointer)
  67. {
  68. if (m_fIOwnTheMemory)
  69. Deletor::DeleteOperation(m_ptThePointer);
  70. m_fIOwnTheMemory = acp.m_fIOwnTheMemory;
  71. m_ptThePointer = acp.Detach();
  72. }
  73. else
  74. {
  75. Assert( (!m_fIOwnTheMemory) || acp.m_fIOwnTheMemory );
  76. // Note: R.H.S "inherits" memory oenership from L.H.S.,
  77. // and L.H.S. ownership is cancelled by Detach!
  78. bool ftmp = acp.m_fIOwnTheMemory;
  79. acp.Detach();
  80. m_fIOwnTheMemory = ftmp;
  81. }
  82. return (*this);
  83. }
  84. CAutoPointer<T, Deletor>&
  85. operator=(int null)
  86. {
  87. Assert(null == 0);
  88. return operator=(reinterpret_cast<T*>(NULL));
  89. }
  90. bool
  91. operator==(const CAutoPointer<T, Deletor>& acp)
  92. {
  93. return m_ptThePointer == acp.m_ptThePointer;
  94. }
  95. // If it is our memory delete the pointer.
  96. ~CAutoPointer()
  97. {
  98. if(m_fIOwnTheMemory)
  99. Deletor::DeleteOperation(m_ptThePointer);
  100. }
  101. // Return the pointer and mark that it is no longer our memory.
  102. T*
  103. Detach() const
  104. {
  105. // This is to escape the const restriction. We don't change the pointer
  106. // but we still do the marking.
  107. ((CAutoPointer<T, Deletor>*)this)->m_fIOwnTheMemory = FALSE;
  108. return (m_ptThePointer);
  109. }
  110. // Return the actual pointer if you want to use it someplace
  111. T*
  112. Get() const
  113. {
  114. return m_ptThePointer;
  115. }
  116. // Return if the pointer is valid.
  117. bool
  118. IsValid()
  119. {
  120. return !!m_ptThePointer;
  121. }
  122. // Indirection
  123. T&
  124. operator *() const
  125. {
  126. return * Get();
  127. }
  128. // Dereference
  129. T*
  130. operator ->() const
  131. {
  132. return Get();
  133. }
  134. protected:
  135. // The pointer to keep.
  136. T* m_ptThePointer;
  137. // Is the memory ours?
  138. bool m_fIOwnTheMemory;
  139. };
  140. #define CONSTRUCTORS(AutoPointer) \
  141. \
  142. AutoPointer(m_PointerType* pt = NULL, \
  143. BOOL fOwnMemory = TRUE) \
  144. :CAutoPointer<m_PointerType, m_Deletor>(pt, fOwnMemory) \
  145. { \
  146. } \
  147. \
  148. AutoPointer(const AutoPointer<m_PointerType>& aop) \
  149. :CAutoPointer<m_PointerType, m_Deletor>(aop) \
  150. { \
  151. } \
  152. #define ASSIGNMENT_OPERATORS(AutoPointer) \
  153. \
  154. AutoPointer<m_PointerType>& \
  155. operator=(const AutoPointer<m_PointerType>& acp) \
  156. { \
  157. CAutoPointer<m_PointerType, m_Deletor>::operator=(acp); \
  158. \
  159. return *this; \
  160. } \
  161. \
  162. AutoPointer<m_PointerType>& \
  163. operator=(int null) \
  164. { \
  165. CAutoPointer<m_PointerType, m_Deletor>::operator=(null); \
  166. \
  167. return *this; \
  168. }
  169. ////////////////////////////////////////////////////////////////////////////////
  170. //
  171. // CAutoClassPointer class definition
  172. //
  173. ////////////////////////////////////////////////////////////////////////////////
  174. template <class T>
  175. class CClassDeletor
  176. {
  177. public:
  178. static
  179. void
  180. DeleteOperation(T* pt)
  181. {
  182. if (pt)
  183. delete pt;
  184. }
  185. };
  186. template<class T>
  187. class CAutoClassPointer : public CAutoPointer<T, CClassDeletor<T> >
  188. {
  189. public:
  190. CONSTRUCTORS(CAutoClassPointer);
  191. ASSIGNMENT_OPERATORS(CAutoClassPointer);
  192. };
  193. ////////////////////////////////////////////////////////////////////////////////
  194. //
  195. // CAutoOlePointer class definition
  196. //
  197. ////////////////////////////////////////////////////////////////////////////////
  198. template <class T>
  199. class COleDeletor
  200. {
  201. public:
  202. static
  203. void
  204. DeleteOperation(T* pt)
  205. {
  206. if (pt)
  207. pt->Release();
  208. }
  209. };
  210. template<class T>
  211. class CAutoOlePointer : public CAutoPointer<T, COleDeletor<T> >
  212. {
  213. public:
  214. CONSTRUCTORS(CAutoOlePointer);
  215. ASSIGNMENT_OPERATORS(CAutoOlePointer);
  216. public:
  217. T** operator &()
  218. {
  219. m_fIOwnTheMemory = TRUE;
  220. return &m_ptThePointer;
  221. }
  222. };
  223. ////////////////////////////////////////////////////////////////////////////////
  224. //
  225. // CAutoTaskPointer class definition
  226. //
  227. ////////////////////////////////////////////////////////////////////////////////
  228. template <class T>
  229. class CTaskDeletor
  230. {
  231. public:
  232. static
  233. void
  234. DeleteOperation(T* pt)
  235. {
  236. if (pt)
  237. CoTaskMemFree(pt);
  238. }
  239. };
  240. template<class T>
  241. class CAutoTaskPointer : public CAutoPointer<T, CTaskDeletor<T> >
  242. {
  243. public:
  244. CONSTRUCTORS(CAutoTaskPointer);
  245. ASSIGNMENT_OPERATORS(CAutoTaskPointer);
  246. public:
  247. T** operator &()
  248. {
  249. m_fIOwnTheMemory = TRUE;
  250. return &m_ptThePointer;
  251. }
  252. };
  253. ////////////////////////////////////////////////////////////////////////////////
  254. //
  255. // CAutoMallocPointer class definition
  256. //
  257. ////////////////////////////////////////////////////////////////////////////////
  258. template <class T>
  259. class CMallocDeletor
  260. {
  261. public:
  262. static
  263. void
  264. DeleteOperation(T* pt)
  265. {
  266. if (pt)
  267. free(pt);
  268. }
  269. };
  270. template<class T>
  271. class CAutoMallocPointer : public CAutoPointer<T, CMallocDeletor<T> >
  272. {
  273. public:
  274. CONSTRUCTORS(CAutoMallocPointer);
  275. ASSIGNMENT_OPERATORS(CAutoMallocPointer);
  276. public:
  277. T& operator[](size_t n)
  278. {
  279. return *(Get() + n);
  280. }
  281. };
  282. ////////////////////////////////////////////////////////////////////////////////
  283. //
  284. // CAutoArrayPointer class definition
  285. //
  286. ////////////////////////////////////////////////////////////////////////////////
  287. template <class T>
  288. class CArrayDeletor
  289. {
  290. public:
  291. static
  292. void
  293. DeleteOperation(T* pt)
  294. {
  295. if (pt)
  296. delete[] pt;
  297. }
  298. };
  299. template<class T>
  300. class CAutoArrayPointer : public CAutoPointer<T, CArrayDeletor<T> >
  301. {
  302. public:
  303. CONSTRUCTORS(CAutoArrayPointer);
  304. ASSIGNMENT_OPERATORS(CAutoArrayPointer);
  305. public:
  306. T& operator[](size_t n)
  307. {
  308. return *(Get() + n);
  309. }
  310. };
  311. //
  312. // Simple macro to define the standard COM pointer.
  313. //
  314. #define PQ_COM_SMARTPTR_TYPEDEF(Interface) \
  315. _COM_SMARTPTR_TYPEDEF(Interface, IID_##Interface)
  316. #endif /* AUTOPTR_H */