/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Fabian 'x3n' Landau * Co-authors: * ... * */ #include "OrxonoxStableHeaders.h" #include "Template.h" #include "core/CoreIncludes.h" #include "core/XMLPort.h" #include "util/Debug.h" namespace orxonox { CreateFactory(Template); Template::Template() { RegisterObject(Template); this->bIsLink_ = false; this->bIsReturningXMLElement_ = false; this->baseclassIdentifier_ = 0; } Template::~Template() { } void Template::XMLPort(Element& xmlelement, XMLPort::Mode mode) { SUPER(Template, XMLPort, xmlelement, mode); XMLPortParam(Template, "link", setLink, getLink, xmlelement, mode); XMLPortParam(Template, "baseclass", setBaseclass, getBaseclass, xmlelement, mode); this->setXMLElement(*xmlelement.FirstChildElement(false)); } const Element& Template::getXMLElement() const { if (this->bIsLink_) { Template* temp = Template::getTemplate(this->link_); if (temp) { if (!temp->bIsReturningXMLElement_) { this->bIsReturningXMLElement_ = true; const Element& element = temp->getXMLElement(); this->bIsReturningXMLElement_ = false; return element; } else { COUT(2) << "Warning: Linking from " << this->getName() << " to " << this->link_ << " leads to an infinite loop. Returning own element." << std::endl; } } else { COUT(2) << "Warning: " << this->link_ << " is not an existing Template name. Returning own element." << std::endl; } } return this->xmlelement_; } void Template::setBaseclass(const std::string& baseclass) { this->baseclassIdentifier_ = ClassByString(baseclass); if (this->baseclassIdentifier_) this->baseclass_ = baseclass; } void Template::applyOn(BaseObject* object) { if (this->baseclassIdentifier_) { if (!object->isA(this->baseclassIdentifier_)) { 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; return; } } COUT(4) << object->getLoaderIndentation() << " aplying Template \"" << this->getName() << "\"..." << std::endl; Element temp = this->getXMLElement(); object->XMLPort(temp, XMLPort::LoadObject); } std::map& Template::getTemplateMap() { static std::map templateMap; return templateMap; } Template* Template::getTemplate(const std::string& name) { std::map::iterator it = Template::getTemplateMap().find(name); if (it != Template::getTemplateMap().end()) return it->second; else return 0; } void Template::apply(const std::string& name, BaseObject* object) { std::map::iterator it = Template::getTemplateMap().find(name); if (it != Template::getTemplateMap().end()) it->second->applyOn(object); } }