Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5644 was 5644, checked in by bensch, 18 years ago

orxonox/trunk: better loadparam start and end-cycle.

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