[365] | 1 | /*! |
---|
| 2 | @file Iterator.h |
---|
| 3 | @brief Definition of the Iterator class. |
---|
| 4 | |
---|
| 5 | The Iterator of a given class allows to iterate through an ObjectList, containing all objects of that type. |
---|
| 6 | This is the only way to access the objects in an ObjectList. |
---|
| 7 | |
---|
| 8 | Usage: |
---|
| 9 | for (Iterator<class> it = ObjectList<class>::start(); it != 0; ++it) |
---|
| 10 | { |
---|
| 11 | it->someFunction(...); |
---|
| 12 | class* myObject = *it; |
---|
| 13 | } |
---|
| 14 | |
---|
| 15 | Warning: Don't delete an object directly through the iterator. |
---|
| 16 | */ |
---|
| 17 | |
---|
[221] | 18 | #ifndef _Iterator_H__ |
---|
| 19 | #define _Iterator_H__ |
---|
| 20 | |
---|
| 21 | namespace orxonox |
---|
| 22 | { |
---|
[365] | 23 | //! The iterator allows to iterate through an ObjectList of a given class. |
---|
[221] | 24 | template <class T> |
---|
| 25 | class Iterator |
---|
| 26 | { |
---|
| 27 | public: |
---|
[365] | 28 | /** |
---|
| 29 | @brief Constructor: Sets the element whereon the iterator points to zero. |
---|
| 30 | */ |
---|
[221] | 31 | Iterator() |
---|
| 32 | { |
---|
[248] | 33 | this->element_ = 0; |
---|
[221] | 34 | } |
---|
| 35 | |
---|
[365] | 36 | /** |
---|
| 37 | @brief Constructor: Sets the element whereon the iterator points to a given element. |
---|
| 38 | @param element The element to start with |
---|
| 39 | */ |
---|
[248] | 40 | Iterator(ObjectListElement<T>* element) |
---|
[221] | 41 | { |
---|
[248] | 42 | this->element_ = element; |
---|
[221] | 43 | } |
---|
| 44 | |
---|
[365] | 45 | /** |
---|
| 46 | @brief Overloading of the ++it operator: Iterator iterates to the next object in the list. |
---|
| 47 | @return The Iterator itself |
---|
| 48 | */ |
---|
[225] | 49 | Iterator<T> operator++() |
---|
| 50 | { |
---|
[248] | 51 | this->element_ = this->element_->next_; |
---|
[225] | 52 | return *this; |
---|
| 53 | } |
---|
| 54 | |
---|
[365] | 55 | /** |
---|
| 56 | @brief Overloading of the --it operator: Iterator iterates to the previous object in the list. |
---|
| 57 | @return The Iterator itself |
---|
| 58 | */ |
---|
[225] | 59 | Iterator<T> operator--() |
---|
| 60 | { |
---|
[248] | 61 | this->element_ = this->element_->prev_; |
---|
[225] | 62 | return *this; |
---|
[221] | 63 | } |
---|
| 64 | |
---|
[365] | 65 | /** |
---|
| 66 | @brief Overloading of the *it operator: returns the pointer to the object. |
---|
| 67 | @return The object the Iterator points at |
---|
| 68 | */ |
---|
[221] | 69 | T* operator*() |
---|
| 70 | { |
---|
[248] | 71 | return this->element_->object_; |
---|
[221] | 72 | } |
---|
| 73 | |
---|
[365] | 74 | /** |
---|
| 75 | @brief Overloading of the it-> operator: returns the pointer to the object. |
---|
| 76 | @return The object the Iterator points at |
---|
| 77 | */ |
---|
[221] | 78 | T* operator->() const |
---|
| 79 | { |
---|
[248] | 80 | return this->element_->object_; |
---|
[221] | 81 | |
---|
| 82 | } |
---|
| 83 | |
---|
[365] | 84 | /** |
---|
| 85 | @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object. |
---|
| 86 | @return True if the iterator points to an existing object. |
---|
| 87 | */ |
---|
[221] | 88 | operator bool() |
---|
| 89 | { |
---|
[248] | 90 | return (this->element_ != 0); |
---|
[221] | 91 | } |
---|
| 92 | |
---|
[365] | 93 | /** |
---|
| 94 | @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool. |
---|
| 95 | @param compare The integer (must be zero, everything else makes no sense). |
---|
| 96 | @return True if the iterator points to an existing object. |
---|
| 97 | */ |
---|
[221] | 98 | bool operator!=(int compare) |
---|
| 99 | { |
---|
[365] | 100 | // Comparing with something except zero makes no sense |
---|
[221] | 101 | if (compare != 0) |
---|
| 102 | std::cout << "Warning: Comparing the " << ClassIdentifier<T>::getIdentifier()->getName() << "-List-Iterator with " << compare << " has no effect. Only comparison with 0 works.\n"; |
---|
| 103 | |
---|
[248] | 104 | return (this->element_ != 0); |
---|
[221] | 105 | } |
---|
| 106 | |
---|
| 107 | private: |
---|
[365] | 108 | ObjectListElement<T>* element_; //!< The element the Iterator points to |
---|
[221] | 109 | }; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | #endif |
---|