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 | * 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 "functor_list.h" |
---|
25 | #include "base_object.h" |
---|
26 | |
---|
27 | #include "factory.h" |
---|
28 | #include "debug.h" |
---|
29 | #include "substring.h" |
---|
30 | #include "tinyxml.h" |
---|
31 | #include "helper_functions.h" |
---|
32 | |
---|
33 | // Forward Declaration // |
---|
34 | template<class T> class tList; |
---|
35 | |
---|
36 | /************************ |
---|
37 | *** DESCRIPTION STUFF *** |
---|
38 | ************************/ |
---|
39 | //! A class that handles the description of loadable parameters |
---|
40 | class LoadParamDescription |
---|
41 | { |
---|
42 | friend class BaseLoadParam; |
---|
43 | friend class LoadClassDescription; |
---|
44 | public: |
---|
45 | LoadParamDescription(const char* paramName); |
---|
46 | ~LoadParamDescription(); |
---|
47 | |
---|
48 | void setDescription(const char* descriptionText); |
---|
49 | /** @returns the descriptionString */ |
---|
50 | const char* getDescription() { return this->description; }; |
---|
51 | |
---|
52 | void print() const; |
---|
53 | private: |
---|
54 | char* paramName; //!< The name of the parameter. |
---|
55 | int paramCount; //!< The count of parameters. |
---|
56 | int* types; //!< What kind of parameters does this function take ?? |
---|
57 | char* description; //!< A longer description about this function. |
---|
58 | char** defaultValues; //!< The 'Default Values'. |
---|
59 | }; |
---|
60 | |
---|
61 | //! A class for descriptions of a loadable module |
---|
62 | class LoadClassDescription |
---|
63 | { |
---|
64 | friend class BaseLoadParam; |
---|
65 | public: |
---|
66 | LoadClassDescription(const char* className); |
---|
67 | ~LoadClassDescription(); |
---|
68 | |
---|
69 | static LoadClassDescription* addClass(const char* className); |
---|
70 | LoadParamDescription* addParam(const char* paramName); |
---|
71 | |
---|
72 | static void deleteAllDescriptions(); |
---|
73 | |
---|
74 | static void printAll(const char* fileName = NULL); |
---|
75 | static tList<const char>* searchClassWithShort(const char* classNameBegin); |
---|
76 | // static const LoadParamDescription* getClass(const char* className); |
---|
77 | |
---|
78 | private: |
---|
79 | static bool parametersDescription; //!< if parameter-description should be enabled. |
---|
80 | static tList<LoadClassDescription>* classList; //!< a list, that holds all the loadable classes. (after one instance has been loaded) |
---|
81 | char* className; //!< name of the class |
---|
82 | tList<LoadParamDescription>* paramList; //!< List of parameters this class knows. |
---|
83 | }; |
---|
84 | |
---|
85 | |
---|
86 | /************************** |
---|
87 | **** REAL DECLARATIONS **** |
---|
88 | **************************/ |
---|
89 | //! abstract Base class for a Loadable parameter |
---|
90 | class BaseLoadParam : public BaseObject |
---|
91 | { |
---|
92 | public: |
---|
93 | BaseLoadParam* describe(const char* descriptionText); |
---|
94 | |
---|
95 | protected: |
---|
96 | BaseLoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName, int paramCount, bool multi, const void* pointerToParam, ...); |
---|
97 | |
---|
98 | protected: |
---|
99 | LoadClassDescription* classDesc; //!< The LoadClassDescription of this LoadParameter |
---|
100 | LoadParamDescription* paramDesc; //!< The LoadParameterDescription of this LoadParameter |
---|
101 | const char* loadString; //!< The string loaded by this LoadParam |
---|
102 | const void* pointerToParam; //!< A Pointer to a Parameter. |
---|
103 | }; |
---|
104 | |
---|
105 | |
---|
106 | //! macro that makes it even more easy to load a Parameter |
---|
107 | /** |
---|
108 | * @param className the name of the class to load |
---|
109 | * @param parameterName the name of the parameter to load as written in the XML-file |
---|
110 | * @param function the function to call |
---|
111 | */ |
---|
112 | #define LOAD_PARAM(className, parameterName, paramFunction) \ |
---|
113 | LoadParam<className>(root, #parameterName, this, &className::paramFunction) |
---|
114 | |
---|
115 | /** |
---|
116 | * this Starts a Cycle in the Loading Process |
---|
117 | * be aware, that in the cycle the first parameter of load_param should because |
---|
118 | * called element, and that you must say true at the Fith parameter, or it will fail |
---|
119 | * also you will have to close the Cycle again with LOAD_PARAM_END_CYCLE |
---|
120 | */ |
---|
121 | #define LOAD_PARAM_START_CYCLE const TiXmlElement* element; \ |
---|
122 | element = root->FirstChildElement(); \ |
---|
123 | while( element != NULL) \ |
---|
124 | { |
---|
125 | /** |
---|
126 | * closes a LoadParam Loop |
---|
127 | * @see LOAD_PARAM_START_CYCLE |
---|
128 | */ |
---|
129 | #define LOAD_PARAM_END_CYCLE element = element->NextSiblingElement(); \ |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | /***************************************** |
---|
134 | **** MACROS DEFINITIONS OF LOADABLES ***** |
---|
135 | *****************************************/ |
---|
136 | // 0. TYPES |
---|
137 | /** |
---|
138 | * a Macro to easily implement many different Constructors for the LoadParam-Class with no argument |
---|
139 | */ |
---|
140 | #define LoadParam0() \ |
---|
141 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(), bool multi = false) \ |
---|
142 | : BaseLoadParam(root, pt2Object, paramName, 0, multi, NULL, "") \ |
---|
143 | { \ |
---|
144 | if (loadString != NULL && root != NULL) \ |
---|
145 | (*pt2Object.*function)(); \ |
---|
146 | else \ |
---|
147 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName());\ |
---|
148 | } |
---|
149 | |
---|
150 | // 1. TYPE |
---|
151 | /** |
---|
152 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 1 argument |
---|
153 | * @param type1 The type of the first functionParameter |
---|
154 | */ |
---|
155 | #define LoadParam1(type1) \ |
---|
156 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), \ |
---|
157 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT) \ |
---|
158 | : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, type1##_PARAM, default1) \ |
---|
159 | { \ |
---|
160 | if (loadString != NULL && root != NULL) \ |
---|
161 | (*pt2Object.*function)(type1##_FUNC(loadString, default1)); \ |
---|
162 | else \ |
---|
163 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
164 | } |
---|
165 | |
---|
166 | // 2. TYPES |
---|
167 | /** |
---|
168 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 2 arguments |
---|
169 | * @param type1 The type of the first functionParameter |
---|
170 | * @param type2 The type of the second functionParameter |
---|
171 | */ |
---|
172 | #define LoadParam2(type1, type2) \ |
---|
173 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE), \ |
---|
174 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT) \ |
---|
175 | : BaseLoadParam(root, pt2Object, paramName, 2, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2) \ |
---|
176 | { \ |
---|
177 | if (loadString != NULL && root != NULL) \ |
---|
178 | { \ |
---|
179 | SubString subLoads(loadString); \ |
---|
180 | if (subLoads.getCount() == 2) \ |
---|
181 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2)); \ |
---|
182 | else \ |
---|
183 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
184 | paramName, pt2Object->getClassName(), 2, subLoads.getCount()); \ |
---|
185 | } \ |
---|
186 | else \ |
---|
187 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | // 3. TYPES |
---|
192 | /** |
---|
193 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 3 arguments |
---|
194 | * @param type1 The type of the first functionParameter |
---|
195 | * @param type2 The type of the second functionParameter |
---|
196 | * @param type3 The type of the third functionParameter |
---|
197 | */ |
---|
198 | #define LoadParam3(type1, type2, type3) \ |
---|
199 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE), \ |
---|
200 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT)\ |
---|
201 | : BaseLoadParam(root, pt2Object, paramName, 3, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3) \ |
---|
202 | { \ |
---|
203 | if (loadString != NULL && root != NULL) \ |
---|
204 | { \ |
---|
205 | SubString subLoads(loadString); \ |
---|
206 | if (subLoads.getCount() == 3) \ |
---|
207 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3)); \ |
---|
208 | else \ |
---|
209 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
210 | paramName, pt2Object->getClassName(), 3, subLoads.getCount()); \ |
---|
211 | } \ |
---|
212 | else \ |
---|
213 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | // 4. TYPES |
---|
218 | /** |
---|
219 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 4 arguments |
---|
220 | * @param type1 The type of the first functionParameter |
---|
221 | * @param type2 The type of the second functionParameter |
---|
222 | * @param type3 The type of the third functionParameter |
---|
223 | * @param type4 The type of the forth functionParameter |
---|
224 | */ |
---|
225 | #define LoadParam4(type1, type2, type3, type4) \ |
---|
226 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE), \ |
---|
227 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \ |
---|
228 | type4##_TYPE default4 = type4##_DEFAULT) \ |
---|
229 | : BaseLoadParam(root, pt2Object, paramName, 4, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3, \ |
---|
230 | type4##_PARAM, default4) \ |
---|
231 | { \ |
---|
232 | if (loadString != NULL && root != NULL) \ |
---|
233 | { \ |
---|
234 | SubString subLoads(loadString); \ |
---|
235 | if (subLoads.getCount() == 4) \ |
---|
236 | (*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)); \ |
---|
237 | else \ |
---|
238 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
239 | paramName, pt2Object->getClassName(), 4, subLoads.getCount()); \ |
---|
240 | } \ |
---|
241 | else \ |
---|
242 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | // 5. TYPES |
---|
247 | /** |
---|
248 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 5 arguments |
---|
249 | * @param type1 The type of the first functionParameter |
---|
250 | * @param type2 The type of the second functionParameter |
---|
251 | * @param type3 The type of the third functionParameter |
---|
252 | * @param type4 The type of the forth functionParameter |
---|
253 | * @param type5 The type of the fifth functionParameter |
---|
254 | */ |
---|
255 | #define LoadParam5(type1, type2, type3, type4, type5) \ |
---|
256 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, \ |
---|
257 | void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE, type5##_TYPE), \ |
---|
258 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \ |
---|
259 | type4##_TYPE default4 = type4##_DEFAULT, type5##_TYPE default5 = type5##_DEFAULT ) \ |
---|
260 | : BaseLoadParam(root, pt2Object, paramName, 5, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3, \ |
---|
261 | type4##_PARAM, default4, type5##_PARAM, default5) \ |
---|
262 | { \ |
---|
263 | if (loadString != NULL && root != NULL) \ |
---|
264 | { \ |
---|
265 | SubString subLoads(loadString); \ |
---|
266 | if (subLoads.getCount() == 5) \ |
---|
267 | (*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)); \ |
---|
268 | else \ |
---|
269 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
270 | paramName, pt2Object->getClassName(), 5, subLoads.getCount()); \ |
---|
271 | } \ |
---|
272 | else \ |
---|
273 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
274 | } |
---|
275 | |
---|
276 | // Pointer TYPE |
---|
277 | /** |
---|
278 | * a Macro to easily implement many different Constructors for the LoadParam-Class with one Pointer argument |
---|
279 | * @param type1 The type of the Pointer |
---|
280 | */ |
---|
281 | #define LoadParamPT(type1) \ |
---|
282 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), type1##_TYPE pointerToParam, bool multi = false) \ |
---|
283 | : BaseLoadParam(root, pt2Object, paramName, 1, multi, pointerToParam, type1##_PARAM) \ |
---|
284 | { \ |
---|
285 | if (pointerToParam != NULL && root != NULL) \ |
---|
286 | (*pt2Object.*function)((type1##_TYPE) pointerToParam); \ |
---|
287 | else \ |
---|
288 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
289 | } |
---|
290 | |
---|
291 | //! derived template class, so all the Classes can load something. |
---|
292 | template<class T> class LoadParam : public BaseLoadParam |
---|
293 | { |
---|
294 | public: |
---|
295 | |
---|
296 | #define FUNCTOR_LIST(x) LoadParam ## x |
---|
297 | #include "functor_list.h" |
---|
298 | #undef FUNCTOR_LIST |
---|
299 | |
---|
300 | //! makes functions with one Vector loadable |
---|
301 | //LoadParam1(l_VECTOR); |
---|
302 | |
---|
303 | // loads a Ti-XML-element |
---|
304 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(const TiXmlElement*), bool multi = false) |
---|
305 | : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, "XML") |
---|
306 | { |
---|
307 | if (root != NULL) |
---|
308 | { |
---|
309 | const TiXmlElement* elem = root->FirstChildElement(paramName); |
---|
310 | if (elem != NULL) |
---|
311 | (*pt2Object.*function)(elem); |
---|
312 | else |
---|
313 | PRINTF(4)("%s of %s is empty\n", paramName, pt2Object->getClassName()); |
---|
314 | } |
---|
315 | else |
---|
316 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); |
---|
317 | } |
---|
318 | |
---|
319 | //LoadParamPT(l_XML_ELEM); |
---|
320 | }; |
---|
321 | |
---|
322 | // helper function |
---|
323 | |
---|
324 | const char* grabParameter(const TiXmlElement* root, const char* parameterName); |
---|
325 | |
---|
326 | #endif /* _LOAD_PARAM_H */ |
---|