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 | /*! |
---|
17 | \file load_param.h |
---|
18 | \brief A Class and macro-functions, that makes our lives easy to load-in parameters |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef _LOAD_PARAM_H |
---|
22 | #define _LOAD_PARAM_H |
---|
23 | |
---|
24 | #include "base_object.h" |
---|
25 | #include "vector.h" |
---|
26 | #include "factory.h" |
---|
27 | #include "debug.h" |
---|
28 | #include "substring.h" |
---|
29 | #include "tinyxml.h" |
---|
30 | |
---|
31 | // Forward Declaration // |
---|
32 | template<class T> class tList; |
---|
33 | |
---|
34 | //! macro that makes it even more easy to load a Parameter |
---|
35 | /** |
---|
36 | * @param className the name of the class to load |
---|
37 | * @param parameterName the name of the parameter to load as written in the XML-file |
---|
38 | * @param function the function to call |
---|
39 | */ |
---|
40 | #define LOAD_PARAM(className, parameterName, paramFunction) \ |
---|
41 | LoadParam<className>(root, #parameterName, this, &className::paramFunction) |
---|
42 | |
---|
43 | /** |
---|
44 | * this Starts a Cycle in the Loading Process |
---|
45 | * be aware, that in the cycle the first parameter of load_param should because |
---|
46 | * called element, and that you must say true at the Fith parameter, or it will fail |
---|
47 | * also you will have to close the Cycle again with LOAD_PARAM_END_CYCLE |
---|
48 | */ |
---|
49 | #define LOAD_PARAM_START_CYCLE const TiXmlElement* element; \ |
---|
50 | element = root->FirstChildElement(); \ |
---|
51 | while( element != NULL) \ |
---|
52 | { |
---|
53 | /** |
---|
54 | * closes a LoadParam Loop |
---|
55 | * @see LOAD_PARAM_START_CYCLE |
---|
56 | */ |
---|
57 | #define LOAD_PARAM_END_CYCLE element = element->NextSiblingElement(); \ |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | /** |
---|
63 | useable FunctionParameters are: |
---|
64 | l_INT: int |
---|
65 | l_LONG: long |
---|
66 | l_SHORT: short |
---|
67 | l_FLOAT: float |
---|
68 | l_STRING: const char* |
---|
69 | l_XML_ELEM: TiXmlElement* |
---|
70 | */ |
---|
71 | |
---|
72 | #define l_INT_TYPE int //!< The type of an INT |
---|
73 | #define l_INT_FUNC isInt //!< The function to call to parse INT |
---|
74 | #define l_INT_NAME "int" //!< The name of an INT |
---|
75 | #define l_INT_DEFAULT 0 //!< a default Value for an INT |
---|
76 | |
---|
77 | #define l_LONG_TYPE long //!< The type of a LONG |
---|
78 | #define l_LONG_FUNC isInt //!< The function to parse a LONG |
---|
79 | #define l_LONG_NAME "long" //!< The name of a LONG |
---|
80 | #define l_LONG_DEFAULT 0 //!< a default Value for a LONG |
---|
81 | |
---|
82 | // #define l_SHORT_TYPE short //!< The type of a SHORT |
---|
83 | // #define l_SHORT_FUNC atoi //!< The function to parse a SHORT |
---|
84 | // #define l_SHORT_NAME "short" //!< The name of a SHORT |
---|
85 | // #define l_SHORT_DEFAULT 0 //!< a default Value for a SHORT |
---|
86 | |
---|
87 | #define l_FLOAT_TYPE float //!< The type of a FLOAT |
---|
88 | #define l_FLOAT_FUNC isFloat //!< The function to parse a FLOAT |
---|
89 | #define l_FLOAT_NAME "float" //!< The name of a FLOAT |
---|
90 | #define l_FLOAT_DEFAULT 0.0 //!< a default Value for a FLOAT |
---|
91 | |
---|
92 | //#define l_VECTOR_TYPE const Vector& //!< The type of a VECTOR |
---|
93 | //#define l_VECTOR_FUNC isVector //!< The function to parse a VECTOR |
---|
94 | //#define l_VECTOR_NAME "Vector[x/y/z]" //!< The name of a VECTOR |
---|
95 | //#define l_VECTOR_DEFAULT Vector(0,0,0) //!< Default value for a VECTOR |
---|
96 | |
---|
97 | #define l_STRING_TYPE const char* //!< The type of a STRING |
---|
98 | #define l_STRING_FUNC isString //!< The function to parse a STRING |
---|
99 | #define l_STRING_NAME "string" //!< The name of a STRING |
---|
100 | #define l_STRING_DEFAULT "" //!< a default Value for an STRING |
---|
101 | |
---|
102 | #define l_XML_ELEM_TYPE const TiXmlElement* //!< The type of an XML_ELEM |
---|
103 | #define l_XML_ELEM_FUNC //!< The function to parse an XML_ELEM |
---|
104 | #define l_XML_ELEM_NAME "XML" //!< The name of an XML_ELEM |
---|
105 | #define l_XML_ELEM_DEFAULT NULL //!< The dafault Value for an XML_ELEM |
---|
106 | |
---|
107 | |
---|
108 | /***************************************** |
---|
109 | **** MACROS DEFINITIONS OF LOADABLES ***** |
---|
110 | *****************************************/ |
---|
111 | // 1. TYPE |
---|
112 | /** |
---|
113 | \brief a Macro to easily implement many different Constructors for the LoadParam-Class with 1 argument |
---|
114 | \param type1 The type of the first functionParameter |
---|
115 | */ |
---|
116 | #define LoadParam1(type1) \ |
---|
117 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), \ |
---|
118 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT) \ |
---|
119 | : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, type1##_NAME, default1) \ |
---|
120 | { \ |
---|
121 | if (loadString != NULL && root != NULL) \ |
---|
122 | (*pt2Object.*function)(type1##_FUNC(loadString, default1)); \ |
---|
123 | else \ |
---|
124 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | // 2. TYPES |
---|
129 | /** |
---|
130 | \brief a Macro to easily implement many different Constructors for the LoadParam-Class with 2 arguments |
---|
131 | \param type1 The type of the first functionParameter |
---|
132 | \param type2 The type of the second functionParameter |
---|
133 | */ |
---|
134 | #define LoadParam2(type1, type2) \ |
---|
135 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE), \ |
---|
136 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT) \ |
---|
137 | : BaseLoadParam(root, pt2Object, paramName, 2, multi, NULL, type1##_NAME, default1, type2##_NAME, default2) \ |
---|
138 | { \ |
---|
139 | if (loadString != NULL && root != NULL) \ |
---|
140 | { \ |
---|
141 | SubString subLoads(loadString); \ |
---|
142 | if (subLoads.getCount() == 2) \ |
---|
143 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2)); \ |
---|
144 | else \ |
---|
145 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
146 | paramName, pt2Object->getClassName(), 2, subLoads.getCount()); \ |
---|
147 | } \ |
---|
148 | else \ |
---|
149 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | // 3. TYPES |
---|
154 | /** |
---|
155 | \brief a Macro to easily implement many different Constructors for the LoadParam-Class with 3 arguments |
---|
156 | \param type1 The type of the first functionParameter |
---|
157 | \param type2 The type of the second functionParameter |
---|
158 | \param type3 The type of the third functionParameter |
---|
159 | */ |
---|
160 | #define LoadParam3(type1, type2, type3) \ |
---|
161 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE), \ |
---|
162 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT)\ |
---|
163 | : BaseLoadParam(root, pt2Object, paramName, 3, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3) \ |
---|
164 | { \ |
---|
165 | if (loadString != NULL && root != NULL) \ |
---|
166 | { \ |
---|
167 | SubString subLoads(loadString); \ |
---|
168 | if (subLoads.getCount() == 3) \ |
---|
169 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3)); \ |
---|
170 | else \ |
---|
171 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
172 | paramName, pt2Object->getClassName(), 3, subLoads.getCount()); \ |
---|
173 | } \ |
---|
174 | else \ |
---|
175 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | // 4. TYPES |
---|
180 | /** |
---|
181 | \brief a Macro to easily implement many different Constructors for the LoadParam-Class with 4 arguments |
---|
182 | \param type1 The type of the first functionParameter |
---|
183 | \param type2 The type of the second functionParameter |
---|
184 | \param type3 The type of the third functionParameter |
---|
185 | \param type4 The type of the forth functionParameter |
---|
186 | */ |
---|
187 | #define LoadParam4(type1, type2, type3, type4) \ |
---|
188 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE), \ |
---|
189 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \ |
---|
190 | type4##_TYPE default4 = type4##_DEFAULT) \ |
---|
191 | : BaseLoadParam(root, pt2Object, paramName, 4, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3, \ |
---|
192 | type4##_NAME, default4) \ |
---|
193 | { \ |
---|
194 | if (loadString != NULL && root != NULL) \ |
---|
195 | { \ |
---|
196 | SubString subLoads(loadString); \ |
---|
197 | if (subLoads.getCount() == 4) \ |
---|
198 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3), type4##_FUNC(subLoads.getString(3), default4)); \ |
---|
199 | else \ |
---|
200 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
201 | paramName, pt2Object->getClassName(), 4, subLoads.getCount()); \ |
---|
202 | } \ |
---|
203 | else \ |
---|
204 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | // 5. TYPES |
---|
209 | /** |
---|
210 | \brief a Macro to easily implement many different Constructors for the LoadParam-Class with 5 arguments |
---|
211 | \param type1 The type of the first functionParameter |
---|
212 | \param type2 The type of the second functionParameter |
---|
213 | \param type3 The type of the third functionParameter |
---|
214 | \param type4 The type of the forth functionParameter |
---|
215 | \param type5 The type of the fifth functionParameter |
---|
216 | */ |
---|
217 | #define LoadParam5(type1, type2, type3, type4, type5) \ |
---|
218 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, \ |
---|
219 | void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE, type5##_TYPE), \ |
---|
220 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \ |
---|
221 | type4##_TYPE default4 = type4##_DEFAULT, type5##_TYPE default5 = type5##_DEFAULT ) \ |
---|
222 | : BaseLoadParam(root, pt2Object, paramName, 5, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3, \ |
---|
223 | type4##_NAME, default4, type5##_NAME, default5) \ |
---|
224 | { \ |
---|
225 | if (loadString != NULL && root != NULL) \ |
---|
226 | { \ |
---|
227 | SubString subLoads(loadString); \ |
---|
228 | if (subLoads.getCount() == 5) \ |
---|
229 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3), type4##_FUNC(subLoads.getString(3), default4), type5##_FUNC(subLoads.getString(4), default5)); \ |
---|
230 | else \ |
---|
231 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
232 | paramName, pt2Object->getClassName(), 5, subLoads.getCount()); \ |
---|
233 | } \ |
---|
234 | else \ |
---|
235 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
236 | } |
---|
237 | |
---|
238 | // Pointer TYPE |
---|
239 | /** |
---|
240 | \brief a Macro to easily implement many different Constructors for the LoadParam-Class with one Pointer argument |
---|
241 | \param type1 The type of the Pointer |
---|
242 | */ |
---|
243 | #define LoadParamPT(type1) \ |
---|
244 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), type1##_TYPE pointerToParam, bool multi = false) \ |
---|
245 | : BaseLoadParam(root, pt2Object, paramName, 1, multi, pointerToParam, type1##_NAME) \ |
---|
246 | { \ |
---|
247 | if (pointerToParam != NULL && root != NULL) \ |
---|
248 | (*pt2Object.*function)((type1##_TYPE) pointerToParam); \ |
---|
249 | else \ |
---|
250 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | /*********************** |
---|
255 | *** HELPER FUNCTIONS *** |
---|
256 | ***********************/ |
---|
257 | int isInt(const char* INT, int defaultValue); |
---|
258 | float isFloat(const char* FLOAT, float defaultValue); |
---|
259 | //const Vector& isVector(const char* VECTOR, const Vector& defaultValue); |
---|
260 | const char* isString(const char* STRING, const char* defaultValue); |
---|
261 | |
---|
262 | //TiXmlEmlemnt* isXmlElem(const) |
---|
263 | |
---|
264 | |
---|
265 | /************************ |
---|
266 | *** DESCRIPTION STUFF *** |
---|
267 | ************************/ |
---|
268 | //! A class that handles the description of loadable parameters |
---|
269 | class LoadParamDescription |
---|
270 | { |
---|
271 | friend class BaseLoadParam; |
---|
272 | friend class LoadClassDescription; |
---|
273 | public: |
---|
274 | LoadParamDescription(const char* paramName); |
---|
275 | ~LoadParamDescription(); |
---|
276 | |
---|
277 | void setDescription(const char* descriptionText); |
---|
278 | /** \returns the descriptionString */ |
---|
279 | const char* getDescription() { return this->description; }; |
---|
280 | |
---|
281 | void print() const; |
---|
282 | private: |
---|
283 | char* paramName; //!< The name of the parameter. |
---|
284 | int paramCount; //!< The count of parameters. |
---|
285 | char** types; //!< What kind of parameters does this function take ?? |
---|
286 | char* description; //!< A longer description about this function. |
---|
287 | char** defaultValues; //!< The 'Default Values'. |
---|
288 | }; |
---|
289 | |
---|
290 | //! A class for descriptions of a loadable module |
---|
291 | class LoadClassDescription |
---|
292 | { |
---|
293 | friend class BaseLoadParam; |
---|
294 | public: |
---|
295 | LoadClassDescription(const char* className); |
---|
296 | ~LoadClassDescription(); |
---|
297 | |
---|
298 | static LoadClassDescription* addClass(const char* className); |
---|
299 | LoadParamDescription* addParam(const char* paramName); |
---|
300 | |
---|
301 | |
---|
302 | static void printAll(const char* fileName = NULL); |
---|
303 | |
---|
304 | private: |
---|
305 | static bool parametersDescription; //!< if parameter-description should be enabled. |
---|
306 | static tList<LoadClassDescription>* classList; //!< a list, that holds all the loadable classes. (after one instance has been loaded) |
---|
307 | char* className; //!< name of the class |
---|
308 | tList<LoadParamDescription>* paramList; //!< List of parameters this class knows. |
---|
309 | }; |
---|
310 | |
---|
311 | |
---|
312 | /************************** |
---|
313 | **** REAL DECLARATIONS **** |
---|
314 | **************************/ |
---|
315 | //! abstract Base class for a Loadable parameter |
---|
316 | class BaseLoadParam : public BaseObject |
---|
317 | { |
---|
318 | public: |
---|
319 | BaseLoadParam* describe(const char* descriptionText); |
---|
320 | |
---|
321 | protected: |
---|
322 | BaseLoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName, int paramCount, bool multi, const void* pointerToParam, ...); |
---|
323 | |
---|
324 | protected: |
---|
325 | LoadClassDescription* classDesc; //!< The LoadClassDescription of this LoadParameter |
---|
326 | LoadParamDescription* paramDesc; //!< The LoadParameterDescription of this LoadParameter |
---|
327 | const char* loadString; //!< The string loaded by this LoadParam |
---|
328 | const void* pointerToParam; //!< A Pointer to a Parameter. |
---|
329 | }; |
---|
330 | |
---|
331 | |
---|
332 | //! derived template class, so all the Classes can load something. |
---|
333 | template<class T> class LoadParam : public BaseLoadParam |
---|
334 | { |
---|
335 | public: |
---|
336 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(), bool multi = false) |
---|
337 | : BaseLoadParam(root, pt2Object, paramName, 0, multi, NULL, "") |
---|
338 | { |
---|
339 | if (loadString != NULL && root != NULL) |
---|
340 | (*pt2Object.*function)(); |
---|
341 | else |
---|
342 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); |
---|
343 | } |
---|
344 | |
---|
345 | |
---|
346 | //! makes functions with one string loadable |
---|
347 | LoadParam1(l_STRING); |
---|
348 | //! makes functions with two strings loadable |
---|
349 | LoadParam2(l_STRING, l_STRING); |
---|
350 | //! makes functions with three strings loadable |
---|
351 | LoadParam3(l_STRING, l_STRING, l_STRING); |
---|
352 | //! makes functions with four strings loadable |
---|
353 | LoadParam4(l_STRING, l_STRING, l_STRING, l_STRING); |
---|
354 | |
---|
355 | //! makes functions with one int loadable |
---|
356 | LoadParam1(l_INT); |
---|
357 | //! makes functions with two ints loadable |
---|
358 | LoadParam2(l_INT, l_INT); |
---|
359 | //! makes functions with three ints loadable |
---|
360 | LoadParam3(l_INT, l_INT, l_INT); |
---|
361 | //! makes functions with four ints loadable |
---|
362 | LoadParam4(l_INT, l_INT, l_INT, l_INT); |
---|
363 | |
---|
364 | //! makes functions with one float loadable |
---|
365 | LoadParam1(l_FLOAT); |
---|
366 | //! makes functions with two floats loadable |
---|
367 | LoadParam2(l_FLOAT, l_FLOAT); |
---|
368 | //! makes functions with three floats loadable |
---|
369 | LoadParam3(l_FLOAT, l_FLOAT, l_FLOAT); |
---|
370 | //! makes functions with four floats loadable |
---|
371 | LoadParam4(l_FLOAT, l_FLOAT, l_FLOAT, l_FLOAT); |
---|
372 | //! makes functions with four floats loadable |
---|
373 | LoadParam5(l_FLOAT, l_FLOAT, l_FLOAT, l_FLOAT, l_FLOAT); |
---|
374 | |
---|
375 | //! makes functions with one Vector loadable |
---|
376 | //LoadParam1(l_VECTOR); |
---|
377 | |
---|
378 | // loads a Ti-XML-element |
---|
379 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(const TiXmlElement*), bool multi = false) |
---|
380 | : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, "XML") |
---|
381 | { |
---|
382 | if (root != NULL) |
---|
383 | { |
---|
384 | const TiXmlElement* elem = root->FirstChildElement(paramName); |
---|
385 | if (likely(elem != NULL)) |
---|
386 | (*pt2Object.*function)(elem); |
---|
387 | else |
---|
388 | PRINTF(2)("%s of %s is empty", paramName, pt2Object->getClassName()); |
---|
389 | } |
---|
390 | else |
---|
391 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); |
---|
392 | } |
---|
393 | |
---|
394 | //LoadParamPT(l_XML_ELEM); |
---|
395 | }; |
---|
396 | |
---|
397 | // helper function |
---|
398 | |
---|
399 | const char* grabParameter(const TiXmlElement* root, const char* parameterName); |
---|
400 | |
---|
401 | #endif /* _LOAD_PARAM_H */ |
---|