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