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