Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/ObjectList.h @ 1162

Last change on this file since 1162 was 1063, checked in by landauf, 17 years ago

fixed an interesting bug in ObjectList/MetaObjectList/Iterator: it's now possible to delete multiple objects in one function-call while iterating through the objects. (and it would have crashed with std::list as well, so no stupid comments please :D)

added a feature in Timer.cc, allowing timers to tick faster than orxonox. this is from core2-branch but got lost while merging.

File size: 6.1 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 ObjectList.h
31    @brief Definition and implementation of the ObjectList class.
32
33    The ObjectList is a double-linked list, used by Identifiers to store all objects of a given class.
34    Newly created objects are added through the RegisterObject-macro in its constructor.
35    Use Iterator<class> to iterate through all objects of the class.
36*/
37
38#ifndef _ObjectList_H__
39#define _ObjectList_H__
40
41#include <set>
42
43#include "CorePrereqs.h"
44
45#include "Iterator.h"
46#include "ClassManager.h"
47
48namespace orxonox
49{
50    // ###############################
51    // ###    ObjectListElement    ###
52    // ###############################
53    //! The list-element of the ObjectList
54    template <class T>
55    class ObjectListElement
56    {
57        public:
58            ObjectListElement(T* object);
59
60            T* object_;                     //!< The object
61            ObjectListElement* next_;       //!< The next element in the list
62            ObjectListElement* prev_;       //!< The previous element in the list
63    };
64
65    /**
66        @brief Constructor: Creates the list-element with an object.
67        @param object The object to store
68    */
69    template <class T>
70    ObjectListElement<T>::ObjectListElement(T* object)
71    {
72        this->object_ = object;
73        this->next_ = 0;
74        this->prev_ = 0;
75    }
76
77
78    // ###############################
79    // ###       ObjectList        ###
80    // ###############################
81    //! The ObjectList contains all objects of a given class.
82    /**
83        The ObjectList is used by Identifiers to store all objects of a given class.
84        Use Iterator<class> to iterate through all objects in the list.
85    */
86    template <class T>
87    class ObjectList
88    {
89        public:
90            ObjectList();
91            ~ObjectList();
92
93            ObjectListElement<T>* add(T* object);
94
95            /** @brief Returns the first element in the list. @return The first element */
96            inline static Iterator<T> start()
97                { return Iterator<T>(ClassManager<T>::getIdentifier()->getObjects()->first_); }
98
99            /** @brief Returns the first element in the list. @return The first element */
100            inline static Iterator<T> begin()
101                { return Iterator<T>(ClassManager<T>::getIdentifier()->getObjects()->first_); }
102
103            /** @brief Returns the last element in the list. @return The last element */
104            inline static Iterator<T> end()
105                { return Iterator<T>(ClassManager<T>::getIdentifier()->getObjects()->last_); }
106
107            inline void registerIterator(Iterator<T>* iterator)
108                { this->iterators_.insert(this->iterators_.end(), (void*)iterator); }
109            inline void unregisterIterator(Iterator<T>* iterator)
110                { this->iterators_.erase((void*)iterator); }
111            void notifyIterators(ObjectListElement<T>* element);
112
113            ObjectListElement<T>* first_;       //!< The first element in the list
114            ObjectListElement<T>* last_;        //!< The last element in the list
115
116        private:
117            std::set<void*> iterators_;  //!< A list of iterators pointing on an element in this list
118    };
119
120    /**
121        @brief Constructor: Sets default values.
122    */
123    template <class T>
124    ObjectList<T>::ObjectList()
125    {
126        this->first_ = 0;
127        this->last_ = 0;
128    }
129
130    /**
131        @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS.
132    */
133    template <class T>
134    ObjectList<T>::~ObjectList()
135    {
136        ObjectListElement<T>* temp;
137        while (this->first_)
138        {
139            temp = this->first_->next_;
140            delete this->first_;
141            this->first_ = temp;
142        }
143    }
144
145    /**
146        @brief Increases all Iterators that currently point on the given element (because it gets removed).
147        @param element The element that gets removed
148    */
149    template <class T>
150    void ObjectList<T>::notifyIterators(ObjectListElement<T>* element)
151    {
152        for (std::set<void*>::iterator it = this->iterators_.begin(); it != this->iterators_.end(); ++it)
153            if ((*(*((Iterator<T>*)(*it)))) == element->object_)
154                ++(*((Iterator<T>*)(*it)));
155    }
156
157    /**
158        @brief Adds a new object to the end of the list.
159        @param object The object to add
160        @return The pointer to the new ObjectListElement, needed by the MetaObjectList of the added object
161    */
162    template <class T>
163    ObjectListElement<T>* ObjectList<T>::add(T* object)
164    {
165        if (!this->last_)
166        {
167            // If the list is empty
168            this->last_ = new ObjectListElement<T>(object);
169            this->first_ = this->last_; // There's only one object in the list now
170        }
171        else
172        {
173            // If the list isn't empty
174            ObjectListElement<T>* temp = this->last_;
175            this->last_ = new ObjectListElement<T>(object);
176            this->last_->prev_ = temp;
177            temp->next_ = this->last_;
178        }
179
180        return this->last_;
181    }
182}
183
184#endif /* _ObjectList_H__ */
Note: See TracBrowser for help on using the repository browser.