Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/Template.cc @ 1970

Last change on this file since 1970 was 1970, checked in by landauf, 15 years ago

added Template class

File size: 4.4 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 "OrxonoxStableHeaders.h"
30#include "Template.h"
31
32#include "core/CoreIncludes.h"
33#include "core/XMLPort.h"
34#include "util/Debug.h"
35
36namespace orxonox
37{
38    CreateFactory(Template);
39
40    Template::Template()
41    {
42        RegisterObject(Template);
43
44        this->bIsLink_ = false;
45        this->bIsReturningXMLElement_ = false;
46        this->baseclassIdentifier_ = 0;
47    }
48
49    Template::~Template()
50    {
51    }
52
53    void Template::XMLPort(Element& xmlelement, XMLPort::Mode mode)
54    {
55        SUPER(Template, XMLPort, xmlelement, mode);
56
57        XMLPortParam(Template, "link", setLink, getLink, xmlelement, mode);
58        XMLPortParam(Template, "baseclass", setBaseclass, getBaseclass, xmlelement, mode);
59
60        this->setXMLElement(*xmlelement.FirstChildElement(false));
61    }
62
63    const Element& Template::getXMLElement() const
64    {
65        if (this->bIsLink_)
66        {
67            Template* temp = Template::getTemplate(this->link_);
68            if (temp)
69            {
70                if (!temp->bIsReturningXMLElement_)
71                {
72                    this->bIsReturningXMLElement_ = true;
73                    const Element& element = temp->getXMLElement();
74                    this->bIsReturningXMLElement_ = false;
75                    return element;
76                }
77                else
78                {
79                    COUT(2) << "Warning: Linking from " << this->getName() << " to " << this->link_ << " leads to an infinite loop. Returning own element." << std::endl;
80                }
81            }
82            else
83            {
84                COUT(2) << "Warning: " << this->link_ << " is not an existing Template name. Returning own element." << std::endl;
85            }
86        }
87
88        return this->xmlelement_;
89    }
90
91    void Template::setBaseclass(const std::string& baseclass)
92    {
93        this->baseclassIdentifier_ = ClassByString(baseclass);
94        if (this->baseclassIdentifier_)
95            this->baseclass_ = baseclass;
96    }
97
98    void Template::applyOn(BaseObject* object)
99    {
100        if (this->baseclassIdentifier_)
101        {
102            if (!object->isA(this->baseclassIdentifier_))
103            {
104                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;
105                return;
106            }
107        }
108
109        COUT(4) << object->getLoaderIndentation() << " aplying Template \"" << this->getName() << "\"..." << std::endl;
110
111        Element temp = this->getXMLElement();
112        object->XMLPort(temp, XMLPort::LoadObject);
113    }
114
115    std::map<std::string, Template*>& Template::getTemplateMap()
116    {
117        static std::map<std::string, Template*> templateMap;
118        return templateMap;
119    }
120
121    Template* Template::getTemplate(const std::string& name)
122    {
123        std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name);
124        if (it != Template::getTemplateMap().end())
125            return it->second;
126        else
127            return 0;
128    }
129
130    void Template::apply(const std::string& name, BaseObject* object)
131    {
132        std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name);
133        if (it != Template::getTemplateMap().end())
134            it->second->applyOn(object);
135    }
136}
Note: See TracBrowser for help on using the repository browser.