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