| [4597] | 1 | /* |
|---|
| [4250] | 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: |
|---|
| [4285] | 12 | main-programmer: Benjamin Grauer |
|---|
| [4250] | 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| [5654] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING |
|---|
| 17 | |
|---|
| [7193] | 18 | #include "util/loading/load_param.h" |
|---|
| [5546] | 19 | #include "load_param_description.h" |
|---|
| [4250] | 20 | |
|---|
| [4254] | 21 | #include <stdarg.h> |
|---|
| 22 | |
|---|
| [4256] | 23 | /** |
|---|
| [5653] | 24 | * Constructs a new LoadParameter |
|---|
| 25 | * @param root the XML-element to load this Parameter from |
|---|
| 26 | * @param paramName the Parameter to load |
|---|
| 27 | * @param object the BaseObject, to load this parameter on to (will be cast to executor's Parameter) |
|---|
| 28 | * @param executor the Executor, that executes the loading procedure. |
|---|
| 29 | */ |
|---|
| [7721] | 30 | CLoadParam::CLoadParam(const TiXmlElement* root, const std::string& paramName, BaseObject* object, Executor* executor, bool inLoadCycle) |
|---|
| [8316] | 31 | : object(object), paramName(paramName) |
|---|
| [5645] | 32 | { |
|---|
| [5654] | 33 | this->inLoadCycle = inLoadCycle; |
|---|
| 34 | |
|---|
| 35 | // determin the LoadString. |
|---|
| 36 | if (likely(!inLoadCycle)) |
|---|
| [6613] | 37 | this->loadElem = grabParameterElement(root, paramName); |
|---|
| [7221] | 38 | else if (paramName == root->Value()) |
|---|
| [6613] | 39 | this->loadElem = (TiXmlElement*)root->FirstChild(); |
|---|
| [5645] | 40 | else |
|---|
| [6613] | 41 | this->loadElem = NULL; |
|---|
| [5651] | 42 | |
|---|
| [5654] | 43 | // set the Executor. |
|---|
| [7721] | 44 | this->executor = executor; |
|---|
| [7201] | 45 | |
|---|
| [7221] | 46 | //if (this->executor) |
|---|
| 47 | // this->executor->setName(paramName); |
|---|
| [5645] | 48 | } |
|---|
| [5651] | 49 | |
|---|
| [5653] | 50 | /** |
|---|
| 51 | * This is a VERY SPECIAL deconsrtuctor. |
|---|
| 52 | * It is made, so that it loads the Parameters on destruction. |
|---|
| 53 | * meaning, if an Executor a valid Object exist, and all |
|---|
| 54 | * Execution-Conditions are met, they are executed here. |
|---|
| 55 | */ |
|---|
| [5671] | 56 | CLoadParam::~CLoadParam() |
|---|
| [5645] | 57 | { |
|---|
| 58 | if (likely(this->executor != NULL)) |
|---|
| [5646] | 59 | { |
|---|
| [7221] | 60 | std::string loadString = ""; |
|---|
| [6613] | 61 | if (this->loadElem != NULL && this->loadElem->ToText()) |
|---|
| [7221] | 62 | loadString = this->loadElem->Value(); |
|---|
| [6613] | 63 | if (likely(this->object != NULL) && |
|---|
| [7221] | 64 | ( !loadString.empty() || |
|---|
| [7198] | 65 | ((this->executor->getType() & Executor_NoLoadString) == Executor_NoLoadString))) |
|---|
| [5652] | 66 | { |
|---|
| [7221] | 67 | PRINTF(4)("Loading value '%s' with Parameters '%s' onto: %s::%s\n", this->paramName.c_str(), loadString.c_str(), this->object->getClassName(), this->object->getName()); |
|---|
| [7474] | 68 | (*this->executor)(this->object, SubString(loadString, ",", SubString::WhiteSpaces, false, '\\')); |
|---|
| [5652] | 69 | } |
|---|
| [5645] | 70 | delete this->executor; |
|---|
| [5646] | 71 | } |
|---|
| [5645] | 72 | } |
|---|
| 73 | |
|---|
| [5653] | 74 | /** |
|---|
| [7198] | 75 | * @brief set the default values of the executor |
|---|
| 76 | * @param value0 the first default value |
|---|
| 77 | * @param value1 the second default value |
|---|
| 78 | * @param value2 the third default value |
|---|
| 79 | * @param value3 the fourth default value |
|---|
| 80 | * @param value4 the fifth default value |
|---|
| [5653] | 81 | */ |
|---|
| [7198] | 82 | CLoadParam& CLoadParam::defaultValues(const MultiType& value0, const MultiType& value1, |
|---|
| 83 | const MultiType& value2, const MultiType& value3, |
|---|
| 84 | const MultiType& value4) |
|---|
| [5652] | 85 | { |
|---|
| [7198] | 86 | assert(this->executor != NULL); |
|---|
| 87 | this->executor->defaultValues(value0, value1, value2, value3, value4); |
|---|
| [5652] | 88 | |
|---|
| [5708] | 89 | return *this; |
|---|
| [5652] | 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| [4860] | 94 | /** |
|---|
| [4836] | 95 | * @param descriptionText The text to set as a description for this Parameter |
|---|
| 96 | * @returns a pointer to itself. |
|---|
| [4256] | 97 | */ |
|---|
| [7221] | 98 | CLoadParam& CLoadParam::describe(const std::string& descriptionText) |
|---|
| [4254] | 99 | { |
|---|
| [7221] | 100 | if (LoadClassDescription::parametersDescription && this->paramDesc && this->paramDesc->getDescription().empty()) |
|---|
| [7198] | 101 | { |
|---|
| 102 | this->paramDesc->setDescription(descriptionText); |
|---|
| 103 | } |
|---|
| [5708] | 104 | return *this; |
|---|
| [4254] | 105 | } |
|---|
| 106 | |
|---|
| [5100] | 107 | // const LoadParamDescription* LoadParamDescription::getClass(const char* className) |
|---|
| 108 | // { |
|---|
| 109 | // tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator(); |
|---|
| [5115] | 110 | // LoadClassDescription* enumClassDesc = iterator->firstElement(); |
|---|
| [5100] | 111 | // while (enumClassDesc) |
|---|
| 112 | // { |
|---|
| 113 | // if (!strcmp(enumClassDesc->className, classNameBegin, className)) |
|---|
| 114 | // { |
|---|
| 115 | // delete iterator; |
|---|
| 116 | // return enumClassDesc; |
|---|
| 117 | // } |
|---|
| 118 | // enumClassDesc = iterator->nextElement(); |
|---|
| 119 | // } |
|---|
| 120 | // delete iterator; |
|---|
| 121 | // |
|---|
| 122 | // return NULL; |
|---|
| 123 | // } |
|---|
| 124 | |
|---|
| [5655] | 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | /* |
|---|
| 129 | * @param object The object this Parameter is loaded too. |
|---|
| 130 | * @param root: the XML-element to load this option from. |
|---|
| 131 | * @param paramName: The name of the parameter loaded. |
|---|
| 132 | * @param paramCount: how many parameters this loading-function takes |
|---|
| 133 | * @param multi: if false LoadParam assumes only one occurence of this parameter in root, if true it assumes multiple occurences. |
|---|
| 134 | * @param ...: the parameter information (1. Parameter, 2. Default Value for the Parameter, ...) |
|---|
| 135 | */ |
|---|
| 136 | /*LoadParam::LoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName, |
|---|
| 137 | int paramCount, bool multi, const void* pointerToParam, ...) |
|---|
| 138 | { |
|---|
| 139 | this->setClassID(CL_LOAD_PARAM, "LoadParam"); |
|---|
| 140 | this->executor = NULL; |
|---|
| 141 | |
|---|
| 142 | this->loadString = NULL; |
|---|
| 143 | this->pointerToParam = pointerToParam; |
|---|
| 144 | |
|---|
| 145 | if (paramCount == 0 || this->pointerToParam != NULL) |
|---|
| 146 | this->loadString = "none"; |
|---|
| 147 | else |
|---|
| 148 | { |
|---|
| 149 | if (likely(!multi)) |
|---|
| 150 | this->loadString = grabParameter(root, paramName); |
|---|
| 151 | else |
|---|
| 152 | { |
|---|
| 153 | if (!strcmp(root->Value(), paramName)) |
|---|
| 154 | { |
|---|
| 155 | const TiXmlNode* val = root->FirstChild(); |
|---|
| 156 | if( val->ToText()) |
|---|
| 157 | this->loadString = val->Value(); |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | this->paramDesc = NULL; |
|---|
| 163 | if (LoadClassDescription::parametersDescription) |
|---|
| 164 | { |
|---|
| 165 | // locating the class |
|---|
| 166 | this->classDesc = LoadClassDescription::addClass(object->getClassName()); |
|---|
| 167 | |
|---|
| 168 | if ((this->paramDesc = this->classDesc->addParam(paramName)) != NULL) |
|---|
| 169 | { |
|---|
| 170 | |
|---|
| 171 | this->paramDesc->paramCount = paramCount; |
|---|
| 172 | this->paramDesc->types = new int[paramCount]; |
|---|
| 173 | this->paramDesc->defaultValues = new char*[paramCount]; |
|---|
| 174 | |
|---|
| 175 | va_list types; |
|---|
| 176 | va_start (types, pointerToParam); |
|---|
| 177 | char defaultVal[512]; |
|---|
| 178 | for(int i = 0; i < paramCount; i++) |
|---|
| 179 | { |
|---|
| 180 | defaultVal[0] = '\0'; |
|---|
| 181 | // parameters parsed |
|---|
| 182 | int tmpType = va_arg (types, int); |
|---|
| 183 | this->paramDesc->types[i] = tmpType; |
|---|
| 184 | switch (tmpType) |
|---|
| 185 | { |
|---|
| 186 | case MT_INT: |
|---|
| 187 | sprintf(defaultVal, "%d", va_arg(types, int)); |
|---|
| 188 | break; |
|---|
| 189 | // case MT_LONG: |
|---|
| 190 | // sprintf(defaultVal, "%0.3f", va_arg(types, l_LONG_TYPE)); |
|---|
| 191 | // break; |
|---|
| 192 | case MT_FLOAT: |
|---|
| 193 | sprintf(defaultVal, "%0.3f", va_arg(types, double)); |
|---|
| 194 | break; |
|---|
| 195 | case MT_STRING: |
|---|
| 196 | sprintf(defaultVal, "%s", va_arg(types, l_STRING_TYPE)); |
|---|
| 197 | break; |
|---|
| 198 | case MT_EXT1: |
|---|
| 199 | sprintf(defaultVal, ""); |
|---|
| 200 | break; |
|---|
| 201 | } |
|---|
| 202 | this->paramDesc->defaultValues[i] = new char[strlen(defaultVal)+1]; |
|---|
| 203 | strcpy(this->paramDesc->defaultValues[i], defaultVal); |
|---|
| 204 | } |
|---|
| 205 | va_end(types); |
|---|
| 206 | |
|---|
| 207 | int argCount = 0; |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | }*/ |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | ////////////////////// |
|---|
| 222 | // HELPER FUNCTIONS // |
|---|
| 223 | ////////////////////// |
|---|
| [4492] | 224 | /** |
|---|
| [4836] | 225 | * @param root: The XML-element to grab a parameter from |
|---|
| 226 | * @param parameterName: the parameter to grab |
|---|
| 227 | * @returns the Value of the parameter if found, NULL otherwise |
|---|
| [4492] | 228 | */ |
|---|
| [7221] | 229 | std::string grabParameter(const TiXmlElement* root, const std::string& parameterName) |
|---|
| [4492] | 230 | { |
|---|
| 231 | const TiXmlElement* element; |
|---|
| 232 | const TiXmlNode* node; |
|---|
| [4597] | 233 | |
|---|
| [7221] | 234 | if (root == NULL) |
|---|
| 235 | return ""; |
|---|
| [4597] | 236 | |
|---|
| [4492] | 237 | element = root->FirstChildElement( parameterName); |
|---|
| [7221] | 238 | if( element == NULL) return ""; |
|---|
| [4597] | 239 | |
|---|
| [4492] | 240 | node = element->FirstChild(); |
|---|
| 241 | while( node != NULL) |
|---|
| [7198] | 242 | { |
|---|
| 243 | if( node->ToText()) return node->Value(); |
|---|
| 244 | node = node->NextSibling(); |
|---|
| 245 | } |
|---|
| [7221] | 246 | return ""; |
|---|
| [4492] | 247 | } |
|---|
| [6613] | 248 | |
|---|
| 249 | /** |
|---|
| 250 | * @param root: The XML-element to grab a parameter from |
|---|
| 251 | * @param parameterName: the parameter to grab |
|---|
| 252 | * @returns the Element of the parameter if found, NULL otherwise |
|---|
| 253 | */ |
|---|
| [7221] | 254 | const TiXmlElement* grabParameterElement(const TiXmlElement* root, const std::string& parameterName) |
|---|
| [6613] | 255 | { |
|---|
| 256 | const TiXmlElement* element; |
|---|
| 257 | const TiXmlNode* node; |
|---|
| 258 | |
|---|
| [7221] | 259 | if (root == NULL) |
|---|
| [6613] | 260 | return NULL; |
|---|
| 261 | |
|---|
| 262 | element = root->FirstChildElement( parameterName); |
|---|
| 263 | if( element == NULL) return NULL; |
|---|
| 264 | |
|---|
| 265 | node = element->FirstChild(); |
|---|
| 266 | while( node != NULL) |
|---|
| 267 | { |
|---|
| 268 | if( node->ToText()) return (TiXmlElement*)node; |
|---|
| 269 | node = node->NextSibling(); |
|---|
| 270 | } |
|---|
| 271 | return NULL; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|