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