Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/Presentation_HS16/src/libraries/core/BaseObject.h

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

merged branch cpp11_v3 back to trunk

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