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.

406 lines
8.9 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright � Microsoft Corporation. All rights reserved.
  4. //
  5. // RefPtrCo.h
  6. //
  7. // Purpose: definition of TRefPointerCollection template
  8. //
  9. //***************************************************************************
  10. #if _MSC_VER > 1000
  11. #pragma once
  12. #endif
  13. #ifndef __REFPTRCOLLECTION_H__
  14. #define __REFPTRCOLLECTION_H__
  15. #include <chptrarr.h>
  16. // Enumeration helpers
  17. typedef DWORD REFPTRCOLLECTION_POSITION;
  18. #define REFPTRCOLLECTION_START 0xFFFFFFFF;
  19. template <class TYPED_PTR> class TRefPointerCollection : public CThreadBase
  20. {
  21. public:
  22. // Construction/Destruction
  23. TRefPointerCollection();
  24. ~TRefPointerCollection();
  25. TRefPointerCollection(const TRefPointerCollection& trpc);
  26. // Allows addition and enumeration of collection
  27. BOOL Add( TYPED_PTR* ptr );
  28. BOOL BeginEnum( REFPTRCOLLECTION_POSITION& pos );
  29. TYPED_PTR* GetNext( REFPTRCOLLECTION_POSITION& pos );
  30. void EndEnum( void );
  31. void Empty( void );
  32. int GetSize( void ) const;
  33. protected:
  34. // Allows easy and quick transference of data (it was =, but
  35. // because we'll inherit classes off the template, we won't
  36. // inherit that particular overload (some C++ thingie)
  37. const TRefPointerCollection<TYPED_PTR>& Copy( const TRefPointerCollection<TYPED_PTR>& );
  38. private:
  39. CHPtrArray m_ptrArray;
  40. };
  41. ////////////////////////////////////////////////////////////////////////
  42. //
  43. // Function: TRefPointerCollection::TRefPointerCollection
  44. //
  45. // Class Constructor.
  46. //
  47. // Inputs: None.
  48. //
  49. // Outputs: None.
  50. //
  51. // Return: None.
  52. //
  53. // Comments: None.
  54. //
  55. ////////////////////////////////////////////////////////////////////////
  56. template <class TYPED_PTR>
  57. TRefPointerCollection<TYPED_PTR>::TRefPointerCollection( void )
  58. : CThreadBase(),
  59. m_ptrArray()
  60. {
  61. }
  62. ////////////////////////////////////////////////////////////////////////
  63. //
  64. // Function: CRefPointerCollection::~CRefPointerCollection
  65. //
  66. // Class Destructor.
  67. //
  68. // Inputs: None.
  69. //
  70. // Outputs: None.
  71. //
  72. // Return: None.
  73. //
  74. // Comments: None.
  75. //
  76. ////////////////////////////////////////////////////////////////////////
  77. template <class TYPED_PTR>
  78. TRefPointerCollection<TYPED_PTR>::~TRefPointerCollection( void )
  79. {
  80. Empty();
  81. }
  82. ////////////////////////////////////////////////////////////////////////
  83. //
  84. // Function: CRefPointerCollection::CRefPointerCollection
  85. // Copy constructor
  86. //
  87. // Inputs: None.
  88. //
  89. // Outputs: None.
  90. //
  91. // Return: None.
  92. //
  93. // Comments: None.
  94. //
  95. ////////////////////////////////////////////////////////////////////////
  96. template <class TYPED_PTR>
  97. TRefPointerCollection<TYPED_PTR>::TRefPointerCollection(
  98. const TRefPointerCollection& trpc)
  99. {
  100. Copy(trpc);
  101. }
  102. ////////////////////////////////////////////////////////////////////////
  103. //
  104. // Function: TRefPointerCollection::Add
  105. //
  106. // Adds a new referenced pointer to the collection.
  107. //
  108. // Inputs: T* ptr - Pointer to add.
  109. //
  110. // Outputs: None.
  111. //
  112. // Return: TRUE/FALSE Success/Failure of Add.
  113. //
  114. // Comments: AddRefs the pointer, then adds it to the array. We
  115. // will need Write Access to do this.
  116. //
  117. ////////////////////////////////////////////////////////////////////////
  118. template <class TYPED_PTR>
  119. BOOL TRefPointerCollection<TYPED_PTR>::Add( TYPED_PTR* ptr )
  120. {
  121. BOOL fReturn = FALSE;
  122. if ( NULL != ptr )
  123. {
  124. // Get write access
  125. if ( BeginWrite() )
  126. {
  127. try
  128. {
  129. // If Add succeeds, the pointer will be released when it
  130. // is removed.
  131. ptr->AddRef();
  132. if ( m_ptrArray.Add( (void*) ptr ) >= 0 )
  133. {
  134. fReturn = TRUE;
  135. }
  136. else
  137. {
  138. ptr->Release(); // Add failed, so Release the AddRef
  139. }
  140. }
  141. catch ( ... )
  142. {
  143. EndWrite() ;
  144. throw;
  145. }
  146. EndWrite(); // Release the BeginWrite()
  147. }
  148. }
  149. return fReturn;
  150. }
  151. ////////////////////////////////////////////////////////////////////////
  152. //
  153. // Function: TRefPointerCollection::BeginEnum
  154. //
  155. // Gains Read Access to the collection, then returns an appropriate
  156. // REFPTRCOLLECTION_POSITION to get the first index in the array.
  157. //
  158. // Inputs: None.
  159. //
  160. // Outputs: REFPTRCOLLECTION_POSITION& pos - Position we retrieved.
  161. //
  162. // Return: BOOL TRUE/FALSE - Access was granted
  163. //
  164. // Comments: We need Read Access to do this. This can effectively
  165. // lock out other threads.
  166. //
  167. ////////////////////////////////////////////////////////////////////////
  168. template <class TYPED_PTR>
  169. BOOL TRefPointerCollection<TYPED_PTR>::BeginEnum( REFPTRCOLLECTION_POSITION& pos )
  170. {
  171. BOOL fReturn = FALSE;
  172. if ( BeginRead() )
  173. {
  174. pos = REFPTRCOLLECTION_START;
  175. fReturn = TRUE;
  176. }
  177. return fReturn;
  178. }
  179. ////////////////////////////////////////////////////////////////////////
  180. //
  181. // Function: TRefPointerCollection::EndEnum
  182. //
  183. // Signals the end of an enumeration.
  184. //
  185. // Inputs: None.
  186. //
  187. // Outputs: None.
  188. //
  189. // Return: BOOL TRUE/FALSE - Access was granted
  190. //
  191. // Comments: Ends Read Access granted by calling BeginEnum().
  192. //
  193. ////////////////////////////////////////////////////////////////////////
  194. template <class TYPED_PTR>
  195. void TRefPointerCollection<TYPED_PTR>::EndEnum( void )
  196. {
  197. EndRead();
  198. }
  199. ////////////////////////////////////////////////////////////////////////
  200. //
  201. // Function: TRefPointerCollection::GetNext
  202. //
  203. // Uses the REFPTRCOLLECTION_POSITION to get the next index in the
  204. // collection.
  205. //
  206. // Inputs: None.
  207. //
  208. // Outputs: REFPTRCOLLECTION_POSITION& pos - Position we retrieved.
  209. //
  210. // Return: T* NULL if failure.
  211. //
  212. // Comments: We need Read Access to do this. The pointer is AddRef'd
  213. // on the way out. User must Release the pointer himself.
  214. //
  215. ////////////////////////////////////////////////////////////////////////
  216. template <class TYPED_PTR>
  217. TYPED_PTR* TRefPointerCollection<TYPED_PTR>::GetNext( REFPTRCOLLECTION_POSITION& pos )
  218. {
  219. TYPED_PTR* ptr = NULL;
  220. if ( BeginRead() )
  221. {
  222. if ( ++pos < (DWORD) m_ptrArray.GetSize() )
  223. {
  224. ptr = (TYPED_PTR*) m_ptrArray.GetAt( pos );
  225. if ( NULL != ptr )
  226. {
  227. ptr->AddRef();
  228. }
  229. }
  230. EndRead();
  231. }
  232. return ptr;
  233. }
  234. ////////////////////////////////////////////////////////////////////////
  235. //
  236. // Function: TRefPointerCollection::Empty
  237. //
  238. // Empties out the collection, Releasing Pointers as it does do.
  239. //
  240. // Inputs: None.
  241. //
  242. // Outputs: None.
  243. //
  244. // Return: None.
  245. //
  246. // Comments: We need Write Access to do this.
  247. //
  248. ////////////////////////////////////////////////////////////////////////
  249. template <class TYPED_PTR>
  250. void TRefPointerCollection<TYPED_PTR>::Empty( void )
  251. {
  252. // By default this is an infinite wait, so it best come back
  253. BeginWrite();
  254. try
  255. {
  256. int nSize = m_ptrArray.GetSize();
  257. // Only empty it if it is not empty
  258. if ( nSize > 0 )
  259. {
  260. TYPED_PTR* ptr = NULL;
  261. for ( int nCtr = 0; nCtr < nSize; nCtr++ )
  262. {
  263. ptr = (TYPED_PTR*) m_ptrArray[nCtr];
  264. if ( NULL != ptr )
  265. {
  266. ptr->Release(); // AddRef we did when we added it
  267. }
  268. }
  269. // Now dump the array
  270. m_ptrArray.RemoveAll();
  271. } // IF nSize > 0
  272. }
  273. catch ( ... )
  274. {
  275. EndWrite() ;
  276. throw;
  277. }
  278. EndWrite();
  279. }
  280. ////////////////////////////////////////////////////////////////////////
  281. //
  282. // Function: TRefPointerCollection::Copy
  283. //
  284. // Empties out the collection, copies in another one, addrefing
  285. // pointers as we go.
  286. //
  287. // Inputs: const T& collection
  288. //
  289. // Outputs: None.
  290. //
  291. // Return: const T& this
  292. //
  293. // Comments: We need Write Access to do this.
  294. //
  295. ////////////////////////////////////////////////////////////////////////
  296. template <class TYPED_PTR>
  297. const TRefPointerCollection<TYPED_PTR>& TRefPointerCollection<TYPED_PTR>::Copy( const TRefPointerCollection<TYPED_PTR>& collection )
  298. {
  299. // By default this is an infinite wait, so it best come back
  300. BeginWrite();
  301. try
  302. {
  303. // Dump out the array
  304. Empty();
  305. int nSize = collection.m_ptrArray.GetSize();
  306. for ( int nCount = 0; nCount < nSize; nCount++ )
  307. {
  308. TYPED_PTR* ptr = (TYPED_PTR*) collection.m_ptrArray[nCount];
  309. // Add will automatically AddRef the pointer again.
  310. Add( ptr );
  311. }
  312. }
  313. catch ( ... )
  314. {
  315. EndWrite() ;
  316. throw;
  317. }
  318. EndWrite();
  319. return *this;
  320. }
  321. ////////////////////////////////////////////////////////////////////////
  322. //
  323. // Function: TRefPointerCollection::GetSize
  324. //
  325. // Inputs: None.
  326. //
  327. // Outputs: Number of elements in the collection
  328. //
  329. // Return: None.
  330. //
  331. // Comments: None.
  332. //
  333. ////////////////////////////////////////////////////////////////////////
  334. template <class TYPED_PTR>
  335. int TRefPointerCollection<TYPED_PTR>::GetSize(void) const
  336. {
  337. return m_ptrArray.GetSize();
  338. }
  339. #endif