Source code of Windows XP (NT5)
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.

190 lines
3.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995
  5. //
  6. // File: ptrarray.h
  7. //
  8. // Contents: Handles dynamic arrays of void *
  9. //
  10. // History: 7-13-95 Davepl Created
  11. //
  12. //--------------------------------------------------------------------------
  13. class CPtrArray
  14. {
  15. public:
  16. //
  17. // Constructor / Destructor
  18. //
  19. CPtrArray();
  20. CPtrArray(HANDLE hHeap);
  21. virtual ~CPtrArray();
  22. //
  23. // Attributes
  24. //
  25. int GetSize() const
  26. {
  27. return m_nSize;
  28. }
  29. int GetUpperBound() const
  30. {
  31. return m_nSize-1;
  32. }
  33. BOOL SetSize(int nNewSize, int nGrowBy = -1);
  34. BOOL FreeExtra();
  35. BOOL RemoveAll()
  36. {
  37. return SetSize(0);
  38. }
  39. void* GetAt(int nIndex) const
  40. {
  41. ASSERT(nIndex >= 0 && nIndex < m_nSize);
  42. return m_pData[nIndex];
  43. }
  44. void SetAt(int nIndex, void* newElement)
  45. {
  46. ASSERT(nIndex >= 0 && nIndex < m_nSize);
  47. m_pData[nIndex] = newElement;
  48. }
  49. void*& ElementAt(int nIndex)
  50. {
  51. ASSERT(nIndex >= 0 && nIndex < m_nSize);
  52. return m_pData[nIndex];
  53. }
  54. // Direct Access to the element data (may return NULL)
  55. const void** GetData() const
  56. {
  57. return (const void**)m_pData;
  58. }
  59. void** GetData()
  60. {
  61. return (void**)m_pData;
  62. }
  63. // Potentially growing the array
  64. BOOL SetAtGrow(int nIndex, void* newElement)
  65. {
  66. ASSERT(nIndex >= 0);
  67. if (nIndex >= m_nSize)
  68. {
  69. if (FALSE == SetSize(nIndex+1))
  70. {
  71. return FALSE;
  72. }
  73. }
  74. m_pData[nIndex] = newElement;
  75. return TRUE;
  76. }
  77. BOOL Add(void* newElement, int * pIndex = NULL)
  78. {
  79. if (pIndex)
  80. {
  81. *pIndex = m_nSize;
  82. }
  83. return SetAtGrow(m_nSize, newElement);
  84. }
  85. BOOL Append(const CPtrArray& src, int * pOldSize = NULL)
  86. {
  87. ASSERT(this != &src); // cannot append to itself
  88. int nOldSize = m_nSize;
  89. if (FALSE == SetSize(m_nSize + src.m_nSize))
  90. {
  91. return TRUE;
  92. }
  93. CopyMemory(m_pData + nOldSize, src.m_pData, ((DWORD)src.m_nSize) * sizeof(void*));
  94. if (pOldSize)
  95. {
  96. *pOldSize = nOldSize;
  97. }
  98. return TRUE;
  99. }
  100. BOOL Copy(const CPtrArray& src)
  101. {
  102. ASSERT(this != &src); // cannot append to itself
  103. if (FALSE == SetSize(src.m_nSize))
  104. {
  105. return FALSE;
  106. }
  107. CopyMemory(m_pData, src.m_pData, ((DWORD)src.m_nSize) * sizeof(void*));
  108. return TRUE;
  109. }
  110. // overloaded operator helpers
  111. void* operator[](int nIndex) const
  112. {
  113. return GetAt(nIndex);
  114. }
  115. void*& operator[](int nIndex)
  116. {
  117. return ElementAt(nIndex);
  118. }
  119. // Operations that move elements around
  120. BOOL InsertAt(int nIndex, void* newElement, int nCount = 1);
  121. BOOL InsertAt(int nStartIndex, CPtrArray* pNewArray);
  122. void RemoveAt(int nIndex, int nCount)
  123. {
  124. ASSERT(nIndex >= 0);
  125. ASSERT(nCount >= 0);
  126. ASSERT(nIndex + nCount <= m_nSize);
  127. // just remove a range
  128. int nMoveCount = m_nSize - (nIndex + nCount);
  129. if (nMoveCount)
  130. {
  131. CopyMemory(&m_pData[nIndex], &m_pData[nIndex + nCount], ((DWORD)(nMoveCount)) * sizeof(void*));
  132. }
  133. m_nSize -= nCount;
  134. }
  135. // Implementation
  136. protected:
  137. void** m_pData; // the actual array of data
  138. int m_nSize; // # of elements (upperBound - 1)
  139. int m_nMaxSize; // max allocated
  140. int m_nGrowBy; // grow amount
  141. HANDLE m_hHeap; // heap to allocate from
  142. };