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.3 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Abstract:
  4. Helper templates implementation.
  5. Author:
  6. Souren Aghajanyan (sourenag) 24-Sep-2001
  7. Revision History:
  8. <alias> <date> <comments>
  9. --*/
  10. #pragma once
  11. #include "sysfunc.h"
  12. #define DEFAULT_GROW_SIZE 100
  13. template<class T> class CPtrList
  14. {
  15. T* m_pPtrArray;
  16. UINT m_uiNumberOfElements;
  17. UINT m_uiEnumIndex;
  18. CBuffer m_Buffer;
  19. public:
  20. CPtrList(UINT uiInitialSize = DEFAULT_GROW_SIZE){
  21. if(!uiInitialSize){
  22. uiInitialSize = DEFAULT_GROW_SIZE;
  23. }
  24. m_uiEnumIndex = 0;
  25. m_uiNumberOfElements = 0;
  26. m_pPtrArray = NULL;
  27. SetSize(uiInitialSize);
  28. ASSERT(m_pPtrArray);
  29. }
  30. ~CPtrList()
  31. {
  32. m_Buffer.Free();
  33. }
  34. UINT GetSize(){
  35. UINT uiNumberOfActualItems = 0;
  36. for(UINT i = 0; i < m_uiNumberOfElements; i++){
  37. if(m_pPtrArray[i]){
  38. uiNumberOfActualItems++;
  39. }
  40. }
  41. return uiNumberOfActualItems;
  42. }
  43. BOOL SetSize(UINT uiNewSize)
  44. {
  45. T* pPtrArray = (T*)m_Buffer.ReAllocate(uiNewSize * sizeof(void*));
  46. if(!pPtrArray){
  47. return FALSE;
  48. }
  49. if(m_uiNumberOfElements < uiNewSize){
  50. memset(pPtrArray + m_uiNumberOfElements, 0, (uiNewSize - m_uiNumberOfElements) * sizeof(void*));
  51. }
  52. m_uiNumberOfElements = uiNewSize;
  53. m_pPtrArray = pPtrArray;
  54. return TRUE;
  55. }
  56. BOOL Add(T pPtr)
  57. {
  58. ASSERT(m_pPtrArray);
  59. if(!pPtr){
  60. return FALSE;
  61. }
  62. for(UINT i = 0; i < m_uiNumberOfElements; i++){
  63. if(m_pPtrArray[i] == pPtr){
  64. return TRUE;
  65. }
  66. }
  67. UINT uiEmptyPlace;
  68. for(i = 0; i < m_uiNumberOfElements; i++){
  69. if(!m_pPtrArray[i]){
  70. uiEmptyPlace = i;
  71. break;
  72. }
  73. }
  74. if(i == m_uiNumberOfElements){
  75. if(!SetSize(m_uiNumberOfElements + 1)){
  76. return FALSE;
  77. }
  78. uiEmptyPlace = i;
  79. }
  80. m_pPtrArray[uiEmptyPlace] = pPtr;
  81. return TRUE;
  82. }
  83. VOID RemoveAll()
  84. {
  85. ASSERT(m_pPtrArray);
  86. if(!m_pPtrArray){
  87. return;
  88. }
  89. memset(m_pPtrArray, 0, sizeof(void*) * m_uiNumberOfElements);
  90. m_uiNumberOfElements = NULL;
  91. }
  92. BOOL Remove(T pItemPtr)
  93. {
  94. ASSERT(m_pPtrArray);
  95. if(!pItemPtr){
  96. return FALSE;
  97. }
  98. for(UINT i = 0; i < m_uiNumberOfElements; i++){
  99. if(m_pPtrArray[i] == pItemPtr){
  100. m_pPtrArray[i] = NULL;
  101. return TRUE;
  102. }
  103. }
  104. ASSERT(FALSE);
  105. return FALSE;
  106. }
  107. T BeginEnum()
  108. {
  109. m_uiEnumIndex = 0;
  110. return Next();
  111. }
  112. T Next()
  113. {
  114. for(m_uiEnumIndex; m_uiEnumIndex < m_uiNumberOfElements; m_uiEnumIndex++){
  115. if(m_pPtrArray[m_uiEnumIndex]){
  116. return m_pPtrArray[m_uiEnumIndex++];
  117. }
  118. }
  119. return NULL;
  120. }
  121. };