Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup2012/src/libraries/core/Template.cc @ 9277

Last change on this file since 9277 was 8858, checked in by landauf, 13 years ago

merged output branch back to trunk.

Changes:

  • you have to include util/Output.h instead of util/Debug.h
  • COUT(x) is now called orxout(level)
  • output levels are now defined by an enum instead of numbers. see util/Output.h for the definition
  • it's possible to use output contexts with orxout(level, context). see util/Output.h for some common contexts. you can define more contexts
  • you must use 'endl' at the end of an output message, '\n' does not flush the message

Output levels:

  • instead of COUT(0) use orxout()
  • instead of COUT(1) use orxout(user_error) or orxout(internal_error)
  • instead of COUT(2) use orxout(user_warning) or orxout(internal_warning)
  • instead of COUT(3) use orxout(user_status/user_info) or orxout(internal_status/internal_info)
  • instead of COUT(4) use orxout(verbose)
  • instead of COUT(5) use orxout(verbose_more)
  • instead of COUT(6) use orxout(verbose_ultra)

Guidelines:

  • user_* levels are for the user, visible in the console and the log-file
  • internal_* levels are for developers, visible in the log-file
  • verbose_* levels are for debugging, only visible if the context of the output is activated

Usage in C++:

  • orxout() << "message" << endl;
  • orxout(level) << "message" << endl;
  • orxout(level, context) << "message" << endl;

Usage in Lua:

  • orxout("message")
  • orxout(orxonox.level.levelname, "message")
  • orxout(orxonox.level.levelname, "context", "message")

Usage in Tcl (and in the in-game-console):

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