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
Line 
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
10#ifndef _ObjectList_H__
11#define _ObjectList_H__
12
13namespace orxonox
14{
15    class OrxonoxClass; // Forward declaration
16
17    // ###############################
18    // ###    ObjectListElement    ###
19    // ###############################
20    //! The list-element of the ObjectList
21    template <class T>
22    class ObjectListElement
23    {
24        public:
25            ObjectListElement(T* object);
26
27            T* object_;                     //!< The object
28            ObjectListElement* next_;       //!< The next element in the list
29            ObjectListElement* prev_;       //!< The previous element in the list
30    };
31
32    /**
33        @brief Constructor: Creates the list-element with an object.
34        @param Object The object to store
35    */
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>
49    class Iterator; // Forward declaration
50
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    */
56    template <class T>
57    class ObjectList
58    {
59        public:
60            ObjectList();
61            ~ObjectList();
62            ObjectListElement<T>* add(T* object);
63//            void remove(OrxonoxClass* object, bool bIterateForwards = true);
64
65            /** @returns the first element in the list */
66            inline static Iterator<T> start()
67                { return Iterator<T>(pointer_s->first_); }
68
69            /** @returns the last element in the list */
70            inline static Iterator<T> end()
71                { return Iterator<T>(pointer_s->last_); }
72
73            ObjectListElement<T>* first_;       //!< The first element in the list
74            ObjectListElement<T>* last_;        //!< The last element in the list
75
76        private:
77            static ObjectList<T>* pointer_s;    //!< A static pointer to the last created list (different for all T)
78    };
79
80    template <class T>
81    ObjectList<T>* ObjectList<T>::pointer_s = 0; // Set the static member variable pointer_s to zero
82
83    /**
84        @brief Constructor: Sets first_ and last_ to zero and the static member variable pointer_s to _this_
85    */
86    template <class T>
87    ObjectList<T>::ObjectList()
88    {
89        this->first_ = 0;
90        this->last_ = 0;
91
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.
94        this->pointer_s = this;
95    }
96
97    /**
98        @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS.
99    */
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
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    */
117    template <class T>
118    ObjectListElement<T>* ObjectList<T>::add(T* object)
119    {
120        if (!this->last_)
121        {
122            // If the list is empty
123            this->last_ = new ObjectListElement<T>(object);
124            this->first_ = this->last_; // There's only one object in the list now
125        }
126        else
127        {
128            // If the list isn't empty
129            ObjectListElement<T>* temp = this->last_;
130            this->last_ = new ObjectListElement<T>(object);
131            this->last_->prev_ = temp;
132            temp->next_ = this->last_;
133        }
134
135        return this->last_;
136    }
137
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    /*
145    template <class T>
146    void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards)
147    {
148        if (!object || !this->first_ || !this->last_)
149            return;
150
151        // If there's only one object in the list, we have to set first_ and last_ to zero
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
164        // Now we are sure we have more than one element in the list
165        if (bIterateForwards)
166        {
167            // Start at the beginning of the list
168
169            // Check if it's the first object
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
180            // Iterate through the whole list
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
192                        this->last_ = temp; // If there is no next_, we deleted the last element and have to update the last_ pointer.
193
194                    return;
195                }
196
197                temp = temp->next_;
198            }
199        }
200        else
201        {
202            // Start at the end of the list
203
204            // Check if it's the last object
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
215            // Iterate through the whole list
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
227                        this->first_ = temp; // If there is no prev_, we deleted the first element and have to update the first_ pointer.
228
229                    return;
230                }
231
232                temp = temp->prev_;
233            }
234        }
235    }
236    */
237}
238
239#endif
Note: See TracBrowser for help on using the repository browser.