Changeset 9667 for code/trunk/src/libraries/core/object/ObjectListBase.h
- Timestamp:
- Aug 25, 2013, 9:08:42 PM (12 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/core6 merged: 9552-9554,9556-9574,9577-9579,9585-9593,9596-9612,9626-9662
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/core/object/ObjectListBase.h
r9593 r9667 42 42 #include "core/CorePrereqs.h" 43 43 #include <vector> 44 #include "Context.h" 44 45 45 46 namespace orxonox … … 56 57 @param objectBase The object to store 57 58 */ 58 ObjectListBaseElement(Listable* objectBase) : next_(0), prev_(0), objectBase_(objectBase) {} 59 ObjectListBaseElement(Listable* object) : next_(0), prev_(0), objectBase_(object), list_(0) {} 60 virtual ~ObjectListBaseElement() { this->removeFromList(); } 61 62 virtual void changeContext(Context* oldContext, Context* newContext) = 0; 59 63 60 64 ObjectListBaseElement* next_; //!< The next element in the list 61 65 ObjectListBaseElement* prev_; //!< The previous element in the list 62 Listable* objectBase_; 66 Listable* objectBase_; //!< The object 67 ObjectListBase* list_; //!< The list 68 69 protected: 70 void removeFromList(); 63 71 }; 64 72 … … 73 81 public: 74 82 ObjectListElement(T* object) : ObjectListBaseElement(static_cast<Listable*>(object)), object_(object) {} 83 84 virtual void changeContext(Context* oldContext, Context* newContext) 85 { 86 // add object to new context, but only if this element belongs exactly to the old context (and not to a sub-context to avoid re-adding objects 87 // multiple times if they are in multiple contexts) 88 if (oldContext->getObjectList<T>() == this->list_) 89 newContext->addObject(this->object_); 90 91 // remove from old list 92 this->removeFromList(); 93 } 94 75 95 T* object_; //!< The object 76 96 }; 77 97 98 99 // ######################################## 100 // ### ObjectListElementRemovalListener ### 101 // ######################################## 102 /// Gets called by the object list if an element is removed 103 class _CoreExport ObjectListElementRemovalListener 104 { 105 public: 106 virtual ~ObjectListElementRemovalListener() {} 107 virtual void removedElement(ObjectListBaseElement* element) = 0; 108 }; 78 109 79 110 // ############################### … … 97 128 ~ObjectListBase(); 98 129 99 template <class T> 100 inline ObjectListBaseElement* add(T* object) 101 { return this->addElement(new ObjectListElement<T>(object)); } 102 103 ObjectListBaseElement* addElement(ObjectListBaseElement* element); 130 void addElement(ObjectListBaseElement* element); 104 131 void removeElement(ObjectListBaseElement* element); 105 132 106 /// Helper struct, used to export an element and the list to an instance of Iterator. 107 struct Export 108 { 109 Export(ObjectListBase* list, ObjectListBaseElement* element) : list_(list), element_(element) {} 110 ObjectListBase* list_; 111 ObjectListBaseElement* element_; 112 }; 133 size_t size() const { return this->size_; } 113 134 114 135 /// Returns a pointer to the first element in the list. Works only with Iterator. 115 inline Export begin() { return ObjectListBase::Export(this, this->first_); }136 inline ObjectListBaseElement* begin() const { return this->first_; } 116 137 /// Returns a pointer to the element after the last element in the list. Works only with Iterator. 117 inline Export end() { return ObjectListBase::Export(this, 0); }138 inline ObjectListBaseElement* end() const { return 0; } 118 139 /// Returns a pointer to the last element in the list. Works only with Iterator. 119 inline Export rbegin() { return ObjectListBase::Export(this, this->last_); }140 inline ObjectListBaseElement* rbegin() const { return this->last_; } 120 141 /// Returns a pointer to the element in front of the first element in the list. Works only with Iterator. 121 inline Export rend() { return ObjectListBase::Export(this, 0); }142 inline ObjectListBaseElement* rend() const { return 0; } 122 143 123 inline void register Iterator(void* iterator) { this->iterators_.push_back(iterator); }124 inline void unregister Iterator(void* iterator)144 inline void registerRemovalListener(ObjectListElementRemovalListener* listener) { this->listeners_.push_back(listener); } 145 inline void unregisterRemovalListener(ObjectListElementRemovalListener* listener) 125 146 { 126 for (unsigned int i = 0; i < this-> iterators_.size(); ++i)147 for (unsigned int i = 0; i < this->listeners_.size(); ++i) 127 148 { 128 if ( iterators_[i] == iterator)149 if (listeners_[i] == listener) 129 150 { 130 iterators_.erase(iterators_.begin() + i);151 listeners_.erase(listeners_.begin() + i); 131 152 break; 132 153 } 133 154 } 134 155 } 135 inline void registerObjectListIterator(void* iterator) { this->objectListIterators_.push_back(iterator); }136 inline void unregisterObjectListIterator(void* iterator)137 {138 for (unsigned int i = 0; i < this->objectListIterators_.size(); ++i)139 {140 if (objectListIterators_[i] == iterator)141 {142 objectListIterators_.erase(objectListIterators_.begin() + i);143 break;144 }145 }146 }147 void notifyIterators(Listable* object) const;148 156 149 157 private: 150 ObjectListBaseElement* first_; //!< The first element in the list 151 ObjectListBaseElement* last_; //!< The last element in the list 152 std::vector<void*> iterators_; //!< A list of Iterators pointing on an element in this list 153 std::vector<void*> objectListIterators_; //!< A list of ObjectListIterators pointing on an element in this list 158 void notifyRemovalListeners(ObjectListBaseElement* element) const; 159 160 ObjectListBaseElement* first_; //!< The first element in the list 161 ObjectListBaseElement* last_; //!< The last element in the list 162 size_t size_; //!< The number of elements in the list 163 std::vector<ObjectListElementRemovalListener*> listeners_; //!< A list of Iterators pointing on an element in this list 154 164 }; 155 165 }
Note: See TracChangeset
for help on using the changeset viewer.