Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/libraries/core/BaseObject.cc @ 5829

Last change on this file since 5829 was 5821, checked in by rgrieder, 15 years ago

Fixed 3 memory leaks (one of them was a true even a true one ;))

  • XMLPortObjectContainers as XMLPort event containers were not deleted in the identifier
  • EventContainers were not deleted in the BaseObject
  • ClassTreeMask clean() operation only erased redundant subnodes instead of deleting them as well
  • Property svn:eol-style set to native
File size: 11.5 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    @file
31    @brief Implementation of the BaseObject class.
32*/
33
34#include "BaseObject.h"
35
36#include <tinyxml/tinyxml.h>
37
38#include "util/StringUtils.h"
39#include "CoreIncludes.h"
40#include "Event.h"
41#include "EventIncludes.h"
42#include "Functor.h"
43#include "Iterator.h"
44#include "Template.h"
45#include "XMLFile.h"
46#include "XMLNameListener.h"
47#include "XMLPort.h"
48
49namespace orxonox
50{
51    CreateFactory(BaseObject);
52
53    /**
54        @brief Constructor: Registers the object in the BaseObject-list.
55    */
56    BaseObject::BaseObject(BaseObject* creator) : bInitialized_(false)
57    {
58        RegisterRootObject(BaseObject);
59
60        this->bInitialized_ = true;
61
62        this->bActive_ = true;
63        this->bVisible_ = true;
64        this->oldGametype_ = 0;
65
66        this->lastLoadedXMLElement_ = 0;
67
68        this->functorSetMainState_ = 0;
69        this->functorGetMainState_ = 0;
70
71        this->setCreator(creator);
72        if (this->creator_)
73        {
74            this->setFile(this->creator_->getFile());
75            this->setNamespace(this->creator_->getNamespace());
76            this->setScene(this->creator_->getScene());
77            this->setGametype(this->creator_->getGametype());
78        }
79        else
80        {
81            this->file_ = 0;
82            this->namespace_ = 0;
83            this->scene_ = 0;
84            this->gametype_ = 0;
85        }
86    }
87
88    /**
89        @brief Destructor
90    */
91    BaseObject::~BaseObject()
92    {
93        if (this->isInitialized())
94        {
95            for (std::list<BaseObject*>::const_iterator it = this->events_.begin(); it != this->events_.end(); ++it)
96                (*it)->unregisterEventListener(this);
97
98            for (std::map<BaseObject*, std::string>::const_iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
99                it->first->removeEvent(this);
100
101            for (std::map<std::string, EventContainer*>::const_iterator it = this->eventContainers_.begin(); it != this->eventContainers_.end(); ++it)
102                delete it->second;
103
104            if (this->functorSetMainState_)
105                delete this->functorSetMainState_;
106            if (this->functorGetMainState_)
107                delete this->functorGetMainState_;
108        }
109    }
110
111    /**
112        @brief XML loading and saving.
113        @param xmlelement The XML-element
114        @param loading Loading (true) or saving (false)
115        @return The XML-element
116    */
117    void BaseObject::XMLPort(Element& xmlelement, XMLPort::Mode mode)
118    {
119        XMLPortParam(BaseObject, "name", setXMLName, getName, xmlelement, mode);
120        XMLPortParam(BaseObject, "visible", setVisible, isVisible, xmlelement, mode);
121        XMLPortParam(BaseObject, "active", setActive, isActive, xmlelement, mode);
122        XMLPortParam(BaseObject, "mainstate", setMainStateName, getMainStateName, xmlelement, mode);
123
124        XMLPortObjectTemplate(BaseObject, Template, "templates", addTemplate, getTemplate, xmlelement, mode, Template*);
125
126        Element* events = xmlelement.FirstChildElement("events", false);
127
128        if (events)
129        {
130            std::list<std::string> eventnames;
131
132            if (mode == XMLPort::LoadObject || mode == XMLPort::ExpandObject)
133            {
134                for (ticpp::Iterator<ticpp::Element> child = events->FirstChildElement(false); child != child.end(); child++)
135                    eventnames.push_back(child->Value());
136            }
137            else if (mode == XMLPort::SaveObject)
138            {
139                for (std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->getIdentifier()->getXMLPortEventMapBegin(); it != this->getIdentifier()->getXMLPortEventMapEnd(); ++it)
140                    eventnames.push_back(it->first);
141            }
142
143            for (std::list<std::string>::iterator it = eventnames.begin(); it != eventnames.end(); ++it)
144            {
145                std::string sectionname = (*it);
146                ExecutorMember<BaseObject>* loadexecutor = createExecutor(createFunctor(&BaseObject::addEvent), std::string( "BaseObject" ) + "::" + "addEvent");
147                ExecutorMember<BaseObject>* saveexecutor = createExecutor(createFunctor(&BaseObject::getEvent), std::string( "BaseObject" ) + "::" + "getEvent");
148                loadexecutor->setDefaultValue(1, sectionname);
149
150                XMLPortClassObjectContainer<BaseObject, BaseObject>* container = 0;
151                container = static_cast<XMLPortClassObjectContainer<BaseObject, BaseObject>*>(this->getIdentifier()->getXMLPortEventContainer(sectionname));
152                if (!container)
153                {
154                    container = new XMLPortClassObjectContainer<BaseObject, BaseObject>(sectionname, this->getIdentifier(), loadexecutor, saveexecutor, false, true);
155                    this->getIdentifier()->addXMLPortEventContainer(sectionname, container);
156                }
157                container->port(this, *events, mode);
158            }
159        }
160    }
161
162    /**
163        @brief Loads the name of the object through XML and calls all XMLNameListener.
164        @param name The name of the object
165    */
166    void BaseObject::setXMLName(const std::string& name)
167    {
168        this->setName(name);
169
170        for (ObjectList<XMLNameListener>::iterator it = ObjectList<XMLNameListener>::begin(); it != ObjectList<XMLNameListener>::end(); ++it)
171            it->loadedNewXMLName(this);
172    }
173
174    /**
175        @brief Returns the levelfile that loaded this object.
176        @return The levelfile
177    */
178    const std::string& BaseObject::getFilename() const
179    {
180        if (this->file_)
181            return this->file_->getFilename();
182        else
183            return BLANKSTRING;
184    }
185
186    /**
187        @brief Adds a Template to the object.
188        @param name The name of the Template
189    */
190    void BaseObject::addTemplate(const std::string& name)
191    {
192        Template* temp = Template::getTemplate(name);
193        if (temp)
194            this->addTemplate(temp);
195        else
196            COUT(1) << "Error: \"" << name << "\" is not a valid Template name (in class: " << this->getIdentifier()->getName() << ", name: " << this->getName() << ")." << std::endl;
197    }
198
199    /**
200        @brief Adds a Template to the object.
201        @param temp The Template
202    */
203    void BaseObject::addTemplate(Template* temp)
204    {
205        this->templates_.insert(temp);
206        temp->applyOn(this);
207    }
208
209    /**
210        @brief Returns the Template with the given index.
211        @param index The index
212    */
213    Template* BaseObject::getTemplate(unsigned int index) const
214    {
215        unsigned int i = 0;
216        for (std::set<Template*>::const_iterator it = this->templates_.begin(); it != this->templates_.end(); ++it)
217        {
218            if (i == index)
219                return (*it);
220            i++;
221        }
222        return 0;
223    }
224
225    void BaseObject::addEvent(BaseObject* event, const std::string& sectionname)
226    {
227        event->registerEventListener(this, sectionname);
228        this->events_.push_back(event);
229    }
230
231    void BaseObject::removeEvent(BaseObject* event)
232    {
233        this->events_.remove(event);
234    }
235
236    BaseObject* BaseObject::getEvent(unsigned int index) const
237    {
238        unsigned int i = 0;
239        for (std::list<BaseObject*>::const_iterator it = this->events_.begin(); it != this->events_.end(); ++it)
240        {
241            if (i == index)
242                return (*it);
243            ++i;
244        }
245        return 0;
246    }
247
248    void BaseObject::addEventContainer(const std::string& sectionname, EventContainer* container)
249    {
250        std::map<std::string, EventContainer*>::const_iterator it = this->eventContainers_.find(sectionname);
251        if (it != this->eventContainers_.end())
252        {
253            COUT(2) << "Warning: Overwriting EventContainer in class " << this->getIdentifier()->getName() << "." << std::endl;
254            delete (it->second);
255        }
256
257        this->eventContainers_[sectionname] = container;
258    }
259
260    EventContainer* BaseObject::getEventContainer(const std::string& sectionname) const
261    {
262        std::map<std::string, EventContainer*>::const_iterator it = this->eventContainers_.find(sectionname);
263        if (it != this->eventContainers_.end())
264            return ((*it).second);
265        else
266            return 0;
267    }
268
269    void BaseObject::fireEvent()
270    {
271        this->fireEvent(true);
272        this->fireEvent(false);
273    }
274
275    void BaseObject::fireEvent(bool activate)
276    {
277        this->fireEvent(activate, this);
278    }
279
280    void BaseObject::fireEvent(bool activate, BaseObject* originator)
281    {
282        Event event(activate, originator);
283
284        for (std::map<BaseObject*, std::string>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
285        {
286            event.sectionname_ = it->second;
287            it->first->processEvent(event);
288        }
289    }
290
291    void BaseObject::fireEvent(Event& event)
292    {
293        for (std::map<BaseObject*, std::string>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
294            it->first->processEvent(event);
295    }
296
297    void BaseObject::processEvent(Event& event)
298    {
299        ORXONOX_SET_EVENT(BaseObject, "activity", setActive, event);
300        ORXONOX_SET_EVENT(BaseObject, "visibility", setVisible, event);
301    }
302
303    void BaseObject::setMainStateName(const std::string& name)
304    {
305        if (this->mainStateName_ != name)
306        {
307            this->mainStateName_ = name;
308            if (this->functorSetMainState_)
309                delete this->functorSetMainState_;
310            if (this->functorGetMainState_)
311                delete this->functorGetMainState_;
312            this->changedMainState();
313            if (!this->functorSetMainState_)
314                COUT(2) << "Warning: \"" << name << "\" is not a valid MainState." << std::endl;
315        }
316    }
317
318    void BaseObject::setMainState(bool state)
319    {
320        if (this->functorSetMainState_)
321            (*this->functorSetMainState_)(state);
322        else
323            COUT(2) << "Warning: No MainState defined in object \"" << this->getName() << "\" (" << this->getIdentifier()->getName() << ")" << std::endl;
324    }
325
326    bool BaseObject::getMainState() const
327    {
328        if (this->functorGetMainState_)
329        {
330            (*this->functorGetMainState_)();
331            return this->functorGetMainState_->getReturnvalue();
332        }
333        else
334        {
335            COUT(2) << "Warning: No MainState defined in object \"" << this->getName() << "\" (" << this->getIdentifier()->getName() << ")" << std::endl;
336            return false;
337        }
338    }
339
340    void BaseObject::changedMainState()
341    {
342        SetMainState(BaseObject, "activity",   setActive,  isActive);
343        SetMainState(BaseObject, "visibility", setVisible, isVisible);
344    }
345}
Note: See TracBrowser for help on using the repository browser.