| [4597] | 1 | /* |
|---|
| [3940] | 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Christian Meyer |
|---|
| [4250] | 13 | co-programmer: Benjamin Grauer |
|---|
| [3940] | 14 | */ |
|---|
| [5982] | 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING |
|---|
| [3940] | 16 | |
|---|
| [9869] | 17 | #include "factory.h" |
|---|
| [8362] | 18 | #include "debug.h" |
|---|
| [5982] | 19 | //#include "shell_command.h" |
|---|
| 20 | |
|---|
| [9869] | 21 | ObjectListDefinition(Factory); |
|---|
| [3940] | 22 | |
|---|
| [5982] | 23 | //SHELL_COMMAND(create, Factory, fabricate); |
|---|
| [5162] | 24 | |
|---|
| [7221] | 25 | /** |
|---|
| 26 | * @brief constructor |
|---|
| 27 | * |
|---|
| 28 | * set everything to zero and define factoryName |
|---|
| [4003] | 29 | */ |
|---|
| [9869] | 30 | Factory::Factory (const ClassID& classID) |
|---|
| 31 | : _classID(classID) |
|---|
| [3940] | 32 | { |
|---|
| [9869] | 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()); |
|---|
| [4597] | 36 | |
|---|
| [9869] | 37 | Factory::_factoryIDMap[classID] = this; |
|---|
| 38 | Factory::_factoryStringMap[classID.name()] = this; |
|---|
| [3940] | 39 | } |
|---|
| 40 | |
|---|
| [9869] | 41 | /** @brief A Map of all Factories ordered by ID. */ |
|---|
| 42 | Factory::FactoryIDMap Factory::_factoryIDMap; |
|---|
| [4730] | 43 | |
|---|
| [9869] | 44 | /** @brief A Map of all Factories ordered by Name. */ |
|---|
| 45 | Factory::FactoryStringMap Factory::_factoryStringMap; |
|---|
| 46 | |
|---|
| [3940] | 47 | /** |
|---|
| [7221] | 48 | * @brief destructor |
|---|
| [5982] | 49 | */ |
|---|
| [3940] | 50 | Factory::~Factory () |
|---|
| 51 | { |
|---|
| [9869] | 52 | FactoryIDMap::iterator it = Factory::_factoryIDMap.find(this->_classID); |
|---|
| 53 | if (it != Factory::_factoryIDMap.end() && (*it).second == this) |
|---|
| 54 | Factory::_factoryIDMap.erase(it); |
|---|
| [3940] | 55 | |
|---|
| [9869] | 56 | FactoryStringMap::iterator stringIt = Factory::_factoryStringMap.find(this->_classID.name()); |
|---|
| 57 | if (stringIt != Factory::_factoryStringMap.end() && (*stringIt).second == this) |
|---|
| 58 | Factory::_factoryStringMap.erase(stringIt); |
|---|
| [5982] | 59 | } |
|---|
| 60 | |
|---|
| [5984] | 61 | /** |
|---|
| 62 | * @param classID match a classID with this classID |
|---|
| 63 | * @returns true on match, false otherwise |
|---|
| 64 | */ |
|---|
| [9869] | 65 | bool Factory::operator==(int classID) const |
|---|
| [5984] | 66 | { |
|---|
| [9869] | 67 | return (this->_classID == classID); |
|---|
| [5984] | 68 | } |
|---|
| [5982] | 69 | |
|---|
| [3940] | 70 | |
|---|
| [7221] | 71 | /** |
|---|
| 72 | * @brief Compares the Factories Name against a given ClassName |
|---|
| 73 | * @param className the Name of the Class to Query |
|---|
| 74 | * @returns true on match, false otherwise. |
|---|
| 75 | */ |
|---|
| 76 | bool Factory::operator==(const std::string& className) const |
|---|
| 77 | { |
|---|
| [9869] | 78 | return (this->_classID.name() == className); |
|---|
| [7221] | 79 | } |
|---|
| [4739] | 80 | |
|---|
| [7221] | 81 | |
|---|
| [5984] | 82 | /** |
|---|
| [7221] | 83 | * @brief Creates a new Object of type root->Value() (name) |
|---|
| [5984] | 84 | * @param root the XML-Root to match for the newly created Object |
|---|
| 85 | * @returns a new Object of Type root->Value() on match, NULL otherwise |
|---|
| 86 | */ |
|---|
| [9675] | 87 | BaseObject* Factory::fabricate(const TiXmlElement* root) |
|---|
| [5982] | 88 | { |
|---|
| [9869] | 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 | } |
|---|
| [3940] | 100 | } |
|---|
| [5155] | 101 | |
|---|
| [5984] | 102 | |
|---|
| 103 | /** |
|---|
| [9675] | 104 | * @brief Creates a new Object of type className |
|---|
| [5984] | 105 | * @param className the ClassName to match for the newly created Object |
|---|
| 106 | * @returns a new Object of Type className on match, NULL otherwise |
|---|
| 107 | */ |
|---|
| [9675] | 108 | BaseObject* Factory::fabricate(const std::string& className) |
|---|
| [5155] | 109 | { |
|---|
| [9869] | 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()); |
|---|
| [5982] | 119 | return NULL; |
|---|
| [9869] | 120 | } |
|---|
| [5982] | 121 | } |
|---|
| 122 | |
|---|
| [5984] | 123 | /** |
|---|
| [9675] | 124 | * @brief Creates a new Object of type classID |
|---|
| [5984] | 125 | * @param classID the ClassID to match for the newly created Object |
|---|
| 126 | * @returns a new Object of Type classID on match, NULL otherwise |
|---|
| 127 | */ |
|---|
| [9869] | 128 | BaseObject* Factory::fabricate(const ClassID& classID) |
|---|
| [5982] | 129 | { |
|---|
| [9869] | 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()); |
|---|
| [5982] | 139 | return NULL; |
|---|
| [9869] | 140 | } |
|---|
| 141 | } |
|---|
| [5982] | 142 | |
|---|
| 143 | |
|---|
| [9869] | 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()); |
|---|
| [5155] | 150 | } |
|---|
| [9869] | 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 | } |
|---|