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.

102 lines
2.3 KiB

  1. //
  2. // ptrary.h
  3. //
  4. // CPtrArray -- growable pointer array
  5. //
  6. #ifndef PTRARY_H
  7. #define PTRARY_H
  8. #include "private.h"
  9. class CVoidPtrArray
  10. {
  11. public:
  12. CVoidPtrArray()
  13. {
  14. _cElems = 0;
  15. _rgpv = NULL;
  16. _iSize = 0;
  17. }
  18. virtual ~CVoidPtrArray() { cicMemFree(_rgpv); }
  19. inline void Set(int iIndex, void *pv)
  20. {
  21. Assert(iIndex >= 0);
  22. Assert(iIndex < _cElems);
  23. _rgpv[iIndex] = pv;
  24. }
  25. inline void *Get(int iIndex) { return *GetPtr(iIndex); }
  26. inline void **GetPtr(int iIndex)
  27. {
  28. Assert(iIndex >= 0);
  29. Assert(iIndex <= _cElems); // there's code that uses the first invalid offset for loop termination
  30. return &_rgpv[iIndex];
  31. }
  32. BOOL Insert(int iIndex, int cElems);
  33. void Remove(int iIndex, int cElems);
  34. int Move(int iIndexNew, int iIndexOld);
  35. void **Append(int cElems)
  36. {
  37. return Insert(Count(), cElems) ? GetPtr(Count()-cElems) : NULL;
  38. }
  39. void SetCount(int cElems)
  40. {
  41. Assert(cElems >= 0);
  42. Assert(cElems <= _iSize);
  43. _cElems = cElems;
  44. }
  45. int Count() { return _cElems; }
  46. void Clear() { cicMemFree(_rgpv); _rgpv = NULL; _cElems = _iSize = 0; }
  47. void CompactSize(int iSizeNew)
  48. {
  49. void **ppv;
  50. Assert(iSizeNew <= _iSize);
  51. Assert(_cElems <= iSizeNew);
  52. if (iSizeNew == _iSize) // MemReAlloc will actually re-alloc! Don't let it.
  53. return;
  54. if ((ppv = (void **)cicMemReAlloc(_rgpv, iSizeNew*sizeof(void *))) != NULL)
  55. {
  56. _rgpv = ppv;
  57. _iSize = iSizeNew;
  58. }
  59. }
  60. void CompactSize() { CompactSize(_cElems); }
  61. private:
  62. void **_rgpv; // the array
  63. int _cElems; // num eles in the array
  64. int _iSize; // actual size (in void *'s) of the array
  65. };
  66. //
  67. // typesafe version
  68. //
  69. template<class T>
  70. class CPtrArray : public CVoidPtrArray
  71. {
  72. public:
  73. CPtrArray() {}
  74. void Set(int iIndex, T *pT) { CVoidPtrArray::Set(iIndex, pT); }
  75. T *Get(int iIndex) { return (T *)CVoidPtrArray::Get(iIndex); }
  76. T **GetPtr(int iIndex) { return (T **)CVoidPtrArray::GetPtr(iIndex); }
  77. T **Append(int cElems) { return (T **)CVoidPtrArray::Append(cElems); }
  78. };
  79. #endif // PTRARY_H