Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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