| [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" | 
|---|
| [5155] | 18 |  | 
|---|
| [5982] | 19 | //#include "shell_command.h" | 
|---|
 | 20 |  | 
|---|
| [3940] | 21 | using namespace std; | 
|---|
 | 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 |  */ | 
|---|
| [7221] | 30 | Factory::Factory (const std::string& factoryName, ClassID classID) | 
|---|
| [8148] | 31 |   : classID(classID), className(factoryName) | 
|---|
| [3940] | 32 | { | 
|---|
| [4597] | 33 |   this->setClassID(CL_FACTORY, "Factory"); | 
|---|
 | 34 |   this->setName(factoryName); | 
|---|
 | 35 |  | 
|---|
| [5982] | 36 |   if( Factory::factoryList == NULL) | 
|---|
 | 37 |     Factory::factoryList = new std::list<Factory*>; | 
|---|
 | 38 |  | 
|---|
 | 39 |   Factory::factoryList->push_back(this); | 
|---|
| [3940] | 40 | } | 
|---|
 | 41 |  | 
|---|
| [7221] | 42 | /** @brief a reference to the First Factory */ | 
|---|
| [5982] | 43 | std::list<Factory*>* Factory::factoryList = NULL; | 
|---|
| [4730] | 44 |  | 
|---|
| [3940] | 45 | /** | 
|---|
| [7221] | 46 |  * @brief destructor | 
|---|
| [5982] | 47 |  */ | 
|---|
| [3940] | 48 | Factory::~Factory () | 
|---|
 | 49 | { | 
|---|
| [4020] | 50 |   //  printf("%s\n", this->factoryName); | 
|---|
| [4004] | 51 |   //  Factory* tmpDel = this->next; | 
|---|
 | 52 |   //  this->next = NULL; | 
|---|
| [3940] | 53 | } | 
|---|
 | 54 |  | 
|---|
| [5984] | 55 | /** | 
|---|
| [7221] | 56 |  * @brief deletes all the Factories. (cleanup) | 
|---|
| [5984] | 57 |  */ | 
|---|
| [5982] | 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 |   } | 
|---|
 | 70 | } | 
|---|
 | 71 |  | 
|---|
| [5984] | 72 | /** | 
|---|
 | 73 |  * @param classID match a classID with this classID | 
|---|
 | 74 |  * @returns true on match, false otherwise | 
|---|
 | 75 |  */ | 
|---|
 | 76 | bool Factory::operator==(ClassID classID) const | 
|---|
 | 77 | { | 
|---|
 | 78 |   return (this->classID == classID); | 
|---|
 | 79 | } | 
|---|
| [5982] | 80 |  | 
|---|
| [4487] | 81 | /** | 
|---|
| [7221] | 82 |  * @brief Compares the Factories Name against a given ClassName | 
|---|
| [5982] | 83 |  * @param className the Name of the Class to Query | 
|---|
 | 84 |  * @returns true on match, false otherwise. | 
|---|
 | 85 |  */ | 
|---|
 | 86 | bool Factory::operator==(const char* className) const | 
|---|
| [3940] | 87 | { | 
|---|
| [7221] | 88 |   return(className != NULL && this->className == className); | 
|---|
| [5982] | 89 | } | 
|---|
| [3940] | 90 |  | 
|---|
| [7221] | 91 | /** | 
|---|
 | 92 |  * @brief Compares the Factories Name against a given ClassName | 
|---|
 | 93 |  * @param className the Name of the Class to Query | 
|---|
 | 94 |  * @returns true on match, false otherwise. | 
|---|
 | 95 |  */ | 
|---|
 | 96 | bool Factory::operator==(const std::string& className) const | 
|---|
 | 97 | { | 
|---|
 | 98 |   return(this->className == className); | 
|---|
 | 99 | } | 
|---|
| [4739] | 100 |  | 
|---|
| [7221] | 101 |  | 
|---|
| [5984] | 102 | /** | 
|---|
| [7221] | 103 |  * @brief Creates a new Object of type root->Value() (name) | 
|---|
| [5984] | 104 |  * @param root the XML-Root to match for the newly created Object | 
|---|
 | 105 |  * @returns a new Object of Type root->Value() on match, NULL otherwise | 
|---|
 | 106 |  */ | 
|---|
 | 107 |  BaseObject* Factory::fabricate(const TiXmlElement* root) | 
|---|
| [5982] | 108 | { | 
|---|
 | 109 |   if (root == NULL || Factory::factoryList == NULL) | 
|---|
 | 110 |     return NULL; | 
|---|
 | 111 |  | 
|---|
 | 112 |   std::list<Factory*>::const_iterator factory; | 
|---|
 | 113 |   for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++) | 
|---|
 | 114 |     if (*(*factory) == root->Value()) | 
|---|
 | 115 |       break; | 
|---|
 | 116 |  | 
|---|
 | 117 |   if (factory != Factory::factoryList->end()) | 
|---|
| [5155] | 118 |   { | 
|---|
| [7462] | 119 |     PRINTF(2)("Create a new Object of type %s\n", (*factory)->getName()); | 
|---|
| [5982] | 120 |     return (*factory)->fabricateObject(root); | 
|---|
| [5155] | 121 |   } | 
|---|
| [4739] | 122 |   else | 
|---|
 | 123 |   { | 
|---|
| [5982] | 124 |     PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value()); | 
|---|
 | 125 |     return NULL; | 
|---|
| [4739] | 126 |   } | 
|---|
| [3940] | 127 | } | 
|---|
| [5155] | 128 |  | 
|---|
| [5984] | 129 |  | 
|---|
 | 130 | /** | 
|---|
 | 131 |  * Creates a new Object of type className | 
|---|
 | 132 |  * @param className the ClassName to match for the newly created Object | 
|---|
 | 133 |  * @returns a new Object of Type className on match, NULL otherwise | 
|---|
 | 134 |  */ | 
|---|
| [7221] | 135 |  BaseObject* Factory::fabricate(const std::string& className) | 
|---|
| [5155] | 136 | { | 
|---|
| [7221] | 137 |   if (Factory::factoryList == NULL) | 
|---|
| [5982] | 138 |     return NULL; | 
|---|
| [5155] | 139 |  | 
|---|
| [5982] | 140 |   std::list<Factory*>::const_iterator factory; | 
|---|
 | 141 |   for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++) | 
|---|
 | 142 |     if (*(*factory) == className) | 
|---|
 | 143 |       break; | 
|---|
 | 144 |  | 
|---|
 | 145 |   if (factory != Factory::factoryList->end()) | 
|---|
| [5155] | 146 |   { | 
|---|
| [5982] | 147 |     PRINTF(4)("Create a new Object of type %s\n", (*factory)->getName()); | 
|---|
 | 148 |     return (*factory)->fabricateObject(NULL); | 
|---|
 | 149 |   } | 
|---|
 | 150 |   else | 
|---|
 | 151 |   { | 
|---|
| [7676] | 152 |     PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className.c_str()); | 
|---|
| [5982] | 153 |     return NULL; | 
|---|
 | 154 |   } | 
|---|
 | 155 | } | 
|---|
 | 156 |  | 
|---|
| [6341] | 157 |  | 
|---|
| [5984] | 158 | /** | 
|---|
 | 159 |  * Creates a new Object of type classID | 
|---|
 | 160 |  * @param classID the ClassID to match for the newly created Object | 
|---|
 | 161 |  * @returns a new Object of Type classID on match, NULL otherwise | 
|---|
 | 162 |  */ | 
|---|
| [5982] | 163 | BaseObject* Factory::fabricate(ClassID classID) | 
|---|
 | 164 | { | 
|---|
 | 165 |   if (Factory::factoryList == NULL) | 
|---|
 | 166 |     return NULL; | 
|---|
 | 167 |  | 
|---|
 | 168 |   std::list<Factory*>::const_iterator factory; | 
|---|
 | 169 |   for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++) | 
|---|
 | 170 |     if (*(*factory) == classID) | 
|---|
| [5155] | 171 |       break; | 
|---|
| [5982] | 172 |  | 
|---|
 | 173 |   if (factory != Factory::factoryList->end()) | 
|---|
 | 174 |   { | 
|---|
 | 175 |     PRINTF(4)("Create a new Object of type %s\n", (*factory)->getName()); | 
|---|
 | 176 |     return (*factory)->fabricateObject(NULL); | 
|---|
| [5155] | 177 |   } | 
|---|
| [5982] | 178 |   else | 
|---|
 | 179 |   { | 
|---|
| [7221] | 180 |     PRINTF(2)("Could not Fabricate an Object of ClassID '0x%h'\n", classID); | 
|---|
| [5982] | 181 |     return NULL; | 
|---|
 | 182 |   } | 
|---|
| [5155] | 183 | } | 
|---|