Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new Macro for LoadParam

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