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.

146 lines
3.9 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. //@cmember Copy array into passed in array and initialize this array
  70. void TransferTo(CArrayBase &ar);
  71. void Clear (ArrayFlag flag); //@cmember Delete all runs in array
  72. LONG Count() const {return _cel;}//@cmember Get count of runs in array
  73. //@cmember Remove <p celFree> runs from
  74. // array, starting at run <p ielFirst>
  75. void Remove(LONG ielFirst, LONG celFree);
  76. //@cmember Replace runs <p iel> through
  77. // <p iel+cel-1> with runs from <p par>
  78. BOOL Replace (LONG iel, LONG cel, CArrayBase *par);
  79. LONG Size() const {return _cbElem;}//@cmember Get size of a run
  80. //@access Protected Methods
  81. protected:
  82. void* ArAdd (LONG cel, LONG *pielIns); //@cmember Add <p cel> runs
  83. void* ArInsert(LONG iel, LONG celIns); //@cmember Insert <p celIns>
  84. // runs
  85. //@access Protected Data
  86. protected:
  87. char* _prgel; //@cmember Pointer to actual array data
  88. LONG _cel; //@cmember Count of used entries in array
  89. LONG _celMax; //@cmember Count of allocated entries in array
  90. LONG _cbElem; //@cmember Byte count of an individual array element
  91. };
  92. /*
  93. * CArray
  94. *
  95. * @class
  96. * An inline template class providing type-safe access to CArrayBase
  97. *
  98. * @tcarg class | ELEM | the class or struct to be used as array elements
  99. */
  100. template <class ELEM>
  101. class CArray : public CArrayBase
  102. {
  103. //@access Public Methods
  104. public:
  105. CArray () //@cmember Constructor
  106. : CArrayBase(sizeof(ELEM))
  107. {}
  108. ELEM * Elem(LONG iel) const //@cmember Get ptr to <p iel>'th
  109. { // element
  110. return (ELEM *)CArrayBase::Elem(iel);
  111. }
  112. ELEM& GetAt(LONG iel) const //@cmember Get <p iel>'th element
  113. {
  114. return *(ELEM *)CArrayBase::Elem(iel);
  115. }
  116. ELEM* Add (LONG cel, LONG *pielIns) //@cmember Adds <p cel> elements
  117. { // to end of array
  118. return (ELEM*)ArAdd(cel, pielIns);
  119. }
  120. ELEM* Insert (LONG iel, LONG celIns) //@cmember Insert <p celIns>
  121. { // elements at index <p iel>
  122. return (ELEM*)ArInsert(iel, celIns);
  123. }
  124. };
  125. #endif