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.

157 lines
4.0 KiB

  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Filename : VarArray.h
  4. // Purpose : To define a variable size array.
  5. //
  6. // Project : FTFS
  7. // Component: Common
  8. //
  9. // Author : urib
  10. //
  11. // Log:
  12. // Feb 23 1997 urib Creation
  13. // Sep 14 1997 urib Allow VarArray to behave like a C array - buffer.
  14. // Oct 21 1997 urib Fix bug in resizing. Add a GetSize method.
  15. // Jan 26 1999 urib Fix bug #23 VarArray constructor bug.
  16. // Apr 16 2000 LiorM Performance: CVarArray with and w/o ctor dtor
  17. // Dec 24 2000 urib Add an initial embedded array
  18. // Jan 9 2001 urib Fix a bug in SetSize.
  19. //
  20. ////////////////////////////////////////////////////////////////////////////////
  21. #ifndef VARARRAY_H
  22. #define VARARRAY_H
  23. #include <new.h>
  24. #include "Tracer.h"
  25. #include "VarBuff.h"
  26. ////////////////////////////////////////////////////////////////////////////////
  27. //
  28. // CVarArray class definition
  29. //
  30. ////////////////////////////////////////////////////////////////////////////////
  31. template <class T, ULONG ulInitialEmbeddedSizeInItems = 1, bool FSIMPLE = false>
  32. class CVarArray :
  33. // Hide buffer functionallity.
  34. protected CVarBuffer<T, ulInitialEmbeddedSizeInItems>
  35. {
  36. public:
  37. typedef CVarBuffer<T, ulInitialEmbeddedSizeInItems> CVarBufferBaseType;
  38. // Constructor - user can specify recomended initial allocation size.
  39. CVarArray(ULONG ulInitialSizeInItems = 0)
  40. :CVarBufferBaseType(ulInitialSizeInItems)
  41. {
  42. if (!FSIMPLE)
  43. {
  44. ULONG ulCurrent;
  45. for (ulCurrent = 0; ulCurrent < ulInitialSizeInItems; ulCurrent++)
  46. Construct(GetCell(ulCurrent));
  47. }
  48. }
  49. // Returns the array size
  50. ULONG GetSize()
  51. {
  52. return CVarBufferBaseType::GetSize();
  53. }
  54. // Calls the buffer SetSize, and initialize the new cells.
  55. void SetSize(ULONG ulNewSizeInItems)
  56. {
  57. if (!FSIMPLE)
  58. {
  59. ULONG ulSize = GetSize();
  60. //
  61. // Optimization - if the increase is big we do't want to have
  62. // several allocation caused by several GetCell calls.
  63. //
  64. CVarBufferBaseType::SetSize(ulNewSizeInItems);
  65. ULONG ulCurrent;
  66. //
  67. // If size is decreasing destruct the erased cells
  68. //
  69. for (ulCurrent = ulNewSizeInItems; ulCurrent < ulSize; ulCurrent++)
  70. Destruct(GetCell(ulCurrent));
  71. //
  72. // If size is increasing construct the new cells
  73. //
  74. for (ulCurrent = ulSize; ulCurrent < ulNewSizeInItems; ulCurrent++)
  75. Construct(GetCell(ulCurrent));
  76. }
  77. //
  78. // Mark the true current size
  79. //
  80. CVarBufferBaseType::SetSize(ulNewSizeInItems);
  81. Assert(GetSize() == ulNewSizeInItems);
  82. }
  83. // Act like an array.
  84. T& operator[](ULONG ul)
  85. {
  86. return *GetCell(ul);
  87. }
  88. // Act like a C array - memory buffer.
  89. operator T* ()
  90. {
  91. return GetCell(0);
  92. }
  93. // Call cells destructors
  94. ~CVarArray()
  95. {
  96. if (!FSIMPLE)
  97. {
  98. ULONG ulCurrent;
  99. for (ulCurrent = 0; ulCurrent < GetSize(); ulCurrent++)
  100. {
  101. T* pt = GetCell(ulCurrent);
  102. Destruct(pt);
  103. }
  104. }
  105. }
  106. protected:
  107. // Helper fuction to return the address on a cell.
  108. T* GetCell(ULONG ul)
  109. {
  110. if (GetSize() < ul + 1)
  111. SetSize(ul + 1);
  112. return GetBuffer() + ul;
  113. }
  114. static
  115. T* Construct(void* p)
  116. {
  117. #ifdef _PQS_LEAK_DETECTION
  118. #undef new
  119. #endif
  120. return new(p) T;
  121. #ifdef _PQS_LEAK_DETECTION
  122. #define new DEBUG_NEW
  123. #endif
  124. }
  125. static
  126. void Destruct(T* pt)
  127. {
  128. pt->~T();
  129. }
  130. };
  131. #endif /* VARARRAY_H */