summaryrefslogtreecommitdiffstats
path: root/dom/xslt/base/txStack.h
blob: 53ce298625de501f89dc7c2cd8f15774b916474a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */

#ifndef txStack_h___
#define txStack_h___

#include "nsTArray.h"

class txStack : private nsTArray<void*>
{
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");
        return !isEmpty() ? ElementAt(Length() - 1) : nullptr;
    }

    /**
     * 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)
    {
        return AppendElement(aObject) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
    }

    /**
     * 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()
    {
        void* object = nullptr;
        NS_ASSERTION(!isEmpty(), "popping from empty stack");
        if (!isEmpty())
        {
            const uint32_t count = Length() - 1;
            object = ElementAt(count);
            RemoveElementAt(count);
        }
        return object;
    }

    /**
     * Returns true if there are no objects in the stack.
     *
     * @return true if there are no objects in the stack.
     */
    inline bool isEmpty()
    {
        return IsEmpty();
    }

    /**
     * Returns the number of elements in the Stack.
     *
     * @return the number of elements in the Stack.
     */
    inline int32_t size()
    {
        return Length();
    }

private:
    friend class txStackIterator;
};

class txStackIterator
{
public:
    /**
     * Creates an iterator for the given stack.
     *
     * @param aStack the stack to create an iterator for.
     */
    inline
    explicit txStackIterator(txStack* aStack) : mStack(aStack),
                                                mPosition(0)
    {
    }

    /**
     * Returns true if there is more objects on the stack.
     *
     * @return .
     */
    inline bool hasNext()
    {
        return (mPosition < mStack->Length());
    }

    /**
     * Returns the next object pointer from the stack.
     *
     * @return .
     */
    inline void* next()
    {
        if (mPosition == mStack->Length()) {
            return nullptr;
        }
        return mStack->ElementAt(mPosition++);
    }

private:
    txStack* mStack;
    uint32_t mPosition;
};

#endif /* txStack_h___ */