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