Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/core/Iterator.h @ 704

Last change on this file since 704 was 682, checked in by rgrieder, 16 years ago
  • adapted the core to be an actual windows dll (only tested with MSVC)
  • misc header files dependency changes
File size: 5.5 KB
RevLine 
[670]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
[384]28/*!
29    @file Iterator.h
[496]30    @brief Definition and implementation of the Iterator class.
[221]31
[384]32    The Iterator of a given class allows to iterate through an ObjectList, containing all objects of that type.
[496]33    This is the only way to access the objects stored in an ObjectList.
[384]34
35    Usage:
36    for (Iterator<class> it = ObjectList<class>::start(); it != 0; ++it)
37    {
38        it->someFunction(...);
39        class* myObject = *it;
40    }
41
[496]42    Warning: Don't delete objects directly through the iterator.
[384]43*/
44
45#ifndef _Iterator_H__
46#define _Iterator_H__
47
[682]48#include "CorePrereqs.h"
49
50#include "ObjectList.h"
[496]51#include "Debug.h"
52
[221]53namespace orxonox
54{
[384]55    //! The iterator allows to iterate through an ObjectList of a given class.
[221]56    template <class T>
57    class Iterator
58    {
59        public:
[384]60            /**
[496]61                @brief Constructor: Sets the element, whereon the iterator points, to zero.
[384]62            */
[221]63            Iterator()
64            {
[248]65                this->element_ = 0;
[221]66            }
67
[384]68            /**
[496]69                @brief Constructor: Sets the element, whereon the iterator points, to a given element.
[384]70                @param element The element to start with
71            */
[248]72            Iterator(ObjectListElement<T>* element)
[221]73            {
[248]74                this->element_ = element;
[221]75            }
76
[384]77            /**
[496]78                @brief Overloading of the ++it operator: Iterator points to the next object in the list.
[384]79                @return The Iterator itself
80            */
[225]81            Iterator<T> operator++()
82            {
[248]83                this->element_ = this->element_->next_;
[225]84                return *this;
85            }
86
[384]87            /**
[646]88                @brief Overloading of the it++ operator: Iterator points to the next object in the list.
89                @return The Iterator itself
90            */
91            Iterator<T> operator++(int i)
92            {
93                Iterator<T> copy = *this;
94                this->element_ = this->element_->next_;
95                return copy;
96            }
97
98            /**
[496]99                @brief Overloading of the --it operator: Iterator points to the previous object in the list.
[384]100                @return The Iterator itself
101            */
[225]102            Iterator<T> operator--()
103            {
[248]104                this->element_ = this->element_->prev_;
[225]105                return *this;
[221]106            }
107
[384]108            /**
[646]109                @brief Overloading of the it-- operator: Iterator points to the previous object in the list.
110                @return The Iterator itself
111            */
112            Iterator<T> operator--(int i)
113            {
114                Iterator<T> copy = *this;
115                this->element_ = this->element_->prev_;
116                return copy;
117            }
118
119            /**
[384]120                @brief Overloading of the *it operator: returns the pointer to the object.
121                @return The object the Iterator points at
122            */
[221]123            T* operator*()
124            {
[248]125                return this->element_->object_;
[221]126            }
127
[384]128            /**
129                @brief Overloading of the it-> operator: returns the pointer to the object.
130                @return The object the Iterator points at
131            */
[221]132            T* operator->() const
133            {
[248]134                return this->element_->object_;
[221]135
136            }
137
[384]138            /**
139                @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
140                @return True if the iterator points to an existing object.
141            */
[221]142            operator bool()
143            {
[248]144                return (this->element_ != 0);
[221]145            }
146
[384]147            /**
148                @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool.
149                @param compare The integer (must be zero, everything else makes no sense).
150                @return True if the iterator points to an existing object.
151            */
[221]152            bool operator!=(int compare)
153            {
[496]154                // Comparing with anything except zero makes no sense
[221]155                if (compare != 0)
[677]156                    COUT(2) << "Warning: Comparing the " << ClassIdentifier<T>::getIdentifier()->getName() << "-List-Iterator with " << compare << " has no effect. Only comparison with 0 works." << std::endl;
[221]157
[248]158                return (this->element_ != 0);
[221]159            }
160
161        private:
[496]162            ObjectListElement<T>* element_;     //!< The element the Iterator points at
[221]163    };
164}
165
[673]166#endif /* _Iterator_H__ */
Note: See TracBrowser for help on using the repository browser.