Orxonox  0.0.5 Codename: Arcturus
BaseObject.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 
42 #ifndef _BaseObject_H__
43 #define _BaseObject_H__
44 
45 #include "CorePrereqs.h"
46 
47 #include <map>
48 #include <list>
49 
50 #include "util/mbool.h"
51 #include "class/OrxonoxClass.h"
52 #include "class/Super.h"
53 #include "object/StrongPtr.h"
54 
55 namespace orxonox
56 {
57  class Scene;
58  class Gametype;
59  class Level;
60  class ScriptableController;
61 
64  {
65  template <class T> friend class XMLPortClassParamContainer;
66 
67  public:
68  template <class T>
70  {
71  public:
72  inline StrongOrWeakPtr();
73  inline StrongOrWeakPtr(const StrongPtr<T>& ptr);
74  inline StrongOrWeakPtr(const WeakPtr<T>& ptr);
75 
76  inline T* get() const;
77  inline StrongPtr<T> createStrongPtr() const;
78 
79  private:
82  };
83 
84  public:
85  BaseObject(Context* context);
86  virtual ~BaseObject();
87  virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
88  virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
89 
91  inline bool isInitialized() const { return this->bInitialized_; }
92 
94  inline void setName(const std::string& name) { this->oldName_ = this->name_; this->name_ = name; this->changedName(); }
96  inline const std::string& getName() const { return this->name_; }
98  inline const std::string& getOldName() const { return this->oldName_; }
100  virtual void changedName() {}
101 
103  inline void setActive(bool bActive)
104  {
105  if (this->bActive_ != bActive)
106  {
107  this->bActive_ = bActive;
108  this->changedActivity();
109  }
110  }
112  inline const mbool& isActive() const { return this->bActive_; }
114  virtual void changedActivity() {}
115 
117  inline void setVisible(bool bVisible)
118  {
119  if (this->bVisible_ != bVisible)
120  {
121  this->bVisible_ = bVisible;
122  this->changedVisibility();
123  }
124  }
126  inline const mbool& isVisible() const { return this->bVisible_; }
128  virtual void changedVisibility() {}
129 
130  void setMainState(bool state);
131 
133  void setMainStateName(const std::string& name)
134  {
135  if (this->mainStateName_ != name)
136  {
137  this->mainStateName_ = name;
138  this->changedMainStateName();
139  }
140  }
142  inline const std::string& getMainStateName() const { return this->mainStateName_; }
144  virtual void changedMainStateName();
145 
147  inline void setFile(const XMLFile* file) { this->file_ = file; }
149  inline const XMLFile* getFile() const { return this->file_; }
150  const std::string& getFilename() const;
151 
152  void addTemplate(const std::string& name);
153  void addTemplate(Template* temp);
155  inline const std::set<Template*>& getTemplates() const
156  { return this->templates_; }
157 
158  inline void setNamespace(const StrongOrWeakPtr<Namespace>& ns) { this->namespace_ = ns; }
159  inline Namespace* getNamespace() const { return this->namespace_.get(); }
160 
161  inline void setCreator(BaseObject* creator) { this->creator_ = creator; }
162  inline BaseObject* getCreator() const { return this->creator_; }
163 
164  inline void setScene(const StrongOrWeakPtr<Scene>& scene, uint32_t sceneID) { this->scene_ = scene; this->sceneID_=sceneID; }
165  inline Scene* getScene() const { return this->scene_.get(); }
166  virtual inline uint32_t getSceneID() const { return this->sceneID_; }
167 
168  inline void setGametype(const StrongOrWeakPtr<Gametype>& gametype) { this->gametype_ = gametype; }
169  inline Gametype* getGametype() const { return this->gametype_.get(); }
170 
171  inline void setLevel(const StrongOrWeakPtr<Level>& level) { this->level_ = level; }
172  inline Level* getLevel() const { return this->level_.get(); }
173 
174  void addEventSource(BaseObject* source, const std::string& state);
175  void removeEventSource(BaseObject* source);
176  BaseObject* getEventSource(unsigned int index, const std::string& state) const;
177 
178  void addEventListener(BaseObject* listener);
179  BaseObject* getEventListener(unsigned int index) const;
180 
181  void fireEvent(const std::string& name = "");
182  void fireEvent(bool activate, const std::string& name = "");
183  void fireEvent(bool activate, BaseObject* originator, const std::string& name = "");
184  void fireEvent(Event& event);
185 
186  virtual void processEvent(Event& event);
187 
189  inline void setLoaderIndentation(const std::string& indentation) { this->loaderIndentation_ = indentation; }
191  inline const std::string& getLoaderIndentation() const { return this->loaderIndentation_; }
192 
193  static void loadAllEventStates(Element& xmlelement, XMLPort::Mode mode, BaseObject* object, Identifier* identifier);
194 
195 
196  protected:
197  void addEventState(const std::string& name, EventState* container);
198  EventState* getEventState(const std::string& name) const;
199 
206  std::set<std::string> networkTemplateNames_;
207 
208  private:
210  void registerEventListener(BaseObject* object);
212  inline void unregisterEventListener(BaseObject* object)
213  { this->eventListeners_.erase(object); }
214 
215  void setXMLName(const std::string& name);
216  const std::string& getSingleTemplate(void) const;
217  Template* getTemplate(unsigned int index) const;
218  void registerEventStates();
219 
221  const XMLFile* file_;
223  std::map<std::string, std::string> xmlAttributes_;
228  uint32_t sceneID_;
231  std::set<Template*> templates_;
232 
233  std::map<BaseObject*, std::string> eventSources_;
234  std::set<BaseObject*> eventListeners_;
235  std::set<BaseObject*> eventListenersXML_;
236  std::map<std::string, EventState*> eventStates_;
238  };
239 
240  SUPER_FUNCTION(0, BaseObject, XMLPort, false);
241  SUPER_FUNCTION(2, BaseObject, changedActivity, false);
242  SUPER_FUNCTION(3, BaseObject, changedVisibility, false);
243  SUPER_FUNCTION(4, BaseObject, XMLEventPort, false);
244  SUPER_FUNCTION(8, BaseObject, changedName, false);
245 
246  template <class T>
248  {
249  }
250 
251  template <class T>
253  {
254  }
255 
256  template <class T>
258  {
259  }
260 
261  template <class T>
263  {
264  if (this->strongPtr_)
265  return this->strongPtr_;
266  else if (this->weakPtr_)
267  return this->weakPtr_;
268  else
269  return nullptr;
270  }
271 
272  template <class T>
274  {
275  if (this->strongPtr_)
276  return this->strongPtr_; // creates a copy
277  else
278  return this->weakPtr_; // converts automatically to StrongPtr
279  }
280 }
281 
282 #endif /* _BaseObject_H__ */
The BaseObject is the parent of all classes representing an instance in the game. ...
Definition: BaseObject.h:63
std::set< BaseObject * > eventListenersXML_
List of objects which listen to the events of this object through the "eventlisteners" subsection in ...
Definition: BaseObject.h:235
std::shared_ptr< Functor > FunctorPtr
Definition: FunctorPtr.h:57
virtual uint32_t getSceneID() const
Definition: BaseObject.h:166
void source(const std::string &filename)
Reads the content of a file and executes the commands in it line by line.
Definition: ConsoleCommandCompilation.cc:167
StrongOrWeakPtr< Namespace > namespace_
Definition: BaseObject.h:225
const std::string & getOldName() const
Returns the old name of the object.
Definition: BaseObject.h:98
bool bRegisteredEventStates_
Becomes true after the object registered its event states (with XMLEventPort)
Definition: BaseObject.h:237
Definition: Template.h:50
std::set< std::string > networkTemplateNames_
Definition: BaseObject.h:206
The EventState contains information about an event state.
Definition: Event.h:77
T * get() const
Definition: BaseObject.h:262
StrongOrWeakPtr()
Definition: BaseObject.h:247
std::string oldName_
The old name of the object.
Definition: BaseObject.h:201
Scene * getScene() const
Definition: BaseObject.h:165
This is the class from which all objects of the game-logic (not the engine) are derived from...
Definition: OrxonoxClass.h:53
const std::set< Template * > & getTemplates() const
Returns the set of all aplied templates.
Definition: BaseObject.h:155
Shared library macros, enums, constants and forward declarations for the core library ...
::std::string string
Definition: gtest-port.h:756
Structure to describe a single event.
Definition: ScriptController.h:42
void setCreator(BaseObject *creator)
Definition: BaseObject.h:161
A strong pointer which wraps a pointer to an object and keeps this object alive as long as the strong...
Definition: CorePrereqs.h:227
void setVisible(bool bVisible)
Sets the state of the objects visibility.
Definition: BaseObject.h:117
bool isInitialized() const
Returns if the object was initialized (passed the object registration).
Definition: BaseObject.h:91
BaseObject * creator_
Definition: BaseObject.h:226
std::string mainStateName_
Definition: BaseObject.h:204
void setNamespace(const StrongOrWeakPtr< Namespace > &ns)
Definition: BaseObject.h:158
std::map< BaseObject *, std::string > eventSources_
List of objects which send events to this object, mapped to the state which they affect.
Definition: BaseObject.h:233
std::set< Template * > templates_
Definition: BaseObject.h:231
WeakPtr wraps a pointer to an object, which becomes nullptr if the object is deleted.
Definition: CorePrereqs.h:236
void setFile(const XMLFile *file)
Sets a pointer to the xml file that loaded this object.
Definition: BaseObject.h:147
StrongPtr< T > createStrongPtr() const
Definition: BaseObject.h:273
BaseObject * getCreator() const
Definition: BaseObject.h:162
Definition: BaseObject.h:69
void setScene(const StrongOrWeakPtr< Scene > &scene, uint32_t sceneID)
Definition: BaseObject.h:164
SUPER_FUNCTION(0, BaseObject, XMLPort, false)
std::string loaderIndentation_
Indentation of the debug output in the Loader.
Definition: BaseObject.h:224
void setMainStateName(const std::string &name)
Sets the name of the main state (used for event reactions).
Definition: BaseObject.h:133
xmlelement
Definition: Super.h:519
void setName(const std::string &name)
Sets the name of the object.
Definition: BaseObject.h:94
FunctorPtr mainStateFunctor_
Definition: BaseObject.h:205
mbool bVisible_
True = the object is visible.
Definition: BaseObject.h:203
Gametype * getGametype() const
Definition: BaseObject.h:169
StrongOrWeakPtr< Gametype > gametype_
Definition: BaseObject.h:229
Definition: Namespace.h:46
Definition: Level.h:46
const std::string & getName() const
Returns the name of the object.
Definition: BaseObject.h:96
mbool is a small helper class that acts like a bool, but keeps track of the number of its state chang...
Definition: mbool.h:58
StrongOrWeakPtr< Level > level_
Definition: BaseObject.h:230
The Event struct contains information about a fired Event.
Definition: Event.h:51
const mbool & isVisible() const
Returns the state of the objects visibility.
Definition: BaseObject.h:126
std::map< std::string, std::string > xmlAttributes_
Lowercase XML attributes.
Definition: BaseObject.h:223
std::string name_
The name of the object.
Definition: BaseObject.h:200
const std::string & getMainStateName() const
Returns the name of the main state.
Definition: BaseObject.h:142
WeakPtr< T > weakPtr_
Definition: BaseObject.h:81
const mbool & isActive() const
Returns the state of the objects activity.
Definition: BaseObject.h:112
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
void setLoaderIndentation(const std::string &indentation)
Sets the indentation of the debug output in the Loader.
Definition: BaseObject.h:189
void setGametype(const StrongOrWeakPtr< Gametype > &gametype)
Definition: BaseObject.h:168
#define _CoreExport
Definition: CorePrereqs.h:61
Mode
Definition: CorePrereqs.h:102
Level * getLevel() const
Definition: BaseObject.h:172
Definition: Gametype.h:63
The Identifier is used to identify the class of an object and to store information about the class...
Definition: Identifier.h:109
Declaration and implementation of the orxonox::mbool class.
Definition: InputPrereqs.h:78
void setActive(bool bActive)
Sets the state of the objects activity.
Definition: BaseObject.h:103
std::set< BaseObject * > eventListeners_
List of objects which listen to the events of this object.
Definition: BaseObject.h:234
virtual void changedName()
This function gets called if the name of the object changes.
Definition: BaseObject.h:100
std::map< std::string, EventState * > eventStates_
Maps the name of the event states to their helper objects.
Definition: BaseObject.h:236
Definition: Context.h:45
void unregisterEventListener(BaseObject *object)
Removes an event listener from this object.
Definition: BaseObject.h:212
virtual void changedActivity()
This function gets called if the activity of the object changes.
Definition: BaseObject.h:114
uint32_t sceneID_
Definition: BaseObject.h:228
Definition: Scene.h:54
Definition of StrongPtr<T>, wraps a pointer to an object and keeps it alive.
void setLevel(const StrongOrWeakPtr< Level > &level)
Definition: BaseObject.h:171
Namespace * getNamespace() const
Definition: BaseObject.h:159
bool bInitialized_
True if the object was initialized (passed the object registration)
Definition: BaseObject.h:220
const XMLFile * getFile() const
Returns a pointer to the XMLFile that loaded this object.
Definition: BaseObject.h:149
Definition: CorePrereqs.h:243
StrongPtr< T > strongPtr_
Definition: BaseObject.h:80
const std::string & getLoaderIndentation() const
Returns the indentation of the debug output in the Loader.
Definition: BaseObject.h:191
Definition of all super-function related macros, used to call functions of the base class...
mbool bActive_
True = the object is active.
Definition: BaseObject.h:202
internal::String name_
Definition: gtest.cc:2289
Element * lastLoadedXMLElement_
Non nullptr if the TinyXML attributes have already been copied to our own lowercase map...
Definition: BaseObject.h:222
virtual void changedVisibility()
This function gets called if the visibility of the object changes.
Definition: BaseObject.h:128
StrongOrWeakPtr< Scene > scene_
Definition: BaseObject.h:227
Declaration of OrxonoxClass, the base class of all objects in Orxonox.
Definition: XMLFile.h:44
const XMLFile * file_
The XMLFile that loaded this object.
Definition: BaseObject.h:221