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