michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef txStack_h___ michael@0: #define txStack_h___ michael@0: michael@0: #include "nsTArray.h" michael@0: michael@0: class txStack : private nsTArray michael@0: { michael@0: public: michael@0: /** michael@0: * Returns the specified object from the top of this stack, michael@0: * without removing it from the stack. michael@0: * michael@0: * @return a pointer to the object that is the top of this stack. michael@0: */ michael@0: inline void* peek() michael@0: { michael@0: NS_ASSERTION(!isEmpty(), "peeking at empty stack"); michael@0: return !isEmpty() ? ElementAt(Length() - 1) : nullptr; michael@0: } michael@0: michael@0: /** michael@0: * Adds the specified object to the top of this stack. michael@0: * michael@0: * @param obj a pointer to the object that is to be added to the michael@0: * top of this stack. michael@0: */ michael@0: inline nsresult push(void* aObject) michael@0: { michael@0: return AppendElement(aObject) ? NS_OK : NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: /** michael@0: * Removes and returns the specified object from the top of this michael@0: * stack. michael@0: * michael@0: * @return a pointer to the object that was the top of this stack. michael@0: */ michael@0: inline void* pop() michael@0: { michael@0: void* object = nullptr; michael@0: NS_ASSERTION(!isEmpty(), "popping from empty stack"); michael@0: if (!isEmpty()) michael@0: { michael@0: const uint32_t count = Length() - 1; michael@0: object = ElementAt(count); michael@0: RemoveElementAt(count); michael@0: } michael@0: return object; michael@0: } michael@0: michael@0: /** michael@0: * Returns true if there are no objects in the stack. michael@0: * michael@0: * @return true if there are no objects in the stack. michael@0: */ michael@0: inline bool isEmpty() michael@0: { michael@0: return IsEmpty(); michael@0: } michael@0: michael@0: /** michael@0: * Returns the number of elements in the Stack. michael@0: * michael@0: * @return the number of elements in the Stack. michael@0: */ michael@0: inline int32_t size() michael@0: { michael@0: return Length(); michael@0: } michael@0: michael@0: private: michael@0: friend class txStackIterator; michael@0: }; michael@0: michael@0: class txStackIterator michael@0: { michael@0: public: michael@0: /** michael@0: * Creates an iterator for the given stack. michael@0: * michael@0: * @param aStack the stack to create an iterator for. michael@0: */ michael@0: inline michael@0: txStackIterator(txStack* aStack) : mStack(aStack), michael@0: mPosition(0) michael@0: { michael@0: } michael@0: michael@0: /** michael@0: * Returns true if there is more objects on the stack. michael@0: * michael@0: * @return . michael@0: */ michael@0: inline bool hasNext() michael@0: { michael@0: return (mPosition < mStack->Length()); michael@0: } michael@0: michael@0: /** michael@0: * Returns the next object pointer from the stack. michael@0: * michael@0: * @return . michael@0: */ michael@0: inline void* next() michael@0: { michael@0: if (mPosition == mStack->Length()) { michael@0: return nullptr; michael@0: } michael@0: return mStack->ElementAt(mPosition++); michael@0: } michael@0: michael@0: private: michael@0: txStack* mStack; michael@0: uint32_t mPosition; michael@0: }; michael@0: michael@0: #endif /* txStack_h___ */