Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

hopefully replaced all static pre-main variables with more secure wrapper functions. and now i'll sleep.

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