1 | #ifndef _ObjectList_H__ |
---|
2 | #define _ObjectList_H__ |
---|
3 | |
---|
4 | namespace orxonox |
---|
5 | { |
---|
6 | class OrxonoxClass; |
---|
7 | |
---|
8 | // ############################### |
---|
9 | // ### ObjectListElement ### |
---|
10 | // ############################### |
---|
11 | template <class T> |
---|
12 | class ObjectListElement |
---|
13 | { |
---|
14 | public: |
---|
15 | ObjectListElement(T* object); |
---|
16 | ~ObjectListElement(); |
---|
17 | |
---|
18 | T* object_; |
---|
19 | ObjectListElement* next_; |
---|
20 | ObjectListElement* prev_; |
---|
21 | }; |
---|
22 | |
---|
23 | template <class T> |
---|
24 | ObjectListElement<T>::ObjectListElement(T* object) |
---|
25 | { |
---|
26 | this->object_ = object; |
---|
27 | this->next_ = 0; |
---|
28 | this->prev_ = 0; |
---|
29 | } |
---|
30 | |
---|
31 | template <class T> |
---|
32 | ObjectListElement<T>::~ObjectListElement() |
---|
33 | { |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | // ############################### |
---|
38 | // ### ObjectList ### |
---|
39 | // ############################### |
---|
40 | template <class T> |
---|
41 | class Iterator; |
---|
42 | |
---|
43 | template <class T> |
---|
44 | class ObjectList |
---|
45 | { |
---|
46 | public: |
---|
47 | ObjectList(); |
---|
48 | ~ObjectList(); |
---|
49 | ObjectListElement<T>* add(T* object); |
---|
50 | void remove(OrxonoxClass* object, bool bIterateForwards = true); |
---|
51 | |
---|
52 | inline static Iterator<T> start() |
---|
53 | { Iterator<T>(pointer_s->first_); } |
---|
54 | inline static Iterator<T> end() |
---|
55 | { Iterator<T>(pointer_s->last_); } |
---|
56 | |
---|
57 | ObjectListElement<T>* first_; |
---|
58 | ObjectListElement<T>* last_; |
---|
59 | |
---|
60 | private: |
---|
61 | static ObjectList<T>* pointer_s; |
---|
62 | }; |
---|
63 | |
---|
64 | template <class T> |
---|
65 | ObjectList<T>* ObjectList<T>::pointer_s = 0; |
---|
66 | |
---|
67 | template <class T> |
---|
68 | ObjectList<T>::ObjectList() |
---|
69 | { |
---|
70 | this->first_ = 0; |
---|
71 | this->last_ = 0; |
---|
72 | |
---|
73 | this->pointer_s = this; |
---|
74 | } |
---|
75 | |
---|
76 | template <class T> |
---|
77 | ObjectList<T>::~ObjectList() |
---|
78 | { |
---|
79 | ObjectListElement<T>* temp; |
---|
80 | while (this->first_) |
---|
81 | { |
---|
82 | temp = this->first_->next_; |
---|
83 | delete this->first_; |
---|
84 | this->first_ = temp; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | template <class T> |
---|
89 | ObjectListElement<T>* ObjectList<T>::add(T* object) |
---|
90 | { |
---|
91 | if (!this->last_) |
---|
92 | { |
---|
93 | this->last_ = new ObjectListElement<T>(object); |
---|
94 | this->first_ = this->last_; |
---|
95 | } |
---|
96 | else |
---|
97 | { |
---|
98 | ObjectListElement<T>* temp = this->last_; |
---|
99 | this->last_ = new ObjectListElement<T>(object); |
---|
100 | this->last_->prev_ = temp; |
---|
101 | temp->next_ = this->last_; |
---|
102 | } |
---|
103 | |
---|
104 | return this->last_; |
---|
105 | } |
---|
106 | |
---|
107 | template <class T> |
---|
108 | void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards) |
---|
109 | { |
---|
110 | if (!object || !this->first_ || !this->last_) |
---|
111 | return; |
---|
112 | |
---|
113 | if (this->first_ == this->last_) |
---|
114 | { |
---|
115 | if (this->first_->object_ == object) |
---|
116 | { |
---|
117 | delete this->first_; |
---|
118 | this->first_ = 0; |
---|
119 | this->last_ = 0; |
---|
120 | } |
---|
121 | |
---|
122 | return; |
---|
123 | } |
---|
124 | |
---|
125 | if (bIterateForwards) |
---|
126 | { |
---|
127 | if (this->first_->object_ == object) |
---|
128 | { |
---|
129 | ObjectListElement<T>* temp = this->first_->next_; |
---|
130 | delete this->first_; |
---|
131 | this->first_ = temp; |
---|
132 | this->first_->prev_ = 0; |
---|
133 | |
---|
134 | return; |
---|
135 | } |
---|
136 | |
---|
137 | ObjectListElement<T>* temp = this->first_; |
---|
138 | while (temp->next_) |
---|
139 | { |
---|
140 | if (temp->next_->object_ == object) |
---|
141 | { |
---|
142 | ObjectListElement<T>* temp2 = temp->next_->next_; |
---|
143 | delete temp->next_; |
---|
144 | temp->next_ = temp2; |
---|
145 | if (temp2) |
---|
146 | temp2->prev_ = temp; |
---|
147 | else |
---|
148 | this->last_ = temp; |
---|
149 | |
---|
150 | return; |
---|
151 | } |
---|
152 | |
---|
153 | temp = temp->next_; |
---|
154 | } |
---|
155 | } |
---|
156 | else |
---|
157 | { |
---|
158 | if (this->last_->object_ == object) |
---|
159 | { |
---|
160 | ObjectListElement<T>* temp = this->last_->prev_; |
---|
161 | delete this->last_; |
---|
162 | this->last_ = temp; |
---|
163 | this->last_->next_ = 0; |
---|
164 | |
---|
165 | return; |
---|
166 | } |
---|
167 | |
---|
168 | ObjectListElement<T>* temp = this->last_; |
---|
169 | while (temp->prev_) |
---|
170 | { |
---|
171 | if (temp->prev_->object_ == object) |
---|
172 | { |
---|
173 | ObjectListElement<T>* temp2 = temp->prev_->prev_; |
---|
174 | delete temp->prev_; |
---|
175 | temp->prev_ = temp2; |
---|
176 | if (temp2) |
---|
177 | temp2->next_ = temp; |
---|
178 | else |
---|
179 | this->first_ = temp; |
---|
180 | |
---|
181 | return; |
---|
182 | } |
---|
183 | |
---|
184 | temp = temp->prev_; |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | #endif |
---|