[790] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Fabian 'x3n' Landau |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
[871] | 28 | /** |
---|
[790] | 29 | @file ObjectList.h |
---|
| 30 | @brief Definition and implementation of the ObjectList class. |
---|
| 31 | |
---|
| 32 | The ObjectList is a double-linked list, used by Identifiers to store all objects of a given class. |
---|
| 33 | Newly created objects are added through the RegisterObject-macro in its constructor. |
---|
| 34 | Use Iterator<class> to iterate through all objects of the class. |
---|
| 35 | */ |
---|
| 36 | |
---|
[197] | 37 | #ifndef _ObjectList_H__ |
---|
| 38 | #define _ObjectList_H__ |
---|
| 39 | |
---|
[790] | 40 | #include "CorePrereqs.h" |
---|
| 41 | #include "Iterator.h" |
---|
[871] | 42 | #include "ClassManager.h" |
---|
[790] | 43 | |
---|
[197] | 44 | namespace orxonox |
---|
| 45 | { |
---|
[224] | 46 | // ############################### |
---|
| 47 | // ### ObjectListElement ### |
---|
| 48 | // ############################### |
---|
[790] | 49 | //! The list-element of the ObjectList |
---|
[224] | 50 | template <class T> |
---|
[197] | 51 | class ObjectListElement |
---|
| 52 | { |
---|
| 53 | public: |
---|
[224] | 54 | ObjectListElement(T* object); |
---|
[197] | 55 | |
---|
[790] | 56 | T* object_; //!< The object |
---|
| 57 | ObjectListElement* next_; //!< The next element in the list |
---|
| 58 | ObjectListElement* prev_; //!< The previous element in the list |
---|
[197] | 59 | }; |
---|
| 60 | |
---|
[790] | 61 | /** |
---|
| 62 | @brief Constructor: Creates the list-element with an object. |
---|
| 63 | @param object The object to store |
---|
| 64 | */ |
---|
[224] | 65 | template <class T> |
---|
| 66 | ObjectListElement<T>::ObjectListElement(T* object) |
---|
| 67 | { |
---|
| 68 | this->object_ = object; |
---|
| 69 | this->next_ = 0; |
---|
| 70 | this->prev_ = 0; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | // ############################### |
---|
| 75 | // ### ObjectList ### |
---|
| 76 | // ############################### |
---|
[790] | 77 | //! The ObjectList contains all objects of a given class. |
---|
| 78 | /** |
---|
| 79 | The ObjectList is used by Identifiers to store all objects of a given class. |
---|
| 80 | Use Iterator<class> to iterate through all objects in the list. |
---|
| 81 | */ |
---|
[224] | 82 | template <class T> |
---|
[197] | 83 | class ObjectList |
---|
| 84 | { |
---|
| 85 | public: |
---|
[871] | 86 | ObjectList(); |
---|
| 87 | ~ObjectList(); |
---|
[790] | 88 | |
---|
[246] | 89 | ObjectListElement<T>* add(T* object); |
---|
[790] | 90 | // void remove(OrxonoxClass* object, bool bIterateForwards = true); |
---|
[197] | 91 | |
---|
[871] | 92 | /** @brief Returns the first element in the list. @return The first element */ |
---|
[248] | 93 | inline static Iterator<T> start() |
---|
[871] | 94 | { return Iterator<T>(ClassManager<T>::getIdentifier()->getObjects()->first_); } |
---|
[790] | 95 | |
---|
[871] | 96 | /** @brief Returns the first element in the list. @return The first element */ |
---|
[790] | 97 | inline static Iterator<T> begin() |
---|
[871] | 98 | { return Iterator<T>(ClassManager<T>::getIdentifier()->getObjects()->first_); } |
---|
[790] | 99 | |
---|
[871] | 100 | /** @brief Returns the last element in the list. @return The last element */ |
---|
[248] | 101 | inline static Iterator<T> end() |
---|
[871] | 102 | { return Iterator<T>(ClassManager<T>::getIdentifier()->getObjects()->last_); } |
---|
[248] | 103 | |
---|
[790] | 104 | ObjectListElement<T>* first_; //!< The first element in the list |
---|
| 105 | ObjectListElement<T>* last_; //!< The last element in the list |
---|
[197] | 106 | }; |
---|
[224] | 107 | |
---|
[790] | 108 | /** |
---|
| 109 | @brief Constructor: Sets default values. |
---|
| 110 | */ |
---|
[224] | 111 | template <class T> |
---|
| 112 | ObjectList<T>::ObjectList() |
---|
| 113 | { |
---|
| 114 | this->first_ = 0; |
---|
| 115 | this->last_ = 0; |
---|
| 116 | } |
---|
| 117 | |
---|
[790] | 118 | /** |
---|
| 119 | @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS. |
---|
| 120 | */ |
---|
[224] | 121 | template <class T> |
---|
| 122 | ObjectList<T>::~ObjectList() |
---|
| 123 | { |
---|
| 124 | ObjectListElement<T>* temp; |
---|
| 125 | while (this->first_) |
---|
| 126 | { |
---|
| 127 | temp = this->first_->next_; |
---|
| 128 | delete this->first_; |
---|
| 129 | this->first_ = temp; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
[790] | 133 | /** |
---|
| 134 | @brief Adds a new object to the end of the list. |
---|
| 135 | @param object The object to add |
---|
| 136 | @return The pointer to the new ObjectListElement, needed by the MetaObjectList of the added object |
---|
| 137 | */ |
---|
| 138 | template <class T> |
---|
[246] | 139 | ObjectListElement<T>* ObjectList<T>::add(T* object) |
---|
[224] | 140 | { |
---|
| 141 | if (!this->last_) |
---|
| 142 | { |
---|
[790] | 143 | // If the list is empty |
---|
[224] | 144 | this->last_ = new ObjectListElement<T>(object); |
---|
[790] | 145 | this->first_ = this->last_; // There's only one object in the list now |
---|
[224] | 146 | } |
---|
| 147 | else |
---|
| 148 | { |
---|
[790] | 149 | // If the list isn't empty |
---|
[224] | 150 | ObjectListElement<T>* temp = this->last_; |
---|
| 151 | this->last_ = new ObjectListElement<T>(object); |
---|
| 152 | this->last_->prev_ = temp; |
---|
| 153 | temp->next_ = this->last_; |
---|
| 154 | } |
---|
[246] | 155 | |
---|
| 156 | return this->last_; |
---|
[224] | 157 | } |
---|
| 158 | |
---|
[790] | 159 | |
---|
| 160 | // /** |
---|
| 161 | // @brief Removes an object from the list. |
---|
| 162 | // @param object The object to remove |
---|
| 163 | // @param bIterateForwards If true: Start searching the object at the beginning of the list |
---|
| 164 | // */ |
---|
| 165 | /* |
---|
[224] | 166 | template <class T> |
---|
| 167 | void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards) |
---|
| 168 | { |
---|
| 169 | if (!object || !this->first_ || !this->last_) |
---|
| 170 | return; |
---|
| 171 | |
---|
[790] | 172 | // If there's only one object in the list, we have to set first_ and last_ to zero |
---|
[224] | 173 | if (this->first_ == this->last_) |
---|
| 174 | { |
---|
| 175 | if (this->first_->object_ == object) |
---|
| 176 | { |
---|
| 177 | delete this->first_; |
---|
| 178 | this->first_ = 0; |
---|
| 179 | this->last_ = 0; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | return; |
---|
| 183 | } |
---|
| 184 | |
---|
[790] | 185 | // Now we are sure we have more than one element in the list |
---|
[224] | 186 | if (bIterateForwards) |
---|
| 187 | { |
---|
[790] | 188 | // Start at the beginning of the list |
---|
| 189 | |
---|
| 190 | // Check if it's the first object |
---|
[224] | 191 | if (this->first_->object_ == object) |
---|
| 192 | { |
---|
| 193 | ObjectListElement<T>* temp = this->first_->next_; |
---|
| 194 | delete this->first_; |
---|
| 195 | this->first_ = temp; |
---|
| 196 | this->first_->prev_ = 0; |
---|
| 197 | |
---|
| 198 | return; |
---|
| 199 | } |
---|
| 200 | |
---|
[790] | 201 | // Iterate through the whole list |
---|
[224] | 202 | ObjectListElement<T>* temp = this->first_; |
---|
| 203 | while (temp->next_) |
---|
| 204 | { |
---|
| 205 | if (temp->next_->object_ == object) |
---|
| 206 | { |
---|
| 207 | ObjectListElement<T>* temp2 = temp->next_->next_; |
---|
| 208 | delete temp->next_; |
---|
| 209 | temp->next_ = temp2; |
---|
| 210 | if (temp2) |
---|
| 211 | temp2->prev_ = temp; |
---|
| 212 | else |
---|
[790] | 213 | this->last_ = temp; // If there is no next_, we deleted the last element and have to update the last_ pointer. |
---|
[224] | 214 | |
---|
| 215 | return; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | temp = temp->next_; |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | else |
---|
| 222 | { |
---|
[790] | 223 | // Start at the end of the list |
---|
| 224 | |
---|
| 225 | // Check if it's the last object |
---|
[224] | 226 | if (this->last_->object_ == object) |
---|
| 227 | { |
---|
| 228 | ObjectListElement<T>* temp = this->last_->prev_; |
---|
| 229 | delete this->last_; |
---|
| 230 | this->last_ = temp; |
---|
| 231 | this->last_->next_ = 0; |
---|
| 232 | |
---|
| 233 | return; |
---|
| 234 | } |
---|
| 235 | |
---|
[790] | 236 | // Iterate through the whole list |
---|
[224] | 237 | ObjectListElement<T>* temp = this->last_; |
---|
| 238 | while (temp->prev_) |
---|
| 239 | { |
---|
| 240 | if (temp->prev_->object_ == object) |
---|
| 241 | { |
---|
| 242 | ObjectListElement<T>* temp2 = temp->prev_->prev_; |
---|
| 243 | delete temp->prev_; |
---|
| 244 | temp->prev_ = temp2; |
---|
| 245 | if (temp2) |
---|
| 246 | temp2->next_ = temp; |
---|
| 247 | else |
---|
[790] | 248 | this->first_ = temp; // If there is no prev_, we deleted the first element and have to update the first_ pointer. |
---|
[224] | 249 | |
---|
| 250 | return; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | temp = temp->prev_; |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | } |
---|
[790] | 257 | */ |
---|
[197] | 258 | } |
---|
| 259 | |
---|
[790] | 260 | #endif /* _ObjectList_H__ */ |
---|