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