Changeset 1747 for code/trunk/src/core/ObjectList.h
- Timestamp:
- Sep 9, 2008, 4:25:52 AM (17 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/core3 (added) merged: 1573-1574,1583-1586,1591-1594,1596-1597,1603,1606-1607,1610-1611,1655,1658,1676-1679,1681-1685,1687,1716-1723,1725-1729,1736
- Property svn:mergeinfo changed
-
code/trunk/src/core/ObjectList.h
r1543 r1747 31 31 @brief Definition and implementation of the ObjectList class. 32 32 33 The ObjectList is a double-linked list, used by Identifiers to store all objects of a given class. 34 Newly created objects are added through the RegisterObject-macro in its constructor. 33 The ObjectList is a wrapper of an ObjectListBase of a given class. 35 34 Use Iterator<class> to iterate through all objects of the class. 36 35 */ … … 39 38 #define _ObjectList_H__ 40 39 41 #include <set>42 43 40 #include "CorePrereqs.h" 44 41 45 #include "Iterator.h"46 42 #include "Identifier.h" 43 #include "ObjectListIterator.h" 47 44 48 45 namespace orxonox 49 46 { 50 47 // ############################### 51 // ### ObjectListElement ###52 // ###############################53 //! The list-element of the ObjectList54 template <class T>55 class ObjectListElement56 {57 public:58 ObjectListElement(T* object);59 60 T* object_; //!< The object61 ObjectListElement* next_; //!< The next element in the list62 ObjectListElement* prev_; //!< The previous element in the list63 };64 65 /**66 @brief Constructor: Creates the list-element with an object.67 @param object The object to store68 */69 template <class T>70 ObjectListElement<T>::ObjectListElement(T* object)71 {72 this->object_ = object;73 this->next_ = 0;74 this->prev_ = 0;75 }76 77 78 // ###############################79 48 // ### ObjectList ### 80 49 // ############################### 81 //! The ObjectList contains all objects of agiven class.50 //! The ObjectList contains all objects of the given class. 82 51 /** 83 The ObjectList is used by Identifiers to store all objects of a given class.84 Use Iterator<class> to iterate through all objects in the list.52 Wraps the ObjectListBase of the corresponding Identifier. 53 Use ObjectListIterator<class> to iterate through all objects in the list. 85 54 */ 86 55 template <class T> … … 88 57 { 89 58 public: 90 ObjectList(); 91 ~ObjectList(); 59 typedef ObjectListIterator<T> iterator; 92 60 93 ObjectListElement<T>* add(T* object); 61 /** @brief Returns an Iterator to the first element in the list. @return The Iterator */ 62 inline static ObjectListElement<T>* begin() 63 { return ((ObjectListElement<T>*)ClassIdentifier<T>::getIdentifier()->getObjects()->begin().element_); } 94 64 95 /** @brief Returns the first element in the list. @return The first element*/96 inline static Iterator<T> start()97 { return Iterator<T>(ClassIdentifier<T>::getIdentifier()->getObjects()->first_); }65 /** @brief Returns an Iterator to the element after the last element in the list. @return The Iterator */ 66 inline static ObjectListElement<T>* end() 67 { return ((ObjectListElement<T>*)ClassIdentifier<T>::getIdentifier()->getObjects()->end().element_); } 98 68 99 /** @brief Returns the first element in the list. @return The first element*/100 inline static Iterator<T>begin()101 { return Iterator<T>(ClassIdentifier<T>::getIdentifier()->getObjects()->first_); }69 /** @brief Returns an Iterator to the last element in the list. @return The Iterator */ 70 inline static ObjectListElement<T>* rbegin() 71 { return ((ObjectListElement<T>*)ClassIdentifier<T>::getIdentifier()->getObjects()->rbegin().element_); } 102 72 103 /** @brief Returns the last element in the list. @return The last element */ 104 inline static Iterator<T> end() 105 { return Iterator<T>(ClassIdentifier<T>::getIdentifier()->getObjects()->last_); } 106 107 inline void registerIterator(Iterator<T>* iterator) 108 { this->iterators_.insert(this->iterators_.end(), (void*)iterator); } 109 inline void unregisterIterator(Iterator<T>* iterator) 110 { this->iterators_.erase((void*)iterator); } 111 void notifyIterators(ObjectListElement<T>* element); 112 113 ObjectListElement<T>* first_; //!< The first element in the list 114 ObjectListElement<T>* last_; //!< The last element in the list 115 116 private: 117 std::set<void*> iterators_; //!< A list of iterators pointing on an element in this list 73 /** @brief Returns an Iterator to the element before the first element in the list. @return The Iterator */ 74 inline static ObjectListElement<T>* rend() 75 { return ((ObjectListElement<T>*)ClassIdentifier<T>::getIdentifier()->getObjects()->rend().element_); } 118 76 }; 119 120 /**121 @brief Constructor: Sets default values.122 */123 template <class T>124 ObjectList<T>::ObjectList()125 {126 this->first_ = 0;127 this->last_ = 0;128 }129 130 /**131 @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS.132 */133 template <class T>134 ObjectList<T>::~ObjectList()135 {136 ObjectListElement<T>* temp;137 while (this->first_)138 {139 temp = this->first_->next_;140 delete this->first_;141 this->first_ = temp;142 }143 }144 145 /**146 @brief Increases all Iterators that currently point on the given element (because it gets removed).147 @param element The element that gets removed148 */149 template <class T>150 void ObjectList<T>::notifyIterators(ObjectListElement<T>* element)151 {152 for (std::set<void*>::iterator it = this->iterators_.begin(); it != this->iterators_.end(); ++it)153 if ((*(*((Iterator<T>*)(*it)))) == element->object_)154 ++(*((Iterator<T>*)(*it)));155 }156 157 /**158 @brief Adds a new object to the end of the list.159 @param object The object to add160 @return The pointer to the new ObjectListElement, needed by the MetaObjectList of the added object161 */162 template <class T>163 ObjectListElement<T>* ObjectList<T>::add(T* object)164 {165 if (!this->last_)166 {167 // If the list is empty168 this->last_ = new ObjectListElement<T>(object);169 this->first_ = this->last_; // There's only one object in the list now170 }171 else172 {173 // If the list isn't empty174 ObjectListElement<T>* temp = this->last_;175 this->last_ = new ObjectListElement<T>(object);176 this->last_->prev_ = temp;177 temp->next_ = this->last_;178 }179 180 return this->last_;181 }182 77 } 183 78
Note: See TracChangeset
for help on using the changeset viewer.