Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/core/object/IteratorBase.h @ 10467

Last change on this file since 10467 was 10467, checked in by landauf, 9 years ago

trying to fix compiler error on buildserver

  • Property svn:eol-style set to native
File size: 7.1 KB
Line 
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
29/**
30    @file
31    @ingroup Object ObjectList
32    @brief Definition of the IteratorBase class, used to iterate through object-lists.
33*/
34
35#ifndef _IteratorBase_H__
36#define _IteratorBase_H__
37
38#include "core/CorePrereqs.h"
39
40#include <boost/static_assert.hpp>
41#include <boost/type_traits/is_base_of.hpp>
42
43#include "ObjectListBase.h"
44
45namespace orxonox
46{
47    /**
48        @brief The Iterator allows to iterate through object lists.
49        It serves as base class for @ref ObjectListIterator and @ref Iterator
50    */
51    template <class T, class I>
52    class IteratorBase : public ObjectListElementRemovalListener
53    {
54        BOOST_STATIC_ASSERT((boost::is_base_of<Listable, T>::value));
55
56        public:
57            /**
58                @brief Constructor: Sets the element, whereon the iterator points, to zero.
59            */
60            inline IteratorBase(ObjectListBaseElement* element)
61            {
62                this->element_ = element;
63                this->registerIterator();
64            }
65
66            /**
67                @brief Constructor: Sets this element to the element of another Iterator.
68                @param other The other Iterator
69            */
70            inline IteratorBase(const IteratorBase& other)
71            {
72                this->element_ = other.element_;
73                this->registerIterator();
74            }
75
76            /**
77                @brief Unregisters the Iterator from the ObjectList.
78            */
79            inline ~IteratorBase()
80            {
81                this->unregisterIterator();
82            }
83
84            /**
85                @brief Assigns a given element.
86                @param element The element
87            */
88            inline IteratorBase<T, I>& operator=(ObjectListElement<T>* element)
89            {
90                this->setElement(element);
91                return (*this);
92            }
93
94            /**
95                @brief Assigns the element of another Iterator.
96                @param other The other Iterator
97            */
98            inline IteratorBase<T, I>& operator=(const IteratorBase<T, I>& other)
99            {
100                this->setElement(other.element_);
101                return (*this);
102            }
103
104            /**
105                @brief Overloading of the ++it operator: Iterator points to the next object in the list.
106                @return The Iterator itself
107            */
108            inline const IteratorBase<T, I>& operator++()
109            {
110                this->element_ = this->element_->next_;
111                return *this;
112            }
113
114            /**
115                @brief Overloading of the it++ operator: Iterator points to the next object in the list.
116                @return The Iterator itself
117            */
118            inline I operator++(int)
119            {
120                I copy = *this;
121                this->element_ = this->element_->next_;
122                return copy;
123            }
124
125            /**
126                @brief Overloading of the --it operator: Iterator points to the previous object in the list.
127                @return The Iterator itself
128            */
129            inline const IteratorBase<T, I>& operator--()
130            {
131                this->element_ = this->element_->prev_;
132                return *this;
133            }
134
135            /**
136                @brief Overloading of the it-- operator: Iterator points to the previous object in the list.
137                @return The Iterator itself
138            */
139            inline I operator--(int i)
140            {
141                I copy = *this;
142                this->element_ = this->element_->prev_;
143                return copy;
144            }
145
146            /**
147                @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
148                @return True if the Iterator points to an existing object.
149            */
150            inline operator bool() const
151            {
152                return (this->element_ != NULL);
153            }
154
155            /**
156                @brief Overloading of the == operator to compare with another Iterator.
157                @param compare The other Iterator
158                @return True if the iterators point to the same element
159            */
160            inline bool operator==(const IteratorBase<T, I>& compare) const
161            {
162                return (this->element_ == compare.element_);
163            }
164
165            /**
166                @brief Overloading of the != operator to compare with another Iterator.
167                @param compare The other Iterator
168                @return True if the iterators point to different elements
169            */
170            inline bool operator!=(const IteratorBase<T, I>& compare) const
171            {
172                return (this->element_ != compare.element_);
173            }
174
175            /**
176                @brief Increments the Iterator if it points at the given element.
177                @param object The object to compare with
178            */
179            virtual void removedElement(ObjectListBaseElement* element)
180            {
181                if (this->element_ == element)
182                    this->operator++();
183            }
184
185        protected:
186            inline void setElement(ObjectListBaseElement* element)
187            {
188                this->unregisterIterator();
189                this->element_ = element;
190                this->registerIterator();
191            }
192
193            /**
194             * @brief Registers the Iterator at the list to which it belongs
195             */
196            inline void registerIterator()
197            {
198                if (this->element_)
199                {
200                    this->list_ = this->element_->list_;
201                    this->list_->registerRemovalListener(this);
202                }
203                else
204                    this->list_ = NULL;
205            }
206
207            /**
208             * @brief Unregisters the Iterator from the list (if any)
209             */
210            inline void unregisterIterator()
211            {
212                if (this->list_)
213                    this->list_->unregisterRemovalListener(this);
214            }
215
216            ObjectListBaseElement* element_;    //!< The element the Iterator points at
217            ObjectListBase* list_;              //!< The list in which the Iterator registered itself
218    };
219}
220
221#endif /* _IteratorBase_H__ */
Note: See TracBrowser for help on using the repository browser.