Changeset 1747 for code/trunk/src/core/Iterator.h
- Timestamp:
- Sep 9, 2008, 4:25:52 AM (17 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/core3 (added) merged: 1573-1574,1583-1586,1591-1594,1596-1597,1603,1606-1607,1610-1611,1655,1658,1676-1679,1681-1685,1687,1716-1723,1725-1729,1736
- Property svn:mergeinfo changed
-
code/trunk/src/core/Iterator.h
r1625 r1747 31 31 @brief Definition and implementation of the Iterator class. 32 32 33 The Iterator of a given class allows to iterate through an ObjectList , containing all objects of that type.34 This is the only way to access the objects stored in an ObjectList.33 The Iterator of a given class allows to iterate through an ObjectList. Objects in 34 this list are casted to the template argument of the Iterator. 35 35 36 36 Usage: 37 for (Iterator< class> it = ObjectList<class>::start(); it != 0; ++it)37 for (Iterator<myClass> it = anyidentifier->getObjects().begin(); it != anyidentifier->getObjects().end(); ++it) 38 38 { 39 39 it->someFunction(...); 40 class* myObject = *it;40 myClass* myObject = *it; 41 41 } 42 43 Warning: Don't delete objects directly through the iterator.44 42 */ 45 43 … … 49 47 #include "CorePrereqs.h" 50 48 51 #include "ObjectList.h" 49 #include "ObjectListBase.h" 50 #include "ObjectListIterator.h" 51 #include "OrxonoxClass.h" 52 52 53 53 namespace orxonox 54 54 { 55 //! The iterator allows to iterate through an ObjectList of a given class.56 template <class T >55 //! The Iterator allows to iterate through a given ObjectList 56 template <class T = OrxonoxClass> 57 57 class Iterator 58 58 { … … 61 61 @brief Constructor: Sets the element, whereon the iterator points, to zero. 62 62 */ 63 Iterator()63 inline Iterator() 64 64 { 65 65 this->element_ = 0; 66 ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this); 67 } 68 69 /** 70 @brief Constructor: Sets the element, whereon the iterator points, to a given element. 71 @param element The element to start with 72 */ 73 Iterator(ObjectListElement<T>* element) 74 { 75 this->element_ = element; 76 ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this); 66 this->list_ = 0; 67 } 68 69 /** 70 @brief Constructor: Sets this element to the exported element. 71 @param exp The exported element 72 */ 73 inline Iterator(const ObjectListBase::Export& exp) 74 { 75 this->element_ = exp.element_; 76 this->list_ = exp.list_; 77 this->iterator_ = this->list_->registerIterator(this); 78 } 79 80 /** 81 @brief Constructor: Sets this element to the element of another Iterator. 82 @param other The other Iterator 83 */ 84 inline Iterator(const Iterator<T>& other) 85 { 86 this->element_ = other.element_; 87 this->list_ = other.list_; 88 this->iterator_ = this->list_->registerIterator(this); 89 } 90 91 /** 92 @brief Constructor: Sets this element to a given element 93 @param element The element 94 */ 95 template <class O> 96 inline Iterator(ObjectListElement<O>* element) 97 { 98 this->element_ = (element) ? (ObjectListBaseElement*)element : 0; 99 this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects(); 100 this->iterator_ = this->list_->registerIterator(this); 101 } 102 103 /** 104 @brief Constructor: Sets this element to the element an ObjectListIterator. 105 @param other The ObjectListIterator 106 */ 107 template <class O> 108 inline Iterator(const ObjectListIterator<O>& other) 109 { 110 this->element_ = (other.element_) ? (ObjectListBaseElement*)other.element_ : 0; 111 this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects(); 112 this->iterator_ = this->list_->registerIterator(this); 77 113 } 78 114 … … 80 116 @brief Unregisters the Iterator from the ObjectList. 81 117 */ 82 ~Iterator() 83 { 84 ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterIterator(this); 85 } 86 87 /** 88 @brief Assigns an element to the iterator. 118 inline ~Iterator() 119 { 120 this->list_->unregisterIterator(this->iterator_); 121 } 122 123 /** 124 @brief Assigns an exported element. 125 @param exp The exported element 126 */ 127 inline const Iterator<T>& operator=(const ObjectListBase::Export& exp) 128 { 129 if (this->list_) 130 this->list_->unregisterIterator(this->iterator_); 131 132 this->element_ = exp.element_; 133 this->list_ = exp.list_; 134 this->iterator_ = this->list_->registerIterator(this); 135 136 return (*this); 137 } 138 139 /** 140 @brief Assigns the element of another Iterator. 141 @param other The other Iterator 142 */ 143 inline const Iterator<T>& operator=(const Iterator<T>& other) 144 { 145 if (this->list_) 146 this->list_->unregisterIterator(this->iterator_); 147 148 this->element_ = other.element_; 149 this->list_ = other.list_; 150 this->iterator_ = this->list_->registerIterator(this); 151 152 return (*this); 153 } 154 155 /** 156 @brief Assigns a given element. 89 157 @param element The element 90 158 */ 91 Iterator<T> operator=(ObjectListElement<T>* element) 92 { 93 this->element_ = element; 159 template <class O> 160 inline const Iterator<T>& operator=(ObjectListElement<O>* element) 161 { 162 if (this->list_) 163 this->list_->unregisterIterator(this->iterator_); 164 165 this->element_ = (element) ? (ObjectListBaseElement*)element : 0; 166 this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects(); 167 this->iterator_ = this->list_->registerIterator(this); 168 169 return (*this); 94 170 return *this; 171 } 172 173 /** 174 @brief Assigns the element of an ObjectListIterator. 175 @param other The ObjectListIterator 176 */ 177 template <class O> 178 inline const Iterator<T>& operator=(const ObjectListIterator<O>& other) 179 { 180 if (this->list_) 181 this->list_->unregisterIterator(this->iterator_); 182 183 this->element_ = (other.element_) ? (ObjectListBaseElement*)other.element_ : 0; 184 this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects(); 185 this->iterator_ = this->list_->registerIterator(this); 186 187 return (*this); 95 188 } 96 189 … … 99 192 @return The Iterator itself 100 193 */ 101 Iterator<T>operator++()194 inline const Iterator<T>& operator++() 102 195 { 103 196 if (this->element_) … … 110 203 @return The Iterator itself 111 204 */ 112 Iterator<T> operator++(int i)205 inline Iterator<T> operator++(int i) 113 206 { 114 207 Iterator<T> copy = *this; … … 122 215 @return The Iterator itself 123 216 */ 124 Iterator<T>operator--()217 inline const Iterator<T>& operator--() 125 218 { 126 219 if (this->element_) … … 133 226 @return The Iterator itself 134 227 */ 135 Iterator<T> operator--(int i)228 inline Iterator<T> operator--(int i) 136 229 { 137 230 Iterator<T> copy = *this; … … 145 238 @return The object the Iterator points at 146 239 */ 147 T* operator*()148 { 149 if (this->element_) 150 return this->element_->object_;240 inline T* operator*() const 241 { 242 if (this->element_) 243 return dynamic_cast<T*>(this->element_->objectBase_); 151 244 else 152 245 return 0; … … 157 250 @return The object the Iterator points at 158 251 */ 159 T* operator->() const160 { 161 if (this->element_) 162 return this->element_->object_;252 inline T* operator->() const 253 { 254 if (this->element_) 255 return dynamic_cast<T*>(this->element_->objectBase_); 163 256 else 164 257 return 0; 165 166 258 } 167 259 168 260 /** 169 261 @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object. 170 @return True if the iterator points to an existing object.171 */ 172 operator bool()262 @return True if the Iterator points to an existing object. 263 */ 264 inline operator bool() const 173 265 { 174 266 return (this->element_ != 0); … … 176 268 177 269 /** 178 @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool. 179 @param compare The integer (must be zero, everything else makes no sense). 180 @return True if the iterator points to an existing object. 181 */ 182 bool operator!=(ObjectListElement<T>* compare) 183 { 184 return (this->element_ != compare); 185 } 186 187 private: 188 ObjectListElement<T>* element_; //!< The element the Iterator points at 270 @brief Overloading of the == operator to compare with another Iterator. 271 @param compare The other Iterator 272 @return True if the iterators point to the same element 273 */ 274 inline bool operator==(const Iterator<T>& compare) const 275 { 276 return (this->element_ == compare.element_); 277 } 278 279 /** 280 @brief Overloading of the != operator to compare with another Iterator. 281 @param compare The other Iterator 282 @return True if the iterators point to different elements 283 */ 284 inline bool operator!=(const Iterator<T>& compare) const 285 { 286 return (this->element_ != compare.element_); 287 } 288 289 /** 290 @brief Increments the Iterator if it points at the given object. 291 @param object The object to compare with 292 */ 293 inline void incrementIfEqual(OrxonoxClass* object) 294 { 295 if (this->element_ && this->element_->objectBase_ == object) 296 this->operator++(); 297 } 298 299 protected: 300 ObjectListBaseElement* element_; //!< The element the Iterator points at 301 ObjectListBase* list_; //!< The list wherein the element is 302 std::list<void*>::iterator iterator_; //!< The iterator in the notifying list of the ObjectList 189 303 }; 304 305 typedef Iterator<OrxonoxClass> BaseIterator; 190 306 } 191 307 308 // Include ObjectList.h so the user only has to include one file: Iterator.h 309 #include "ObjectList.h" 310 192 311 #endif /* _Iterator_H__ */
Note: See TracChangeset
for help on using the changeset viewer.