Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/core/Iterator.h @ 365

Last change on this file since 365 was 365, checked in by landauf, 16 years ago

added comments

File size: 3.7 KB
Line 
1/*!
2    @file Iterator.h
3    @brief Definition 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 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 an object directly through the iterator.
16*/
17
18#ifndef _Iterator_H__
19#define _Iterator_H__
20
21namespace orxonox
22{
23    //! The iterator allows to iterate through an ObjectList of a given class.
24    template <class T>
25    class Iterator
26    {
27        public:
28            /**
29                @brief Constructor: Sets the element whereon the iterator points to zero.
30            */
31            Iterator()
32            {
33                this->element_ = 0;
34            }
35
36            /**
37                @brief Constructor: Sets the element whereon the iterator points to a given element.
38                @param element The element to start with
39            */
40            Iterator(ObjectListElement<T>* element)
41            {
42                this->element_ = element;
43            }
44
45            /**
46                @brief Overloading of the ++it operator: Iterator iterates to the next object in the list.
47                @return The Iterator itself
48            */
49            Iterator<T> operator++()
50            {
51                this->element_ = this->element_->next_;
52                return *this;
53            }
54
55            /**
56                @brief Overloading of the --it operator: Iterator iterates to the previous object in the list.
57                @return The Iterator itself
58            */
59            Iterator<T> operator--()
60            {
61                this->element_ = this->element_->prev_;
62                return *this;
63            }
64
65            /**
66                @brief Overloading of the *it operator: returns the pointer to the object.
67                @return The object the Iterator points at
68            */
69            T* operator*()
70            {
71                return this->element_->object_;
72            }
73
74            /**
75                @brief Overloading of the it-> operator: returns the pointer to the object.
76                @return The object the Iterator points at
77            */
78            T* operator->() const
79            {
80                return this->element_->object_;
81
82            }
83
84            /**
85                @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
86                @return True if the iterator points to an existing object.
87            */
88            operator bool()
89            {
90                return (this->element_ != 0);
91            }
92
93            /**
94                @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool.
95                @param compare The integer (must be zero, everything else makes no sense).
96                @return True if the iterator points to an existing object.
97            */
98            bool operator!=(int compare)
99            {
100                // Comparing with something except zero makes no sense
101                if (compare != 0)
102                    std::cout << "Warning: Comparing the " << ClassIdentifier<T>::getIdentifier()->getName() << "-List-Iterator with " << compare << " has no effect. Only comparison with 0 works.\n";
103
104                return (this->element_ != 0);
105            }
106
107        private:
108            ObjectListElement<T>* element_;     //!< The element the Iterator points to
109    };
110}
111
112#endif
Note: See TracBrowser for help on using the repository browser.