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