Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/core/ObjectList.h @ 732

Last change on this file since 732 was 729, checked in by rgrieder, 16 years ago
  • fixed multiple template instantiation problem under windows
  • removed some warnings by introducing explicit casts
File size: 8.2 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 last element in the list */
95            inline static Iterator<T> end()
96                { return Iterator<T>(getList()->last_); }
97
98            ObjectListElement<T>* first_;       //!< The first element in the list
99            ObjectListElement<T>* last_;        //!< The last element in the list
100
101        private:
102            ObjectList();
103            ~ObjectList();
104    };
105
106    /**
107        @brief Constructor: Sets default values.
108    */
109    template <class T>
110    ObjectList<T>::ObjectList()
111    {
112        this->first_ = 0;
113        this->last_ = 0;
114    }
115
116    /**
117        @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS.
118    */
119    template <class T>
120    ObjectList<T>::~ObjectList()
121    {
122        ObjectListElement<T>* temp;
123        while (this->first_)
124        {
125            temp = this->first_->next_;
126            delete this->first_;
127            this->first_ = temp;
128        }
129    }
130
131    /**
132        @returns a pointer to the only existing instance for the given class T.
133    */
134    template <class T>
135    ObjectList<T>* ObjectList<T>::getList()
136    {
137        static ObjectList<T> theOnlyObjectListObjectForClassT = ObjectList<T>();
138        return &theOnlyObjectListObjectForClassT;
139    }
140
141    /**
142        @brief Adds a new object to the end of the list.
143        @param object The object to add
144        @return The pointer to the new ObjectListElement, needed by the MetaObjectList of the added object
145    */
146    template <class T>
147    ObjectListElement<T>* ObjectList<T>::add(T* object)
148    {
149        if (!this->last_)
150        {
151            // If the list is empty
152            this->last_ = new ObjectListElement<T>(object);
153            this->first_ = this->last_; // There's only one object in the list now
154        }
155        else
156        {
157            // If the list isn't empty
158            ObjectListElement<T>* temp = this->last_;
159            this->last_ = new ObjectListElement<T>(object);
160            this->last_->prev_ = temp;
161            temp->next_ = this->last_;
162        }
163
164        return this->last_;
165    }
166
167
168//    /**
169//        @brief Removes an object from the list.
170//        @param object The object to remove
171//        @param bIterateForwards If true: Start searching the object at the beginning of the list
172//    */
173    /*
174    template <class T>
175    void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards)
176    {
177        if (!object || !this->first_ || !this->last_)
178            return;
179
180        // If there's only one object in the list, we have to set first_ and last_ to zero
181        if (this->first_ == this->last_)
182        {
183            if (this->first_->object_ == object)
184            {
185                delete this->first_;
186                this->first_ = 0;
187                this->last_ = 0;
188            }
189
190            return;
191        }
192
193        // Now we are sure we have more than one element in the list
194        if (bIterateForwards)
195        {
196            // Start at the beginning of the list
197
198            // Check if it's the first object
199            if (this->first_->object_ == object)
200            {
201                ObjectListElement<T>* temp = this->first_->next_;
202                delete this->first_;
203                this->first_ = temp;
204                this->first_->prev_ = 0;
205
206                return;
207            }
208
209            // Iterate through the whole list
210            ObjectListElement<T>* temp = this->first_;
211            while (temp->next_)
212            {
213                if (temp->next_->object_ == object)
214                {
215                    ObjectListElement<T>* temp2 = temp->next_->next_;
216                    delete temp->next_;
217                    temp->next_ = temp2;
218                    if (temp2)
219                        temp2->prev_ = temp;
220                    else
221                        this->last_ = temp; // If there is no next_, we deleted the last element and have to update the last_ pointer.
222
223                    return;
224                }
225
226                temp = temp->next_;
227            }
228        }
229        else
230        {
231            // Start at the end of the list
232
233            // Check if it's the last object
234            if (this->last_->object_ == object)
235            {
236                ObjectListElement<T>* temp = this->last_->prev_;
237                delete this->last_;
238                this->last_ = temp;
239                this->last_->next_ = 0;
240
241                return;
242            }
243
244            // Iterate through the whole list
245            ObjectListElement<T>* temp = this->last_;
246            while (temp->prev_)
247            {
248                if (temp->prev_->object_ == object)
249                {
250                    ObjectListElement<T>* temp2 = temp->prev_->prev_;
251                    delete temp->prev_;
252                    temp->prev_ = temp2;
253                    if (temp2)
254                        temp2->next_ = temp;
255                    else
256                        this->first_ = temp; // If there is no prev_, we deleted the first element and have to update the first_ pointer.
257
258                    return;
259                }
260
261                temp = temp->prev_;
262            }
263        }
264    }
265    */
266}
267
268#endif /* _ObjectList_H__ */
Note: See TracBrowser for help on using the repository browser.