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 "base_object.h" |
---|
25 | |
---|
26 | #include "executor/executor.h" |
---|
27 | #include "executor/executor_specials.h" |
---|
28 | |
---|
29 | #include "helper_functions.h" |
---|
30 | |
---|
31 | // Forward Declaration // |
---|
32 | template<class T> class tList; |
---|
33 | class LoadClassDescription; |
---|
34 | class LoadParamDescription; |
---|
35 | class MultiType; |
---|
36 | |
---|
37 | |
---|
38 | /** |
---|
39 | * Loads a Parameter from ROOT named PARAMETER_NAME |
---|
40 | * onto OBJECT of CLASS, trough the FUNCTION |
---|
41 | * @param ROOT the TiXmlElement to load the Parameter from |
---|
42 | * @param PARAMETER_NAME the Name of the Parameter to load |
---|
43 | * @param OBJECT The BaseObject to load the new setting to. |
---|
44 | * @param CLASS What Class the BaseObejct is of (this is for identifying the Functuon) |
---|
45 | * @param FUNCTION The function of Class to Load (if you want to call &CLASS::FUNCTION write FUNCTION here). |
---|
46 | */ |
---|
47 | #define LoadParamNEW(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION) \ |
---|
48 | LoadParamBase(ROOT, PARAMETER_NAME, OBJECT, ExecutorObjective<CLASS>(&CLASS::FUNCTION), false) |
---|
49 | |
---|
50 | #define LoadParam_CYCLE(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION) \ |
---|
51 | LoadParamBase(ROOT, PARAMETER_NAME, OBJECT, ExecutorObjective<CLASS>(&CLASS::FUNCTION), true) |
---|
52 | |
---|
53 | #define LoadParamXML(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION) \ |
---|
54 | LoadParamBase(ROOT, PARAMETER_NAME, OBJECT, ExecutorXML<CLASS>(&CLASS::FUNCTION, ROOT, PARAMETER_NAME), false) |
---|
55 | |
---|
56 | #define LoadParamXML_CYCLE(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION) \ |
---|
57 | LoadParamBase(ROOT, PARAMETER_NAME, OBJECT, ExecutorXML<CLASS>(&CLASS::FUNCTION, ROOT, PARAMETER_NAME), true) |
---|
58 | |
---|
59 | |
---|
60 | /** |
---|
61 | * this Starts a Cycle in the Loading Process |
---|
62 | * be aware, that in the cycle the first parameter of load_param should because |
---|
63 | * called element, and that you must say true at the Fith parameter, or it will fail |
---|
64 | * also you will have to close the Cycle again with LOAD_PARAM_END_CYCLE |
---|
65 | * |
---|
66 | * @param ROOT The root XLM-element to search element under. |
---|
67 | * @param ELEMENT the element to search |
---|
68 | */ |
---|
69 | #define LOAD_PARAM_START_CYCLE(ROOT, ELEMENT) \ |
---|
70 | const TiXmlElement* ELEMENT; \ |
---|
71 | ELEMENT= ROOT->FirstChildElement(); \ |
---|
72 | while( ELEMENT != NULL) \ |
---|
73 | { |
---|
74 | /** |
---|
75 | * closes a LoadParam Loop |
---|
76 | * @see LOAD_PARAM_START_CYCLE |
---|
77 | * @param ELEMENT the Element to step through. |
---|
78 | */ |
---|
79 | #define LOAD_PARAM_END_CYCLE(ELEMENT) \ |
---|
80 | ELEMENT = ELEMENT->NextSiblingElement(); \ |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | /************************** |
---|
87 | **** REAL DECLARATIONS **** |
---|
88 | **************************/ |
---|
89 | //! abstract Base class for a Loadable parameter |
---|
90 | class LoadParamBase : public BaseObject |
---|
91 | { |
---|
92 | public: |
---|
93 | LoadParamBase(const TiXmlElement* root, const char* paramName, BaseObject* object, const Executor& executor, bool inLoadCycle = false); |
---|
94 | ~LoadParamBase(); |
---|
95 | |
---|
96 | LoadParamBase* describe(const char* descriptionText); |
---|
97 | LoadParamBase* defaultValues(unsigned int count, ...); |
---|
98 | |
---|
99 | protected: |
---|
100 | LoadParamBase(const TiXmlElement* root, BaseObject* object, const char* paramName, int paramCount, bool multi, const void* pointerToParam, ...); |
---|
101 | |
---|
102 | protected: |
---|
103 | bool inLoadCycle; |
---|
104 | Executor* executor; |
---|
105 | BaseObject* object; |
---|
106 | const char* paramName; |
---|
107 | |
---|
108 | LoadClassDescription* classDesc; //!< The LoadClassDescription of this LoadParameter |
---|
109 | LoadParamDescription* paramDesc; //!< The LoadParameterDescription of this LoadParameter |
---|
110 | const char* loadString; //!< The string loaded by this LoadParam |
---|
111 | const void* pointerToParam; //!< A Pointer to a Parameter. |
---|
112 | |
---|
113 | MultiType* defaultValue; |
---|
114 | }; |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | /***************************************** |
---|
119 | **** MACROS DEFINITIONS OF LOADABLES ***** |
---|
120 | *****************************************/ |
---|
121 | // 0. TYPES |
---|
122 | /** |
---|
123 | * a Macro to easily implement many different Constructors for the LoadParam-Class with no argument |
---|
124 | */ |
---|
125 | #define LoadParam0() \ |
---|
126 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(), bool multi = false) \ |
---|
127 | : LoadParamBase(root, pt2Object, paramName, 0, multi, NULL) \ |
---|
128 | { \ |
---|
129 | if (loadString != NULL && root != NULL) \ |
---|
130 | (*pt2Object.*function)(); \ |
---|
131 | else \ |
---|
132 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName());\ |
---|
133 | } |
---|
134 | |
---|
135 | // 1. TYPE |
---|
136 | /** |
---|
137 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 1 argument |
---|
138 | * @param type1 The type of the first functionParameter |
---|
139 | */ |
---|
140 | #define LoadParam1(type1) \ |
---|
141 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), \ |
---|
142 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT) \ |
---|
143 | : LoadParamBase(root, pt2Object, paramName, 1, multi, NULL, type1##_PARAM, default1) \ |
---|
144 | { \ |
---|
145 | if (loadString != NULL && root != NULL) \ |
---|
146 | (*pt2Object.*function)(type1##_FUNC(loadString, default1)); \ |
---|
147 | else \ |
---|
148 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
149 | } |
---|
150 | |
---|
151 | // 2. TYPES |
---|
152 | /** |
---|
153 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 2 arguments |
---|
154 | * @param type1 The type of the first functionParameter |
---|
155 | * @param type2 The type of the second functionParameter |
---|
156 | * |
---|
157 | * @TODO DEFAULT VALUES HACK |
---|
158 | */ |
---|
159 | #define LoadParam2(type1, type2) \ |
---|
160 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE), \ |
---|
161 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT) \ |
---|
162 | : LoadParamBase(root, pt2Object, paramName, 2, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2) \ |
---|
163 | { \ |
---|
164 | if (loadString != NULL && root != NULL) \ |
---|
165 | { \ |
---|
166 | SubString subLoads(loadString); \ |
---|
167 | if (subLoads.getCount() >= 1) \ |
---|
168 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2)); \ |
---|
169 | else \ |
---|
170 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
171 | paramName, pt2Object->getClassName(), 2, subLoads.getCount()); \ |
---|
172 | } \ |
---|
173 | else \ |
---|
174 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | // 3. TYPES |
---|
179 | /** |
---|
180 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 3 arguments |
---|
181 | * @param type1 The type of the first functionParameter |
---|
182 | * @param type2 The type of the second functionParameter |
---|
183 | * @param type3 The type of the third functionParameter |
---|
184 | */ |
---|
185 | #define LoadParam3(type1, type2, type3) \ |
---|
186 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE), \ |
---|
187 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT)\ |
---|
188 | : LoadParamBase(root, pt2Object, paramName, 3, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3) \ |
---|
189 | { \ |
---|
190 | if (loadString != NULL && root != NULL) \ |
---|
191 | { \ |
---|
192 | SubString subLoads(loadString); \ |
---|
193 | if (subLoads.getCount() == 3) \ |
---|
194 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3)); \ |
---|
195 | else \ |
---|
196 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
197 | paramName, pt2Object->getClassName(), 3, subLoads.getCount()); \ |
---|
198 | } \ |
---|
199 | else \ |
---|
200 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | // 4. TYPES |
---|
205 | /** |
---|
206 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 4 arguments |
---|
207 | * @param type1 The type of the first functionParameter |
---|
208 | * @param type2 The type of the second functionParameter |
---|
209 | * @param type3 The type of the third functionParameter |
---|
210 | * @param type4 The type of the forth functionParameter |
---|
211 | */ |
---|
212 | #define LoadParam4(type1, type2, type3, type4) \ |
---|
213 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE), \ |
---|
214 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \ |
---|
215 | type4##_TYPE default4 = type4##_DEFAULT) \ |
---|
216 | : LoadParamBase(root, pt2Object, paramName, 4, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3, \ |
---|
217 | type4##_PARAM, default4) \ |
---|
218 | { \ |
---|
219 | if (loadString != NULL && root != NULL) \ |
---|
220 | { \ |
---|
221 | SubString subLoads(loadString); \ |
---|
222 | if (subLoads.getCount() == 4) \ |
---|
223 | (*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)); \ |
---|
224 | else \ |
---|
225 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
226 | paramName, pt2Object->getClassName(), 4, subLoads.getCount()); \ |
---|
227 | } \ |
---|
228 | else \ |
---|
229 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | // 5. TYPES |
---|
234 | /** |
---|
235 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 5 arguments |
---|
236 | * @param type1 The type of the first functionParameter |
---|
237 | * @param type2 The type of the second functionParameter |
---|
238 | * @param type3 The type of the third functionParameter |
---|
239 | * @param type4 The type of the forth functionParameter |
---|
240 | * @param type5 The type of the fifth functionParameter |
---|
241 | */ |
---|
242 | #define LoadParam5(type1, type2, type3, type4, type5) \ |
---|
243 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, \ |
---|
244 | void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE, type5##_TYPE), \ |
---|
245 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \ |
---|
246 | type4##_TYPE default4 = type4##_DEFAULT, type5##_TYPE default5 = type5##_DEFAULT ) \ |
---|
247 | : LoadParamBase(root, pt2Object, paramName, 5, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3, \ |
---|
248 | type4##_PARAM, default4, type5##_PARAM, default5) \ |
---|
249 | { \ |
---|
250 | if (loadString != NULL && root != NULL) \ |
---|
251 | { \ |
---|
252 | SubString subLoads(loadString); \ |
---|
253 | if (subLoads.getCount() == 5) \ |
---|
254 | (*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)); \ |
---|
255 | else \ |
---|
256 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
257 | paramName, pt2Object->getClassName(), 5, subLoads.getCount()); \ |
---|
258 | } \ |
---|
259 | else \ |
---|
260 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
261 | } |
---|
262 | |
---|
263 | // Pointer TYPE |
---|
264 | /** |
---|
265 | * a Macro to easily implement many different Constructors for the LoadParam-Class with one Pointer argument |
---|
266 | * @param type1 The type of the Pointer |
---|
267 | */ |
---|
268 | #define LoadParamPT(type1) \ |
---|
269 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), type1##_TYPE pointerToParam, bool multi = false) \ |
---|
270 | : LoadParamBase(root, pt2Object, paramName, 1, multi, pointerToParam, type1##_PARAM) \ |
---|
271 | { \ |
---|
272 | if (pointerToParam != NULL && root != NULL) \ |
---|
273 | (*pt2Object.*function)((type1##_TYPE) pointerToParam); \ |
---|
274 | else \ |
---|
275 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
276 | } |
---|
277 | |
---|
278 | //! derived template class, so all the Classes can load something. |
---|
279 | template<class T> class LoadParam : public LoadParamBase |
---|
280 | { |
---|
281 | public: |
---|
282 | |
---|
283 | #define FUNCTOR_LIST(x) LoadParam ## x |
---|
284 | #include "functor_list.h" |
---|
285 | #undef FUNCTOR_LIST |
---|
286 | }; |
---|
287 | |
---|
288 | // helper function |
---|
289 | |
---|
290 | const char* grabParameter(const TiXmlElement* root, const char* parameterName); |
---|
291 | |
---|
292 | #endif /* _LOAD_PARAM_H */ |
---|