|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef txStack_h___ |
|
7 #define txStack_h___ |
|
8 |
|
9 #include "nsTArray.h" |
|
10 |
|
11 class txStack : private nsTArray<void*> |
|
12 { |
|
13 public: |
|
14 /** |
|
15 * Returns the specified object from the top of this stack, |
|
16 * without removing it from the stack. |
|
17 * |
|
18 * @return a pointer to the object that is the top of this stack. |
|
19 */ |
|
20 inline void* peek() |
|
21 { |
|
22 NS_ASSERTION(!isEmpty(), "peeking at empty stack"); |
|
23 return !isEmpty() ? ElementAt(Length() - 1) : nullptr; |
|
24 } |
|
25 |
|
26 /** |
|
27 * Adds the specified object to the top of this stack. |
|
28 * |
|
29 * @param obj a pointer to the object that is to be added to the |
|
30 * top of this stack. |
|
31 */ |
|
32 inline nsresult push(void* aObject) |
|
33 { |
|
34 return AppendElement(aObject) ? NS_OK : NS_ERROR_OUT_OF_MEMORY; |
|
35 } |
|
36 |
|
37 /** |
|
38 * Removes and returns the specified object from the top of this |
|
39 * stack. |
|
40 * |
|
41 * @return a pointer to the object that was the top of this stack. |
|
42 */ |
|
43 inline void* pop() |
|
44 { |
|
45 void* object = nullptr; |
|
46 NS_ASSERTION(!isEmpty(), "popping from empty stack"); |
|
47 if (!isEmpty()) |
|
48 { |
|
49 const uint32_t count = Length() - 1; |
|
50 object = ElementAt(count); |
|
51 RemoveElementAt(count); |
|
52 } |
|
53 return object; |
|
54 } |
|
55 |
|
56 /** |
|
57 * Returns true if there are no objects in the stack. |
|
58 * |
|
59 * @return true if there are no objects in the stack. |
|
60 */ |
|
61 inline bool isEmpty() |
|
62 { |
|
63 return IsEmpty(); |
|
64 } |
|
65 |
|
66 /** |
|
67 * Returns the number of elements in the Stack. |
|
68 * |
|
69 * @return the number of elements in the Stack. |
|
70 */ |
|
71 inline int32_t size() |
|
72 { |
|
73 return Length(); |
|
74 } |
|
75 |
|
76 private: |
|
77 friend class txStackIterator; |
|
78 }; |
|
79 |
|
80 class txStackIterator |
|
81 { |
|
82 public: |
|
83 /** |
|
84 * Creates an iterator for the given stack. |
|
85 * |
|
86 * @param aStack the stack to create an iterator for. |
|
87 */ |
|
88 inline |
|
89 txStackIterator(txStack* aStack) : mStack(aStack), |
|
90 mPosition(0) |
|
91 { |
|
92 } |
|
93 |
|
94 /** |
|
95 * Returns true if there is more objects on the stack. |
|
96 * |
|
97 * @return . |
|
98 */ |
|
99 inline bool hasNext() |
|
100 { |
|
101 return (mPosition < mStack->Length()); |
|
102 } |
|
103 |
|
104 /** |
|
105 * Returns the next object pointer from the stack. |
|
106 * |
|
107 * @return . |
|
108 */ |
|
109 inline void* next() |
|
110 { |
|
111 if (mPosition == mStack->Length()) { |
|
112 return nullptr; |
|
113 } |
|
114 return mStack->ElementAt(mPosition++); |
|
115 } |
|
116 |
|
117 private: |
|
118 txStack* mStack; |
|
119 uint32_t mPosition; |
|
120 }; |
|
121 |
|
122 #endif /* txStack_h___ */ |