Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cpp11_v2/src/libraries/core/object/IteratorBase.h @ 10770

Last change on this file since 10770 was 10770, checked in by landauf, 8 years ago

made conversion to bool operators explicit

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