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.

106 lines
2.8 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: ArrayPtr.h
  4. //
  5. // Module: CMMON32.EXE
  6. //
  7. // Synopsis: Implement class CPtrArray, a array of void*, which grows dynamicly
  8. // This class is exactly the same as the one defined by MFC.
  9. // Help on the class also comes with vc help
  10. //
  11. // Copyright (c) 1998-1999 Microsoft Corporation
  12. //
  13. // Author: fengsun Created 2/17/98
  14. //
  15. //+----------------------------------------------------------------------------
  16. #ifndef ARRAYPTR_H
  17. #define ARRAYPTR_H
  18. #include "windows.h"
  19. #include "CmDebug.h"
  20. class CPtrArray
  21. {
  22. public:
  23. // Construction
  24. CPtrArray();
  25. ~CPtrArray();
  26. // Attributes
  27. int GetSize() const;
  28. int GetUpperBound() const;
  29. void SetSize(int nNewSize, int nGrowBy = -1);
  30. // Operations
  31. // Clean up
  32. void FreeExtra();
  33. void RemoveAll();
  34. // Accessing elements
  35. void* GetAt(int nIndex) const;
  36. void SetAt(int nIndex, void* newElement);
  37. void*& ElementAt(int nIndex);
  38. // Direct Access to the element data (may return NULL)
  39. const void** GetData() const;
  40. void** GetData();
  41. // Potentially growing the array
  42. void SetAtGrow(int nIndex, void* newElement);
  43. int Add(void* newElement);
  44. int Append(const CPtrArray& src);
  45. void Copy(const CPtrArray& src);
  46. // overloaded operator helpers
  47. void* operator[](int nIndex) const;
  48. void*& operator[](int nIndex);
  49. // Operations that move elements around
  50. void InsertAt(int nIndex, void* newElement, int nCount = 1);
  51. void RemoveAt(int nIndex, int nCount = 1);
  52. void InsertAt(int nStartIndex, CPtrArray* pNewArray);
  53. // Implementation
  54. protected:
  55. void** m_pData; // the actual array of data
  56. int m_nSize; // # of elements (upperBound - 1)
  57. int m_nMaxSize; // max allocated
  58. int m_nGrowBy; // grow amount
  59. public:
  60. #ifdef DEBUG
  61. void AssertValid() const; //assert this is valid, for debugging
  62. #endif
  63. };
  64. inline int CPtrArray::GetSize() const
  65. { return m_nSize; }
  66. inline int CPtrArray::GetUpperBound() const
  67. { return m_nSize-1; }
  68. inline void CPtrArray::RemoveAll()
  69. { SetSize(0); }
  70. inline void* CPtrArray::GetAt(int nIndex) const
  71. { MYDBGASSERT(nIndex >= 0 && nIndex < m_nSize);
  72. return m_pData[nIndex]; }
  73. inline void CPtrArray::SetAt(int nIndex, void* newElement)
  74. { MYDBGASSERT(nIndex >= 0 && nIndex < m_nSize);
  75. m_pData[nIndex] = newElement; }
  76. inline void*& CPtrArray::ElementAt(int nIndex)
  77. { MYDBGASSERT(nIndex >= 0 && nIndex < m_nSize);
  78. return m_pData[nIndex]; }
  79. inline int CPtrArray::Add(void* newElement)
  80. { int nIndex = m_nSize;
  81. SetAtGrow(nIndex, newElement);
  82. return nIndex; }
  83. inline void* CPtrArray::operator[](int nIndex) const
  84. { return GetAt(nIndex); }
  85. inline void*& CPtrArray::operator[](int nIndex)
  86. { return ElementAt(nIndex); }
  87. #endif