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.

581 lines
9.0 KiB

  1. /*++
  2. Copyright (C) 2000 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. list.cxx
  6. Abstract:
  7. List template class.
  8. Note that the class uses a friend set of functions for maintaining
  9. ordering. Also the ordering is not operator based but instead
  10. uses a template class with helper functions to maintain order. The
  11. helper functions use another ordering class that compares different
  12. elements. The advantage of this approach over operators is that the
  13. same class can be ordered by different keys.
  14. Author:
  15. Steve Kiraly (SteveKi) 03-Mar-2000
  16. Revision History:
  17. Mark Lawrence (MLawrenc) 29-Mark-2001
  18. --*/
  19. #ifndef _CORE_LIST_HXX_
  20. #define _CORE_LIST_HXX_
  21. namespace NCoreLibrary
  22. {
  23. /********************************************************************
  24. TLink class, used for intrusive lists.
  25. ********************************************************************/
  26. class TLink
  27. {
  28. SIGNATURE('tlnk');
  29. public:
  30. TLink(
  31. VOID
  32. );
  33. virtual
  34. ~TLink(
  35. VOID
  36. );
  37. HRESULT
  38. IsValid(
  39. VOID
  40. ) const;
  41. HRESULT
  42. IsLinked(
  43. VOID
  44. ) const;
  45. HRESULT
  46. Link(
  47. IN TLink *pNode
  48. );
  49. VOID
  50. UnLink(
  51. VOID
  52. );
  53. TLink *
  54. Next(
  55. VOID
  56. ) const;
  57. TLink *
  58. Prev(
  59. VOID
  60. ) const;
  61. private:
  62. //
  63. // Copying and assignement are not defined.
  64. //
  65. TLink(
  66. const TLink &rhs
  67. );
  68. const TLink &
  69. operator=(
  70. const TLink &rhs
  71. );
  72. TLink *m_pNext;
  73. TLink *m_pPrev;
  74. };
  75. /********************************************************************
  76. List class, this is an intrusive list implementation.
  77. ********************************************************************/
  78. template<class T>
  79. class TList
  80. {
  81. SIGNATURE('tlst');
  82. public:
  83. TList(
  84. VOID
  85. );
  86. ~TList(
  87. VOID
  88. );
  89. HRESULT
  90. IsValid(
  91. VOID
  92. ) const;
  93. BOOL
  94. IsEmpty(
  95. VOID
  96. );
  97. BOOL
  98. IsLast(
  99. IN TLink *pLink
  100. );
  101. HRESULT
  102. AddAtHead(
  103. IN TLink *pLink
  104. );
  105. HRESULT
  106. AddAtTail(
  107. IN TLink *pLink
  108. );
  109. HRESULT
  110. Insert(
  111. IN TLink *pLink,
  112. IN TLink *pHere = NULL
  113. );
  114. T *
  115. RemoveAtHead(
  116. VOID
  117. );
  118. T *
  119. RemoveAtTail(
  120. VOID
  121. );
  122. HRESULT
  123. Remove(
  124. IN TLink *pLink
  125. );
  126. T *
  127. Head(
  128. VOID
  129. ) const;
  130. T *
  131. Tail(
  132. VOID
  133. ) const;
  134. class TIterator
  135. {
  136. public:
  137. TIterator(
  138. IN TList<T> *pRoot
  139. ) : m_pRoot(&pRoot->m_Root),
  140. m_pCurrent(NULL)
  141. {
  142. }
  143. ~TIterator(
  144. VOID
  145. )
  146. {
  147. }
  148. HRESULT
  149. Initialize(
  150. IN TList<T> *pRoot
  151. )
  152. {
  153. m_pRoot = NULL;
  154. m_pCurrent = NULL;
  155. if (pRoot)
  156. {
  157. m_pRoot = &pRoot->m_Root;
  158. }
  159. return m_pRoot ? S_OK : E_POINTER;
  160. }
  161. HRESULT
  162. IsValid(
  163. VOID
  164. ) const
  165. {
  166. return m_pRoot ? S_OK : E_FAIL;
  167. }
  168. VOID
  169. First(
  170. VOID
  171. )
  172. {
  173. if (m_pRoot)
  174. {
  175. m_pCurrent = m_pRoot->Next();
  176. }
  177. }
  178. VOID
  179. Next(
  180. VOID
  181. )
  182. {
  183. if (m_pCurrent)
  184. {
  185. m_pCurrent = m_pCurrent->Next();
  186. }
  187. }
  188. BOOL
  189. IsDone(
  190. VOID
  191. ) const
  192. {
  193. BOOL bIsDone = TRUE;
  194. if (m_pRoot)
  195. {
  196. bIsDone = (m_pCurrent == m_pRoot);
  197. }
  198. return bIsDone;
  199. }
  200. T *
  201. Current(
  202. VOID
  203. ) const
  204. {
  205. return static_cast<T *>(m_pCurrent);
  206. }
  207. VOID
  208. Current(
  209. OUT T **ppNode
  210. )
  211. {
  212. *ppNode = static_cast<T *>(m_pCurrent);
  213. }
  214. private:
  215. //
  216. // Copying and assignment are not defined.
  217. //
  218. NO_COPY(TIterator);
  219. TLink *m_pRoot;
  220. TLink *m_pCurrent;
  221. };
  222. friend class NCoreLibrary::TList<T>::TIterator;
  223. private:
  224. //
  225. // Copying and assignement are not defined.
  226. //
  227. TList(
  228. const TList<T> &rhs
  229. );
  230. const TList<T> &
  231. operator=(
  232. const TList<T> &rhs
  233. );
  234. TLink m_Root;
  235. };
  236. /********************************************************************
  237. LinkNi class, this is a non intrusive list node
  238. ********************************************************************/
  239. template<class T>
  240. class TLinkNi : public TLink
  241. {
  242. SIGNATURE('tlnk');
  243. public:
  244. TLinkNi(
  245. IN T *pInfo
  246. );
  247. ~TLinkNi(
  248. VOID
  249. );
  250. HRESULT
  251. IsValid(
  252. VOID
  253. ) const;
  254. T *
  255. Get(
  256. VOID
  257. ) const;
  258. TLinkNi<T> *
  259. Next(
  260. VOID
  261. ) const;
  262. TLinkNi<T> *
  263. Prev(
  264. VOID
  265. ) const;
  266. private:
  267. //
  268. // Copying and assignement are not defined.
  269. //
  270. TLinkNi(
  271. const TLinkNi<T> &rhs
  272. );
  273. const TLinkNi<T> &
  274. operator=(
  275. const TLinkNi<T> &rhs
  276. );
  277. T *m_pInfo;
  278. };
  279. /********************************************************************
  280. TListNi class, this is a non intrusive list
  281. ********************************************************************/
  282. template<class T>
  283. class TListNi
  284. {
  285. SIGNATURE('tlni');
  286. public:
  287. TListNi(
  288. VOID
  289. );
  290. ~TListNi(
  291. VOID
  292. );
  293. HRESULT
  294. IsValid(
  295. VOID
  296. ) const;
  297. BOOL
  298. IsEmpty(
  299. VOID
  300. ) const;
  301. HRESULT
  302. AddAtHead(
  303. IN T *pData
  304. );
  305. HRESULT
  306. AddAtTail(
  307. IN T *pData
  308. );
  309. HRESULT
  310. Insert(
  311. IN T *pData,
  312. IN TLinkNi<T> *pHere = NULL
  313. );
  314. T *
  315. RemoveAtHead(
  316. VOID
  317. );
  318. T *
  319. RemoveAtTail(
  320. VOID
  321. );
  322. HRESULT
  323. Remove(
  324. IN TLinkNi<T> *pNode
  325. );
  326. HRESULT
  327. Remove(
  328. IN T *pData
  329. );
  330. T *
  331. Head(
  332. VOID
  333. ) const;
  334. T *
  335. Tail(
  336. VOID
  337. ) const;
  338. class TIterator
  339. {
  340. public:
  341. TIterator(
  342. IN TListNi<T> *pList
  343. ) : m_pRoot(&pList->m_Root),
  344. m_pCurrent(NULL)
  345. {
  346. }
  347. ~TIterator(
  348. VOID
  349. )
  350. {
  351. }
  352. HRESULT
  353. Initialize(
  354. IN TListNi<T> *pRoot
  355. )
  356. {
  357. m_pRoot = NULL;
  358. m_pCurrent = NULL;
  359. if (pRoot)
  360. {
  361. m_pRoot = &pRoot->m_Root;
  362. }
  363. return m_pRoot ? S_OK : E_POINTER;
  364. }
  365. HRESULT
  366. IsValid(
  367. VOID
  368. ) const
  369. {
  370. return m_pRoot ? S_OK : E_FAIL;
  371. }
  372. VOID
  373. First(
  374. VOID
  375. )
  376. {
  377. if (m_pRoot)
  378. {
  379. m_pCurrent = m_pRoot->Next();
  380. }
  381. }
  382. VOID
  383. Next(
  384. VOID
  385. )
  386. {
  387. if (m_pCurrent)
  388. {
  389. m_pCurrent = m_pCurrent->Next();
  390. }
  391. }
  392. BOOL
  393. IsDone(
  394. VOID
  395. ) const
  396. {
  397. BOOL bIsDone = TRUE;
  398. if (m_pRoot)
  399. {
  400. bIsDone = (m_pCurrent == m_pRoot);
  401. }
  402. return bIsDone;
  403. }
  404. T *
  405. Current(
  406. VOID
  407. ) const
  408. {
  409. return m_pCurrent->Get();
  410. }
  411. VOID
  412. Current(
  413. OUT TLinkNi<T> **ppNode
  414. ) const
  415. {
  416. if (ppNode)
  417. {
  418. *ppNode = m_pCurrent;
  419. }
  420. }
  421. private:
  422. //
  423. // Copying and assignement are not defined.
  424. //
  425. NO_COPY(TIterator);
  426. TLinkNi<T> *m_pRoot;
  427. TLinkNi<T> *m_pCurrent;
  428. };
  429. friend class NCoreLibrary::TListNi<T>::TIterator;
  430. private:
  431. //
  432. // Copying and assignement are not defined.
  433. //
  434. TListNi(
  435. const TListNi<T> &rhs
  436. );
  437. const TListNi<T> &
  438. operator=(
  439. const TListNi<T> &rhs
  440. );
  441. TLinkNi<T> m_Root;
  442. };
  443. #include "CSRlist.inl"
  444. } // namespace
  445. #endif // _CORE_LIST_HXX_