Line data Source code
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 : #include "txList.h"
7 :
8 : //----------------------------/
9 : //- Implementation of txList -/
10 : //----------------------------/
11 :
12 : /**
13 : * Default constructor for a txList;
14 : **/
15 :
16 0 : txList::txList() {
17 0 : firstItem = 0;
18 0 : lastItem = 0;
19 0 : itemCount = 0;
20 0 : } //-- txList;
21 :
22 : /**
23 : * txList destructor, cleans up ListItems, but will not delete the Object
24 : * references
25 : */
26 0 : txList::~txList() {
27 0 : clear();
28 0 : } //-- ~txList
29 :
30 0 : nsresult txList::add(void* objPtr)
31 : {
32 0 : return insertBefore(objPtr, 0);
33 : } //-- add
34 :
35 : /**
36 : * Returns the number of items in this txList
37 : **/
38 0 : int32_t List::getLength() {
39 0 : return itemCount;
40 : } //-- getLength
41 :
42 :
43 : /**
44 : * Inserts the given Object pointer as the item just after refItem.
45 : * If refItem is a null pointer the Object will be inserted at the
46 : * beginning of the txList (ie, insert after nothing).
47 : * This method assumes refItem is a member of this list, and since this
48 : * is a private method, I feel that's a valid assumption
49 : **/
50 0 : nsresult txList::insertAfter(void* objPtr, ListItem* refItem)
51 : {
52 : //-- if refItem == null insert at front
53 0 : if (!refItem)
54 0 : return insertBefore(objPtr, firstItem);
55 0 : return insertBefore(objPtr, refItem->nextItem);
56 : } //-- insertAfter
57 :
58 : /**
59 : * Inserts the given Object pointer as the item just before refItem.
60 : * If refItem is a null pointer the Object will be inserted at the
61 : * end of the txList (ie, insert before nothing).
62 : * This method assumes refItem is a member of this list, and since this
63 : * is a private method, I feel that's a valid assumption
64 : **/
65 0 : nsresult txList::insertBefore(void* objPtr, ListItem* refItem)
66 : {
67 0 : ListItem* item = new ListItem;
68 0 : item->objPtr = objPtr;
69 0 : item->nextItem = 0;
70 0 : item->prevItem = 0;
71 :
72 : //-- if refItem == null insert at end
73 0 : if (!refItem) {
74 : //-- add to back of list
75 0 : if (lastItem) {
76 0 : lastItem->nextItem = item;
77 0 : item->prevItem = lastItem;
78 : }
79 0 : lastItem = item;
80 0 : if (!firstItem)
81 0 : firstItem = item;
82 : }
83 : else {
84 : //-- insert before given item
85 0 : item->nextItem = refItem;
86 0 : item->prevItem = refItem->prevItem;
87 0 : refItem->prevItem = item;
88 :
89 0 : if (item->prevItem)
90 0 : item->prevItem->nextItem = item;
91 : else
92 0 : firstItem = item;
93 : }
94 :
95 : // increase the item count
96 0 : ++itemCount;
97 :
98 0 : return NS_OK;
99 : } //-- insertBefore
100 :
101 0 : txList::ListItem* txList::remove(ListItem* item) {
102 :
103 0 : if (!item)
104 0 : return item;
105 :
106 : //-- adjust the previous item's next pointer
107 0 : if (item->prevItem) {
108 0 : item->prevItem->nextItem = item->nextItem;
109 : }
110 : //-- adjust the next item's previous pointer
111 0 : if (item->nextItem) {
112 0 : item->nextItem->prevItem = item->prevItem;
113 : }
114 :
115 : //-- adjust first and last items
116 0 : if (item == firstItem)
117 0 : firstItem = item->nextItem;
118 0 : if (item == lastItem)
119 0 : lastItem = item->prevItem;
120 :
121 : //-- decrease Item count
122 0 : --itemCount;
123 0 : return item;
124 : } //-- remove
125 :
126 0 : void txList::clear()
127 : {
128 0 : ListItem* item = firstItem;
129 0 : while (item) {
130 0 : ListItem* tItem = item;
131 0 : item = item->nextItem;
132 : delete tItem;
133 : }
134 0 : firstItem = 0;
135 0 : lastItem = 0;
136 0 : itemCount = 0;
137 0 : }
138 :
139 : //------------------------------------/
140 : //- Implementation of txListIterator -/
141 : //------------------------------------/
142 :
143 :
144 : /**
145 : * Creates a new txListIterator for the given txList
146 : * @param list, the txList to create an Iterator for
147 : **/
148 0 : txListIterator::txListIterator(txList* list) {
149 0 : this->list = list;
150 0 : currentItem = 0;
151 0 : atEndOfList = false;
152 0 : } //-- txListIterator
153 :
154 : /**
155 : * Adds the Object pointer to the txList pointed to by this txListIterator.
156 : * The Object pointer is inserted as the next item in the txList
157 : * based on the current position within the txList
158 : * @param objPtr the Object pointer to add to the list
159 : **/
160 0 : nsresult txListIterator::addAfter(void* objPtr)
161 : {
162 0 : if (currentItem || !atEndOfList)
163 0 : return list->insertAfter(objPtr, currentItem);
164 0 : return list->insertBefore(objPtr, 0);
165 :
166 : } //-- addAfter
167 :
168 : /**
169 : * Adds the Object pointer to the txList pointed to by this txListIterator.
170 : * The Object pointer is inserted as the previous item in the txList
171 : * based on the current position within the txList
172 : * @param objPtr the Object pointer to add to the list
173 : **/
174 0 : nsresult txListIterator::addBefore(void* objPtr)
175 : {
176 0 : if (currentItem || atEndOfList)
177 0 : return list->insertBefore(objPtr, currentItem);
178 0 : return list->insertAfter(objPtr, 0);
179 :
180 : } //-- addBefore
181 :
182 : /**
183 : * Returns true if a successful call to the next() method can be made
184 : * @return true if a successful call to the next() method can be made,
185 : * otherwise false
186 : **/
187 0 : bool txListIterator::hasNext() {
188 0 : bool hasNext = false;
189 0 : if (currentItem)
190 0 : hasNext = (currentItem->nextItem != 0);
191 0 : else if (!atEndOfList)
192 0 : hasNext = (list->firstItem != 0);
193 :
194 0 : return hasNext;
195 : } //-- hasNext
196 :
197 : /**
198 : * Returns the next Object pointer in the list
199 : **/
200 0 : void* txListIterator::next() {
201 :
202 0 : void* obj = 0;
203 0 : if (currentItem)
204 0 : currentItem = currentItem->nextItem;
205 0 : else if (!atEndOfList)
206 0 : currentItem = list->firstItem;
207 :
208 0 : if (currentItem)
209 0 : obj = currentItem->objPtr;
210 : else
211 0 : atEndOfList = true;
212 :
213 0 : return obj;
214 : } //-- next
215 :
216 : /**
217 : * Returns the previous Object in the list
218 : **/
219 0 : void* txListIterator::previous() {
220 :
221 0 : void* obj = 0;
222 :
223 0 : if (currentItem)
224 0 : currentItem = currentItem->prevItem;
225 0 : else if (atEndOfList)
226 0 : currentItem = list->lastItem;
227 :
228 0 : if (currentItem)
229 0 : obj = currentItem->objPtr;
230 :
231 0 : atEndOfList = false;
232 :
233 0 : return obj;
234 : } //-- previous
235 :
236 : /**
237 : * Returns the current Object
238 : **/
239 0 : void* txListIterator::current() {
240 :
241 0 : if (currentItem)
242 0 : return currentItem->objPtr;
243 :
244 0 : return 0;
245 : } //-- current
246 :
247 : /**
248 : * Removes the Object last returned by the next() or previous() methods;
249 : * @return the removed Object pointer
250 : **/
251 0 : void* txListIterator::remove() {
252 :
253 0 : void* obj = 0;
254 0 : if (currentItem) {
255 0 : obj = currentItem->objPtr;
256 0 : txList::ListItem* item = currentItem;
257 0 : previous(); //-- make previous item the current item
258 0 : list->remove(item);
259 : delete item;
260 : }
261 0 : return obj;
262 : } //-- remove
263 :
264 : /**
265 : * Resets the current location within the txList to the beginning of the txList
266 : **/
267 0 : void txListIterator::reset() {
268 0 : atEndOfList = false;
269 0 : currentItem = 0;
270 0 : } //-- reset
271 :
272 : /**
273 : * Move the iterator to right after the last element
274 : **/
275 0 : void txListIterator::resetToEnd() {
276 0 : atEndOfList = true;
277 0 : currentItem = 0;
278 0 : } //-- moveToEnd
|