1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
8 //----------------------------/
9 //- Implementation of txList -/
10 //----------------------------/
13 * Default constructor for a txList;
23 * txList destructor, cleans up ListItems, but will not delete the Object
26 txList::~txList() { clear(); } //-- ~txList
28 void txList::add(void* objPtr
) { insertBefore(objPtr
, nullptr); } //-- add
31 * Returns the number of items in this txList
33 int32_t List::getLength() { return itemCount
; } //-- getLength
36 * Inserts the given Object pointer as the item just after refItem.
37 * If refItem is a null pointer the Object will be inserted at the
38 * beginning of the txList (ie, insert after nothing).
39 * This method assumes refItem is a member of this list, and since this
40 * is a private method, I feel that's a valid assumption
42 void txList::insertAfter(void* objPtr
, ListItem
* refItem
) {
43 insertBefore(objPtr
, refItem
? refItem
->nextItem
: firstItem
);
47 * Inserts the given Object pointer as the item just before refItem.
48 * If refItem is a null pointer the Object will be inserted at the
49 * end of the txList (ie, insert before nothing).
50 * This method assumes refItem is a member of this list, and since this
51 * is a private method, I feel that's a valid assumption
53 void txList::insertBefore(void* objPtr
, ListItem
* refItem
) {
54 ListItem
* item
= new ListItem
;
55 item
->objPtr
= objPtr
;
59 //-- if refItem == null insert at end
61 //-- add to back of list
63 lastItem
->nextItem
= item
;
64 item
->prevItem
= lastItem
;
67 if (!firstItem
) firstItem
= item
;
69 //-- insert before given item
70 item
->nextItem
= refItem
;
71 item
->prevItem
= refItem
->prevItem
;
72 refItem
->prevItem
= item
;
75 item
->prevItem
->nextItem
= item
;
80 // increase the item count
84 txList::ListItem
* txList::remove(ListItem
* item
) {
85 if (!item
) return item
;
87 //-- adjust the previous item's next pointer
89 item
->prevItem
->nextItem
= item
->nextItem
;
91 //-- adjust the next item's previous pointer
93 item
->nextItem
->prevItem
= item
->prevItem
;
96 //-- adjust first and last items
97 if (item
== firstItem
) firstItem
= item
->nextItem
;
98 if (item
== lastItem
) lastItem
= item
->prevItem
;
100 //-- decrease Item count
105 void txList::clear() {
106 ListItem
* item
= firstItem
;
108 ListItem
* tItem
= item
;
109 item
= item
->nextItem
;
117 //------------------------------------/
118 //- Implementation of txListIterator -/
119 //------------------------------------/
122 * Creates a new txListIterator for the given txList
123 * @param list, the txList to create an Iterator for
125 txListIterator::txListIterator(txList
* list
) {
129 } //-- txListIterator
132 * Adds the Object pointer to the txList pointed to by this txListIterator.
133 * The Object pointer is inserted as the next item in the txList
134 * based on the current position within the txList
135 * @param objPtr the Object pointer to add to the list
137 void txListIterator::addAfter(void* objPtr
) {
138 if (currentItem
|| !atEndOfList
) {
139 list
->insertAfter(objPtr
, currentItem
);
141 list
->insertBefore(objPtr
, nullptr);
146 * Adds the Object pointer to the txList pointed to by this txListIterator.
147 * The Object pointer is inserted as the previous item in the txList
148 * based on the current position within the txList
149 * @param objPtr the Object pointer to add to the list
151 void txListIterator::addBefore(void* objPtr
) {
152 if (currentItem
|| atEndOfList
) {
153 list
->insertBefore(objPtr
, currentItem
);
155 list
->insertAfter(objPtr
, nullptr);
160 * Returns true if a successful call to the next() method can be made
161 * @return true if a successful call to the next() method can be made,
164 bool txListIterator::hasNext() {
165 bool hasNext
= false;
167 hasNext
= (currentItem
->nextItem
!= 0);
168 else if (!atEndOfList
)
169 hasNext
= (list
->firstItem
!= 0);
175 * Returns the next Object pointer in the list
177 void* txListIterator::next() {
180 currentItem
= currentItem
->nextItem
;
181 else if (!atEndOfList
)
182 currentItem
= list
->firstItem
;
185 obj
= currentItem
->objPtr
;
193 * Returns the previous Object in the list
195 void* txListIterator::previous() {
199 currentItem
= currentItem
->prevItem
;
200 else if (atEndOfList
)
201 currentItem
= list
->lastItem
;
203 if (currentItem
) obj
= currentItem
->objPtr
;
211 * Returns the current Object
213 void* txListIterator::current() {
214 if (currentItem
) return currentItem
->objPtr
;
220 * Removes the Object last returned by the next() or previous() methods;
221 * @return the removed Object pointer
223 void* txListIterator::remove() {
226 obj
= currentItem
->objPtr
;
227 txList::ListItem
* item
= currentItem
;
228 previous(); //-- make previous item the current item
236 * Resets the current location within the txList to the beginning of the txList
238 void txListIterator::reset() {
244 * Move the iterator to right after the last element
246 void txListIterator::resetToEnd() {