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.

99 lines
1.7 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. STACKT.H
  5. Abstract:
  6. History:
  7. --*/
  8. #ifndef __HMM_STACK_TEMPL__H_
  9. #define __HMM_STACK_TEMPL__H_
  10. #include <windows.h>
  11. template<class TElement>
  12. class CHmmStack
  13. {
  14. protected:
  15. TElement* m_aStack;
  16. int m_nSize;
  17. int m_nIndex;
  18. public:
  19. inline CHmmStack(int nSize)
  20. {
  21. m_nSize = nSize;
  22. m_aStack = new TElement[nSize];
  23. m_nIndex = 0;
  24. }
  25. virtual ~CHmmStack()
  26. {
  27. delete [] m_aStack;
  28. }
  29. inline BOOL Push(IN const TElement& Val)
  30. {
  31. if(m_nIndex == m_nSize)
  32. {
  33. return FALSE;
  34. }
  35. else
  36. {
  37. m_aStack[m_nIndex++] = Val;
  38. return TRUE;
  39. }
  40. }
  41. inline BOOL Pop(OUT TElement& Val)
  42. {
  43. if(m_nIndex == 0)
  44. {
  45. return FALSE;
  46. }
  47. else
  48. {
  49. Val = m_aStack[--m_nIndex];
  50. return TRUE;
  51. }
  52. }
  53. inline BOOL Peek(OUT TElement& Val)
  54. {
  55. if(m_nIndex == 0)
  56. {
  57. return FALSE;
  58. }
  59. else
  60. {
  61. Val = m_aStack[m_nIndex-1];
  62. return TRUE;
  63. }
  64. }
  65. inline void PopToSize(int nNewSize)
  66. {
  67. m_nIndex = nNewSize;
  68. }
  69. inline BOOL IsEmpty() {return m_nIndex == 0;}
  70. inline void Empty() {m_nIndex = 0;}
  71. inline int GetSize() {return m_nIndex;}
  72. };
  73. template<class TPointee>
  74. class CUniquePointerStack : public CHmmStack<TPointee*>
  75. {
  76. public:
  77. CUniquePointerStack<TPointee>(int nSize)
  78. : CHmmStack<TPointee*>(nSize){}
  79. ~CUniquePointerStack<TPointee>()
  80. {
  81. for(int i = 0; i < m_nIndex; i++)
  82. {
  83. delete m_aStack[i];
  84. }
  85. }
  86. };
  87. #endif