[670] | 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 | |
---|
[384] | 28 | /*! |
---|
| 29 | @file ObjectList.h |
---|
[496] | 30 | @brief Definition and implementation of the ObjectList class. |
---|
[384] | 31 | |
---|
[496] | 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. |
---|
[384] | 34 | Use Iterator<class> to iterate through all objects of the class. |
---|
| 35 | */ |
---|
| 36 | |
---|
[197] | 37 | #ifndef _ObjectList_H__ |
---|
| 38 | #define _ObjectList_H__ |
---|
| 39 | |
---|
[682] | 40 | #include "CorePrereqs.h" |
---|
[729] | 41 | #include "Iterator.h" |
---|
[682] | 42 | |
---|
[197] | 43 | namespace orxonox |
---|
| 44 | { |
---|
[224] | 45 | // ############################### |
---|
| 46 | // ### ObjectListElement ### |
---|
| 47 | // ############################### |
---|
[384] | 48 | //! The list-element of the ObjectList |
---|
[224] | 49 | template <class T> |
---|
[197] | 50 | class ObjectListElement |
---|
| 51 | { |
---|
| 52 | public: |
---|
[224] | 53 | ObjectListElement(T* object); |
---|
[197] | 54 | |
---|
[384] | 55 | T* object_; //!< The object |
---|
| 56 | ObjectListElement* next_; //!< The next element in the list |
---|
| 57 | ObjectListElement* prev_; //!< The previous element in the list |
---|
[197] | 58 | }; |
---|
| 59 | |
---|
[384] | 60 | /** |
---|
| 61 | @brief Constructor: Creates the list-element with an object. |
---|
[496] | 62 | @param object The object to store |
---|
[384] | 63 | */ |
---|
[224] | 64 | template <class T> |
---|
| 65 | ObjectListElement<T>::ObjectListElement(T* object) |
---|
| 66 | { |
---|
| 67 | this->object_ = object; |
---|
| 68 | this->next_ = 0; |
---|
| 69 | this->prev_ = 0; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | // ############################### |
---|
| 74 | // ### ObjectList ### |
---|
| 75 | // ############################### |
---|
[496] | 76 | //! The ObjectList contains all objects of a given class. |
---|
[384] | 77 | /** |
---|
[496] | 78 | The ObjectList is used by Identifiers to store all objects of a given class. |
---|
[384] | 79 | Use Iterator<class> to iterate through all objects in the list. |
---|
| 80 | */ |
---|
[248] | 81 | template <class T> |
---|
[197] | 82 | class ObjectList |
---|
| 83 | { |
---|
| 84 | public: |
---|
[698] | 85 | static ObjectList<T>* getList(); |
---|
| 86 | |
---|
[246] | 87 | ObjectListElement<T>* add(T* object); |
---|
[384] | 88 | // void remove(OrxonoxClass* object, bool bIterateForwards = true); |
---|
[197] | 89 | |
---|
[384] | 90 | /** @returns the first element in the list */ |
---|
[248] | 91 | inline static Iterator<T> start() |
---|
[698] | 92 | { return Iterator<T>(getList()->first_); } |
---|
[384] | 93 | |
---|
[737] | 94 | /** @returns the first element in the list */ |
---|
| 95 | inline static Iterator<T> begin() |
---|
| 96 | { return Iterator<T>(getList()->first_); } |
---|
| 97 | |
---|
[384] | 98 | /** @returns the last element in the list */ |
---|
[248] | 99 | inline static Iterator<T> end() |
---|
[698] | 100 | { return Iterator<T>(getList()->last_); } |
---|
[248] | 101 | |
---|
[384] | 102 | ObjectListElement<T>* first_; //!< The first element in the list |
---|
| 103 | ObjectListElement<T>* last_; //!< The last element in the list |
---|
[248] | 104 | |
---|
| 105 | private: |
---|
[698] | 106 | ObjectList(); |
---|
| 107 | ~ObjectList(); |
---|
[197] | 108 | }; |
---|
[224] | 109 | |
---|
[384] | 110 | /** |
---|
[496] | 111 | @brief Constructor: Sets default values. |
---|
[384] | 112 | */ |
---|
[248] | 113 | template <class T> |
---|
[224] | 114 | ObjectList<T>::ObjectList() |
---|
| 115 | { |
---|
| 116 | this->first_ = 0; |
---|
| 117 | this->last_ = 0; |
---|
| 118 | } |
---|
| 119 | |
---|
[384] | 120 | /** |
---|
| 121 | @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS. |
---|
| 122 | */ |
---|
[224] | 123 | template <class T> |
---|
| 124 | ObjectList<T>::~ObjectList() |
---|
| 125 | { |
---|
| 126 | ObjectListElement<T>* temp; |
---|
| 127 | while (this->first_) |
---|
| 128 | { |
---|
| 129 | temp = this->first_->next_; |
---|
| 130 | delete this->first_; |
---|
| 131 | this->first_ = temp; |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
[384] | 135 | /** |
---|
[698] | 136 | @returns a pointer to the only existing instance for the given class T. |
---|
| 137 | */ |
---|
| 138 | template <class T> |
---|
| 139 | ObjectList<T>* ObjectList<T>::getList() |
---|
| 140 | { |
---|
| 141 | static ObjectList<T> theOnlyObjectListObjectForClassT = ObjectList<T>(); |
---|
| 142 | return &theOnlyObjectListObjectForClassT; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /** |
---|
[384] | 146 | @brief Adds a new object to the end of the list. |
---|
| 147 | @param object The object to add |
---|
| 148 | @return The pointer to the new ObjectListElement, needed by the MetaObjectList of the added object |
---|
| 149 | */ |
---|
[224] | 150 | template <class T> |
---|
[246] | 151 | ObjectListElement<T>* ObjectList<T>::add(T* object) |
---|
[224] | 152 | { |
---|
| 153 | if (!this->last_) |
---|
| 154 | { |
---|
[384] | 155 | // If the list is empty |
---|
[224] | 156 | this->last_ = new ObjectListElement<T>(object); |
---|
[384] | 157 | this->first_ = this->last_; // There's only one object in the list now |
---|
[224] | 158 | } |
---|
| 159 | else |
---|
| 160 | { |
---|
[384] | 161 | // If the list isn't empty |
---|
[224] | 162 | ObjectListElement<T>* temp = this->last_; |
---|
| 163 | this->last_ = new ObjectListElement<T>(object); |
---|
| 164 | this->last_->prev_ = temp; |
---|
| 165 | temp->next_ = this->last_; |
---|
| 166 | } |
---|
[246] | 167 | |
---|
| 168 | return this->last_; |
---|
[224] | 169 | } |
---|
| 170 | |
---|
[384] | 171 | |
---|
| 172 | // /** |
---|
| 173 | // @brief Removes an object from the list. |
---|
| 174 | // @param object The object to remove |
---|
| 175 | // @param bIterateForwards If true: Start searching the object at the beginning of the list |
---|
| 176 | // */ |
---|
| 177 | /* |
---|
[224] | 178 | template <class T> |
---|
| 179 | void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards) |
---|
| 180 | { |
---|
| 181 | if (!object || !this->first_ || !this->last_) |
---|
| 182 | return; |
---|
| 183 | |
---|
[384] | 184 | // If there's only one object in the list, we have to set first_ and last_ to zero |
---|
[224] | 185 | if (this->first_ == this->last_) |
---|
| 186 | { |
---|
| 187 | if (this->first_->object_ == object) |
---|
| 188 | { |
---|
| 189 | delete this->first_; |
---|
| 190 | this->first_ = 0; |
---|
| 191 | this->last_ = 0; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | return; |
---|
| 195 | } |
---|
| 196 | |
---|
[384] | 197 | // Now we are sure we have more than one element in the list |
---|
[224] | 198 | if (bIterateForwards) |
---|
| 199 | { |
---|
[384] | 200 | // Start at the beginning of the list |
---|
| 201 | |
---|
| 202 | // Check if it's the first object |
---|
[224] | 203 | if (this->first_->object_ == object) |
---|
| 204 | { |
---|
| 205 | ObjectListElement<T>* temp = this->first_->next_; |
---|
| 206 | delete this->first_; |
---|
| 207 | this->first_ = temp; |
---|
| 208 | this->first_->prev_ = 0; |
---|
| 209 | |
---|
| 210 | return; |
---|
| 211 | } |
---|
| 212 | |
---|
[384] | 213 | // Iterate through the whole list |
---|
[224] | 214 | ObjectListElement<T>* temp = this->first_; |
---|
| 215 | while (temp->next_) |
---|
| 216 | { |
---|
| 217 | if (temp->next_->object_ == object) |
---|
| 218 | { |
---|
| 219 | ObjectListElement<T>* temp2 = temp->next_->next_; |
---|
| 220 | delete temp->next_; |
---|
| 221 | temp->next_ = temp2; |
---|
| 222 | if (temp2) |
---|
| 223 | temp2->prev_ = temp; |
---|
| 224 | else |
---|
[384] | 225 | this->last_ = temp; // If there is no next_, we deleted the last element and have to update the last_ pointer. |
---|
[224] | 226 | |
---|
| 227 | return; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | temp = temp->next_; |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | else |
---|
| 234 | { |
---|
[384] | 235 | // Start at the end of the list |
---|
| 236 | |
---|
| 237 | // Check if it's the last object |
---|
[224] | 238 | if (this->last_->object_ == object) |
---|
| 239 | { |
---|
| 240 | ObjectListElement<T>* temp = this->last_->prev_; |
---|
| 241 | delete this->last_; |
---|
| 242 | this->last_ = temp; |
---|
| 243 | this->last_->next_ = 0; |
---|
| 244 | |
---|
| 245 | return; |
---|
| 246 | } |
---|
| 247 | |
---|
[384] | 248 | // Iterate through the whole list |
---|
[224] | 249 | ObjectListElement<T>* temp = this->last_; |
---|
| 250 | while (temp->prev_) |
---|
| 251 | { |
---|
| 252 | if (temp->prev_->object_ == object) |
---|
| 253 | { |
---|
| 254 | ObjectListElement<T>* temp2 = temp->prev_->prev_; |
---|
| 255 | delete temp->prev_; |
---|
| 256 | temp->prev_ = temp2; |
---|
| 257 | if (temp2) |
---|
| 258 | temp2->next_ = temp; |
---|
| 259 | else |
---|
[384] | 260 | this->first_ = temp; // If there is no prev_, we deleted the first element and have to update the first_ pointer. |
---|
[224] | 261 | |
---|
| 262 | return; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | temp = temp->prev_; |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | } |
---|
[384] | 269 | */ |
---|
[197] | 270 | } |
---|
| 271 | |
---|
[673] | 272 | #endif /* _ObjectList_H__ */ |
---|