Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: first Element gets loaded through the new LoadParam procedure.

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