[197] | 1 | #ifndef _ObjectList_H__ |
---|
| 2 | #define _ObjectList_H__ |
---|
| 3 | |
---|
| 4 | namespace orxonox |
---|
| 5 | { |
---|
| 6 | class OrxonoxClass; |
---|
| 7 | |
---|
[224] | 8 | // ############################### |
---|
| 9 | // ### ObjectListElement ### |
---|
| 10 | // ############################### |
---|
| 11 | template <class T> |
---|
[197] | 12 | class ObjectListElement |
---|
| 13 | { |
---|
| 14 | public: |
---|
[224] | 15 | ObjectListElement(T* object); |
---|
[197] | 16 | ~ObjectListElement(); |
---|
| 17 | |
---|
[224] | 18 | T* object_; |
---|
[197] | 19 | ObjectListElement* next_; |
---|
[221] | 20 | ObjectListElement* prev_; |
---|
[197] | 21 | }; |
---|
| 22 | |
---|
[224] | 23 | template <class T> |
---|
| 24 | ObjectListElement<T>::ObjectListElement(T* object) |
---|
| 25 | { |
---|
| 26 | this->object_ = object; |
---|
| 27 | this->next_ = 0; |
---|
| 28 | this->prev_ = 0; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | template <class T> |
---|
| 32 | ObjectListElement<T>::~ObjectListElement() |
---|
| 33 | { |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | // ############################### |
---|
| 38 | // ### ObjectList ### |
---|
| 39 | // ############################### |
---|
| 40 | template <class T> |
---|
[197] | 41 | class ObjectList |
---|
| 42 | { |
---|
| 43 | public: |
---|
| 44 | ObjectList(); |
---|
| 45 | ~ObjectList(); |
---|
[224] | 46 | void add(T* object); |
---|
[221] | 47 | void remove(OrxonoxClass* object, bool bIterateForwards = true); |
---|
[197] | 48 | |
---|
[224] | 49 | ObjectListElement<T>* first_; |
---|
| 50 | ObjectListElement<T>* last_; |
---|
[197] | 51 | }; |
---|
[224] | 52 | |
---|
| 53 | template <class T> |
---|
| 54 | ObjectList<T>::ObjectList() |
---|
| 55 | { |
---|
| 56 | this->first_ = 0; |
---|
| 57 | this->last_ = 0; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | template <class T> |
---|
| 61 | ObjectList<T>::~ObjectList() |
---|
| 62 | { |
---|
| 63 | ObjectListElement<T>* temp; |
---|
| 64 | while (this->first_) |
---|
| 65 | { |
---|
| 66 | temp = this->first_->next_; |
---|
| 67 | delete this->first_; |
---|
| 68 | this->first_ = temp; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | template <class T> |
---|
| 73 | void ObjectList<T>::add(T* object) |
---|
| 74 | { |
---|
| 75 | if (!this->last_) |
---|
| 76 | { |
---|
| 77 | this->last_ = new ObjectListElement<T>(object); |
---|
| 78 | this->first_ = this->last_; |
---|
| 79 | } |
---|
| 80 | else |
---|
| 81 | { |
---|
| 82 | ObjectListElement<T>* temp = this->last_; |
---|
| 83 | this->last_ = new ObjectListElement<T>(object); |
---|
| 84 | this->last_->prev_ = temp; |
---|
| 85 | temp->next_ = this->last_; |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | template <class T> |
---|
| 90 | void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards) |
---|
| 91 | { |
---|
| 92 | if (!object || !this->first_ || !this->last_) |
---|
| 93 | return; |
---|
| 94 | |
---|
| 95 | if (this->first_ == this->last_) |
---|
| 96 | { |
---|
| 97 | if (this->first_->object_ == object) |
---|
| 98 | { |
---|
| 99 | delete this->first_; |
---|
| 100 | this->first_ = 0; |
---|
| 101 | this->last_ = 0; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | return; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | if (bIterateForwards) |
---|
| 108 | { |
---|
| 109 | if (this->first_->object_ == object) |
---|
| 110 | { |
---|
| 111 | ObjectListElement<T>* temp = this->first_->next_; |
---|
| 112 | delete this->first_; |
---|
| 113 | this->first_ = temp; |
---|
| 114 | this->first_->prev_ = 0; |
---|
| 115 | |
---|
| 116 | return; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | ObjectListElement<T>* temp = this->first_; |
---|
| 120 | while (temp->next_) |
---|
| 121 | { |
---|
| 122 | if (temp->next_->object_ == object) |
---|
| 123 | { |
---|
| 124 | ObjectListElement<T>* temp2 = temp->next_->next_; |
---|
| 125 | delete temp->next_; |
---|
| 126 | temp->next_ = temp2; |
---|
| 127 | if (temp2) |
---|
| 128 | temp2->prev_ = temp; |
---|
| 129 | else |
---|
| 130 | this->last_ = temp; |
---|
| 131 | |
---|
| 132 | return; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | temp = temp->next_; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | else |
---|
| 139 | { |
---|
| 140 | if (this->last_->object_ == object) |
---|
| 141 | { |
---|
| 142 | ObjectListElement<T>* temp = this->last_->prev_; |
---|
| 143 | delete this->last_; |
---|
| 144 | this->last_ = temp; |
---|
| 145 | this->last_->next_ = 0; |
---|
| 146 | |
---|
| 147 | return; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | ObjectListElement<T>* temp = this->last_; |
---|
| 151 | while (temp->prev_) |
---|
| 152 | { |
---|
| 153 | if (temp->prev_->object_ == object) |
---|
| 154 | { |
---|
| 155 | ObjectListElement<T>* temp2 = temp->prev_->prev_; |
---|
| 156 | delete temp->prev_; |
---|
| 157 | temp->prev_ = temp2; |
---|
| 158 | if (temp2) |
---|
| 159 | temp2->next_ = temp; |
---|
| 160 | else |
---|
| 161 | this->first_ = temp; |
---|
| 162 | |
---|
| 163 | return; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | temp = temp->prev_; |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | } |
---|
[197] | 170 | } |
---|
| 171 | |
---|
| 172 | #endif |
---|