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
RevLine 
[790]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[790]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
[871]29/**
[790]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
[197]38#ifndef _ObjectList_H__
39#define _ObjectList_H__
40
[790]41#include "CorePrereqs.h"
[1062]42
[790]43#include "Iterator.h"
[871]44#include "ClassManager.h"
[790]45
[197]46namespace orxonox
47{
[224]48    // ###############################
49    // ###    ObjectListElement    ###
50    // ###############################
[790]51    //! The list-element of the ObjectList
[224]52    template <class T>
[197]53    class ObjectListElement
54    {
55        public:
[224]56            ObjectListElement(T* object);
[197]57
[790]58            T* object_;                     //!< The object
59            ObjectListElement* next_;       //!< The next element in the list
60            ObjectListElement* prev_;       //!< The previous element in the list
[197]61    };
62
[790]63    /**
64        @brief Constructor: Creates the list-element with an object.
65        @param object The object to store
66    */
[224]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    // ###############################
[790]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    */
[224]84    template <class T>
[197]85    class ObjectList
86    {
87        public:
[871]88            ObjectList();
89            ~ObjectList();
[790]90
[246]91            ObjectListElement<T>* add(T* object);
[790]92//            void remove(OrxonoxClass* object, bool bIterateForwards = true);
[197]93
[871]94            /** @brief Returns the first element in the list. @return The first element */
[248]95            inline static Iterator<T> start()
[871]96                { return Iterator<T>(ClassManager<T>::getIdentifier()->getObjects()->first_); }
[790]97
[871]98            /** @brief Returns the first element in the list. @return The first element */
[790]99            inline static Iterator<T> begin()
[871]100                { return Iterator<T>(ClassManager<T>::getIdentifier()->getObjects()->first_); }
[790]101
[871]102            /** @brief Returns the last element in the list. @return The last element */
[248]103            inline static Iterator<T> end()
[871]104                { return Iterator<T>(ClassManager<T>::getIdentifier()->getObjects()->last_); }
[248]105
[790]106            ObjectListElement<T>* first_;       //!< The first element in the list
107            ObjectListElement<T>* last_;        //!< The last element in the list
[197]108    };
[224]109
[790]110    /**
111        @brief Constructor: Sets default values.
112    */
[224]113    template <class T>
114    ObjectList<T>::ObjectList()
115    {
116        this->first_ = 0;
117        this->last_ = 0;
118    }
119
[790]120    /**
121        @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS.
122    */
[224]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
[790]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>
[246]141    ObjectListElement<T>* ObjectList<T>::add(T* object)
[224]142    {
143        if (!this->last_)
144        {
[790]145            // If the list is empty
[224]146            this->last_ = new ObjectListElement<T>(object);
[790]147            this->first_ = this->last_; // There's only one object in the list now
[224]148        }
149        else
150        {
[790]151            // If the list isn't empty
[224]152            ObjectListElement<T>* temp = this->last_;
153            this->last_ = new ObjectListElement<T>(object);
154            this->last_->prev_ = temp;
155            temp->next_ = this->last_;
156        }
[246]157
158        return this->last_;
[224]159    }
160
[790]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    /*
[224]168    template <class T>
169    void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards)
170    {
171        if (!object || !this->first_ || !this->last_)
172            return;
173
[790]174        // If there's only one object in the list, we have to set first_ and last_ to zero
[224]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
[790]187        // Now we are sure we have more than one element in the list
[224]188        if (bIterateForwards)
189        {
[790]190            // Start at the beginning of the list
191
192            // Check if it's the first object
[224]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
[790]203            // Iterate through the whole list
[224]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
[790]215                        this->last_ = temp; // If there is no next_, we deleted the last element and have to update the last_ pointer.
[224]216
217                    return;
218                }
219
220                temp = temp->next_;
221            }
222        }
223        else
224        {
[790]225            // Start at the end of the list
226
227            // Check if it's the last object
[224]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
[790]238            // Iterate through the whole list
[224]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
[790]250                        this->first_ = temp; // If there is no prev_, we deleted the first element and have to update the first_ pointer.
[224]251
252                    return;
253                }
254
255                temp = temp->prev_;
256            }
257        }
258    }
[790]259    */
[197]260}
261
[790]262#endif /* _ObjectList_H__ */
Note: See TracBrowser for help on using the repository browser.