1 | /*! |
---|
2 | @file MetaObjectList.h |
---|
3 | @brief Definition of the MetaObjectList class. |
---|
4 | |
---|
5 | The MetaObjectList is a single-linked list, containing all list-elements and their |
---|
6 | lists wherein the object, owning the MetaObjectList, is registered. |
---|
7 | This allows much faster deletion of objects because no iteration is needed. |
---|
8 | */ |
---|
9 | |
---|
10 | #ifndef _MetaObjectList_H__ |
---|
11 | #define _MetaObjectList_H__ |
---|
12 | |
---|
13 | #include "ObjectList.h" |
---|
14 | #include "Identifier.h" |
---|
15 | #include "Debug.h" |
---|
16 | |
---|
17 | namespace orxonox |
---|
18 | { |
---|
19 | //! Base-class of MetaObjectListElement, because those is a template |
---|
20 | class BaseMetaObjectListElement |
---|
21 | { |
---|
22 | public: |
---|
23 | /** @brief Default destructor */ |
---|
24 | virtual ~BaseMetaObjectListElement() {}; |
---|
25 | |
---|
26 | BaseMetaObjectListElement* next_; //!< The next Element in the list |
---|
27 | }; |
---|
28 | |
---|
29 | // ############################### |
---|
30 | // ### MetaObjectListElement ### |
---|
31 | // ############################### |
---|
32 | //! The list-element of the MetaObjectList |
---|
33 | template <class T> |
---|
34 | class MetaObjectListElement : public BaseMetaObjectListElement |
---|
35 | { |
---|
36 | public: |
---|
37 | MetaObjectListElement(ObjectList<T>* list, ObjectListElement<T>* element); |
---|
38 | ~MetaObjectListElement(); |
---|
39 | |
---|
40 | ObjectListElement<T>* element_; //!< The list element, containing the object |
---|
41 | ObjectList<T>* list_; //!< The list, containing the element |
---|
42 | }; |
---|
43 | |
---|
44 | /** |
---|
45 | @brief Constructor: Creates the list-element with given list and element. |
---|
46 | */ |
---|
47 | template <class T> |
---|
48 | MetaObjectListElement<T>::MetaObjectListElement(ObjectList<T>* list, ObjectListElement<T>* element) |
---|
49 | { |
---|
50 | this->element_ = element; |
---|
51 | this->list_ = list; |
---|
52 | this->next_ = 0; |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | @brief Destructor: Removes the ObjectListElement from the ObjectList by linking next_ and prev_ of the ObjectListElement. |
---|
57 | */ |
---|
58 | template <class T> |
---|
59 | MetaObjectListElement<T>::~MetaObjectListElement() |
---|
60 | { |
---|
61 | if (this->element_->next_) |
---|
62 | this->element_->next_->prev_ = this->element_->prev_; |
---|
63 | else |
---|
64 | this->list_->last_ = this->element_->prev_; // If there is no next_, we deleted the last object and have to update the last_ pointer of the list |
---|
65 | |
---|
66 | if (this->element_->prev_) |
---|
67 | this->element_->prev_->next_ = this->element_->next_; |
---|
68 | else |
---|
69 | this->list_->first_ = this->element_->next_; // If there is no prev_, we deleted the first object and have to update the first_ pointer of the list |
---|
70 | |
---|
71 | |
---|
72 | COUT(4) << "*** Removing Object from " << ClassIdentifier<T>::getIdentifier()->getName() << "-list.\n"; |
---|
73 | delete this->element_; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | // ############################### |
---|
78 | // ### ObjectList ### |
---|
79 | // ############################### |
---|
80 | //! The MetaObjectList contains ObjectListElements and their ObjectLists. |
---|
81 | /** |
---|
82 | The MetaObjectList is a single-linked list, containing all list-elements and their |
---|
83 | lists wherein the object that owns the MetaObjectList is registered. |
---|
84 | This allows much faster deletion of objects because no iteration is needed. |
---|
85 | */ |
---|
86 | class MetaObjectList |
---|
87 | { |
---|
88 | public: |
---|
89 | MetaObjectList(); |
---|
90 | ~MetaObjectList(); |
---|
91 | template <class T> |
---|
92 | void add(ObjectList<T>* list, ObjectListElement<T>* element); |
---|
93 | |
---|
94 | BaseMetaObjectListElement* first_; //!< The first element in the list |
---|
95 | }; |
---|
96 | |
---|
97 | /** |
---|
98 | @brief Adds an ObjectList and an element of that list to the MetaObjectList. |
---|
99 | @param list The ObjectList wherein the element is |
---|
100 | @param element The element wherein the object is |
---|
101 | */ |
---|
102 | template <class T> |
---|
103 | void MetaObjectList::add(ObjectList<T>* list, ObjectListElement<T>* element) |
---|
104 | { |
---|
105 | BaseMetaObjectListElement* temp = this->first_; |
---|
106 | this->first_ = new MetaObjectListElement<T>(list, element); |
---|
107 | this->first_->next_ = temp; |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | #endif |
---|