Changeset 790 for code/trunk/src/orxonox/core/ObjectList.h
- Timestamp:
- Feb 7, 2008, 5:01:44 PM (17 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
-
Property
svn:ignore
set to
dependencies
-
Property
svn:ignore
set to
-
code/trunk/src/orxonox/core/ObjectList.h
r258 r790 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 28 /*! 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 1 37 #ifndef _ObjectList_H__ 2 38 #define _ObjectList_H__ 3 39 40 #include "CorePrereqs.h" 41 #include "Iterator.h" 42 4 43 namespace orxonox 5 44 { 6 class OrxonoxClass;7 8 45 // ############################### 9 46 // ### ObjectListElement ### 10 47 // ############################### 48 //! The list-element of the ObjectList 11 49 template <class T> 12 50 class ObjectListElement … … 14 52 public: 15 53 ObjectListElement(T* object); 16 ~ObjectListElement(); 17 18 T* object_; 19 ObjectListElement* next_; 20 ObjectListElement* prev_; 54 55 T* object_; //!< The object 56 ObjectListElement* next_; //!< The next element in the list 57 ObjectListElement* prev_; //!< The previous element in the list 21 58 }; 22 59 60 /** 61 @brief Constructor: Creates the list-element with an object. 62 @param object The object to store 63 */ 23 64 template <class T> 24 65 ObjectListElement<T>::ObjectListElement(T* object) … … 29 70 } 30 71 31 template <class T>32 ObjectListElement<T>::~ObjectListElement()33 {34 }35 36 72 37 73 // ############################### 38 74 // ### ObjectList ### 39 75 // ############################### 40 template <class T> 41 class Iterator; 42 76 //! The ObjectList contains all objects of a given class. 77 /** 78 The ObjectList is used by Identifiers to store all objects of a given class. 79 Use Iterator<class> to iterate through all objects in the list. 80 */ 43 81 template <class T> 44 82 class ObjectList 45 83 { 46 84 public: 85 static ObjectList<T>* getList(); 86 87 ObjectListElement<T>* add(T* object); 88 // void remove(OrxonoxClass* object, bool bIterateForwards = true); 89 90 /** @returns the first element in the list */ 91 inline static Iterator<T> start() 92 { return Iterator<T>(getList()->first_); } 93 94 /** @returns the first element in the list */ 95 inline static Iterator<T> begin() 96 { return Iterator<T>(getList()->first_); } 97 98 /** @returns the last element in the list */ 99 inline static Iterator<T> end() 100 { return Iterator<T>(getList()->last_); } 101 102 ObjectListElement<T>* first_; //!< The first element in the list 103 ObjectListElement<T>* last_; //!< The last element in the list 104 105 private: 47 106 ObjectList(); 48 107 ~ObjectList(); 49 ObjectListElement<T>* add(T* object);50 void remove(OrxonoxClass* object, bool bIterateForwards = true);51 52 inline static Iterator<T> start()53 { return Iterator<T>(pointer_s->first_); }54 inline static Iterator<T> end()55 { return Iterator<T>(pointer_s->last_); }56 57 ObjectListElement<T>* first_;58 ObjectListElement<T>* last_;59 60 private:61 static ObjectList<T>* pointer_s;62 108 }; 63 109 64 template <class T>65 ObjectList<T>* ObjectList<T>::pointer_s = 0;66 110 /** 111 @brief Constructor: Sets default values. 112 */ 67 113 template <class T> 68 114 ObjectList<T>::ObjectList() … … 70 116 this->first_ = 0; 71 117 this->last_ = 0; 72 73 this->pointer_s = this; 74 } 75 118 } 119 120 /** 121 @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS. 122 */ 76 123 template <class T> 77 124 ObjectList<T>::~ObjectList() … … 86 133 } 87 134 135 /** 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 /** 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 */ 88 150 template <class T> 89 151 ObjectListElement<T>* ObjectList<T>::add(T* object) … … 91 153 if (!this->last_) 92 154 { 155 // If the list is empty 93 156 this->last_ = new ObjectListElement<T>(object); 94 this->first_ = this->last_; 157 this->first_ = this->last_; // There's only one object in the list now 95 158 } 96 159 else 97 160 { 161 // If the list isn't empty 98 162 ObjectListElement<T>* temp = this->last_; 99 163 this->last_ = new ObjectListElement<T>(object); … … 105 169 } 106 170 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 /* 107 178 template <class T> 108 179 void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards) … … 111 182 return; 112 183 184 // If there's only one object in the list, we have to set first_ and last_ to zero 113 185 if (this->first_ == this->last_) 114 186 { … … 123 195 } 124 196 197 // Now we are sure we have more than one element in the list 125 198 if (bIterateForwards) 126 199 { 200 // Start at the beginning of the list 201 202 // Check if it's the first object 127 203 if (this->first_->object_ == object) 128 204 { … … 135 211 } 136 212 213 // Iterate through the whole list 137 214 ObjectListElement<T>* temp = this->first_; 138 215 while (temp->next_) … … 146 223 temp2->prev_ = temp; 147 224 else 148 this->last_ = temp; 225 this->last_ = temp; // If there is no next_, we deleted the last element and have to update the last_ pointer. 149 226 150 227 return; … … 156 233 else 157 234 { 235 // Start at the end of the list 236 237 // Check if it's the last object 158 238 if (this->last_->object_ == object) 159 239 { … … 166 246 } 167 247 248 // Iterate through the whole list 168 249 ObjectListElement<T>* temp = this->last_; 169 250 while (temp->prev_) … … 177 258 temp2->next_ = temp; 178 259 else 179 this->first_ = temp; 260 this->first_ = temp; // If there is no prev_, we deleted the first element and have to update the first_ pointer. 180 261 181 262 return; … … 186 267 } 187 268 } 269 */ 188 270 } 189 271 190 #endif 272 #endif /* _ObjectList_H__ */
Note: See TracChangeset
for help on using the changeset viewer.