[221] | 1 | #ifndef _Iterator_H__ |
---|
| 2 | #define _Iterator_H__ |
---|
| 3 | |
---|
| 4 | namespace orxonox |
---|
| 5 | { |
---|
| 6 | template <class T> |
---|
| 7 | class Iterator |
---|
| 8 | { |
---|
| 9 | public: |
---|
| 10 | Iterator() |
---|
| 11 | { |
---|
| 12 | this->elementForwards_ = ClassIdentifier<T>::getIdentifier()->objects_.first_; |
---|
| 13 | this->elementBackwards_ = ClassIdentifier<T>::getIdentifier()->objects_.last_; |
---|
| 14 | this->iteratingForwards_ = true; |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | Iterator<T> operator++(int step) |
---|
| 18 | { |
---|
| 19 | Iterator<T> copy = *this; |
---|
| 20 | |
---|
| 21 | if (step < 1) |
---|
| 22 | step = 1; |
---|
| 23 | |
---|
| 24 | for (int i = 0; i < step; i++) |
---|
| 25 | this->elementForwards_ = this->elementForwards_->next_; |
---|
| 26 | |
---|
| 27 | return copy; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | Iterator<T> operator--(int step) |
---|
| 31 | { |
---|
| 32 | Iterator<T> copy = *this; |
---|
| 33 | |
---|
| 34 | if (this->iteratingForwards_) |
---|
| 35 | { |
---|
| 36 | this->iteratingForwards_ = false; |
---|
| 37 | } |
---|
| 38 | else |
---|
| 39 | { |
---|
| 40 | if (step < 1) |
---|
| 41 | step = 1; |
---|
| 42 | |
---|
| 43 | for (int i = 0; i < step; i++) |
---|
| 44 | this->elementBackwards_ = this->elementBackwards_->prev_; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | return copy; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | T* operator*() |
---|
| 51 | { |
---|
| 52 | if (this->iteratingForwards_) |
---|
| 53 | return dynamic_cast<T*>(this->elementForwards_->object_); |
---|
| 54 | else |
---|
| 55 | return dynamic_cast<T*>(this->elementBackwards_->object_); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | T* operator->() const |
---|
| 59 | { |
---|
| 60 | if (this->iteratingForwards_) |
---|
| 61 | return dynamic_cast<T*>(this->elementForwards_->object_); |
---|
| 62 | else |
---|
| 63 | return dynamic_cast<T*>(this->elementBackwards_->object_); |
---|
| 64 | |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | operator bool() |
---|
| 68 | { |
---|
| 69 | if (this->iteratingForwards_) |
---|
| 70 | return (this->elementForwards_ != 0); |
---|
| 71 | else |
---|
| 72 | return (this->elementBackwards_->prev_ != 0); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | bool operator!=(int compare) |
---|
| 76 | { |
---|
| 77 | if (compare != 0) |
---|
| 78 | std::cout << "Warning: Comparing the " << ClassIdentifier<T>::getIdentifier()->getName() << "-List-Iterator with " << compare << " has no effect. Only comparison with 0 works.\n"; |
---|
| 79 | |
---|
| 80 | if (this->iteratingForwards_) |
---|
| 81 | return (this->elementForwards_ != 0); |
---|
| 82 | else |
---|
| 83 | return (this->elementBackwards_->prev_ != 0); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | private: |
---|
| 88 | ObjectListElement* elementForwards_; |
---|
| 89 | ObjectListElement* elementBackwards_; |
---|
| 90 | bool iteratingForwards_; |
---|
| 91 | }; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | #endif |
---|