1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
11 class txStack
: private nsTArray
<void*> {
14 * Returns the specified object from the top of this stack,
15 * without removing it from the stack.
17 * @return a pointer to the object that is the top of this stack.
20 NS_ASSERTION(!isEmpty(), "peeking at empty stack");
21 return !isEmpty() ? ElementAt(Length() - 1) : nullptr;
25 * Adds the specified object to the top of this stack.
27 * @param obj a pointer to the object that is to be added to the
30 inline void push(void* aObject
) { AppendElement(aObject
); }
33 * Removes and returns the specified object from the top of this
36 * @return a pointer to the object that was the top of this stack.
39 void* object
= nullptr;
40 NS_ASSERTION(!isEmpty(), "popping from empty stack");
42 object
= PopLastElement();
48 * Returns true if there are no objects in the stack.
50 * @return true if there are no objects in the stack.
52 inline bool isEmpty() { return IsEmpty(); }
55 * Returns the number of elements in the Stack.
57 * @return the number of elements in the Stack.
59 inline int32_t size() { return Length(); }
62 friend class txStackIterator
;
65 class txStackIterator
{
68 * Creates an iterator for the given stack.
70 * @param aStack the stack to create an iterator for.
72 inline explicit txStackIterator(txStack
* aStack
)
73 : mStack(aStack
), mPosition(0) {}
76 * Returns true if there is more objects on the stack.
80 inline bool hasNext() { return (mPosition
< mStack
->Length()); }
83 * Returns the next object pointer from the stack.
88 if (mPosition
== mStack
->Length()) {
91 return mStack
->ElementAt(mPosition
++);
99 #endif /* txStack_h___ */