Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core6/src/libraries/core/object/IteratorBase.h @ 9598

Last change on this file since 9598 was 9598, checked in by landauf, 11 years ago

added common base template for Iterator and ObjectListIterator in order to avoid code duplication

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