Changeset 1574 for code/branches/core3/src/core/Iterator.h
- Timestamp:
- Jun 9, 2008, 4:35:38 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core3/src/core/Iterator.h
r1543 r1574 35 35 36 36 Usage: 37 for (Iterator< class> it = ObjectList<class>::start(); it != 0; ++it)37 for (Iterator<myClass> it = ObjectList<myClass>::begin(); it != ObjectList<myClass>::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 "IteratorBase.h" 52 50 53 51 namespace orxonox 54 52 { 55 //! The iterator allows to iterate through anObjectList of a given class.53 //! The Iterator allows to iterate through the ObjectList of a given class. 56 54 template <class T> 57 class Iterator 55 class Iterator : public IteratorBase 58 56 { 59 57 public: … … 61 59 @brief Constructor: Sets the element, whereon the iterator points, to zero. 62 60 */ 63 Iterator()61 inline Iterator() 64 62 { 65 this->element_ = 0;66 63 ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this); 67 64 } … … 71 68 @param element The element to start with 72 69 */ 73 Iterator(ObjectListElement<T>*element)70 inline Iterator(ObjectListBaseElement* element) : IteratorBase((ObjectListBaseElement*)element) 74 71 { 75 this->element_ = element;76 72 ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this); 77 73 } … … 80 76 @brief Unregisters the Iterator from the ObjectList. 81 77 */ 82 ~Iterator()78 inline ~Iterator() 83 79 { 84 80 ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterIterator(this); 85 }86 87 /**88 @brief Assigns an element to the iterator.89 @param element The element90 */91 Iterator<T> operator=(ObjectListElement<T>* element)92 {93 this->element_ = element;94 81 } 95 82 … … 98 85 @return The Iterator itself 99 86 */ 100 Iterator<T> operator++()87 inline Iterator<T> operator++() 101 88 { 102 if (this->element_) 103 this->element_ = this->element_->next_; 89 IteratorBase::operator++(); 104 90 return *this; 105 91 } … … 109 95 @return The Iterator itself 110 96 */ 111 Iterator<T> operator++(int i)97 inline Iterator<T> operator++(int i) 112 98 { 113 99 Iterator<T> copy = *this; 114 if (this->element_) 115 this->element_ = this->element_->next_; 100 IteratorBase::operator++(); 116 101 return copy; 117 102 } … … 121 106 @return The Iterator itself 122 107 */ 123 Iterator<T> operator--()108 inline Iterator<T> operator--() 124 109 { 125 if (this->element_) 126 this->element_ = this->element_->prev_; 110 IteratorBase::operator--(); 127 111 return *this; 128 112 } … … 132 116 @return The Iterator itself 133 117 */ 134 Iterator<T> operator--(int i)118 inline Iterator<T> operator--(int i) 135 119 { 136 120 Iterator<T> copy = *this; 137 if (this->element_) 138 this->element_ = this->element_->prev_; 121 IteratorBase::operator--(); 139 122 return copy; 140 123 } … … 144 127 @return The object the Iterator points at 145 128 */ 146 T* operator*()129 inline T* operator*() 147 130 { 148 if (this->element_) 149 return this->element_->object_; 150 else 151 return 0; 131 return dynamic_cast<T*>(IteratorBase::operator*()); 152 132 } 153 133 … … 156 136 @return The object the Iterator points at 157 137 */ 158 T* operator->() const138 inline T* operator->() const 159 139 { 160 if (this->element_) 161 return this->element_->object_; 162 else 163 return 0; 164 140 return dynamic_cast<T*>(IteratorBase::operator->()); 165 141 } 166 142 167 143 /** 168 @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object. 169 @return True if the iterator points to an existing object. 144 @brief Overloading of the == operator to compare with another Iterator. 145 @param compare The other Iterator 146 @return True if the iterators point to the same element 170 147 */ 171 operator bool()148 inline bool operator==(const Iterator<T>& compare) 172 149 { 173 return (this->element_ != 0);150 return (this->element_ == compare.element_); 174 151 } 175 152 176 153 /** 177 @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool.178 @param compare The integer (must be zero, everything else makes no sense).179 @return True if the iterator points to an existing object.154 @brief Overloading of the != operator to compare with another Iterator. 155 @param compare The other Iterator 156 @return True if the iterators point to different elements 180 157 */ 181 bool operator!=(ObjectListElement<T>*compare)158 inline bool operator!=(const Iterator<T>& compare) 182 159 { 183 return (this->element_ != compare );160 return (this->element_ != compare.element_); 184 161 } 185 186 private:187 ObjectListElement<T>* element_; //!< The element the Iterator points at188 162 }; 189 163 } 190 164 165 // Include ObjectList.h so the user only has to include one file: Iterator.h 166 #include "ObjectList.h" 167 191 168 #endif /* _Iterator_H__ */
Note: See TracChangeset
for help on using the changeset viewer.