Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: almost mac compatibility

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