/* 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 "factory.h" //#include "shell_command.h" using namespace std; //SHELL_COMMAND(create, Factory, fabricate); /* -------------------------------------------------- * Factory * -------------------------------------------------- */ /** * constructor set everything to zero and define factoryName */ Factory::Factory (const char* factoryName, ClassID classID) { this->setClassID(CL_FACTORY, "Factory"); this->setName(factoryName); this->classID = classID; this->className = factoryName; if( Factory::factoryList == NULL) Factory::factoryList = new std::list; Factory::factoryList->push_back(this); } /** a reference to the First Factory */ std::list* Factory::factoryList = NULL; /** * destructor * * clear the Q */ Factory::~Factory () { // printf("%s\n", this->factoryName); // Factory* tmpDel = this->next; // this->next = NULL; } 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; } } /** * 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 char* className) const { return(className != NULL && !strcmp(className, this->className)); } BaseObject* Factory::fabricate(const TiXmlElement* root) { if (root == NULL || Factory::factoryList == NULL) return NULL; std::list::const_iterator factory; for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++) if (*(*factory) == root->Value()) break; if (factory != Factory::factoryList->end()) { PRINTF(4)("Create a new Object of type %s\n", (*factory)->getName()); return (*factory)->fabricateObject(root); } else { PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value()); return NULL; } } BaseObject* Factory::fabricate(const char* className) { if (className == NULL || Factory::factoryList == NULL) return NULL; std::list::const_iterator factory; for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++) if (*(*factory) == className) break; if (factory != Factory::factoryList->end()) { PRINTF(4)("Create a new Object of type %s\n", (*factory)->getName()); return (*factory)->fabricateObject(NULL); } else { PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className); return NULL; } } BaseObject* Factory::fabricate(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) break; if (factory != Factory::factoryList->end()) { PRINTF(4)("Create a new Object of type %s\n", (*factory)->getName()); return (*factory)->fabricateObject(NULL); } else { PRINTF(2)("Could not Fabricate an Object of ClassID '%h'\n", classID); return NULL; } }