Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/core/ObjectList.h @ 365

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

added comments

File size: 7.5 KB
RevLine 
[365]1/*!
2    @file ObjectList.h
3    @brief Definition of the ObjectList class.
4
5    The ObjectList is a double-linked list, used by Identifiers to store all objects of a specific class in it.
6    Created objects are added through the RegisterObject-macro in its constructor.
7    Use Iterator<class> to iterate through all objects of the class.
8*/
9
[197]10#ifndef _ObjectList_H__
11#define _ObjectList_H__
12
13namespace orxonox
14{
[365]15    class OrxonoxClass; // Forward declaration
[197]16
[224]17    // ###############################
18    // ###    ObjectListElement    ###
19    // ###############################
[365]20    //! The list-element of the ObjectList
[224]21    template <class T>
[197]22    class ObjectListElement
23    {
24        public:
[224]25            ObjectListElement(T* object);
[197]26
[365]27            T* object_;                     //!< The object
28            ObjectListElement* next_;       //!< The next element in the list
29            ObjectListElement* prev_;       //!< The previous element in the list
[197]30    };
31
[365]32    /**
33        @brief Constructor: Creates the list-element with an object.
34        @param Object The object to store
35    */
[224]36    template <class T>
37    ObjectListElement<T>::ObjectListElement(T* object)
38    {
39        this->object_ = object;
40        this->next_ = 0;
41        this->prev_ = 0;
42    }
43
44
45    // ###############################
46    // ###       ObjectList        ###
47    // ###############################
48    template <class T>
[365]49    class Iterator; // Forward declaration
[248]50
[365]51    //! The ObjectList contains all objects of a specific class.
52    /**
53        The ObjectList is used by Identifiers to store all objects of a specific class in it.
54        Use Iterator<class> to iterate through all objects in the list.
55    */
[248]56    template <class T>
[197]57    class ObjectList
58    {
59        public:
60            ObjectList();
61            ~ObjectList();
[246]62            ObjectListElement<T>* add(T* object);
[365]63//            void remove(OrxonoxClass* object, bool bIterateForwards = true);
[197]64
[365]65            /** @returns the first element in the list */
[248]66            inline static Iterator<T> start()
[254]67                { return Iterator<T>(pointer_s->first_); }
[365]68
69            /** @returns the last element in the list */
[248]70            inline static Iterator<T> end()
[254]71                { return Iterator<T>(pointer_s->last_); }
[248]72
[365]73            ObjectListElement<T>* first_;       //!< The first element in the list
74            ObjectListElement<T>* last_;        //!< The last element in the list
[248]75
76        private:
[365]77            static ObjectList<T>* pointer_s;    //!< A static pointer to the last created list (different for all T)
[197]78    };
[224]79
80    template <class T>
[365]81    ObjectList<T>* ObjectList<T>::pointer_s = 0; // Set the static member variable pointer_s to zero
[248]82
[365]83    /**
84        @brief Constructor: Sets first_ and last_ to zero and the static member variable pointer_s to _this_
85    */
[248]86    template <class T>
[224]87    ObjectList<T>::ObjectList()
88    {
89        this->first_ = 0;
90        this->last_ = 0;
[248]91
[365]92        // ObjectLists are only created by Identifiers and therefore only one ObjectList of each T will exist.
93        // Thats why pointer_s is in fact a pointer to the only ObjectList for a type, which makes it almost a singleton.
[248]94        this->pointer_s = this;
[224]95    }
96
[365]97    /**
98        @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS.
99    */
[224]100    template <class T>
101    ObjectList<T>::~ObjectList()
102    {
103        ObjectListElement<T>* temp;
104        while (this->first_)
105        {
106            temp = this->first_->next_;
107            delete this->first_;
108            this->first_ = temp;
109        }
110    }
111
[365]112    /**
113        @brief Adds a new object to the end of the list.
114        @param object The object to add
115        @return The pointer to the new ObjectListElement, needed by the MetaObjectList of the added object
116    */
[224]117    template <class T>
[246]118    ObjectListElement<T>* ObjectList<T>::add(T* object)
[224]119    {
120        if (!this->last_)
121        {
[365]122            // If the list is empty
[224]123            this->last_ = new ObjectListElement<T>(object);
[365]124            this->first_ = this->last_; // There's only one object in the list now
[224]125        }
126        else
127        {
[365]128            // If the list isn't empty
[224]129            ObjectListElement<T>* temp = this->last_;
130            this->last_ = new ObjectListElement<T>(object);
131            this->last_->prev_ = temp;
132            temp->next_ = this->last_;
133        }
[246]134
135        return this->last_;
[224]136    }
137
[365]138
139//    /**
140//        @brief Removes an object from the list.
141//        @param object The object to remove
142//        @param bIterateForwards If true: Start searching the object at the beginning of the list
143//    */
144    /*
[224]145    template <class T>
146    void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards)
147    {
148        if (!object || !this->first_ || !this->last_)
149            return;
150
[365]151        // If there's only one object in the list, we have to set first_ and last_ to zero
[224]152        if (this->first_ == this->last_)
153        {
154            if (this->first_->object_ == object)
155            {
156                delete this->first_;
157                this->first_ = 0;
158                this->last_ = 0;
159            }
160
161            return;
162        }
163
[365]164        // Now we are sure we have more than one element in the list
[224]165        if (bIterateForwards)
166        {
[365]167            // Start at the beginning of the list
168
169            // Check if it's the first object
[224]170            if (this->first_->object_ == object)
171            {
172                ObjectListElement<T>* temp = this->first_->next_;
173                delete this->first_;
174                this->first_ = temp;
175                this->first_->prev_ = 0;
176
177                return;
178            }
179
[365]180            // Iterate through the whole list
[224]181            ObjectListElement<T>* temp = this->first_;
182            while (temp->next_)
183            {
184                if (temp->next_->object_ == object)
185                {
186                    ObjectListElement<T>* temp2 = temp->next_->next_;
187                    delete temp->next_;
188                    temp->next_ = temp2;
189                    if (temp2)
190                        temp2->prev_ = temp;
191                    else
[365]192                        this->last_ = temp; // If there is no next_, we deleted the last element and have to update the last_ pointer.
[224]193
194                    return;
195                }
196
197                temp = temp->next_;
198            }
199        }
200        else
201        {
[365]202            // Start at the end of the list
203
204            // Check if it's the last object
[224]205            if (this->last_->object_ == object)
206            {
207                ObjectListElement<T>* temp = this->last_->prev_;
208                delete this->last_;
209                this->last_ = temp;
210                this->last_->next_ = 0;
211
212                return;
213            }
214
[365]215            // Iterate through the whole list
[224]216            ObjectListElement<T>* temp = this->last_;
217            while (temp->prev_)
218            {
219                if (temp->prev_->object_ == object)
220                {
221                    ObjectListElement<T>* temp2 = temp->prev_->prev_;
222                    delete temp->prev_;
223                    temp->prev_ = temp2;
224                    if (temp2)
225                        temp2->next_ = temp;
226                    else
[365]227                        this->first_ = temp; // If there is no prev_, we deleted the first element and have to update the first_ pointer.
[224]228
229                    return;
230                }
231
232                temp = temp->prev_;
233            }
234        }
235    }
[365]236    */
[197]237}
238
239#endif
Note: See TracBrowser for help on using the repository browser.