Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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