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.

227 lines
6.8 KiB

  1. /****************************************************************************/
  2. /** Microsoft OS/2 LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1990 **/
  4. /****************************************************************************/
  5. /****************************************************************************\
  6. stack.hxx
  7. LM 3.0 Generic stack packackage
  8. This header file contains a generic stack "macro" that can be used to
  9. create an efficient stack implementation for any type.
  10. FILE HISTORY:
  11. johnl 27-Jun-1990 Hacked from Mikemon's stack implementation
  12. KeithMo 09-Oct-1991 Win32 Conversion.
  13. KeithMo 23-Oct-1991 Added forward references.
  14. \****************************************************************************/
  15. /****************************************************************************\
  16. *
  17. *NAME: STACK
  18. *
  19. *WORKBOOK: STACK
  20. *
  21. *SYNOPSIS: Parameterized stack implementation
  22. *
  23. *INTERFACE: DECLARE_STACK_OF(itemtype) - Produces a declaration for a stack
  24. * of itemtype.
  25. * DEFINE_STACK_OF(itemtype) - Produces code for a stack of
  26. * itemtype.
  27. * STACK_OF(itemtype) MyStack ;- Declares stack of itemtype called
  28. * MyStack
  29. *
  30. * void Clear( ) - Specifically delete all elements in the
  31. * stack.
  32. *
  33. * UINT QueryNumElem( ) - Returns the number of elements
  34. * in the stack.
  35. *
  36. * APIERR Push( ) - Adds the pointer object to the stack
  37. *
  38. * itemtype* Pop( ) - Removes the next item from the stack and
  39. * returns it to the user.
  40. *
  41. * itemtype* Peek( ) - Returns a pointer to the item on the
  42. * stack that would be returned on the next pop call.
  43. *
  44. *PARENT:
  45. *
  46. *USES: NONE
  47. *
  48. *HISTORY:
  49. * Johnl 27-Jun-1990 Created (Hacked from Mikemon's code)
  50. * JohnL 18-Oct-1990 Takes pointers rather then objects
  51. *
  52. *
  53. \****************************************************************************/
  54. #ifndef _STACK_HXX_
  55. #define _STACK_HXX_
  56. #include "iter.hxx"
  57. #define STACK_OF(type) STACK_OF_##type
  58. #define ITER_ST_OF(type) ITER_ST_##type
  59. #define DECLARE_STACK_OF(type) \
  60. \
  61. class stack_node_of_##type; \
  62. class STACK_OF(type); \
  63. class ITER_ST_OF(type); \
  64. \
  65. class stack_node_of_##type \
  66. { \
  67. friend class STACK_OF(type) ; \
  68. friend class ITER_ST_OF(type) ; \
  69. \
  70. protected: \
  71. stack_node_of_##type *next; \
  72. type *item; \
  73. }; \
  74. \
  75. \
  76. DECLARE_ITER_OF(type) \
  77. \
  78. class ITER_ST_OF(type) : public ITER_OF(type) /* Unsafe stack iterator */ \
  79. { \
  80. public: \
  81. ITER_ST_OF(type)( const STACK_OF(type)& st ) ; \
  82. ~ITER_ST_OF(type)() ; \
  83. void Reset( void ) ; \
  84. type* operator()(void) { return Next(); } \
  85. type* Next( void ) ; \
  86. \
  87. private: \
  88. stack_node_of_##type *_pstknodeCurrent ; \
  89. const STACK_OF(type) *_pstk ; \
  90. \
  91. }; \
  92. \
  93. class STACK_OF(type) \
  94. { \
  95. friend class ITER_ST_OF(type) ; \
  96. \
  97. private: \
  98. UINT uNumElem ; \
  99. stack_node_of_##type *StackBase; \
  100. public: \
  101. STACK_OF(type)(); \
  102. ~STACK_OF(type)(); \
  103. void Clear( void ) ; \
  104. UINT QueryNumElem( void ) ; \
  105. APIERR Push( type * const Item); \
  106. type *Pop( void ); \
  107. type *Peek( void ) ; \
  108. }; \
  109. \
  110. /*#define DEFINE_STACK_OF(type)*/ \
  111. STACK_OF(type)::STACK_OF(type)() \
  112. { \
  113. StackBase = (stack_node_of_##type *) NULL ; \
  114. uNumElem = 0 ; \
  115. } \
  116. \
  117. STACK_OF(type)::~STACK_OF(type)() \
  118. { \
  119. Clear() ; \
  120. } \
  121. \
  122. UINT STACK_OF(type)::QueryNumElem( void ) \
  123. { \
  124. return ( uNumElem ) ; \
  125. } \
  126. \
  127. void STACK_OF(type)::Clear( void ) \
  128. { \
  129. type *item ; \
  130. \
  131. while ( item = Pop() ) \
  132. delete item ; \
  133. \
  134. StackBase = (stack_node_of_##type *) 0 ; \
  135. uNumElem = 0 ; \
  136. } \
  137. \
  138. APIERR STACK_OF(type)::Push( type * const pItem) \
  139. { \
  140. UIASSERT( pItem != NULL ) ; \
  141. \
  142. stack_node_of_##type *node = new stack_node_of_##type; \
  143. \
  144. if (node == (stack_node_of_##type *) NULL ) \
  145. return(ERROR_NOT_ENOUGH_MEMORY); \
  146. else \
  147. node->item = pItem ; \
  148. \
  149. node->next = StackBase; \
  150. StackBase = node; \
  151. uNumElem++ ; \
  152. return(NERR_Success); \
  153. } \
  154. \
  155. type *STACK_OF(type)::Pop() \
  156. { \
  157. stack_node_of_##type *node; \
  158. type *pitem; \
  159. \
  160. if (StackBase == (stack_node_of_##type *) NULL) \
  161. return((type *) NULL); \
  162. \
  163. node = StackBase; \
  164. pitem = StackBase->item; \
  165. StackBase = StackBase->next; \
  166. delete node; \
  167. uNumElem-- ; \
  168. return(pitem); \
  169. } \
  170. type *STACK_OF(type)::Peek() \
  171. { \
  172. if (StackBase == NULL) \
  173. return((type *) NULL); \
  174. else \
  175. return (StackBase->item); \
  176. } \
  177. \
  178. ITER_ST_OF(type) :: ITER_ST_OF(type)( const STACK_OF(type)& stk ) \
  179. { \
  180. _pstk = &stk ; \
  181. _pstknodeCurrent = stk.StackBase ; \
  182. } \
  183. \
  184. ITER_ST_OF(type) :: ~ITER_ST_OF(type)() \
  185. { \
  186. ; \
  187. } \
  188. \
  189. type * ITER_ST_OF(type) :: Next( void ) \
  190. { \
  191. if (_pstknodeCurrent) \
  192. { \
  193. stack_node_of_##type *_pstknodeTmp = _pstknodeCurrent ; \
  194. \
  195. _pstknodeCurrent = _pstknodeCurrent->next ; \
  196. return (_pstknodeTmp->item) ; \
  197. } \
  198. else \
  199. { \
  200. return ((type *) NULL) ; \
  201. } \
  202. \
  203. } \
  204. \
  205. \
  206. void ITER_ST_OF(type) :: Reset( void ) \
  207. { \
  208. _pstknodeCurrent = _pstk->StackBase ; \
  209. }
  210. #define DECLARE_STACK_OF(type)
  211. DECL_STACK_OF(type,DLL_TEMPLATE)
  212. #endif // _STACK_HXX_