FixedStack.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. template<class T, int N>
  7. class FixedStack
  8. {
  9. private:
  10. T itemList[N];
  11. int curIndex;
  12. public:
  13. FixedStack(): curIndex(-1)
  14. {
  15. }
  16. void Push(T item)
  17. {
  18. AssertMsg(curIndex < N - 1, "Stack overflow");
  19. if (curIndex >= N - 1)
  20. {
  21. Js::Throw::FatalInternalError();
  22. }
  23. this->itemList[++this->curIndex] = item;
  24. }
  25. T* Pop()
  26. {
  27. AssertMsg(curIndex >= 0, "Stack Underflow");
  28. if (curIndex < 0)
  29. {
  30. Js::Throw::FatalInternalError();
  31. }
  32. return &this->itemList[this->curIndex--];
  33. }
  34. T* Peek()
  35. {
  36. AssertMsg(curIndex >= 0, "No element present");
  37. if (curIndex < 0)
  38. {
  39. Js::Throw::FatalInternalError();
  40. }
  41. return & this->itemList[this->curIndex];
  42. }
  43. int Count()
  44. {
  45. return 1 + this->curIndex;
  46. }
  47. };