Orxonox  0.0.5 Codename: Arcturus
IteratorBase.h
Go to the documentation of this file.
1 /*
2  * ORXONOX - the hottest 3D action shooter ever to exist
3  * > www.orxonox.net <
4  *
5  *
6  * License notice:
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * Author:
23  * Fabian 'x3n' Landau
24  * Co-authors:
25  * ...
26  *
27  */
28 
35 #ifndef _IteratorBase_H__
36 #define _IteratorBase_H__
37 
38 #include "core/CorePrereqs.h"
39 
40 #include "ObjectListBase.h"
41 
42 namespace orxonox
43 {
48  template <class T, class I>
50  {
51  static_assert(std::is_base_of<Listable, T>::value, "IteratorBase can only be used with Listables");
52 
53  public:
57  inline IteratorBase(ObjectListElement<T>* element = nullptr)
58  {
59  this->element_ = element;
60  this->registerIterator();
61  }
62 
67  template <class OT, class OI>
68  inline IteratorBase(const IteratorBase<OT, OI>& other)
69  {
70  this->element_ = other.getElement();
71  this->registerIterator();
72  }
73 
77  inline ~IteratorBase()
78  {
79  this->unregisterIterator();
80  }
81 
87  {
88  this->setElement(element);
89  return (*this);
90  }
91 
97  {
98  this->setElement(other.element_);
99  return (*this);
100  }
101 
107  {
108  this->element_ = this->element_->next_;
109  return *this;
110  }
111 
116  inline I operator++(int)
117  {
118  I copy = *this;
119  this->element_ = this->element_->next_;
120  return copy;
121  }
122 
128  {
129  this->element_ = this->element_->prev_;
130  return *this;
131  }
132 
137  inline I operator--(int i)
138  {
139  I copy = *this;
140  this->element_ = this->element_->prev_;
141  return copy;
142  }
143 
148  inline explicit operator bool() const
149  {
150  return (this->element_ != nullptr);
151  }
152 
158  inline bool operator==(const IteratorBase<T, I>& compare) const
159  {
160  return (this->element_ == compare.element_);
161  }
162 
168  inline bool operator!=(const IteratorBase<T, I>& compare) const
169  {
170  return (this->element_ != compare.element_);
171  }
172 
177  virtual void removedElement(ObjectListBaseElement* element) override
178  {
179  if (this->element_ == element)
180  this->operator++();
181  }
182 
184  {
185  return this->element_;
186  }
187 
188  protected:
189  inline void setElement(ObjectListBaseElement* element)
190  {
191  this->unregisterIterator();
192  this->element_ = element;
193  this->registerIterator();
194  }
195 
199  inline void registerIterator()
200  {
201  if (this->element_)
202  {
203  this->list_ = this->element_->list_;
204  this->list_->registerRemovalListener(this);
205  }
206  else
207  this->list_ = nullptr;
208  }
209 
213  inline void unregisterIterator()
214  {
215  if (this->list_)
216  this->list_->unregisterRemovalListener(this);
217  }
218 
221  };
222 }
223 
224 #endif /* _IteratorBase_H__ */
IteratorBase< T, I > & operator=(ObjectListElement< T > *element)
Assigns a given element.
Definition: IteratorBase.h:86
The list-element that actually contains the object.
Definition: CorePrereqs.h:208
virtual void removedElement(ObjectListBaseElement *element) override
Increments the Iterator if it points at the given element.
Definition: IteratorBase.h:177
Declaration of the ObjectListBase class which stores all objects of each class.
~IteratorBase()
Unregisters the Iterator from the ObjectList.
Definition: IteratorBase.h:77
Shared library macros, enums, constants and forward declarations for the core library ...
void setElement(ObjectListBaseElement *element)
Definition: IteratorBase.h:189
Gets called by the object list if an element is removed.
Definition: ObjectListBase.h:103
ObjectListBaseElement * element_
The element the Iterator points at.
Definition: IteratorBase.h:219
The ObjectListBase contains all objects of a given class.
Definition: ObjectListBase.h:125
The Iterator allows to iterate through object lists.
Definition: IteratorBase.h:49
const IteratorBase< T, I > & operator++()
Overloading of the ++it operator: Iterator points to the next object in the list. ...
Definition: IteratorBase.h:106
void unregisterIterator()
Unregisters the Iterator from the list (if any)
Definition: IteratorBase.h:213
ObjectListBaseElement * prev_
The previous element in the list.
Definition: ObjectListBase.h:65
const IteratorBase< T, I > & operator--()
Overloading of the –it operator: Iterator points to the previous object in the list.
Definition: IteratorBase.h:127
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
I operator++(int)
Overloading of the it++ operator: Iterator points to the next object in the list. ...
Definition: IteratorBase.h:116
IteratorBase< T, I > & operator=(const IteratorBase< T, I > &other)
Assigns the element of another Iterator.
Definition: IteratorBase.h:96
Definition: InputPrereqs.h:81
void registerIterator()
Registers the Iterator at the list to which it belongs.
Definition: IteratorBase.h:199
bool operator==(const IteratorBase< T, I > &compare) const
Overloading of the == operator to compare with another Iterator.
Definition: IteratorBase.h:158
bool operator!=(const IteratorBase< T, I > &compare) const
Overloading of the != operator to compare with another Iterator.
Definition: IteratorBase.h:168
IteratorBase(const IteratorBase< OT, OI > &other)
Constructor: Sets the element, whereon the iterator points, to the given element of another type...
Definition: IteratorBase.h:68
ObjectListBaseElement * getElement() const
Definition: IteratorBase.h:183
IteratorBase(ObjectListElement< T > *element=nullptr)
Constructor: Sets the element, whereon the iterator points, to the given element. ...
Definition: IteratorBase.h:57
The list-element of the ObjectListBase.
Definition: ObjectListBase.h:52
ObjectListBase * list_
The list in which the Iterator registered itself.
Definition: IteratorBase.h:220
I operator--(int i)
Overloading of the it– operator: Iterator points to the previous object in the list.
Definition: IteratorBase.h:137
ObjectListBaseElement * next_
The next element in the list.
Definition: ObjectListBase.h:64
void registerRemovalListener(ObjectListElementRemovalListener *listener)
Definition: ObjectListBase.h:145
void unregisterRemovalListener(ObjectListElementRemovalListener *listener)
Definition: ObjectListBase.h:146
ObjectListBase * list_
The list.
Definition: ObjectListBase.h:67