[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 | |
---|
[7193] | 17 | #include "util/loading/factory.h" |
---|
[8362] | 18 | #include "debug.h" |
---|
[5982] | 19 | //#include "shell_command.h" |
---|
| 20 | |
---|
[9715] | 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 | */ |
---|
[9715] | 30 | Factory::Factory (const ClassID& classID) |
---|
[9709] | 31 | : _classID(classID) |
---|
[3940] | 32 | { |
---|
[9725] | 33 | PRINTF(4)("Factory::create(%s::%d)\n", classID.name().c_str(), classID.id()); |
---|
[9709] | 34 | //this->registerObject(this, Factory::_objectList); |
---|
| 35 | this->setName(classID.name()); |
---|
[4597] | 36 | |
---|
[9686] | 37 | if( Factory::_factoryList == NULL) |
---|
| 38 | Factory::_factoryList = new std::list<Factory*>; |
---|
[5982] | 39 | |
---|
[9686] | 40 | Factory::_factoryList->push_back(this); |
---|
[3940] | 41 | } |
---|
| 42 | |
---|
[7221] | 43 | /** @brief a reference to the First Factory */ |
---|
[9686] | 44 | std::list<Factory*>* Factory::_factoryList = NULL; |
---|
[4730] | 45 | |
---|
[3940] | 46 | /** |
---|
[7221] | 47 | * @brief destructor |
---|
[5982] | 48 | */ |
---|
[3940] | 49 | Factory::~Factory () |
---|
| 50 | { |
---|
[4020] | 51 | // printf("%s\n", this->factoryName); |
---|
[4004] | 52 | // Factory* tmpDel = this->next; |
---|
| 53 | // this->next = NULL; |
---|
[3940] | 54 | } |
---|
| 55 | |
---|
[5984] | 56 | /** |
---|
[7221] | 57 | * @brief deletes all the Factories. (cleanup) |
---|
[5984] | 58 | */ |
---|
[5982] | 59 | void Factory::deleteFactories() |
---|
| 60 | { |
---|
[9686] | 61 | if (Factory::_factoryList != NULL) |
---|
[5982] | 62 | { |
---|
[9686] | 63 | while(!Factory::_factoryList->empty()) |
---|
[5982] | 64 | { |
---|
[9686] | 65 | delete Factory::_factoryList->front(); |
---|
| 66 | Factory::_factoryList->pop_front(); |
---|
[5982] | 67 | } |
---|
[9686] | 68 | delete Factory::_factoryList; |
---|
| 69 | Factory::_factoryList = NULL; |
---|
[5982] | 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
[5984] | 73 | /** |
---|
| 74 | * @param classID match a classID with this classID |
---|
| 75 | * @returns true on match, false otherwise |
---|
| 76 | */ |
---|
[9684] | 77 | bool Factory::operator==(int classID) const |
---|
[5984] | 78 | { |
---|
[9686] | 79 | return (this->_classID == classID); |
---|
[5984] | 80 | } |
---|
[5982] | 81 | |
---|
[3940] | 82 | |
---|
[7221] | 83 | /** |
---|
| 84 | * @brief Compares the Factories Name against a given ClassName |
---|
| 85 | * @param className the Name of the Class to Query |
---|
| 86 | * @returns true on match, false otherwise. |
---|
| 87 | */ |
---|
| 88 | bool Factory::operator==(const std::string& className) const |
---|
| 89 | { |
---|
[9709] | 90 | return (this->_classID.name() == className); |
---|
[7221] | 91 | } |
---|
[4739] | 92 | |
---|
[7221] | 93 | |
---|
[5984] | 94 | /** |
---|
[7221] | 95 | * @brief Creates a new Object of type root->Value() (name) |
---|
[5984] | 96 | * @param root the XML-Root to match for the newly created Object |
---|
| 97 | * @returns a new Object of Type root->Value() on match, NULL otherwise |
---|
| 98 | */ |
---|
[9675] | 99 | BaseObject* Factory::fabricate(const TiXmlElement* root) |
---|
[5982] | 100 | { |
---|
[9712] | 101 | assert (Factory::_factoryList != NULL); |
---|
[5982] | 102 | |
---|
| 103 | std::list<Factory*>::const_iterator factory; |
---|
[9686] | 104 | for (factory = Factory::_factoryList->begin(); factory != Factory::_factoryList->end(); factory++) |
---|
[5982] | 105 | if (*(*factory) == root->Value()) |
---|
[9675] | 106 | { |
---|
| 107 | PRINTF(2)("Create a new Object of type %s\n", (*factory)->getCName()); |
---|
| 108 | return (*factory)->fabricateObject(root); |
---|
| 109 | } |
---|
[5982] | 110 | |
---|
[9675] | 111 | PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value()); |
---|
| 112 | return NULL; |
---|
[3940] | 113 | } |
---|
[5155] | 114 | |
---|
[5984] | 115 | |
---|
| 116 | /** |
---|
[9675] | 117 | * @brief Creates a new Object of type className |
---|
[5984] | 118 | * @param className the ClassName to match for the newly created Object |
---|
| 119 | * @returns a new Object of Type className on match, NULL otherwise |
---|
| 120 | */ |
---|
[9675] | 121 | BaseObject* Factory::fabricate(const std::string& className) |
---|
[5155] | 122 | { |
---|
[9686] | 123 | if (Factory::_factoryList == NULL) |
---|
[5982] | 124 | return NULL; |
---|
[5155] | 125 | |
---|
[5982] | 126 | std::list<Factory*>::const_iterator factory; |
---|
[9686] | 127 | for (factory = Factory::_factoryList->begin(); factory != Factory::_factoryList->end(); factory++) |
---|
[5982] | 128 | if (*(*factory) == className) |
---|
[9675] | 129 | { |
---|
| 130 | PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName()); |
---|
| 131 | return (*factory)->fabricateObject(NULL); |
---|
| 132 | } |
---|
| 133 | PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className.c_str()); |
---|
| 134 | return NULL; |
---|
[5982] | 135 | } |
---|
| 136 | |
---|
[5984] | 137 | /** |
---|
[9675] | 138 | * @brief Creates a new Object of type classID |
---|
[5984] | 139 | * @param classID the ClassID to match for the newly created Object |
---|
| 140 | * @returns a new Object of Type classID on match, NULL otherwise |
---|
| 141 | */ |
---|
[9715] | 142 | BaseObject* Factory::fabricate(const ClassID& classID) |
---|
[5982] | 143 | { |
---|
[9686] | 144 | if (Factory::_factoryList == NULL) |
---|
[5982] | 145 | return NULL; |
---|
| 146 | |
---|
| 147 | std::list<Factory*>::const_iterator factory; |
---|
[9686] | 148 | for (factory = Factory::_factoryList->begin(); factory != Factory::_factoryList->end(); factory++) |
---|
[5982] | 149 | if (*(*factory) == classID) |
---|
[9709] | 150 | { |
---|
| 151 | PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName()); |
---|
| 152 | return (*factory)->fabricateObject(NULL); |
---|
[5982] | 153 | |
---|
[9709] | 154 | } |
---|
| 155 | PRINTF(2)("Could not Fabricate an Object of ClassID '%d'\n", classID.id()); |
---|
[9675] | 156 | return NULL; |
---|
[5155] | 157 | } |
---|