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.

144 lines
3.8 KiB

  1. /* @doc INTERNAL
  2. *
  3. * @module _ARRAY.H Generic Array Class |
  4. *
  5. * This module declares a generic array class for constant sized
  6. * elements (although the elements themselves may be of any size).
  7. *
  8. * Original Author: <nl>
  9. * Christian Fortini
  10. *
  11. * History: <nl>
  12. * 6/25/95 alexgo Cleanup and Commenting
  13. *
  14. * Copyright (c) 1995-1997, Microsoft Corporation. All rights reserved.
  15. */
  16. #ifndef _ARRAY_H
  17. #define _ARRAY_H
  18. class CFormatRunPtr;
  19. /*
  20. * ArrayFlag
  21. *
  22. * @enum Defines flags used with the array class
  23. */
  24. enum tagArrayFlag
  25. {
  26. AF_KEEPMEM = 1, //@emem Don't delete any memory
  27. AF_DELETEMEM = 2, //@emem Delete as much memory as possible
  28. };
  29. //@type ArrayFlag | flags controlling the usage of generic arrays
  30. //(and specifically how memory is handled)
  31. typedef enum tagArrayFlag ArrayFlag;
  32. /*
  33. * CArrayBase
  34. *
  35. * @class The CArrayBase class implements a generic array class. It should
  36. * never be used directly; use the type-safe template, <c CArray>, instead.
  37. *
  38. * @devnote There are exactly two legal states for an array: empty or not.
  39. * If an array is empty, the following must be true:
  40. *
  41. * <md CArrayBase::_prgel> == NULL; <nl>
  42. * <md CArrayBase::_cel> == 0; <nl>
  43. * <md CArrayBase::_celMax> == 0;
  44. *
  45. * Otherwise, the following must be true:
  46. *
  47. * <md CArrayBase::_prgel> != NULL; <nl>
  48. * <md CArrayBase::_cel> <lt>= <md CArrayBase::_celMax>; <nl>
  49. * <md CArrayBase::_celMax> <gt> 0;
  50. *
  51. *
  52. * An array starts empty, transitions to not-empty as elements are inserted
  53. * and will transistion back to empty if all of the array elements are
  54. * removed.
  55. *
  56. */
  57. class CArrayBase
  58. {
  59. //@access Public Methods
  60. public:
  61. #ifdef DEBUG
  62. BOOL Invariant() const; //@cmember Validates state consistentency
  63. void* Elem(LONG iel) const; //@cmember Get ptr to <p iel>'th run
  64. #else
  65. void* Elem(LONG iel) const {return _prgel + iel*_cbElem;}
  66. #endif
  67. CArrayBase (LONG cbElem); //@cmember Constructor
  68. ~CArrayBase () {Clear(AF_DELETEMEM);}
  69. void Clear (ArrayFlag flag); //@cmember Delete all runs in array
  70. LONG Count() const {return _cel;}//@cmember Get count of runs in array
  71. //@cmember Remove <p celFree> runs from
  72. // array, starting at run <p ielFirst>
  73. void Remove(LONG ielFirst, LONG celFree);
  74. //@cmember Replace runs <p iel> through
  75. // <p iel+cel-1> with runs from <p par>
  76. BOOL Replace (LONG iel, LONG cel, CArrayBase *par);
  77. LONG Size() const {return _cbElem;}//@cmember Get size of a run
  78. //@access Protected Methods
  79. protected:
  80. void* ArAdd (LONG cel, LONG *pielIns); //@cmember Add <p cel> runs
  81. void* ArInsert(LONG iel, LONG celIns); //@cmember Insert <p celIns>
  82. // runs
  83. //@access Protected Data
  84. protected:
  85. char* _prgel; //@cmember Pointer to actual array data
  86. LONG _cel; //@cmember Count of used entries in array
  87. LONG _celMax; //@cmember Count of allocated entries in array
  88. LONG _cbElem; //@cmember Byte count of an individual array element
  89. };
  90. /*
  91. * CArray
  92. *
  93. * @class
  94. * An inline template class providing type-safe access to CArrayBase
  95. *
  96. * @tcarg class | ELEM | the class or struct to be used as array elements
  97. */
  98. template <class ELEM>
  99. class CArray : public CArrayBase
  100. {
  101. //@access Public Methods
  102. public:
  103. CArray () //@cmember Constructor
  104. : CArrayBase(sizeof(ELEM))
  105. {}
  106. ELEM * Elem(LONG iel) const //@cmember Get ptr to <p iel>'th
  107. { // element
  108. return (ELEM *)CArrayBase::Elem(iel);
  109. }
  110. ELEM& GetAt(LONG iel) const //@cmember Get <p iel>'th element
  111. {
  112. return *(ELEM *)CArrayBase::Elem(iel);
  113. }
  114. ELEM* Add (LONG cel, LONG *pielIns) //@cmember Adds <p cel> elements
  115. { // to end of array
  116. return (ELEM*)ArAdd(cel, pielIns);
  117. }
  118. ELEM* Insert (LONG iel, LONG celIns) //@cmember Insert <p celIns>
  119. { // elements at index <p iel>
  120. return (ELEM*)ArInsert(iel, celIns);
  121. }
  122. };
  123. #endif