Changeset 9869 in orxonox.OLD for trunk/src/lib/util/loading/factory.cc
- Timestamp:
- Oct 3, 2006, 12:19:30 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/util/loading/factory.cc
r9675 r9869 15 15 #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING 16 16 17 #include " util/loading/factory.h"17 #include "factory.h" 18 18 #include "debug.h" 19 19 //#include "shell_command.h" 20 20 21 21 ObjectListDefinition(Factory); 22 22 23 23 //SHELL_COMMAND(create, Factory, fabricate); … … 28 28 * set everything to zero and define factoryName 29 29 */ 30 Factory::Factory (const std::string& factoryName, ClassIDclassID)31 : classID(classID), className(factoryName)30 Factory::Factory (const ClassID& classID) 31 : _classID(classID) 32 32 { 33 this->setClassID(CL_FACTORY, "Factory"); 34 this->setName(factoryName); 33 PRINTF(4)("Factory::create(%s::%d)\n", classID.name().c_str(), classID.id()); 34 //this->registerObject(this, Factory::_objectList); 35 this->setName(classID.name()); 35 36 36 if( Factory::factoryList == NULL) 37 Factory::factoryList = new std::list<Factory*>; 38 39 Factory::factoryList->push_back(this); 37 Factory::_factoryIDMap[classID] = this; 38 Factory::_factoryStringMap[classID.name()] = this; 40 39 } 41 40 42 /** @brief a reference to the First Factory */ 43 std::list<Factory*>* Factory::factoryList = NULL; 41 /** @brief A Map of all Factories ordered by ID. */ 42 Factory::FactoryIDMap Factory::_factoryIDMap; 43 44 /** @brief A Map of all Factories ordered by Name. */ 45 Factory::FactoryStringMap Factory::_factoryStringMap; 44 46 45 47 /** … … 48 50 Factory::~Factory () 49 51 { 50 // printf("%s\n", this->factoryName); 51 // Factory* tmpDel = this->next; 52 // this->next = NULL; 53 } 52 FactoryIDMap::iterator it = Factory::_factoryIDMap.find(this->_classID); 53 if (it != Factory::_factoryIDMap.end() && (*it).second == this) 54 Factory::_factoryIDMap.erase(it); 54 55 55 /** 56 * @brief deletes all the Factories. (cleanup) 57 */ 58 void Factory::deleteFactories() 59 { 60 if (Factory::factoryList != NULL) 61 { 62 while(!Factory::factoryList->empty()) 63 { 64 delete Factory::factoryList->front(); 65 Factory::factoryList->pop_front(); 66 } 67 delete Factory::factoryList; 68 Factory::factoryList = NULL; 69 } 56 FactoryStringMap::iterator stringIt = Factory::_factoryStringMap.find(this->_classID.name()); 57 if (stringIt != Factory::_factoryStringMap.end() && (*stringIt).second == this) 58 Factory::_factoryStringMap.erase(stringIt); 70 59 } 71 60 … … 74 63 * @returns true on match, false otherwise 75 64 */ 76 bool Factory::operator==( ClassIDclassID) const65 bool Factory::operator==(int classID) const 77 66 { 78 return (this-> classID == classID);67 return (this->_classID == classID); 79 68 } 80 69 81 /**82 * @brief Compares the Factories Name against a given ClassName83 * @param className the Name of the Class to Query84 * @returns true on match, false otherwise.85 */86 bool Factory::operator==(const char* className) const87 {88 return (className != NULL && this->className == className);89 }90 70 91 71 /** … … 96 76 bool Factory::operator==(const std::string& className) const 97 77 { 98 return (this-> className== className);78 return (this->_classID.name() == className); 99 79 } 100 80 … … 107 87 BaseObject* Factory::fabricate(const TiXmlElement* root) 108 88 { 109 assert (root != NULL && Factory::factoryList != NULL); 110 111 std::list<Factory*>::const_iterator factory; 112 for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++) 113 if (*(*factory) == root->Value()) 114 { 115 PRINTF(2)("Create a new Object of type %s\n", (*factory)->getCName()); 116 return (*factory)->fabricateObject(root); 117 } 118 119 PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value()); 120 return NULL; 89 FactoryStringMap::const_iterator it = Factory::_factoryStringMap.find(root->Value()); 90 if (it != Factory::_factoryStringMap.end()) 91 { 92 PRINTF(2)("Create a new Object of type %s\n", (*it).second->getCName()); 93 return (*it).second->fabricateObject(root); 94 } 95 else 96 { 97 PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value()); 98 return NULL; 99 } 121 100 } 122 101 … … 129 108 BaseObject* Factory::fabricate(const std::string& className) 130 109 { 131 if (Factory::factoryList == NULL) 110 FactoryStringMap::const_iterator it = Factory::_factoryStringMap.find(className); 111 if (it != Factory::_factoryStringMap.end()) 112 { 113 PRINTF(2)("Create a new Object of type %s\n", (*it).second->getCName()); 114 return (*it).second->fabricateObject(NULL); 115 } 116 else 117 { 118 PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className.c_str()); 132 119 return NULL; 133 134 std::list<Factory*>::const_iterator factory; 135 for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++) 136 if (*(*factory) == className) 137 { 138 PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName()); 139 return (*factory)->fabricateObject(NULL); 140 } 141 PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className.c_str()); 142 return NULL; 120 } 143 121 } 144 145 122 146 123 /** … … 149 126 * @returns a new Object of Type classID on match, NULL otherwise 150 127 */ 151 BaseObject* Factory::fabricate( ClassIDclassID)128 BaseObject* Factory::fabricate(const ClassID& classID) 152 129 { 153 if (Factory::factoryList == NULL) 130 FactoryIDMap::const_iterator it = Factory::_factoryIDMap.find(classID); 131 if (it != Factory::_factoryIDMap.end()) 132 { 133 PRINTF(4)("Create a new Object of type %s\n", (*it).second->getCName()); 134 return (*it).second->fabricateObject(NULL); 135 } 136 else 137 { 138 PRINTF(2)("Could not Fabricate an Object of ClassID '%d'\n", classID.id()); 154 139 return NULL; 140 } 141 } 155 142 156 std::list<Factory*>::const_iterator factory;157 for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)158 if (*(*factory) == classID)159 {160 PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName());161 return (*factory)->fabricateObject(NULL);162 143 163 } 164 PRINTF(2)("Could not Fabricate an Object of ClassID '0x%h'\n", classID); 165 return NULL; 144 /** 145 * @brief print out some nice litte debug information about the Factory. 146 */ 147 void Factory::debug() const 148 { 149 PRINTF(0)("Factory of class '%s' with ClassID: %d\n", this->_classID.name().c_str(), this->_classID.id()); 166 150 } 151 152 /** 153 * @brief Prints out some nice Debug information about all factories 154 */ 155 void Factory::debugAll() 156 { 157 PRINTF(0)("Debugging all %d Factories\n", Factory::_factoryStringMap.size()); 158 Factory::FactoryStringMap::const_iterator it; 159 for (it = Factory::_factoryStringMap.begin(); it != Factory::_factoryStringMap.end(); ++it) 160 (*it).second->debug(); 161 }
Note: See TracChangeset
for help on using the changeset viewer.