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