[1574] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
[2171] | 30 | @file |
---|
[7363] | 31 | @ingroup Object ObjectList |
---|
[1574] | 32 | @brief Definition of the ObjectListBase class. |
---|
| 33 | |
---|
| 34 | The ObjectListBase is a double-linked list, used by Identifiers to store all objects of a given class. |
---|
| 35 | Newly created objects are added through the RegisterObject-macro in its constructor. |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #ifndef _ObjectListBase_H__ |
---|
| 39 | #define _ObjectListBase_H__ |
---|
| 40 | |
---|
[3196] | 41 | #include "CorePrereqs.h" |
---|
| 42 | |
---|
[2784] | 43 | #include <vector> |
---|
[3196] | 44 | #include "OrxonoxClass.h" |
---|
[1574] | 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
| 48 | // ############################### |
---|
| 49 | // ### ObjectListBaseElement ### |
---|
| 50 | // ############################### |
---|
| 51 | //! The list-element of the ObjectListBase |
---|
| 52 | class _CoreExport ObjectListBaseElement |
---|
| 53 | { |
---|
| 54 | public: |
---|
| 55 | /** |
---|
| 56 | @brief Constructor: Creates the list-element with an object. |
---|
[7297] | 57 | @param objectBase The object to store |
---|
[1574] | 58 | */ |
---|
[1591] | 59 | ObjectListBaseElement(OrxonoxClass* objectBase) : next_(0), prev_(0), objectBase_(objectBase) {} |
---|
[1574] | 60 | |
---|
| 61 | ObjectListBaseElement* next_; //!< The next element in the list |
---|
| 62 | ObjectListBaseElement* prev_; //!< The previous element in the list |
---|
[1591] | 63 | OrxonoxClass* objectBase_; |
---|
[1574] | 64 | }; |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | // ############################### |
---|
[1591] | 68 | // ### ObjectListElement ### |
---|
| 69 | // ############################### |
---|
| 70 | //! The list-element that actually contains the object |
---|
| 71 | template <class T> |
---|
| 72 | class ObjectListElement : public ObjectListBaseElement |
---|
| 73 | { |
---|
| 74 | public: |
---|
[2896] | 75 | ObjectListElement(T* object) : ObjectListBaseElement(static_cast<OrxonoxClass*>(object)), object_(object) {} |
---|
[1591] | 76 | T* object_; //!< The object |
---|
| 77 | }; |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | // ############################### |
---|
[1574] | 81 | // ### ObjectListBase ### |
---|
| 82 | // ############################### |
---|
| 83 | //! The ObjectListBase contains all objects of a given class. |
---|
| 84 | /** |
---|
| 85 | The ObjectListBase is used by Identifiers to store all objects of their given class. |
---|
| 86 | Use ObjectList<T> to get the list of all T's and Iterator<T> to iterate through them. |
---|
| 87 | */ |
---|
| 88 | class _CoreExport ObjectListBase |
---|
| 89 | { |
---|
| 90 | friend class MetaObjectListElement; |
---|
| 91 | |
---|
| 92 | public: |
---|
| 93 | ObjectListBase(Identifier* identifier); |
---|
| 94 | ~ObjectListBase(); |
---|
| 95 | |
---|
[1591] | 96 | ObjectListBaseElement* add(ObjectListBaseElement* element); |
---|
[1574] | 97 | |
---|
[1591] | 98 | struct Export |
---|
| 99 | { |
---|
| 100 | Export(ObjectListBase* list, ObjectListBaseElement* element) : list_(list), element_(element) {} |
---|
| 101 | ObjectListBase* list_; |
---|
| 102 | ObjectListBaseElement* element_; |
---|
| 103 | }; |
---|
| 104 | |
---|
[1574] | 105 | /** @brief Returns a pointer to the first element in the list. @return The element */ |
---|
[1591] | 106 | inline Export begin() { return ObjectListBase::Export(this, this->first_); } |
---|
[1574] | 107 | /** @brief Returns a pointer to the element after the last element in the list. @return The element */ |
---|
[1591] | 108 | inline Export end() { return ObjectListBase::Export(this, 0); } |
---|
[1574] | 109 | /** @brief Returns a pointer to the last element in the list. @return The element */ |
---|
[1591] | 110 | inline Export rbegin() { return ObjectListBase::Export(this, this->last_); } |
---|
[1574] | 111 | /** @brief Returns a pointer to the element in front of the first element in the list. @return The element */ |
---|
[1591] | 112 | inline Export rend() { return ObjectListBase::Export(this, 0); } |
---|
[1574] | 113 | |
---|
[2784] | 114 | inline void registerIterator(void* iterator) { this->iterators_.push_back(iterator); } |
---|
| 115 | inline void unregisterIterator(void* iterator) |
---|
| 116 | { |
---|
| 117 | for (unsigned int i = 0; i < this->iterators_.size(); ++i) |
---|
| 118 | { |
---|
| 119 | if (iterators_[i] == iterator) |
---|
| 120 | { |
---|
| 121 | iterators_.erase(iterators_.begin() + i); |
---|
| 122 | break; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | inline void registerObjectListIterator(void* iterator) { this->objectListIterators_.push_back(iterator); } |
---|
| 127 | inline void unregisterObjectListIterator(void* iterator) |
---|
| 128 | { |
---|
| 129 | for (unsigned int i = 0; i < this->objectListIterators_.size(); ++i) |
---|
| 130 | { |
---|
| 131 | if (objectListIterators_[i] == iterator) |
---|
| 132 | { |
---|
| 133 | objectListIterators_.erase(objectListIterators_.begin() + i); |
---|
| 134 | break; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | } |
---|
[1591] | 138 | void notifyIterators(OrxonoxClass* object) const; |
---|
[1574] | 139 | |
---|
| 140 | inline Identifier* getIdentifier() const { return this->identifier_; } |
---|
| 141 | |
---|
| 142 | private: |
---|
[3196] | 143 | Identifier* identifier_; //!< The Iterator owning this list |
---|
| 144 | ObjectListBaseElement* first_; //!< The first element in the list |
---|
| 145 | ObjectListBaseElement* last_; //!< The last element in the list |
---|
[2784] | 146 | std::vector<void*> iterators_; //!< A list of Iterators pointing on an element in this list |
---|
| 147 | std::vector<void*> objectListIterators_; //!< A list of ObjectListIterators pointing on an element in this list |
---|
[1574] | 148 | }; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | #endif /* _ObjectListBase_H__ */ |
---|