Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/core/ObjectList.h @ 871

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

merged core branch to trunk

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