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.

169 lines
3.9 KiB

  1. namespace CertSrv
  2. {
  3. template <class C> class TPtrListEnum;
  4. template <class C> class TPtrList
  5. {
  6. public:
  7. TPtrList() : m_pHead(NULL), m_dwCount(0) {}
  8. ~TPtrList(){ Cleanup(); }
  9. bool AddHead(C* pData);
  10. bool AddTail(C* pData);
  11. void Cleanup();
  12. DWORD GetCount() const { return m_dwCount;}
  13. bool IsEmpty() const {return NULL==m_pHead;}
  14. bool InsertAt(C* pData, DWORD dwIndex);
  15. bool RemoveAt(DWORD dwIndex);
  16. C* GetAt(DWORD dwIndex);
  17. C* Find(C& Data);
  18. DWORD FindIndex(C& Data);
  19. friend class TPtrListEnum<C>;
  20. protected:
  21. struct TPtrListNode
  22. {
  23. TPtrListNode(C* pData) :
  24. m_pData(pData), m_pNext(NULL) {}
  25. ~TPtrListNode() { delete m_pData;}
  26. C* m_pData;
  27. TPtrListNode* m_pNext;
  28. };
  29. typedef TPtrListNode* TNODEPTR;
  30. typedef TPtrListNode TNODE;
  31. TNODEPTR m_pHead;
  32. DWORD m_dwCount;
  33. };
  34. template <class C> class TPtrListEnum
  35. {
  36. public:
  37. TPtrListEnum() : m_pList(NULL), m_pCrt(NULL) {}
  38. TPtrListEnum(const TPtrList<C>& List) { Set(List);}
  39. void Set(const TPtrList<C>& List) { m_pList = &List; Reset(); }
  40. void Reset() { m_pCrt = m_pList->m_pHead; }
  41. C* Next();
  42. protected:
  43. const TPtrList<C> *m_pList;
  44. typename TPtrList<C>::TNODEPTR m_pCrt;
  45. };
  46. template <class C> C* TPtrListEnum<C>::Next()
  47. {
  48. if(!m_pCrt)
  49. return NULL;
  50. C* pResult = m_pCrt->m_pData;
  51. m_pCrt = m_pCrt->m_pNext;
  52. return pResult;
  53. }
  54. template <class C> void TPtrList<C>::Cleanup()
  55. {
  56. TNODEPTR pCrt, pNext;
  57. for(pCrt = m_pHead; pCrt; pCrt=pNext)
  58. {
  59. pNext = pCrt->m_pNext;
  60. delete pCrt;
  61. }
  62. m_pHead = NULL;
  63. m_dwCount = 0;
  64. }
  65. template <class C> bool TPtrList<C>::AddHead(C* pData)
  66. {
  67. TNODEPTR pNew = new TNODE(pData);
  68. if(!pNew)
  69. return false;
  70. pNew->m_pNext = m_pHead;
  71. m_pHead = pNew;
  72. m_dwCount++;
  73. return true;
  74. }
  75. template <class C> bool TPtrList<C>::AddTail(C* pData)
  76. {
  77. TNODEPTR pNew = new TNODE(pData);
  78. if(!pNew)
  79. return false;
  80. for(TNODEPTR *ppCrt = &m_pHead; *ppCrt; ppCrt = &(*ppCrt)->m_pNext)
  81. NULL;
  82. *ppCrt = pNew;
  83. m_dwCount++;
  84. return true;
  85. }
  86. template <class C> C* TPtrList<C>::Find(C& Data)
  87. {
  88. TPtrListEnum<C> ListEnum(*this);
  89. for(C* pResult = ListEnum.Next(); pResult; pResult= ListEnum.Next())
  90. {
  91. if(*pResult == Data)
  92. return pResult;
  93. }
  94. return NULL;
  95. }
  96. #define DWORD_MAX 0xffffffff
  97. template <class C> DWORD TPtrList<C>::FindIndex(C& Data)
  98. {
  99. DWORD dwIndex = 0;
  100. TPtrListEnum<C> ListEnum(*this);
  101. for(C* pResult = ListEnum.Next();
  102. pResult;
  103. pResult= ListEnum.Next(), dwIndex++)
  104. {
  105. if(*pResult == Data)
  106. return dwIndex;
  107. }
  108. return DWORD_MAX;
  109. }
  110. template <class C> bool TPtrList<C>::InsertAt(C* pData, DWORD dwIndex)
  111. {
  112. DWORD dwCrt;
  113. TNODEPTR *ppCrt;
  114. TNODEPTR pNew = new TNODE(pData);
  115. if(!pNew)
  116. return false;
  117. for(ppCrt = &m_pHead, dwCrt=0;
  118. NULL!=*ppCrt && (dwCrt<dwIndex);
  119. ppCrt = &(*ppCrt)->m_pNext, dwCrt++)
  120. NULL;
  121. pNew->m_pNext = *ppCrt;
  122. *ppCrt = pNew;
  123. m_dwCount++;
  124. return true;
  125. }
  126. template <class C> bool TPtrList<C>::RemoveAt(DWORD dwIndex)
  127. {
  128. DWORD dwCrt;
  129. TNODEPTR *ppCrt, pDel;
  130. for(ppCrt = &m_pHead, dwCrt=0;
  131. NULL!=*ppCrt && (dwCrt<dwIndex);
  132. ppCrt = &(*ppCrt)->m_pNext, dwCrt++)
  133. NULL;
  134. if(!*ppCrt)
  135. return false;
  136. pDel = *ppCrt;
  137. *ppCrt = (*ppCrt)->m_pNext;
  138. delete pDel;
  139. m_dwCount--;
  140. return true;
  141. }
  142. template <class C> C* TPtrList<C>::GetAt(DWORD dwIndex)
  143. {
  144. DWORD dwCrt;
  145. TNODEPTR pCrt;
  146. for(pCrt = m_pHead, dwCrt=0;
  147. NULL!=pCrt && (dwCrt<dwIndex);
  148. pCrt = pCrt->m_pNext, dwCrt++)
  149. NULL;
  150. return pCrt?pCrt->m_pData:NULL;
  151. }
  152. } // namespace CertSrv