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++() |
---|
31 | { |
---|
32 | this->elementForwards_ = this->elementForwards_->next_; |
---|
33 | return *this; |
---|
34 | } |
---|
35 | |
---|
36 | Iterator<T> operator--(int step) |
---|
37 | { |
---|
38 | Iterator<T> copy = *this; |
---|
39 | |
---|
40 | if (this->iteratingForwards_) |
---|
41 | { |
---|
42 | this->iteratingForwards_ = false; |
---|
43 | } |
---|
44 | else |
---|
45 | { |
---|
46 | if (step < 1) |
---|
47 | step = 1; |
---|
48 | |
---|
49 | for (int i = 0; i < step; i++) |
---|
50 | this->elementBackwards_ = this->elementBackwards_->prev_; |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | Iterator<T> operator--() |
---|
55 | { |
---|
56 | if (this->iteratingForwards_) |
---|
57 | this->iteratingForwards_ = false; |
---|
58 | else |
---|
59 | this->elementBackwards_ = this->elementBackwards_->prev_; |
---|
60 | |
---|
61 | return *this; |
---|
62 | } |
---|
63 | |
---|
64 | T* operator*() |
---|
65 | { |
---|
66 | if (this->iteratingForwards_) |
---|
67 | return this->elementForwards_->object_; |
---|
68 | else |
---|
69 | return this->elementBackwards_->object_; |
---|
70 | } |
---|
71 | |
---|
72 | T* operator->() const |
---|
73 | { |
---|
74 | if (this->iteratingForwards_) |
---|
75 | return this->elementForwards_->object_; |
---|
76 | else |
---|
77 | return this->elementBackwards_->object_; |
---|
78 | |
---|
79 | } |
---|
80 | |
---|
81 | operator bool() |
---|
82 | { |
---|
83 | if (this->iteratingForwards_) |
---|
84 | return (this->elementForwards_ != 0); |
---|
85 | else |
---|
86 | return (this->elementBackwards_->prev_ != 0); |
---|
87 | } |
---|
88 | |
---|
89 | bool operator!=(int compare) |
---|
90 | { |
---|
91 | if (compare != 0) |
---|
92 | std::cout << "Warning: Comparing the " << ClassIdentifier<T>::getIdentifier()->getName() << "-List-Iterator with " << compare << " has no effect. Only comparison with 0 works.\n"; |
---|
93 | |
---|
94 | if (this->iteratingForwards_) |
---|
95 | return (this->elementForwards_ != 0); |
---|
96 | else |
---|
97 | return (this->elementBackwards_->prev_ != 0); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | private: |
---|
102 | ObjectListElement<T>* elementForwards_; |
---|
103 | ObjectListElement<T>* elementBackwards_; |
---|
104 | bool iteratingForwards_; |
---|
105 | }; |
---|
106 | } |
---|
107 | |
---|
108 | #endif |
---|