Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/loading/load_param.h @ 5651

Last change on this file since 5651 was 5651, checked in by bensch, 19 years ago

orxonox/trunk: new (LoadParam && ExecutorSpecials)-Functionality that enables the Loading of XML-elements.
The new Executor really works out, and makes the understanding of loadParam way easier (given you won't read Executor :)

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