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.

161 lines
3.7 KiB

  1. //-------------------------------------------------------------------------
  2. //
  3. // File: tptrlist.hxx
  4. //
  5. // Contents: Contains template pointer list class def/impl for:
  6. //
  7. // -- TPtrListNode
  8. // -- TPtrList
  9. //
  10. // History: 03/21/98 BogdanT Created
  11. //
  12. //--------------------------------------------------------------------------
  13. #ifndef __TDLIST_HXX__
  14. #define __TDLIST_HXX__
  15. #include <wtypes.h>
  16. //+--------------------------------------------------------------------------
  17. //
  18. // Class: TPtrListNode
  19. //
  20. // Synopsis: pointer list node object definition
  21. //
  22. // History: 03/21/98 BogdanT Created
  23. //
  24. //---------------------------------------------------------------------------
  25. template <class C> class TPtrListNode
  26. {
  27. public:
  28. TPtrListNode() { m_pData = NULL; m_pNext = NULL; }
  29. TPtrListNode(C* pData) { m_pData = pData; m_pNext = NULL; }
  30. C *m_pData;
  31. TPtrListNode<C> *m_pNext;
  32. };
  33. //+--------------------------------------------------------------------------
  34. //
  35. // Class: TPtrList
  36. //
  37. // Synopsis: pointer list object definition
  38. //
  39. // History: 03/21/98 BogdanT Created
  40. //
  41. //
  42. //---------------------------------------------------------------------------
  43. template <class C> class TPtrList
  44. {
  45. public:
  46. TPtrList() { m_pHead = NULL; m_pCrt = NULL; }
  47. ~TPtrList();
  48. BOOL IsEmpty() { return NULL==m_pHead; }
  49. ULONG GetCount();
  50. void EnumReset() { m_pCrt = m_pHead; }
  51. C* EnumNext();
  52. BOOL PtrFound(C* p);
  53. BOOL AddHead(C* pData);
  54. BOOL AddTail(C* pData);
  55. void Cleanup();
  56. protected:
  57. TPtrListNode<C> *m_pHead;
  58. TPtrListNode<C> *m_pCrt;
  59. };
  60. template <class C> TPtrList<C>::~TPtrList()
  61. {
  62. TPtrListNode<C> *pCrt, *pNext;
  63. pCrt = m_pHead;
  64. while(NULL != pCrt)
  65. {
  66. pNext = pCrt->m_pNext;
  67. delete pCrt;
  68. pCrt = pNext;
  69. }
  70. }
  71. template <class C> ULONG TPtrList<C>::GetCount()
  72. {
  73. ULONG count = 0;
  74. TPtrListNode<C> *pCount;
  75. for(pCount=m_pHead;NULL!=pCount;pCount=pCount->m_pNext)
  76. count++;
  77. return count;
  78. }
  79. template <class C> C* TPtrList<C>::EnumNext()
  80. {
  81. TPtrListNode<C> *pCrt;
  82. if(NULL == m_pCrt)
  83. return NULL;
  84. pCrt = m_pCrt;
  85. if(NULL != m_pCrt)
  86. m_pCrt = m_pCrt->m_pNext;
  87. return pCrt->m_pData;
  88. }
  89. template <class C> BOOL TPtrList<C>::AddHead(C *pData)
  90. {
  91. TPtrListNode<C> *pNew = new TPtrListNode<C>(pData);
  92. if(NULL == pNew)
  93. return FALSE;
  94. pNew->m_pNext = m_pHead;
  95. m_pHead = pNew;
  96. EnumReset();
  97. return TRUE;
  98. }
  99. template <class C> BOOL TPtrList<C>::AddTail(C *pData)
  100. {
  101. TPtrListNode<C> *pNew = new TPtrListNode<C>(pData);
  102. TPtrListNode<C> *pCrt = m_pHead;
  103. if(NULL == pNew)
  104. return FALSE;
  105. if(NULL == m_pHead)
  106. {
  107. m_pHead = pNew;
  108. EnumReset();
  109. }
  110. else
  111. {
  112. while(NULL != pCrt->m_pNext)
  113. pCrt = pCrt->m_pNext;
  114. pCrt->m_pNext = pNew;
  115. }
  116. return TRUE;
  117. }
  118. template <class C> void TPtrList<C>::Cleanup()
  119. {
  120. TPtrListNode<C> *pCrt, *pNext;
  121. pCrt = m_pHead;
  122. while(NULL != pCrt)
  123. {
  124. pNext = pCrt->m_pNext;
  125. delete pCrt->m_pData;
  126. delete pCrt;
  127. pCrt = pNext;
  128. }
  129. m_pHead = NULL;
  130. }
  131. template <class C> BOOL TPtrList<C>::PtrFound(C* p)
  132. {
  133. TPtrListNode<C> *pCrt;
  134. for(pCrt = m_pHead; pCrt!=NULL && pCrt->m_pData!=p; pCrt=pCrt->m_pNext)
  135. NULL;
  136. return (pCrt != NULL); //bugbug: returns TRUE even if p is NULL, maybe
  137. // we should assert on null
  138. }
  139. #endif //__TDLIST_HXX__