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 | |
---|
21 | #include <stdarg.h> |
---|
22 | |
---|
23 | /** |
---|
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 | */ |
---|
30 | CLoadParam::CLoadParam(const TiXmlElement* root, const std::string& paramName, BaseObject* object, Executor* executor, bool inLoadCycle) |
---|
31 | : object(object), paramName(paramName) |
---|
32 | { |
---|
33 | this->object = object; |
---|
34 | this->inLoadCycle = inLoadCycle; |
---|
35 | |
---|
36 | // determin the LoadString. |
---|
37 | if (likely(!inLoadCycle)) |
---|
38 | this->loadElem = grabParameterElement(root, paramName); |
---|
39 | else if (paramName == root->Value()) |
---|
40 | this->loadElem = (TiXmlElement*)root->FirstChild(); |
---|
41 | else |
---|
42 | this->loadElem = NULL; |
---|
43 | |
---|
44 | // set the Executor. |
---|
45 | this->executor = executor; |
---|
46 | |
---|
47 | //if (this->executor) |
---|
48 | // this->executor->setName(paramName); |
---|
49 | } |
---|
50 | |
---|
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 | */ |
---|
57 | CLoadParam::~CLoadParam() |
---|
58 | { |
---|
59 | if (likely(this->executor != NULL)) |
---|
60 | { |
---|
61 | std::string loadString = ""; |
---|
62 | if (this->loadElem != NULL && this->loadElem->ToText()) |
---|
63 | loadString = this->loadElem->Value(); |
---|
64 | if (likely(this->object != NULL) && |
---|
65 | ( !loadString.empty() || |
---|
66 | ((this->executor->getType() & Executor_NoLoadString) == Executor_NoLoadString))) |
---|
67 | { |
---|
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()); |
---|
69 | (*this->executor)(this->object, SubString(loadString, ",", SubString::WhiteSpaces, false, '\\')); |
---|
70 | } |
---|
71 | delete this->executor; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
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 |
---|
82 | */ |
---|
83 | CLoadParam& CLoadParam::defaultValues(const MultiType& value0, const MultiType& value1, |
---|
84 | const MultiType& value2, const MultiType& value3, |
---|
85 | const MultiType& value4) |
---|
86 | { |
---|
87 | assert(this->executor != NULL); |
---|
88 | this->executor->defaultValues(value0, value1, value2, value3, value4); |
---|
89 | |
---|
90 | return *this; |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | /** |
---|
96 | * @param descriptionText The text to set as a description for this Parameter |
---|
97 | * @returns a pointer to itself. |
---|
98 | */ |
---|
99 | CLoadParam& CLoadParam::describe(const std::string& descriptionText) |
---|
100 | { |
---|
101 | if (LoadClassDescription::parametersDescription && this->paramDesc && this->paramDesc->getDescription().empty()) |
---|
102 | { |
---|
103 | this->paramDesc->setDescription(descriptionText); |
---|
104 | } |
---|
105 | return *this; |
---|
106 | } |
---|
107 | |
---|
108 | // const LoadParamDescription* LoadParamDescription::getClass(const char* className) |
---|
109 | // { |
---|
110 | // tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator(); |
---|
111 | // LoadClassDescription* enumClassDesc = iterator->firstElement(); |
---|
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 | |
---|
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 | ////////////////////// |
---|
225 | /** |
---|
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 |
---|
229 | */ |
---|
230 | std::string grabParameter(const TiXmlElement* root, const std::string& parameterName) |
---|
231 | { |
---|
232 | const TiXmlElement* element; |
---|
233 | const TiXmlNode* node; |
---|
234 | |
---|
235 | if (root == NULL) |
---|
236 | return ""; |
---|
237 | |
---|
238 | element = root->FirstChildElement( parameterName); |
---|
239 | if( element == NULL) return ""; |
---|
240 | |
---|
241 | node = element->FirstChild(); |
---|
242 | while( node != NULL) |
---|
243 | { |
---|
244 | if( node->ToText()) return node->Value(); |
---|
245 | node = node->NextSibling(); |
---|
246 | } |
---|
247 | return ""; |
---|
248 | } |
---|
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 | */ |
---|
255 | const TiXmlElement* grabParameterElement(const TiXmlElement* root, const std::string& parameterName) |
---|
256 | { |
---|
257 | const TiXmlElement* element; |
---|
258 | const TiXmlNode* node; |
---|
259 | |
---|
260 | if (root == NULL) |
---|
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 | |
---|