Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/BaseObject.h @ 10298

Last change on this file since 10298 was 10298, checked in by landauf, 9 years ago

fixed potential crash during level-unloading: Namespace is a context and is thus potentially used by other objects → store a smartPtr on the namespace object

  • Property svn:eol-style set to native
File size: 11.6 KB
RevLine 
[1505]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/**
[7401]30    @defgroup BaseObject BaseObject
31    @ingroup Core
32*/
33
34/**
[2171]35    @file
[7401]36    @ingroup BaseObject
37    @brief Declaration of BaseObject, the base class of all objects in Orxonox.
[1505]38
39    The BaseObject is the parent of all classes representing an instance in the game.
40*/
41
42#ifndef _BaseObject_H__
43#define _BaseObject_H__
44
45#include "CorePrereqs.h"
[5781]46
47#include <map>
48#include <list>
49
50#include "util/mbool.h"
[9667]51#include "class/OrxonoxClass.h"
52#include "class/Super.h"
53#include "object/SmartPtr.h"
[1505]54
55namespace orxonox
56{
[5781]57    class Scene;
58    class Gametype;
[7163]59    class Level;
[5781]60
[7401]61    /// The BaseObject is the parent of all classes representing an instance in the game.
[9667]62    class _CoreExport BaseObject : public OrxonoxClass
[1505]63    {
[5781]64        template <class T> friend class XMLPortClassParamContainer;
65
[1505]66        public:
[9667]67            BaseObject(Context* context);
[1505]68            virtual ~BaseObject();
[5781]69            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
[5929]70            virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
[1505]71
[1558]72            /** @brief Returns if the object was initialized (passed the object registration). @return True was the object is initialized */
73            inline bool isInitialized() const { return this->bInitialized_; }
74
[5781]75            /** @brief Sets the name of the object. @param name The name */
76            inline void setName(const std::string& name) { this->oldName_ = this->name_; this->name_ = name; this->changedName(); }
77            /** @brief Returns the name of the object. */
78            inline const std::string& getName() const { return this->name_; }
79            /** @brief Returns the old name of the object. */
80            inline const std::string& getOldName() const { return this->oldName_; }
81            /** @brief This function gets called if the name of the object changes. */
82            virtual void changedName() {}
83
84            /** @brief Sets the state of the objects activity. @param bActive True = active */
85            inline void setActive(bool bActive)
86            {
87                if (this->bActive_ != bActive)
88                {
89                    this->bActive_ = bActive;
90                    this->changedActivity();
91                }
92            }
93            /** @brief Returns the state of the objects activity. @return The state of the activity */
94            inline const mbool& isActive() const { return this->bActive_; }
95            /** @brief This function gets called if the activity of the object changes. */
96            virtual void changedActivity() {}
97
98            /** @brief Sets the state of the objects visibility. @param bVisible True = visible */
99            inline void setVisible(bool bVisible)
100            {
101                if (this->bVisible_ != bVisible)
102                {
103                    this->bVisible_ = bVisible;
104                    this->changedVisibility();
105                }
106            }
107            /** @brief Returns the state of the objects visibility. @return The state of the visibility */
108            inline const mbool& isVisible() const { return this->bVisible_; }
109            /** @brief This function gets called if the visibility of the object changes. */
110            virtual void changedVisibility() {}
111
112            void setMainState(bool state);
113
[5929]114            /** @brief Sets the name of the main state (used for event reactions). */
115            void setMainStateName(const std::string& name)
116            {
117                if (this->mainStateName_ != name)
118                {
119                    this->mainStateName_ = name;
120                    this->changedMainStateName();
121                }
122            }
123            /** @brief Returns the name of the main state. */
[5781]124            inline const std::string& getMainStateName() const { return this->mainStateName_; }
[5929]125            /** @brief This function gets called if the main state name of the object changes. */
126            virtual void changedMainStateName();
[5781]127
128            /** @brief Sets a pointer to the xml file that loaded this object. @param file The pointer to the XMLFile */
129            inline void setFile(const XMLFile* file) { this->file_ = file; }
130            /** @brief Returns a pointer to the XMLFile that loaded this object. @return The XMLFile */
131            inline const XMLFile* getFile() const { return this->file_; }
132            const std::string& getFilename() const;
133
134            void addTemplate(const std::string& name);
135            void addTemplate(Template* temp);
136            /** @brief Returns the set of all aplied templates. */
137            inline const std::set<Template*>& getTemplates() const
138                { return this->templates_; }
139
[10298]140            inline void setNamespace(const SmartPtr<Namespace>& ns) { this->namespace_ = ns; }
141            inline const SmartPtr<Namespace>& getNamespace() const { return this->namespace_; }
[5781]142
[2087]143            inline void setCreator(BaseObject* creator) { this->creator_ = creator; }
144            inline BaseObject* getCreator() const { return this->creator_; }
145
[5929]146            inline void setScene(const SmartPtr<Scene>& scene, uint32_t sceneID) { this->scene_ = scene; this->sceneID_=sceneID; }
147            inline const SmartPtr<Scene>& getScene() const { return this->scene_; }
148            inline virtual uint32_t getSceneID() const { return this->sceneID_; }
[5781]149
[5929]150            inline void setGametype(const SmartPtr<Gametype>& gametype)
[5781]151            {
152                if (gametype != this->gametype_)
153                {
154                    this->oldGametype_ = this->gametype_;
155                    this->gametype_ = gametype;
156                    this->changedGametype();
157                }
158            }
[5929]159            inline const SmartPtr<Gametype>& getGametype() const { return this->gametype_; }
[5781]160            inline Gametype* getOldGametype() const { return this->oldGametype_; }
161            virtual void changedGametype() {}
162
[7163]163            inline void setLevel(const SmartPtr<Level>& level)
164            {
165                if (level != this->level_)
166                {
167                    this->level_ = level;
168                    this->changedLevel();
169                }
170            }
171            inline const SmartPtr<Level>& getLevel() const { return this->level_; }
172            virtual void changedLevel() {}
173
[5929]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;
[6417]177
[5929]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 = "");
[5781]184            void fireEvent(Event& event);
185
186            virtual void processEvent(Event& event);
187
188            /** @brief Sets the indentation of the debug output in the Loader. @param indentation The indentation */
189            inline void setLoaderIndentation(const std::string& indentation) { this->loaderIndentation_ = indentation; }
190            /** @brief Returns the indentation of the debug output in the Loader. @return The indentation */
191            inline const std::string& getLoaderIndentation() const { return this->loaderIndentation_; }
[6417]192
[5929]193            static void loadAllEventStates(Element& xmlelement, XMLPort::Mode mode, BaseObject* object, Identifier* identifier);
[5781]194
195        protected:
[5929]196            void addEventState(const std::string& name, EventState* container);
197            EventState* getEventState(const std::string& name) const;
198
[7163]199            std::string             name_;                     //!< The name of the object
200            std::string             oldName_;                  //!< The old name of the object
201            mbool                   bActive_;                  //!< True = the object is active
202            mbool                   bVisible_;                 //!< True = the object is visible
203            std::string             mainStateName_;
[7284]204            FunctorPtr              mainStateFunctor_;
[7163]205            std::set<std::string>   networkTemplateNames_;
[5781]206
[2087]207        private:
[5929]208            /** @brief Adds an object which listens to the events of this object. */
[6800]209            void registerEventListener(BaseObject* object);
[5929]210            /** @brief Removes an event listener from this object. */
211            inline void unregisterEventListener(BaseObject* object)
212                { this->eventListeners_.erase(object); }
213
[5781]214            void setXMLName(const std::string& name);
[6524]215            const std::string& getSingleTemplate(void) const;
[5781]216            Template* getTemplate(unsigned int index) const;
[5929]217            void registerEventStates();
[5781]218
[2662]219            bool                   bInitialized_;              //!< True if the object was initialized (passed the object registration)
[5781]220            const XMLFile*         file_;                      //!< The XMLFile that loaded this object
221            Element*               lastLoadedXMLElement_;      //!< Non 0 if the TinyXML attributes have already been copied to our own lowercase map
222            std::map<std::string, std::string> xmlAttributes_; //!< Lowercase XML attributes
223            std::string            loaderIndentation_;         //!< Indentation of the debug output in the Loader
[10298]224            SmartPtr<Namespace>    namespace_;
[2662]225            BaseObject*            creator_;
[5929]226            SmartPtr<Scene>        scene_;
227            uint32_t               sceneID_;
228            SmartPtr<Gametype>     gametype_;
[5781]229            Gametype*              oldGametype_;
[7163]230            SmartPtr<Level>        level_;
[5781]231            std::set<Template*>    templates_;
[6417]232
[5929]233            std::map<BaseObject*, std::string>  eventSources_;           //!< List of objects which send events to this object, mapped to the state which they affect
234            std::set<BaseObject*>               eventListeners_;         //!< List of objects which listen to the events of this object
235            std::set<BaseObject*>               eventListenersXML_;      //!< List of objects which listen to the events of this object through the "eventlisteners" subsection in XML
236            std::map<std::string, EventState*>  eventStates_;            //!< Maps the name of the event states to their helper objects
237            bool                                bRegisteredEventStates_; //!< Becomes true after the object registered its event states (with XMLEventPort)
[1505]238    };
[5781]239
240    SUPER_FUNCTION(0, BaseObject, XMLPort, false);
241    SUPER_FUNCTION(2, BaseObject, changedActivity, false);
242    SUPER_FUNCTION(3, BaseObject, changedVisibility, false);
[5929]243    SUPER_FUNCTION(4, BaseObject, XMLEventPort, false);
244    SUPER_FUNCTION(8, BaseObject, changedName, false);
245    SUPER_FUNCTION(9, BaseObject, changedGametype, false);
[1505]246}
247
248#endif /* _BaseObject_H__ */
Note: See TracBrowser for help on using the repository browser.