Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN_test/src/orxonox/core/ObjectList.h @ 759

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

added removeObjects to the Identifier while constructing something new, but maybe it's not even used. never mind :P

File size: 8.4 KB
Line 
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
28/*!
29    @file ObjectList.h
30    @brief Definition and implementation of the ObjectList class.
31
32    The ObjectList is a double-linked list, used by Identifiers to store all objects of a given class.
33    Newly created objects are added through the RegisterObject-macro in its constructor.
34    Use Iterator<class> to iterate through all objects of the class.
35*/
36
37#ifndef _ObjectList_H__
38#define _ObjectList_H__
39
40#include "CorePrereqs.h"
41#include "Iterator.h"
42
43namespace orxonox
44{
45    // ###############################
46    // ###    ObjectListElement    ###
47    // ###############################
48    //! The list-element of the ObjectList
49    template <class T>
50    class ObjectListElement
51    {
52        public:
53            ObjectListElement(T* object);
54
55            T* object_;                     //!< The object
56            ObjectListElement* next_;       //!< The next element in the list
57            ObjectListElement* prev_;       //!< The previous element in the list
58    };
59
60    /**
61        @brief Constructor: Creates the list-element with an object.
62        @param object The object to store
63    */
64    template <class T>
65    ObjectListElement<T>::ObjectListElement(T* object)
66    {
67        this->object_ = object;
68        this->next_ = 0;
69        this->prev_ = 0;
70    }
71
72
73    // ###############################
74    // ###       ObjectList        ###
75    // ###############################
76    //! The ObjectList contains all objects of a given class.
77    /**
78        The ObjectList is used by Identifiers to store all objects of a given class.
79        Use Iterator<class> to iterate through all objects in the list.
80    */
81    template <class T>
82    class ObjectList
83    {
84        public:
85            static ObjectList<T>* getList();
86
87            ObjectListElement<T>* add(T* object);
88//            void remove(OrxonoxClass* object, bool bIterateForwards = true);
89
90            /** @returns the first element in the list */
91            inline static Iterator<T> start()
92                { return Iterator<T>(getList()->first_); }
93
94            /** @returns the first element in the list */
95            inline static Iterator<T> begin()
96                { return Iterator<T>(getList()->first_); }
97
98            /** @returns the last element in the list */
99            inline static Iterator<T> end()
100                { return Iterator<T>(getList()->last_); }
101
102            ObjectListElement<T>* first_;       //!< The first element in the list
103            ObjectListElement<T>* last_;        //!< The last element in the list
104
105        private:
106            ObjectList();
107            ~ObjectList();
108    };
109
110    /**
111        @brief Constructor: Sets default values.
112    */
113    template <class T>
114    ObjectList<T>::ObjectList()
115    {
116        this->first_ = 0;
117        this->last_ = 0;
118    }
119
120    /**
121        @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS.
122    */
123    template <class T>
124    ObjectList<T>::~ObjectList()
125    {
126        ObjectListElement<T>* temp;
127        while (this->first_)
128        {
129            temp = this->first_->next_;
130            delete this->first_;
131            this->first_ = temp;
132        }
133    }
134
135    /**
136        @returns a pointer to the only existing instance for the given class T.
137    */
138    template <class T>
139    ObjectList<T>* ObjectList<T>::getList()
140    {
141        static ObjectList<T> theOnlyObjectListObjectForClassT = ObjectList<T>();
142        return &theOnlyObjectListObjectForClassT;
143    }
144
145    /**
146        @brief Adds a new object to the end of the list.
147        @param object The object to add
148        @return The pointer to the new ObjectListElement, needed by the MetaObjectList of the added object
149    */
150    template <class T>
151    ObjectListElement<T>* ObjectList<T>::add(T* object)
152    {
153        if (!this->last_)
154        {
155            // If the list is empty
156            this->last_ = new ObjectListElement<T>(object);
157            this->first_ = this->last_; // There's only one object in the list now
158        }
159        else
160        {
161            // If the list isn't empty
162            ObjectListElement<T>* temp = this->last_;
163            this->last_ = new ObjectListElement<T>(object);
164            this->last_->prev_ = temp;
165            temp->next_ = this->last_;
166        }
167
168        return this->last_;
169    }
170
171
172//    /**
173//        @brief Removes an object from the list.
174//        @param object The object to remove
175//        @param bIterateForwards If true: Start searching the object at the beginning of the list
176//    */
177    /*
178    template <class T>
179    void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards)
180    {
181        if (!object || !this->first_ || !this->last_)
182            return;
183
184        // If there's only one object in the list, we have to set first_ and last_ to zero
185        if (this->first_ == this->last_)
186        {
187            if (this->first_->object_ == object)
188            {
189                delete this->first_;
190                this->first_ = 0;
191                this->last_ = 0;
192            }
193
194            return;
195        }
196
197        // Now we are sure we have more than one element in the list
198        if (bIterateForwards)
199        {
200            // Start at the beginning of the list
201
202            // Check if it's the first object
203            if (this->first_->object_ == object)
204            {
205                ObjectListElement<T>* temp = this->first_->next_;
206                delete this->first_;
207                this->first_ = temp;
208                this->first_->prev_ = 0;
209
210                return;
211            }
212
213            // Iterate through the whole list
214            ObjectListElement<T>* temp = this->first_;
215            while (temp->next_)
216            {
217                if (temp->next_->object_ == object)
218                {
219                    ObjectListElement<T>* temp2 = temp->next_->next_;
220                    delete temp->next_;
221                    temp->next_ = temp2;
222                    if (temp2)
223                        temp2->prev_ = temp;
224                    else
225                        this->last_ = temp; // If there is no next_, we deleted the last element and have to update the last_ pointer.
226
227                    return;
228                }
229
230                temp = temp->next_;
231            }
232        }
233        else
234        {
235            // Start at the end of the list
236
237            // Check if it's the last object
238            if (this->last_->object_ == object)
239            {
240                ObjectListElement<T>* temp = this->last_->prev_;
241                delete this->last_;
242                this->last_ = temp;
243                this->last_->next_ = 0;
244
245                return;
246            }
247
248            // Iterate through the whole list
249            ObjectListElement<T>* temp = this->last_;
250            while (temp->prev_)
251            {
252                if (temp->prev_->object_ == object)
253                {
254                    ObjectListElement<T>* temp2 = temp->prev_->prev_;
255                    delete temp->prev_;
256                    temp->prev_ = temp2;
257                    if (temp2)
258                        temp2->next_ = temp;
259                    else
260                        this->first_ = temp; // If there is no prev_, we deleted the first element and have to update the first_ pointer.
261
262                    return;
263                }
264
265                temp = temp->prev_;
266            }
267        }
268    }
269    */
270}
271
272#endif /* _ObjectList_H__ */
Note: See TracBrowser for help on using the repository browser.