txStack.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef txStack_h___
  6. #define txStack_h___
  7. #include "nsTArray.h"
  8. class txStack : private nsTArray<void*>
  9. {
  10. public:
  11. /**
  12. * Returns the specified object from the top of this stack,
  13. * without removing it from the stack.
  14. *
  15. * @return a pointer to the object that is the top of this stack.
  16. */
  17. inline void* peek()
  18. {
  19. NS_ASSERTION(!isEmpty(), "peeking at empty stack");
  20. return !isEmpty() ? ElementAt(Length() - 1) : nullptr;
  21. }
  22. /**
  23. * Adds the specified object to the top of this stack.
  24. *
  25. * @param obj a pointer to the object that is to be added to the
  26. * top of this stack.
  27. */
  28. inline nsresult push(void* aObject)
  29. {
  30. return AppendElement(aObject) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
  31. }
  32. /**
  33. * Removes and returns the specified object from the top of this
  34. * stack.
  35. *
  36. * @return a pointer to the object that was the top of this stack.
  37. */
  38. inline void* pop()
  39. {
  40. void* object = nullptr;
  41. NS_ASSERTION(!isEmpty(), "popping from empty stack");
  42. if (!isEmpty())
  43. {
  44. const uint32_t count = Length() - 1;
  45. object = ElementAt(count);
  46. RemoveElementAt(count);
  47. }
  48. return object;
  49. }
  50. /**
  51. * Returns true if there are no objects in the stack.
  52. *
  53. * @return true if there are no objects in the stack.
  54. */
  55. inline bool isEmpty()
  56. {
  57. return IsEmpty();
  58. }
  59. /**
  60. * Returns the number of elements in the Stack.
  61. *
  62. * @return the number of elements in the Stack.
  63. */
  64. inline int32_t size()
  65. {
  66. return Length();
  67. }
  68. private:
  69. friend class txStackIterator;
  70. };
  71. class txStackIterator
  72. {
  73. public:
  74. /**
  75. * Creates an iterator for the given stack.
  76. *
  77. * @param aStack the stack to create an iterator for.
  78. */
  79. inline
  80. explicit txStackIterator(txStack* aStack) : mStack(aStack),
  81. mPosition(0)
  82. {
  83. }
  84. /**
  85. * Returns true if there is more objects on the stack.
  86. *
  87. * @return .
  88. */
  89. inline bool hasNext()
  90. {
  91. return (mPosition < mStack->Length());
  92. }
  93. /**
  94. * Returns the next object pointer from the stack.
  95. *
  96. * @return .
  97. */
  98. inline void* next()
  99. {
  100. if (mPosition == mStack->Length()) {
  101. return nullptr;
  102. }
  103. return mStack->ElementAt(mPosition++);
  104. }
  105. private:
  106. txStack* mStack;
  107. uint32_t mPosition;
  108. };
  109. #endif /* txStack_h___ */