Windows NT 4.0 source code leak
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.

160 lines
4.0 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. dynarray.hxx
  5. Abstract:
  6. Dynamic Arrays are collections of objects of the same type.
  7. They have a value representing the number of instances and
  8. a pointer to memory containing a array of these objects.
  9. This file contains generic template to be instanced into
  10. a particular class.
  11. Author:
  12. Steven Zeck (stevez) 07/01/90
  13. --*/
  14. #ifndef _DYNARRAY_
  15. #define _DYNARRAY_
  16. char * Copy (void *pTo, void *pFrom, int cb);
  17. char * NewCopy (void *pBuff, int cb);
  18. #define DYN_ARRAY(NAME, TYPE) DYN_ARRAY_TYPE(NAME, TYPE, int)
  19. #define DYN_ARRAY_TYPE(NAME, TYPE, SIZE_TYPE) \
  20. \
  21. typedef TYPE *p##TYPE; \
  22. \
  23. class NAME { \
  24. \
  25. private: \
  26. \
  27. SIZE_TYPE cMax; /* number of elements in array */ \
  28. TYPE *pBase; /* pointer to base type */ \
  29. \
  30. public: \
  31. \
  32. NAME() { \
  33. cMax = 0; pBase = 0; \
  34. } \
  35. NAME(SIZE_TYPE cCur) { \
  36. cMax = cCur; \
  37. pBase = (cCur)? (TYPE *) new char [cMax * sizeof(TYPE)]: 0; \
  38. }; \
  39. NAME(SIZE_TYPE cCur, TYPE *pType) { \
  40. cMax = cCur; \
  41. pBase = pType; \
  42. }; \
  43. NAME(NAME &DAi) { \
  44. *this = DAi; \
  45. }; \
  46. void Free() { /* manual destructure */ \
  47. if (pBase) \
  48. delete(pBase); \
  49. } \
  50. \
  51. /* following routes allow access to the items */ \
  52. \
  53. TYPE& operator [] (SIZE_TYPE iType) { \
  54. \
  55. /* ASSERT(pBase && iType < cMax); */ \
  56. return (pBase[iType]); \
  57. } \
  58. \
  59. TYPE& operator * () { \
  60. \
  61. /* ASSERT(pBase && cMax); */ \
  62. return (pBase[0]); \
  63. } \
  64. \
  65. p##TYPE& pCur() { return(pBase); } \
  66. SIZE_TYPE& cCur() { return(cMax); } \
  67. SIZE_TYPE Size() { return(cMax * sizeof(TYPE)); } \
  68. \
  69. /* conversion methods */ \
  70. \
  71. operator SIZE_TYPE() { return(cMax); } \
  72. \
  73. /* Copy methods */ \
  74. \
  75. NAME Dup() { \
  76. NAME DAt(cMax, (TYPE *)NewCopy(pBase, (int) cMax * sizeof(TYPE)));\
  77. \
  78. return(DAt); \
  79. } \
  80. \
  81. /* Copy to a buffer, returning the next place in the buff */ \
  82. \
  83. char * CopyBuff(char *pb) { \
  84. \
  85. return((char *) Copy(pb, pBase, (int) cMax * sizeof(TYPE))); \
  86. } \
  87. \
  88. /* Copy to a buffer w/descriptor */ \
  89. \
  90. char * Marshall(char *pb) { \
  91. \
  92. pb = Copy(pb, this, sizeof(*this)); \
  93. return((char *) Copy(pb, pBase, (int) cMax * sizeof(TYPE))); \
  94. } \
  95. \
  96. int MarshallSize() { \
  97. \
  98. return(sizeof(*this) + Size()); \
  99. } \
  100. }; \
  101. \
  102. class NAME##_ITER { /* iterator for the class */ \
  103. \
  104. private: \
  105. \
  106. SIZE_TYPE cLeft; /* number of elements in array */ \
  107. TYPE *pCur; /* pointer to base type */ \
  108. \
  109. public: \
  110. \
  111. NAME##_ITER(NAME& aDA) { /* Bind an interator to a DA */ \
  112. cLeft = aDA; \
  113. pCur = aDA.pCur(); \
  114. } \
  115. \
  116. void Reset(NAME& aDA, SIZE_TYPE offset = 0) { /* reBind an interator to a DA */ \
  117. cLeft = aDA - offset; \
  118. pCur = aDA.pCur() + offset; \
  119. } \
  120. \
  121. NAME##_ITER() { \
  122. cLeft = 0; \
  123. pCur = 0; \
  124. } \
  125. \
  126. p##TYPE operator ++() { /* move to the next element */ \
  127. pCur++; \
  128. \
  129. if (--cLeft <= 0) \
  130. pCur = (TYPE *)0; \
  131. \
  132. return(pCur); \
  133. } \
  134. \
  135. SIZE_TYPE cCur() { return(cLeft); } \
  136. \
  137. /* you access through an implicit type conversion */ \
  138. \
  139. operator p##TYPE() { return(pCur); } \
  140. \
  141. TYPE& operator * () { \
  142. \
  143. /* ASSERT(pCur && cLeft);*/ \
  144. return(*pCur); \
  145. } \
  146. };
  147. #endif //_DYNARRAY_