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.

72 lines
2.0 KiB

  1. #pragma once
  2. // simple array-based stack class
  3. // Entries and exits are copies; this class is only suitable for small types,
  4. // like basic types(int, char, etc) and pointers
  5. // If you store dynamically allocated pointers, you are responsible for their deletion
  6. // This class DOES NOT TAKE OWNERSHIP of the objects if you use pointers.
  7. // Methods:
  8. // CStack(int iSizeHint=10)
  9. // Makes an empty stack with initial capacity of iSizeHint
  10. // ~CStack()
  11. // Deletes ONLY the internal data allocated by CStack (ie, the array).
  12. // Type Top()
  13. // Returns a copy of the last object pushed on the stack
  14. // Using this method when the stack is empty produces undefined behavior.
  15. // void Pop()
  16. // Removes the last entry from the stack (loses the refrence to it.)
  17. // Using this method when the stack is empty produces undefined behavior.
  18. // HRESULT Push(Type tobj)
  19. // Pushes a copy of tobj onto the stack. This method will return S_OK unless
  20. // it needs to resize the stack and does not have enough memory, in which case it
  21. // returns E_OUTOFMEMORY
  22. // BOOL IsEmpty()
  23. // Returns TRUE if the stack is empty, else FALSE.
  24. template<class Type>
  25. class CStack
  26. {
  27. protected:
  28. CSimpleArray<Type> m_srgArray;
  29. public:
  30. CStack(int iSizeHint = 10)
  31. {
  32. ATLASSERT(iSizeHint > 0);
  33. // note: iSizeHint is no longer used
  34. }
  35. ~CStack()
  36. {
  37. #ifdef DEBUG
  38. int nSize = m_srgArray.GetSize();
  39. ATLASSERT(nSize >= 0);
  40. #endif
  41. m_srgArray.RemoveAll();
  42. }
  43. Type Top()
  44. {
  45. int nSize = m_srgArray.GetSize();
  46. ATLASSERT(nSize > 0);
  47. return m_srgArray[nSize - 1];
  48. }
  49. void Pop()
  50. {
  51. int nSize = m_srgArray.GetSize();
  52. ATLASSERT(nSize > 0);
  53. m_srgArray.RemoveAt(nSize - 1);
  54. }
  55. HRESULT Push(Type tobj)
  56. {
  57. return m_srgArray.Add(tobj);
  58. }
  59. BOOL IsEmpty()
  60. {
  61. return (m_srgArray.GetSize() <= 0);
  62. }
  63. };