|
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 TRANSFRMX_LIST_H |
|
7 #define TRANSFRMX_LIST_H |
|
8 |
|
9 #include "txCore.h" |
|
10 |
|
11 class txListIterator; |
|
12 |
|
13 /** |
|
14 * Represents an ordered list of Object pointers. Modeled after a Java 2 List. |
|
15 **/ |
|
16 class txList : public txObject { |
|
17 |
|
18 friend class txListIterator; |
|
19 |
|
20 public: |
|
21 |
|
22 /** |
|
23 * Creates an empty txList |
|
24 **/ |
|
25 txList(); |
|
26 |
|
27 /** |
|
28 * txList destructor, object references will not be deleted. |
|
29 **/ |
|
30 ~txList(); |
|
31 |
|
32 /** |
|
33 * Returns the number of items in this txList |
|
34 **/ |
|
35 int32_t getLength(); |
|
36 |
|
37 /** |
|
38 * Returns true if there are no items in this txList |
|
39 */ |
|
40 inline bool isEmpty() |
|
41 { |
|
42 return itemCount == 0; |
|
43 } |
|
44 |
|
45 /** |
|
46 * Adds the given Object to the list |
|
47 **/ |
|
48 nsresult add(void* objPtr); |
|
49 |
|
50 /* |
|
51 * Removes all the objects from the list |
|
52 */ |
|
53 void clear(); |
|
54 |
|
55 protected: |
|
56 |
|
57 struct ListItem { |
|
58 ListItem* nextItem; |
|
59 ListItem* prevItem; |
|
60 void* objPtr; |
|
61 }; |
|
62 |
|
63 /** |
|
64 * Removes the given ListItem pointer from the list |
|
65 **/ |
|
66 ListItem* remove(ListItem* sItem); |
|
67 |
|
68 private: |
|
69 txList(const txList& aOther); // not implemented |
|
70 |
|
71 ListItem* firstItem; |
|
72 ListItem* lastItem; |
|
73 int32_t itemCount; |
|
74 |
|
75 nsresult insertAfter(void* objPtr, ListItem* sItem); |
|
76 nsresult insertBefore(void* objPtr, ListItem* sItem); |
|
77 }; |
|
78 |
|
79 |
|
80 |
|
81 /** |
|
82 * An Iterator for the txList Class |
|
83 **/ |
|
84 class txListIterator { |
|
85 |
|
86 public: |
|
87 /** |
|
88 * Creates a new txListIterator for the given txList |
|
89 * @param list, the txList to create an Iterator for |
|
90 **/ |
|
91 txListIterator(txList* list); |
|
92 |
|
93 /** |
|
94 * Adds the Object pointer to the txList pointed to by this txListIterator. |
|
95 * The Object pointer is inserted as the next item in the txList |
|
96 * based on the current position within the txList |
|
97 * @param objPtr the Object pointer to add to the list |
|
98 **/ |
|
99 nsresult addAfter(void* objPtr); |
|
100 |
|
101 /** |
|
102 * Adds the Object pointer to the txList pointed to by this txListIterator. |
|
103 * The Object pointer is inserted as the previous item in the txList |
|
104 * based on the current position within the txList |
|
105 * @param objPtr the Object pointer to add to the list |
|
106 **/ |
|
107 nsresult addBefore(void* objPtr); |
|
108 |
|
109 /** |
|
110 * Returns true if a successful call to the next() method can be made |
|
111 * @return true if a successful call to the next() method can be made, |
|
112 * otherwise false |
|
113 **/ |
|
114 bool hasNext(); |
|
115 |
|
116 /** |
|
117 * Returns the next Object pointer from the list |
|
118 **/ |
|
119 void* next(); |
|
120 |
|
121 /** |
|
122 * Returns the previous Object pointer from the list |
|
123 **/ |
|
124 void* previous(); |
|
125 |
|
126 /** |
|
127 * Returns the current Object |
|
128 **/ |
|
129 void* current(); |
|
130 |
|
131 /** |
|
132 * Removes the Object last returned by the next() or previous() methods; |
|
133 * @return the removed Object pointer |
|
134 **/ |
|
135 void* remove(); |
|
136 |
|
137 /** |
|
138 * Resets the current location within the txList to the beginning of the txList |
|
139 **/ |
|
140 void reset(); |
|
141 |
|
142 /** |
|
143 * Resets the current location within the txList to the end of the txList |
|
144 **/ |
|
145 void resetToEnd(); |
|
146 |
|
147 private: |
|
148 |
|
149 //-- points to the current list item |
|
150 txList::ListItem* currentItem; |
|
151 |
|
152 //-- points to the list to iterator over |
|
153 txList* list; |
|
154 |
|
155 //-- we've moved off the end of the list |
|
156 bool atEndOfList; |
|
157 }; |
|
158 |
|
159 typedef txList List; |
|
160 |
|
161 #endif |