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.

475 lines
11 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. ARRTEMPL.H
  5. Abstract:
  6. This file defines a simple template for an array of arbitrary pointers.
  7. Actual growing array functionality is provided by CFlexArray.
  8. Classes defined:
  9. template CPointerArray
  10. History:
  11. 11/27/96 a-levn Compiles.
  12. --*/
  13. #ifndef __ARRAY_TEMPLATE__H_
  14. #define __ARRAY_TEMPLATE__H_
  15. #pragma warning(disable:4786)
  16. #include <map>
  17. #include <WT_strutils.h>
  18. using namespace std;
  19. class wcsless : public binary_function<LPWSTR, LPWSTR, bool>
  20. {
  21. public:
  22. bool operator()(const LPWSTR& wcs1, const LPWSTR& wcs2) const
  23. {return wcscmp(wcs1, wcs2) < 0;}
  24. };
  25. class wcsiless : public binary_function<LPWSTR, LPWSTR, bool>
  26. {
  27. public:
  28. bool operator()(const LPWSTR& wcs1, const LPWSTR& wcs2) const
  29. {return wbem_wcsicmp(wcs1, wcs2) < 0;}
  30. };
  31. class CReleaseMe
  32. {
  33. protected:
  34. IUnknown* m_pUnk;
  35. public:
  36. CReleaseMe(IUnknown* pUnk) : m_pUnk(pUnk){}
  37. ~CReleaseMe() {if(m_pUnk) m_pUnk->Release();}
  38. };
  39. template<class T>
  40. class CTemplateReleaseMe
  41. {
  42. protected:
  43. T* m_p;
  44. public:
  45. CTemplateReleaseMe(T* p) : m_p(p){}
  46. ~CTemplateReleaseMe() {if(m_p) m_p->Release();}
  47. };
  48. template<class T>
  49. class CDeleteMe
  50. {
  51. protected:
  52. T* m_p;
  53. public:
  54. CDeleteMe(T* p) : m_p(p){}
  55. ~CDeleteMe() {delete m_p;}
  56. };
  57. class CCloseMe
  58. {
  59. protected:
  60. HANDLE m_h;
  61. public:
  62. CCloseMe(HANDLE hToClose){m_h = hToClose;};
  63. ~CCloseMe(){if(m_h && m_h != INVALID_HANDLE_VALUE)CloseHandle(m_h);};
  64. };
  65. class CfcloseMe
  66. {
  67. protected:
  68. FILE * m_h;
  69. public:
  70. CfcloseMe(FILE * ToClose){m_h = ToClose;};
  71. ~CfcloseMe(){if(m_h != NULL)fclose(m_h);};
  72. };
  73. typedef CCloseMe CCloseHandle;
  74. class CRegCloseMe
  75. {
  76. protected:
  77. HKEY m_h;
  78. public:
  79. CRegCloseMe(HKEY hToClose){m_h = hToClose;};
  80. ~CRegCloseMe(){if(m_h)RegCloseKey(m_h);};
  81. };
  82. template<class T>
  83. class CVectorDeleteMe
  84. {
  85. protected:
  86. T* m_p;
  87. T** m_pp;
  88. public:
  89. CVectorDeleteMe(T* p) : m_p(p), m_pp(NULL){}
  90. CVectorDeleteMe(T** pp) : m_p(NULL), m_pp(pp){}
  91. ~CVectorDeleteMe() {if(m_p) delete [] m_p; else if(m_pp) delete [] *m_pp;}
  92. };
  93. class CClearMe
  94. {
  95. protected:
  96. VARIANT* m_pv;
  97. public:
  98. CClearMe(VARIANT* pv) : m_pv(pv){}
  99. ~CClearMe() {VariantClear(m_pv);}
  100. };
  101. class CSysFreeMe
  102. {
  103. protected:
  104. BSTR m_str;
  105. public:
  106. CSysFreeMe(BSTR str) : m_str(str){}
  107. ~CSysFreeMe() {SysFreeString(m_str);}
  108. };
  109. class CUnaccessMe
  110. {
  111. protected:
  112. SAFEARRAY* m_psa;
  113. public:
  114. CUnaccessMe(SAFEARRAY* psa) : m_psa(psa){}
  115. ~CUnaccessMe() {SafeArrayUnaccessData(m_psa);}
  116. };
  117. class CMemFreeMe
  118. {
  119. protected:
  120. void* m_pMem;
  121. public:
  122. CMemFreeMe( void* pMem ) : m_pMem(pMem){}
  123. ~CMemFreeMe() { if ( NULL != m_pMem ) CoTaskMemFree(m_pMem);}
  124. };
  125. #include <WT_flexarry.h>
  126. //#include <flexq.h>
  127. //#include <smallarr.h>
  128. //*****************************************************************************
  129. //
  130. // class CPointerArray
  131. //
  132. // Array of pointers to TMember, where TMember is any class. See CFlexArray
  133. // in coredll\flexarry.h/cpp for documentation.
  134. //
  135. //*****************************************************************************
  136. template <class TMember>
  137. class CNullManager
  138. {
  139. public:
  140. void AddRefElement(TMember*){}
  141. void ReleaseElement(TMember*){}
  142. };
  143. template <class TMember, class TManager = CNullManager<TMember>,
  144. class TArray = CFlexArray>
  145. class CPointerArray
  146. {
  147. protected:
  148. TArray m_Array;
  149. TManager m_Manager;
  150. public:
  151. CPointerArray(const TManager& Manager = TManager())
  152. : m_Manager(Manager){}
  153. ~CPointerArray();
  154. int GetSize() const
  155. {return m_Array.Size();}
  156. void SetSize(int nNewSize)
  157. {m_Array.SetSize(nNewSize);}
  158. const TMember* GetAt(int nIndex) const
  159. {return (TMember*)m_Array.GetAt(nIndex);}
  160. TMember* GetAt(int nIndex)
  161. {return (TMember*)m_Array.GetAt(nIndex);}
  162. const TMember* operator[](int nIndex) const
  163. {return (TMember*)m_Array.GetAt(nIndex);}
  164. TMember* operator[](int nIndex)
  165. {return (TMember*)m_Array.GetAt(nIndex);}
  166. void SetAt(int nIndex, TMember* pElement, TMember** ppOld = NULL);
  167. bool RemoveAt(int nIndex, TMember** ppOld = NULL);
  168. bool InsertAt(int nIndex, TMember* pElement);
  169. int Add(TMember* pElement);
  170. TMember** GetArrayPtr();
  171. TMember** UnbindPtr();
  172. void RemoveAll();
  173. void Swap(int nIndex1, int nIndex2);
  174. void Trim() {m_Array.Trim();}
  175. protected:
  176. void AddRefElement(TMember* p){m_Manager.AddRefElement(p);}
  177. void ReleaseElement(TMember* p){m_Manager.ReleaseElement(p);}
  178. };
  179. /*
  180. template<class TMember>
  181. class CPointerSmallArray :
  182. public CPointerArray<TMember, CNullManager<TMember>, CSmallArray>
  183. {
  184. };
  185. */
  186. //*****************************************************************************
  187. //
  188. // class CPointerQueue
  189. //
  190. // Queue of pointers to TMember, where TMember is any class. See CFlexQueue
  191. // in coredll\flexq.h/cpp for documentation.
  192. //
  193. //*****************************************************************************
  194. /*
  195. template <class TMember, class TManager = CNullManager<TMember> >
  196. class CPointerQueue
  197. {
  198. protected:
  199. CFlexQueue m_Queue;
  200. TManager m_Manager;
  201. public:
  202. CPointerQueue(int nInitialSize = 1, const TManager& Manager = TManager())
  203. : m_Manager(Manager), m_Queue(nInitialSize){}
  204. Clear();
  205. ~CPointerQueue()
  206. {Clear();}
  207. inline int GetQueueSize() const
  208. {return m_Queue.GetQueueSize();}
  209. bool Enqueue(TMember* pNew)
  210. {
  211. AddRefElement(pNew);
  212. return m_Queue.Enqueue(pNew);
  213. }
  214. TMember* Dequeue()
  215. {
  216. TMember* p = (TMember*)m_Queue.Dequeue();
  217. return p;
  218. }
  219. bool Requeue(TMember* pNew)
  220. {
  221. AddRefElement(pNew);
  222. return m_Queue.Requeue(pNew);
  223. }
  224. TMember* Unqueue()
  225. {
  226. TMember* p = (TMember*)m_Queue.Unqueue();
  227. AddRefElement(p);
  228. return p;
  229. }
  230. protected:
  231. void AddRefElement(TMember* p){m_Manager.AddRefElement(p);}
  232. void ReleaseElement(TMember* p){m_Manager.ReleaseElement(p);}
  233. };
  234. */
  235. //*****************************************************************************
  236. //
  237. // UNIQUE POINTER ARRAY
  238. //
  239. //*****************************************************************************
  240. template <class TMember>
  241. class CUniqueManager
  242. {
  243. public:
  244. void AddRefElement(TMember*){}
  245. void ReleaseElement(TMember* pMember) {delete pMember;}
  246. };
  247. template<class TMember>
  248. class CUniquePointerArray :
  249. public CPointerArray<TMember, CUniqueManager<TMember> >
  250. {
  251. };
  252. /*
  253. template<class TMember>
  254. class CUniquePointerSmallArray :
  255. public CPointerArray<TMember, CUniqueManager<TMember>, CSmallArray>
  256. {
  257. };
  258. */
  259. /*
  260. template<class TMember>
  261. class CUniquePointerQueue :
  262. public CPointerQueue<TMember, CUniqueManager<TMember> >
  263. {
  264. public:
  265. CUniquePointerQueue<TMember>(int nInitialSize = 1)
  266. : CPointerQueue<TMember, CUniqueManager<TMember> >(nInitialSize)
  267. {}
  268. };
  269. */
  270. //*****************************************************************************
  271. //
  272. // REFED POINTER ARRAY
  273. //
  274. //*****************************************************************************
  275. template <class TMember>
  276. class CReferenceManager
  277. {
  278. public:
  279. void AddRefElement(TMember* pMember) {if(pMember)pMember->AddRef();}
  280. void ReleaseElement(TMember* pMember) {if(pMember)pMember->Release();}
  281. };
  282. template<class TMember>
  283. class CRefedPointerArray :
  284. public CPointerArray<TMember, CReferenceManager<TMember> >
  285. {
  286. };
  287. /*
  288. template<class TMember>
  289. class CRefedPointerSmallArray :
  290. public CPointerArray<TMember, CReferenceManager<TMember>, CSmallArray>
  291. {
  292. };
  293. */
  294. /*
  295. template<class TMember>
  296. class CRefedPointerQueue :
  297. public CPointerQueue<TMember, CReferenceManager<TMember> >
  298. {
  299. CRefedPointerQueue(int nInitialSize = 1)
  300. : CPointerQueue<TMember, CReferenceManager<TMember> >(nInitialSize)
  301. {}
  302. };
  303. */
  304. //*****************************************************************************
  305. //
  306. // ARRAY OF UNIQUE ARRAYS
  307. //
  308. //*****************************************************************************
  309. template <class TMember>
  310. class CUniqueArrayManager
  311. {
  312. void AddRefElement(TMember**){}
  313. void ReleaseElement(TMember** pMember) {delete [] pMember;}
  314. };
  315. template<class TMember>
  316. class CUniqueArrayArray : public CPointerArray<TMember*,
  317. CUniqueArrayManager<TMember> >
  318. {
  319. };
  320. //*****************************************************************************
  321. //
  322. // IMPLEMENTATION
  323. //
  324. //*****************************************************************************
  325. template <class TMember, class TManager, class TArray>
  326. CPointerArray<TMember, TManager, TArray>::~CPointerArray()
  327. {
  328. RemoveAll();
  329. }
  330. template <class TMember, class TManager, class TArray>
  331. void CPointerArray<TMember, TManager, TArray>::RemoveAll()
  332. {
  333. for(int i = 0; i < m_Array.Size(); i++)
  334. {
  335. ReleaseElement(GetAt(i));
  336. }
  337. m_Array.Empty();
  338. }
  339. template <class TMember, class TManager, class TArray>
  340. void CPointerArray<TMember, TManager, TArray>::SetAt(int nIndex,
  341. TMember* pElement, TMember** ppOld)
  342. {
  343. if(ppOld == NULL)
  344. ReleaseElement(GetAt(nIndex));
  345. else
  346. *ppOld = GetAt(nIndex);
  347. AddRefElement(pElement);
  348. m_Array.SetAt(nIndex, (void*)pElement);
  349. }
  350. template <class TMember, class TManager, class TArray>
  351. bool CPointerArray<TMember, TManager, TArray>::RemoveAt(int nIndex,
  352. TMember** ppOld)
  353. {
  354. if(ppOld == NULL)
  355. ReleaseElement(GetAt(nIndex));
  356. else
  357. *ppOld = GetAt(nIndex);
  358. if(m_Array.RemoveAt(nIndex) != CFlexArray::no_error)
  359. return false;
  360. return true;
  361. }
  362. template <class TMember, class TManager, class TArray>
  363. bool CPointerArray<TMember, TManager, TArray>::InsertAt(int nIndex,
  364. TMember* pElement)
  365. {
  366. AddRefElement(pElement);
  367. if(m_Array.InsertAt(nIndex, (void*)pElement) != CFlexArray::no_error)
  368. {
  369. ReleaseElement(pElement);
  370. return false;
  371. }
  372. return true;
  373. }
  374. template <class TMember, class TManager, class TArray>
  375. int CPointerArray<TMember, TManager, TArray>::Add(TMember* pElement)
  376. {
  377. AddRefElement(pElement);
  378. if(m_Array.Add((void*)pElement) != CFlexArray::no_error)
  379. {
  380. ReleaseElement(pElement);
  381. return -1;
  382. }
  383. return m_Array.Size()-1;
  384. }
  385. template <class TMember, class TManager, class TArray>
  386. TMember** CPointerArray<TMember, TManager, TArray>::GetArrayPtr()
  387. {
  388. return (TMember**)m_Array.GetArrayPtr();
  389. }
  390. template <class TMember, class TManager, class TArray>
  391. TMember** CPointerArray<TMember, TManager, TArray>::UnbindPtr()
  392. {
  393. return (TMember**)m_Array.UnbindPtr();
  394. }
  395. template <class TMember, class TManager, class TArray>
  396. void CPointerArray<TMember, TManager>::Swap(int nIndex1, int nIndex2)
  397. {
  398. void* pTemp = m_Array[nIndex1];
  399. m_Array.SetAt(nIndex1, m_Array[nIndex2]);
  400. m_Array.SetAt(nIndex2, pTemp);
  401. }
  402. /*
  403. template <class TMember, class TManager>
  404. CPointerQueue<TMember, TManager>::Clear()
  405. {
  406. TMember* p;
  407. while(p = (TMember*)m_Queue.Dequeue())
  408. {
  409. ReleaseElement(p);
  410. }
  411. }
  412. */
  413. #endif