dom/xslt/base/txStack.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/xslt/base/txStack.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,122 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef txStack_h___
    1.10 +#define txStack_h___
    1.11 +
    1.12 +#include "nsTArray.h"
    1.13 +
    1.14 +class txStack : private nsTArray<void*>
    1.15 +{
    1.16 +public:
    1.17 +    /**
    1.18 +     * Returns the specified object from the top of this stack,
    1.19 +     * without removing it from the stack.
    1.20 +     *
    1.21 +     * @return a pointer to the object that is the top of this stack.
    1.22 +     */
    1.23 +    inline void* peek()
    1.24 +    {
    1.25 +        NS_ASSERTION(!isEmpty(), "peeking at empty stack");
    1.26 +        return !isEmpty() ? ElementAt(Length() - 1) : nullptr;
    1.27 +    }
    1.28 +
    1.29 +    /**
    1.30 +     * Adds the specified object to the top of this stack.
    1.31 +     *
    1.32 +     * @param obj a pointer to the object that is to be added to the
    1.33 +     * top of this stack.
    1.34 +     */
    1.35 +    inline nsresult push(void* aObject)
    1.36 +    {
    1.37 +        return AppendElement(aObject) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
    1.38 +    }
    1.39 +
    1.40 +    /**
    1.41 +     * Removes and returns the specified object from the top of this
    1.42 +     * stack.
    1.43 +     *
    1.44 +     * @return a pointer to the object that was the top of this stack.
    1.45 +     */
    1.46 +    inline void* pop()
    1.47 +    {
    1.48 +        void* object = nullptr;
    1.49 +        NS_ASSERTION(!isEmpty(), "popping from empty stack");
    1.50 +        if (!isEmpty())
    1.51 +        {
    1.52 +            const uint32_t count = Length() - 1;
    1.53 +            object = ElementAt(count);
    1.54 +            RemoveElementAt(count);
    1.55 +        }
    1.56 +        return object;
    1.57 +    }
    1.58 +
    1.59 +    /**
    1.60 +     * Returns true if there are no objects in the stack.
    1.61 +     *
    1.62 +     * @return true if there are no objects in the stack.
    1.63 +     */
    1.64 +    inline bool isEmpty()
    1.65 +    {
    1.66 +        return IsEmpty();
    1.67 +    }
    1.68 +
    1.69 +    /**
    1.70 +     * Returns the number of elements in the Stack.
    1.71 +     *
    1.72 +     * @return the number of elements in the Stack.
    1.73 +     */
    1.74 +    inline int32_t size()
    1.75 +    {
    1.76 +        return Length();
    1.77 +    }
    1.78 +
    1.79 +private:
    1.80 +    friend class txStackIterator;
    1.81 +};
    1.82 +
    1.83 +class txStackIterator
    1.84 +{
    1.85 +public:
    1.86 +    /**
    1.87 +     * Creates an iterator for the given stack.
    1.88 +     *
    1.89 +     * @param aStack the stack to create an iterator for.
    1.90 +     */
    1.91 +    inline
    1.92 +    txStackIterator(txStack* aStack) : mStack(aStack),
    1.93 +                                       mPosition(0)
    1.94 +    {
    1.95 +    }
    1.96 +
    1.97 +    /**
    1.98 +     * Returns true if there is more objects on the stack.
    1.99 +     *
   1.100 +     * @return .
   1.101 +     */
   1.102 +    inline bool hasNext()
   1.103 +    {
   1.104 +        return (mPosition < mStack->Length());
   1.105 +    }
   1.106 +
   1.107 +    /**
   1.108 +     * Returns the next object pointer from the stack.
   1.109 +     *
   1.110 +     * @return .
   1.111 +     */
   1.112 +    inline void* next()
   1.113 +    {
   1.114 +        if (mPosition == mStack->Length()) {
   1.115 +            return nullptr;
   1.116 +        }
   1.117 +        return mStack->ElementAt(mPosition++);
   1.118 +    }
   1.119 +
   1.120 +private:
   1.121 +    txStack* mStack;
   1.122 +    uint32_t mPosition;
   1.123 +};
   1.124 +
   1.125 +#endif /* txStack_h___ */

mercurial