Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 646 was 646, checked in by landauf, 16 years ago
  • added very bad collision detection (presentation hack :D)
  • added explosions
  • fixed bug in ParticleInterface (it tried to delete SceneManager)

AND:

  • fixed one of the most amazing bugs ever! (the game crashed when I deleted an object through a timer-function. because the timer-functions is called by an iterator, the iterator indirectly delted its object. by overloading the (it++) operator, i was able to solve this problem)
File size: 4.5 KB
Line 
1/*!
2    @file Iterator.h
3    @brief Definition and implementation of the Iterator class.
4
5    The Iterator of a given class allows to iterate through an ObjectList, containing all objects of that type.
6    This is the only way to access the objects stored in an ObjectList.
7
8    Usage:
9    for (Iterator<class> it = ObjectList<class>::start(); it != 0; ++it)
10    {
11        it->someFunction(...);
12        class* myObject = *it;
13    }
14
15    Warning: Don't delete objects directly through the iterator.
16*/
17
18#ifndef _Iterator_H__
19#define _Iterator_H__
20
21#include "Debug.h"
22
23namespace orxonox
24{
25    //! The iterator allows to iterate through an ObjectList of a given class.
26    template <class T>
27    class Iterator
28    {
29        public:
30            /**
31                @brief Constructor: Sets the element, whereon the iterator points, to zero.
32            */
33            Iterator()
34            {
35                this->element_ = 0;
36            }
37
38            /**
39                @brief Constructor: Sets the element, whereon the iterator points, to a given element.
40                @param element The element to start with
41            */
42            Iterator(ObjectListElement<T>* element)
43            {
44                this->element_ = element;
45            }
46
47            /**
48                @brief Overloading of the ++it operator: Iterator points to the next object in the list.
49                @return The Iterator itself
50            */
51            Iterator<T> operator++()
52            {
53                this->element_ = this->element_->next_;
54                return *this;
55            }
56
57            /**
58                @brief Overloading of the it++ operator: Iterator points to the next object in the list.
59                @return The Iterator itself
60            */
61            Iterator<T> operator++(int i)
62            {
63                Iterator<T> copy = *this;
64                this->element_ = this->element_->next_;
65                return copy;
66            }
67
68            /**
69                @brief Overloading of the --it operator: Iterator points to the previous object in the list.
70                @return The Iterator itself
71            */
72            Iterator<T> operator--()
73            {
74                this->element_ = this->element_->prev_;
75                return *this;
76            }
77
78            /**
79                @brief Overloading of the it-- operator: Iterator points to the previous object in the list.
80                @return The Iterator itself
81            */
82            Iterator<T> operator--(int i)
83            {
84                Iterator<T> copy = *this;
85                this->element_ = this->element_->prev_;
86                return copy;
87            }
88
89            /**
90                @brief Overloading of the *it operator: returns the pointer to the object.
91                @return The object the Iterator points at
92            */
93            T* operator*()
94            {
95                return this->element_->object_;
96            }
97
98            /**
99                @brief Overloading of the it-> operator: returns the pointer to the object.
100                @return The object the Iterator points at
101            */
102            T* operator->() const
103            {
104                return this->element_->object_;
105
106            }
107
108            /**
109                @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
110                @return True if the iterator points to an existing object.
111            */
112            operator bool()
113            {
114                return (this->element_ != 0);
115            }
116
117            /**
118                @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool.
119                @param compare The integer (must be zero, everything else makes no sense).
120                @return True if the iterator points to an existing object.
121            */
122            bool operator!=(int compare)
123            {
124                // Comparing with anything except zero makes no sense
125                if (compare != 0)
126                    COUT(2) << "Warning: Comparing the " << ClassIdentifier<T>::getIdentifier()->getName() << "-List-Iterator with " << compare << " has no effect. Only comparison with 0 works.\n";
127
128                return (this->element_ != 0);
129            }
130
131        private:
132            ObjectListElement<T>* element_;     //!< The element the Iterator points at
133    };
134}
135
136#endif
Note: See TracBrowser for help on using the repository browser.