Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/core/Template.cc @ 3154

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

Tried to reduce dependencies in the core. There wasn't much to achieve though…

  • Property svn:eol-style set to native
File size: 5.9 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#include "Template.h"
30
31#include <tinyxml/tinyxml.h>
32#include <tinyxml/ticpp.h>
33
34#include "util/Debug.h"
35#include "core/CoreIncludes.h"
36#include "core/XMLPort.h"
37
38namespace orxonox
39{
40    CreateFactory(Template);
41
42    Template::Template(BaseObject* creator) : BaseObject(creator)
43    {
44        RegisterObject(Template);
45
46        this->xmlelement_ = new TiXmlElement("");
47        this->bIsLink_ = false;
48        this->bLoadDefaults_ = true;
49        this->bIsReturningXMLElement_ = false;
50        this->baseclassIdentifier_ = 0;
51    }
52
53    Template::~Template()
54    {
55        Template::getTemplateMap().erase(this->getName());
56        delete this->xmlelement_;
57    }
58
59    void Template::XMLPort(Element& xmlelement, XMLPort::Mode mode)
60    {
61        SUPER(Template, XMLPort, xmlelement, mode);
62
63        XMLPortParam(Template, "link",      setLink,         getLink,         xmlelement, mode);
64        XMLPortParam(Template, "baseclass", setBaseclass,    getBaseclass,    xmlelement, mode);
65        XMLPortParam(Template, "defaults",  setLoadDefaults, getLoadDefaults, xmlelement, mode).defaultValues(true);
66
67        Element* element = xmlelement.FirstChildElement(false);
68        if (element)
69        {
70            TiXmlElement* tixmlelement = static_cast<TiXmlElement*>(element->GetTiXmlPointer());
71            if (tixmlelement)
72                this->setXMLElement(*tixmlelement);
73        }
74    }
75
76    void Template::changedName()
77    {
78        SUPER(Template, changedName);
79
80        if (this->getName() != "")
81        {
82            std::map<std::string, Template*>::iterator it;
83            it = Template::getTemplateMap().find(this->getOldName());
84            if (it != Template::getTemplateMap().end())
85                Template::getTemplateMap().erase(it);
86
87            it = Template::getTemplateMap().find(this->getName());
88            if (it != Template::getTemplateMap().end())
89                COUT(2) << "Warning: Template with name \"" << this->getName() << "\" already exists." << std::endl;
90            else
91                Template::getTemplateMap()[this->getName()] = this;
92        }
93    }
94
95    void Template::setXMLElement(const TiXmlElement& xmlelement)
96    {
97        *this->xmlelement_ = xmlelement;
98    }
99
100    const TiXmlElement& Template::getXMLElement() const
101    {
102        if (this->bIsLink_)
103        {
104            Template* temp = Template::getTemplate(this->link_);
105            if (temp)
106            {
107                if (!temp->bIsReturningXMLElement_)
108                {
109                    this->bIsReturningXMLElement_ = true;
110                    const TiXmlElement& element = temp->getXMLElement();
111                    this->bIsReturningXMLElement_ = false;
112                    return element;
113                }
114                else
115                {
116                    COUT(2) << "Warning: Linking from " << this->getName() << " to " << this->link_ << " leads to an infinite loop. Returning own element." << std::endl;
117                }
118            }
119            else
120            {
121                COUT(2) << "Warning: " << this->link_ << " is not an existing Template name. Returning own element." << std::endl;
122            }
123        }
124
125        return *this->xmlelement_;
126    }
127
128    void Template::setBaseclass(const std::string& baseclass)
129    {
130        this->baseclassIdentifier_ = ClassByString(baseclass);
131        if (this->baseclassIdentifier_)
132            this->baseclass_ = baseclass;
133    }
134
135    void Template::applyOn(BaseObject* object)
136    {
137        if (this->baseclassIdentifier_)
138        {
139            if (!object->isA(this->baseclassIdentifier_))
140            {
141                COUT(1) << "Error: Can't apply template (name: " << this->getName() << "), object (name: " << object->getName() << ", class: " << object->getIdentifier()->getName() << ") is not a " << this->baseclassIdentifier_->getName() << std::endl;
142                return;
143            }
144        }
145
146        COUT(4) << object->getLoaderIndentation() << " aplying Template \"" << this->getName() << "\"..." << std::endl;
147
148        Element temp = &const_cast<TiXmlElement&>(this->getXMLElement());
149
150        if (this->bLoadDefaults_)
151            object->XMLPort(temp, XMLPort::LoadObject);
152        else
153            object->XMLPort(temp, XMLPort::ExpandObject);
154    }
155
156    std::map<std::string, Template*>& Template::getTemplateMap()
157    {
158        static std::map<std::string, Template*> templateMap;
159        return templateMap;
160    }
161
162    Template* Template::getTemplate(const std::string& name)
163    {
164        std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name);
165        if (it != Template::getTemplateMap().end())
166            return it->second;
167        else
168        {
169            COUT(2) << "Warning: Template with name " << name << " doesn't exist." << std::endl;
170            return 0;
171        }
172    }
173
174    void Template::apply(const std::string& name, BaseObject* object)
175    {
176        std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name);
177        if (it != Template::getTemplateMap().end())
178            it->second->applyOn(object);
179    }
180}
Note: See TracBrowser for help on using the repository browser.