Orxonox  0.0.5 Codename: Arcturus
ObjectListBase.h
Go to the documentation of this file.
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 
39 #ifndef _ObjectListBase_H__
40 #define _ObjectListBase_H__
41 
42 #include "core/CorePrereqs.h"
43 #include <vector>
44 #include "Context.h"
45 
46 namespace orxonox
47 {
48  // ###############################
49  // ### ObjectListBaseElement ###
50  // ###############################
53  {
54  public:
59  ObjectListBaseElement(Listable* object) : next_(nullptr), prev_(nullptr), objectBase_(object), list_(nullptr) {}
60  virtual ~ObjectListBaseElement() { this->removeFromList(); }
61 
62  virtual void changeContext(Context* oldContext, Context* newContext) = 0;
63 
68 
69  protected:
70  void removeFromList();
71  };
72 
73 
74  // ###############################
75  // ### ObjectListElement ###
76  // ###############################
78  template <class T>
80  {
81  public:
82  ObjectListElement(T* object) : ObjectListBaseElement(static_cast<Listable*>(object)), object_(object) {}
83 
84  virtual void changeContext(Context* oldContext, Context* newContext) override
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 
96  };
97 
98 
99  // ########################################
100  // ### ObjectListElementRemovalListener ###
101  // ########################################
104  {
105  public:
107  virtual ~ObjectListElementRemovalListener() = default;
108  virtual void removedElement(ObjectListBaseElement* element) = 0;
109  };
110 
111  // ###############################
112  // ### ObjectListBase ###
113  // ###############################
126  {
127  public:
128  ObjectListBase();
129  ~ObjectListBase();
130 
131  void addElement(ObjectListBaseElement* element);
132  void removeElement(ObjectListBaseElement* element);
133 
134  size_t size() const { return this->size_; }
135 
137  inline ObjectListBaseElement* begin() const { return this->first_; }
139  inline ObjectListBaseElement* end() const { return nullptr; }
141  inline ObjectListBaseElement* rbegin() const { return this->last_; }
143  inline ObjectListBaseElement* rend() const { return nullptr; }
144 
145  inline void registerRemovalListener(ObjectListElementRemovalListener* listener) { this->listeners_.push_back(listener); }
147  {
148  for (unsigned int i = 0; i < this->listeners_.size(); ++i)
149  {
150  if (listeners_[i] == listener)
151  {
152  listeners_.erase(listeners_.begin() + i);
153  break;
154  }
155  }
156  }
157 
158  private:
159  void notifyRemovalListeners(ObjectListBaseElement* element) const;
160 
163  size_t size_;
164  std::vector<ObjectListElementRemovalListener*> listeners_;
165  };
166 }
167 
168 #endif /* _ObjectListBase_H__ */
ObjectListBaseElement * rbegin() const
Returns a pointer to the last element in the list. Works only with Iterator.
Definition: ObjectListBase.h:141
ObjectListBase * getObjectList(const Identifier *identifier)
Definition: Context.cc:93
ObjectListBaseElement * last_
The last element in the list.
Definition: ObjectListBase.h:162
The list-element that actually contains the object.
Definition: CorePrereqs.h:208
Listable * objectBase_
The object.
Definition: ObjectListBase.h:66
Listable stores the entries of all object lists pointing to this instance.
Definition: Listable.h:50
Shared library macros, enums, constants and forward declarations for the core library ...
ObjectListBaseElement(Listable *object)
Constructor: Creates the list-element with an object.
Definition: ObjectListBase.h:59
Gets called by the object list if an element is removed.
Definition: ObjectListBase.h:103
ObjectListBaseElement * end() const
Returns a pointer to the element after the last element in the list. Works only with Iterator...
Definition: ObjectListBase.h:139
The ObjectListBase contains all objects of a given class.
Definition: ObjectListBase.h:125
ObjectListBaseElement * begin() const
Returns a pointer to the first element in the list. Works only with Iterator.
Definition: ObjectListBase.h:137
T * object_
The object.
Definition: ObjectListBase.h:95
ObjectListBaseElement * prev_
The previous element in the list.
Definition: ObjectListBase.h:65
size_t size_
The number of elements in the list.
Definition: ObjectListBase.h:163
ObjectListElement(T *object)
Definition: ObjectListBase.h:82
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
#define _CoreExport
Definition: CorePrereqs.h:61
virtual ~ObjectListBaseElement()
Definition: ObjectListBase.h:60
Definition: InputPrereqs.h:78
ObjectListBaseElement * first_
The first element in the list.
Definition: ObjectListBase.h:161
Definition: Context.h:45
size_t size() const
Definition: ObjectListBase.h:134
virtual void changeContext(Context *oldContext, Context *newContext) override
Definition: ObjectListBase.h:84
std::vector< ObjectListElementRemovalListener * > listeners_
A list of Iterators pointing on an element in this list.
Definition: ObjectListBase.h:164
The list-element of the ObjectListBase.
Definition: ObjectListBase.h:52
ObjectListBaseElement * next_
The next element in the list.
Definition: ObjectListBase.h:64
ObjectListBaseElement * rend() const
Returns a pointer to the element in front of the first element in the list. Works only with Iterator...
Definition: ObjectListBase.h:143
void registerRemovalListener(ObjectListElementRemovalListener *listener)
Definition: ObjectListBase.h:145
void unregisterRemovalListener(ObjectListElementRemovalListener *listener)
Definition: ObjectListBase.h:146
void addObject(T *object)
Definition: Context.h:65
ObjectListBase * list_
The list.
Definition: ObjectListBase.h:67