/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx 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, or (at your option) any later version. ### File Specific: main-programmer: Christian Meyer co-programmer: Benjamin Grauer */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING #include "util/loading/factory.h" #include "debug.h" //#include "shell_command.h" ObjectListDefinition(Factory); //SHELL_COMMAND(create, Factory, fabricate); /** * @brief constructor * * set everything to zero and define factoryName */ Factory::Factory (const ClassID& classID) : _classID(classID) { PRINTF(4)("Factory::create(%s::%d)\n", classID.name().c_str(), classID.id()); //this->registerObject(this, Factory::_objectList); this->setName(classID.name()); if( Factory::_factoryList == NULL) Factory::_factoryList = new std::list; Factory::_factoryList->push_back(this); } /** @brief a reference to the First Factory */ std::list* Factory::_factoryList = NULL; /** * @brief destructor */ Factory::~Factory () { // printf("%s\n", this->factoryName); // Factory* tmpDel = this->next; // this->next = NULL; } /** * @brief deletes all the Factories. (cleanup) */ void Factory::deleteFactories() { if (Factory::_factoryList != NULL) { while(!Factory::_factoryList->empty()) { delete Factory::_factoryList->front(); Factory::_factoryList->pop_front(); } delete Factory::_factoryList; Factory::_factoryList = NULL; } } /** * @param classID match a classID with this classID * @returns true on match, false otherwise */ bool Factory::operator==(int classID) const { return (this->_classID == classID); } /** * @brief Compares the Factories Name against a given ClassName * @param className the Name of the Class to Query * @returns true on match, false otherwise. */ bool Factory::operator==(const std::string& className) const { return (this->_classID.name() == className); } /** * @brief Creates a new Object of type root->Value() (name) * @param root the XML-Root to match for the newly created Object * @returns a new Object of Type root->Value() on match, NULL otherwise */ BaseObject* Factory::fabricate(const TiXmlElement* root) { assert (Factory::_factoryList != NULL); std::list::const_iterator factory; for (factory = Factory::_factoryList->begin(); factory != Factory::_factoryList->end(); factory++) if (*(*factory) == root->Value()) { PRINTF(2)("Create a new Object of type %s\n", (*factory)->getCName()); return (*factory)->fabricateObject(root); } PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value()); return NULL; } /** * @brief Creates a new Object of type className * @param className the ClassName to match for the newly created Object * @returns a new Object of Type className on match, NULL otherwise */ BaseObject* Factory::fabricate(const std::string& className) { if (Factory::_factoryList == NULL) return NULL; std::list::const_iterator factory; for (factory = Factory::_factoryList->begin(); factory != Factory::_factoryList->end(); factory++) if (*(*factory) == className) { PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName()); return (*factory)->fabricateObject(NULL); } PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className.c_str()); return NULL; } /** * @brief Creates a new Object of type classID * @param classID the ClassID to match for the newly created Object * @returns a new Object of Type classID on match, NULL otherwise */ BaseObject* Factory::fabricate(const ClassID& classID) { if (Factory::_factoryList == NULL) return NULL; std::list::const_iterator factory; for (factory = Factory::_factoryList->begin(); factory != Factory::_factoryList->end(); factory++) if (*(*factory) == classID) { PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName()); return (*factory)->fabricateObject(NULL); } PRINTF(2)("Could not Fabricate an Object of ClassID '%d'\n", classID.id()); return NULL; }