Changeset 1591 for code/branches/core3/src/core/Iterator.h
- Timestamp:
- Jun 12, 2008, 2:00:15 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core3/src/core/Iterator.h
r1574 r1591 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<myClass> it = ObjectList<myClass>::begin(); it != ObjectList<myClass>::end(); ++it)37 for (Iterator<myClass> it = anyidentifier->getObjects().begin(); it != anyidentifier->getObjects().end(); ++it) 38 38 { 39 39 it->someFunction(...); … … 47 47 #include "CorePrereqs.h" 48 48 49 #include "IteratorBase.h" 49 #include "ObjectListBase.h" 50 #include "ObjectListIterator.h" 51 #include "OrxonoxClass.h" 50 52 51 53 namespace orxonox 52 54 { 53 //! The Iterator allows to iterate through the ObjectList of a given class.54 template <class T >55 class Iterator : public IteratorBase55 //! The Iterator allows to iterate through a given ObjectList 56 template <class T = OrxonoxClass> 57 class Iterator 56 58 { 57 59 public: … … 61 63 inline Iterator() 62 64 { 63 ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this); 64 } 65 66 /** 67 @brief Constructor: Sets the element, whereon the iterator points, to a given element. 68 @param element The element to start with 69 */ 70 inline Iterator(ObjectListBaseElement* element) : IteratorBase((ObjectListBaseElement*)element) 71 { 72 ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this); 65 this->element_ = 0; 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); 73 113 } 74 114 … … 78 118 inline ~Iterator() 79 119 { 80 ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterIterator(this); 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. 157 @param element The element 158 */ 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); 170 } 171 172 /** 173 @brief Assigns the element of an ObjectListIterator. 174 @param other The ObjectListIterator 175 */ 176 template <class O> 177 inline const Iterator<T>& operator=(const ObjectListIterator<O>& other) 178 { 179 if (this->list_) 180 this->list_->unregisterIterator(this->iterator_); 181 182 this->element_ = (other.element_) ? (ObjectListBaseElement*)other.element_ : 0; 183 this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects(); 184 this->iterator_ = this->list_->registerIterator(this); 185 186 return (*this); 81 187 } 82 188 … … 85 191 @return The Iterator itself 86 192 */ 87 inline Iterator<T> operator++() 88 { 89 IteratorBase::operator++(); 193 inline const Iterator<T>& operator++() 194 { 195 if (this->element_) 196 this->element_ = this->element_->next_; 90 197 return *this; 91 198 } … … 98 205 { 99 206 Iterator<T> copy = *this; 100 IteratorBase::operator++(); 207 if (this->element_) 208 this->element_ = this->element_->next_; 101 209 return copy; 102 210 } … … 106 214 @return The Iterator itself 107 215 */ 108 inline Iterator<T> operator--() 109 { 110 IteratorBase::operator--(); 216 inline const Iterator<T>& operator--() 217 { 218 if (this->element_) 219 this->element_ = this->element_->prev_; 111 220 return *this; 112 221 } … … 119 228 { 120 229 Iterator<T> copy = *this; 121 IteratorBase::operator--(); 230 if (this->element_) 231 this->element_ = this->element_->prev_; 122 232 return copy; 123 233 } … … 127 237 @return The object the Iterator points at 128 238 */ 129 inline T* operator*() 130 { 131 return dynamic_cast<T*>(IteratorBase::operator*()); 239 inline T* operator*() const 240 { 241 if (this->element_) 242 return dynamic_cast<T*>(this->element_->objectBase_); 243 else 244 return 0; 132 245 } 133 246 … … 138 251 inline T* operator->() const 139 252 { 140 return dynamic_cast<T*>(IteratorBase::operator->()); 253 if (this->element_) 254 return dynamic_cast<T*>(this->element_->objectBase_); 255 else 256 return 0; 257 } 258 259 /** 260 @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object. 261 @return True if the Iterator points to an existing object. 262 */ 263 inline operator bool() const 264 { 265 return (this->element_ != 0); 141 266 } 142 267 … … 146 271 @return True if the iterators point to the same element 147 272 */ 148 inline bool operator==(const Iterator<T>& compare) 273 inline bool operator==(const Iterator<T>& compare) const 149 274 { 150 275 return (this->element_ == compare.element_); … … 156 281 @return True if the iterators point to different elements 157 282 */ 158 inline bool operator!=(const Iterator<T>& compare) 283 inline bool operator!=(const Iterator<T>& compare) const 159 284 { 160 285 return (this->element_ != compare.element_); 161 286 } 287 288 /** 289 @brief Increments the Iterator if it points at the given object. 290 @param object The object to compare with 291 */ 292 inline void incrementIfEqual(OrxonoxClass* object) 293 { 294 if (this->element_ && this->element_->objectBase_ == object) 295 this->operator++(); 296 } 297 298 protected: 299 ObjectListBaseElement* element_; //!< The element the Iterator points at 300 ObjectListBase* list_; //!< The list wherein the element is 301 std::list<void*>::iterator iterator_; //!< The iterator in the notifying list of the ObjectList 162 302 }; 303 304 typedef Iterator<OrxonoxClass> BaseIterator; 163 305 } 164 306
Note: See TracChangeset
for help on using the changeset viewer.