Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: cleanup of LoadParam

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