1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is TransforMiiX XSLT processor code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2002
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Peter Van der Beken <peterv@propagandism.org>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef txStack_h___
40 : #define txStack_h___
41 :
42 : #include "nsTArray.h"
43 :
44 : class txStack : private nsTArray<void*>
45 180 : {
46 : public:
47 : /**
48 : * Returns the specified object from the top of this stack,
49 : * without removing it from the stack.
50 : *
51 : * @return a pointer to the object that is the top of this stack.
52 : */
53 0 : inline void* peek()
54 : {
55 0 : NS_ASSERTION(!isEmpty(), "peeking at empty stack");
56 0 : return !isEmpty() ? ElementAt(Length() - 1) : nsnull;
57 : }
58 :
59 : /**
60 : * Adds the specified object to the top of this stack.
61 : *
62 : * @param obj a pointer to the object that is to be added to the
63 : * top of this stack.
64 : */
65 42 : inline nsresult push(void* aObject)
66 : {
67 42 : return AppendElement(aObject) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
68 : }
69 :
70 : /**
71 : * Removes and returns the specified object from the top of this
72 : * stack.
73 : *
74 : * @return a pointer to the object that was the top of this stack.
75 : */
76 40 : inline void* pop()
77 : {
78 40 : void* object = nsnull;
79 40 : NS_ASSERTION(!isEmpty(), "popping from empty stack");
80 40 : if (!isEmpty())
81 : {
82 40 : const PRUint32 count = Length() - 1;
83 40 : object = ElementAt(count);
84 40 : RemoveElementAt(count);
85 : }
86 40 : return object;
87 : }
88 :
89 : /**
90 : * Returns true if there are no objects in the stack.
91 : *
92 : * @return true if there are no objects in the stack.
93 : */
94 206 : inline bool isEmpty()
95 : {
96 206 : return IsEmpty();
97 : }
98 :
99 : /**
100 : * Returns the number of elements in the Stack.
101 : *
102 : * @return the number of elements in the Stack.
103 : */
104 0 : inline PRInt32 size()
105 : {
106 0 : return Length();
107 : }
108 :
109 : private:
110 : friend class txStackIterator;
111 : };
112 :
113 : class txStackIterator
114 : {
115 : public:
116 : /**
117 : * Creates an iterator for the given stack.
118 : *
119 : * @param aStack the stack to create an iterator for.
120 : */
121 : inline
122 6 : txStackIterator(txStack* aStack) : mStack(aStack),
123 6 : mPosition(0)
124 : {
125 6 : }
126 :
127 : /**
128 : * Returns true if there is more objects on the stack.
129 : *
130 : * @return .
131 : */
132 8 : inline bool hasNext()
133 : {
134 8 : return (mPosition < mStack->Length());
135 : }
136 :
137 : /**
138 : * Returns the next object pointer from the stack.
139 : *
140 : * @return .
141 : */
142 2 : inline void* next()
143 : {
144 2 : if (mPosition == mStack->Length()) {
145 0 : return nsnull;
146 : }
147 2 : return mStack->ElementAt(mPosition++);
148 : }
149 :
150 : private:
151 : txStack* mStack;
152 : PRUint32 mPosition;
153 : };
154 :
155 : #endif /* txStack_h___ */
|