Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

copyright notes

File size: 8.4 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
40namespace orxonox
41{
42    class OrxonoxClass; // Forward declaration
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    template <class T>
76    class Iterator; // Forward declaration
77
78    //! The ObjectList contains all objects of a given class.
79    /**
80        The ObjectList is used by Identifiers to store all objects of a given class.
81        Use Iterator<class> to iterate through all objects in the list.
82    */
83    template <class T>
84    class ObjectList
85    {
86        public:
87            ObjectList();
88            ~ObjectList();
89            ObjectListElement<T>* add(T* object);
90//            void remove(OrxonoxClass* object, bool bIterateForwards = true);
91
92            /** @returns the first element in the list */
93            inline static Iterator<T> start()
94                { return Iterator<T>(pointer_s->first_); }
95
96            /** @returns the last element in the list */
97            inline static Iterator<T> end()
98                { return Iterator<T>(pointer_s->last_); }
99
100            ObjectListElement<T>* first_;       //!< The first element in the list
101            ObjectListElement<T>* last_;        //!< The last element in the list
102
103        private:
104            static ObjectList<T>* pointer_s;    //!< A static pointer to the last created list (different for all T)
105    };
106
107    template <class T>
108    ObjectList<T>* ObjectList<T>::pointer_s = 0; // Set the static member variable pointer_s to zero
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        // ObjectLists are only created by Identifiers and therefore only one ObjectList of each T will exist.
120        // Thats why pointer_s is in fact a pointer to the only ObjectList for a type, which makes it almost a singleton.
121        this->pointer_s = this;
122    }
123
124    /**
125        @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS.
126    */
127    template <class T>
128    ObjectList<T>::~ObjectList()
129    {
130        ObjectListElement<T>* temp;
131        while (this->first_)
132        {
133            temp = this->first_->next_;
134            delete this->first_;
135            this->first_ = temp;
136        }
137    }
138
139    /**
140        @brief Adds a new object to the end of the list.
141        @param object The object to add
142        @return The pointer to the new ObjectListElement, needed by the MetaObjectList of the added object
143    */
144    template <class T>
145    ObjectListElement<T>* ObjectList<T>::add(T* object)
146    {
147        if (!this->last_)
148        {
149            // If the list is empty
150            this->last_ = new ObjectListElement<T>(object);
151            this->first_ = this->last_; // There's only one object in the list now
152        }
153        else
154        {
155            // If the list isn't empty
156            ObjectListElement<T>* temp = this->last_;
157            this->last_ = new ObjectListElement<T>(object);
158            this->last_->prev_ = temp;
159            temp->next_ = this->last_;
160        }
161
162        return this->last_;
163    }
164
165
166//    /**
167//        @brief Removes an object from the list.
168//        @param object The object to remove
169//        @param bIterateForwards If true: Start searching the object at the beginning of the list
170//    */
171    /*
172    template <class T>
173    void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards)
174    {
175        if (!object || !this->first_ || !this->last_)
176            return;
177
178        // If there's only one object in the list, we have to set first_ and last_ to zero
179        if (this->first_ == this->last_)
180        {
181            if (this->first_->object_ == object)
182            {
183                delete this->first_;
184                this->first_ = 0;
185                this->last_ = 0;
186            }
187
188            return;
189        }
190
191        // Now we are sure we have more than one element in the list
192        if (bIterateForwards)
193        {
194            // Start at the beginning of the list
195
196            // Check if it's the first object
197            if (this->first_->object_ == object)
198            {
199                ObjectListElement<T>* temp = this->first_->next_;
200                delete this->first_;
201                this->first_ = temp;
202                this->first_->prev_ = 0;
203
204                return;
205            }
206
207            // Iterate through the whole list
208            ObjectListElement<T>* temp = this->first_;
209            while (temp->next_)
210            {
211                if (temp->next_->object_ == object)
212                {
213                    ObjectListElement<T>* temp2 = temp->next_->next_;
214                    delete temp->next_;
215                    temp->next_ = temp2;
216                    if (temp2)
217                        temp2->prev_ = temp;
218                    else
219                        this->last_ = temp; // If there is no next_, we deleted the last element and have to update the last_ pointer.
220
221                    return;
222                }
223
224                temp = temp->next_;
225            }
226        }
227        else
228        {
229            // Start at the end of the list
230
231            // Check if it's the last object
232            if (this->last_->object_ == object)
233            {
234                ObjectListElement<T>* temp = this->last_->prev_;
235                delete this->last_;
236                this->last_ = temp;
237                this->last_->next_ = 0;
238
239                return;
240            }
241
242            // Iterate through the whole list
243            ObjectListElement<T>* temp = this->last_;
244            while (temp->prev_)
245            {
246                if (temp->prev_->object_ == object)
247                {
248                    ObjectListElement<T>* temp2 = temp->prev_->prev_;
249                    delete temp->prev_;
250                    temp->prev_ = temp2;
251                    if (temp2)
252                        temp2->next_ = temp;
253                    else
254                        this->first_ = temp; // If there is no prev_, we deleted the first element and have to update the first_ pointer.
255
256                    return;
257                }
258
259                temp = temp->prev_;
260            }
261        }
262    }
263    */
264}
265
266#endif
Note: See TracBrowser for help on using the repository browser.