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