Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: made bool's loadable

File size: 17.4 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#include "vector.h"
26#include "factory.h"
27#include "debug.h"
28#include "substring.h"
29#include "tinyxml.h"
30
31// Forward Declaration //
32template<class T> class tList;
33
34//! macro that makes it even more easy to load a Parameter
35/**
36 * @param className the name of the class to load
37 * @param parameterName the name of the parameter to load as written in the XML-file
38 * @param function the function to call
39 */
40#define LOAD_PARAM(className, parameterName, paramFunction) \
41        LoadParam<className>(root, #parameterName, this, &className::paramFunction)
42
43/**
44 * this Starts a Cycle in the Loading Process
45 * be aware, that in the cycle the first parameter of load_param should because
46 * called element, and that you must say true at the Fith parameter, or it will fail
47 * also you will have to close the Cycle again with LOAD_PARAM_END_CYCLE
48 */
49#define LOAD_PARAM_START_CYCLE   const TiXmlElement* element; \
50                                 element = root->FirstChildElement(); \
51                                 while( element != NULL) \
52                                  {
53/**
54 * closes a LoadParam Loop
55 * @see LOAD_PARAM_START_CYCLE
56 */
57#define LOAD_PARAM_END_CYCLE        element = element->NextSiblingElement(); \
58                                  }
59
60
61
62/**
63   useable FunctionParameters are:
64   l_INT:       int
65   l_LONG:      long
66   l_SHORT:     short
67   l_FLOAT:     float
68   l_STRING:    const char*
69   l_XML_ELEM:  TiXmlElement*
70*/
71
72#define l_BOOL_TYPE        bool                 //!< The type of an BOOL
73#define l_BOOL_FUNC        isBool               //!< The function to call to parse BOOL
74#define l_BOOL_NAME        "bool"               //!< The name of an BOOL
75#define l_BOOL_DEFAULT     0                    //!< a default Value for an BOOL
76
77
78#define l_INT_TYPE         int                  //!< The type of an INT
79#define l_INT_FUNC         isInt                //!< The function to call to parse INT
80#define l_INT_NAME         "int"                //!< The name of an INT
81#define l_INT_DEFAULT      true                 //!< a default Value for an INT
82
83#define l_LONG_TYPE        long                 //!< The type of a LONG
84#define l_LONG_FUNC        isInt                //!< The function to parse a LONG
85#define l_LONG_NAME        "long"               //!< The name of a LONG
86#define l_LONG_DEFAULT     0                    //!< a default Value for a LONG
87
88// #define l_SHORT_TYPE       short                //!< The type of a SHORT
89// #define l_SHORT_FUNC       atoi                 //!< The function to parse a SHORT
90// #define l_SHORT_NAME       "short"              //!< The name of a SHORT
91// #define l_SHORT_DEFAULT    0                    //!< a default Value for a SHORT
92
93#define l_FLOAT_TYPE       float                //!< The type of a FLOAT
94#define l_FLOAT_FUNC       isFloat              //!< The function to parse a FLOAT
95#define l_FLOAT_NAME       "float"              //!< The name of a FLOAT
96#define l_FLOAT_DEFAULT    0.0                  //!< a default Value for a FLOAT
97
98//#define l_VECTOR_TYPE      const Vector&        //!< The type of a VECTOR
99//#define l_VECTOR_FUNC      isVector             //!< The function to parse a VECTOR
100//#define l_VECTOR_NAME      "Vector[x/y/z]"      //!< The name of a VECTOR
101//#define l_VECTOR_DEFAULT   Vector(0,0,0)        //!< Default value for a VECTOR
102
103#define l_STRING_TYPE      const char*          //!< The type of a STRING
104#define l_STRING_FUNC      isString             //!< The function to parse a STRING
105#define l_STRING_NAME      "string"             //!< The name of a STRING
106#define l_STRING_DEFAULT   ""                   //!< a default Value for an STRING
107
108#define l_XML_ELEM_TYPE    const TiXmlElement*  //!< The type of an XML_ELEM
109#define l_XML_ELEM_FUNC                         //!< The function to parse an XML_ELEM
110#define l_XML_ELEM_NAME    "XML"                //!< The name of an XML_ELEM
111#define l_XML_ELEM_DEFAULT NULL                 //!< The dafault Value for an XML_ELEM
112
113
114/*****************************************
115**** MACROS DEFINITIONS OF LOADABLES *****
116*****************************************/
117// 1. TYPE
118/**
119 *  a Macro to easily implement many different Constructors for the LoadParam-Class with 1 argument
120 * @param type1 The type of the first functionParameter
121*/
122#define LoadParam1(type1) \
123 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), \
124           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT) \
125  : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, type1##_NAME, default1) \
126    { \
127      if (loadString != NULL && root != NULL) \
128        (*pt2Object.*function)(type1##_FUNC(loadString, default1)); \
129      else \
130        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
131    }
132
133
134// 2. TYPES
135/**
136 *  a Macro to easily implement many different Constructors for the LoadParam-Class with 2 arguments
137 * @param type1 The type of the first functionParameter
138 * @param type2 The type of the second functionParameter
139*/
140#define LoadParam2(type1, type2) \
141 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE), \
142           bool multi = false,  type1##_TYPE default1 = type1##_DEFAULT,  type2##_TYPE default2 = type2##_DEFAULT) \
143  : BaseLoadParam(root, pt2Object, paramName, 2, multi, NULL, type1##_NAME, default1, type2##_NAME, default2) \
144    { \
145      if (loadString != NULL && root != NULL) \
146        { \
147          SubString subLoads(loadString); \
148          if (subLoads.getCount() == 2) \
149            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2)); \
150          else \
151            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
152                      paramName, pt2Object->getClassName(), 2, subLoads.getCount()); \
153        } \
154      else \
155        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
156    }
157
158
159// 3. TYPES
160/**
161 *  a Macro to easily implement many different Constructors for the LoadParam-Class with 3 arguments
162 * @param type1 The type of the first functionParameter
163 * @param type2 The type of the second functionParameter
164 * @param type3 The type of the third functionParameter
165*/
166#define LoadParam3(type1, type2, type3) \
167 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE), \
168           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT)\
169  : BaseLoadParam(root, pt2Object, paramName, 3, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3) \
170    { \
171      if (loadString != NULL && root != NULL) \
172        { \
173          SubString subLoads(loadString); \
174          if (subLoads.getCount() == 3) \
175            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3)); \
176          else \
177            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
178                      paramName, pt2Object->getClassName(), 3, subLoads.getCount()); \
179        } \
180      else \
181        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
182    }
183
184
185// 4. TYPES
186/**
187 *  a Macro to easily implement many different Constructors for the LoadParam-Class with 4 arguments
188 * @param type1 The type of the first functionParameter
189 * @param type2 The type of the second functionParameter
190 * @param type3 The type of the third functionParameter
191 * @param type4 The type of the forth functionParameter
192*/
193#define LoadParam4(type1, type2, type3, type4) \
194 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE), \
195           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \
196           type4##_TYPE default4 = type4##_DEFAULT) \
197  : BaseLoadParam(root, pt2Object, paramName, 4, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3, \
198                  type4##_NAME, default4) \
199    { \
200      if (loadString != NULL && root != NULL) \
201        { \
202          SubString subLoads(loadString); \
203          if (subLoads.getCount() == 4) \
204            (*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)); \
205          else \
206            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
207                      paramName, pt2Object->getClassName(), 4, subLoads.getCount()); \
208        } \
209      else \
210        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
211    }
212
213
214// 5. TYPES
215/**
216 *  a Macro to easily implement many different Constructors for the LoadParam-Class with 5 arguments
217 * @param type1 The type of the first functionParameter
218 * @param type2 The type of the second functionParameter
219 * @param type3 The type of the third functionParameter
220 * @param type4 The type of the forth functionParameter
221 * @param type5 The type of the fifth functionParameter
222*/
223#define LoadParam5(type1, type2, type3, type4, type5) \
224 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, \
225           void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE, type5##_TYPE), \
226           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \
227           type4##_TYPE default4 = type4##_DEFAULT, type5##_TYPE default5 = type5##_DEFAULT ) \
228  : BaseLoadParam(root, pt2Object, paramName, 5, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3, \
229                  type4##_NAME, default4, type5##_NAME, default5) \
230    { \
231      if (loadString != NULL && root != NULL) \
232        { \
233          SubString subLoads(loadString); \
234          if (subLoads.getCount() == 5) \
235            (*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)); \
236          else \
237            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
238                      paramName, pt2Object->getClassName(), 5, subLoads.getCount()); \
239        } \
240      else \
241        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
242    }
243
244// Pointer TYPE
245/**
246 *  a Macro to easily implement many different Constructors for the LoadParam-Class with one Pointer argument
247 * @param type1 The type of the Pointer
248 */
249#define LoadParamPT(type1) \
250 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), type1##_TYPE pointerToParam, bool multi = false) \
251  : BaseLoadParam(root, pt2Object, paramName, 1, multi, pointerToParam, type1##_NAME) \
252{ \
253      if (pointerToParam != NULL && root != NULL) \
254        (*pt2Object.*function)((type1##_TYPE) pointerToParam); \
255      else \
256        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
257}
258
259
260/***********************
261*** HELPER FUNCTIONS ***
262***********************/
263bool          isBool(const char* BOOL, bool defaultValue);
264int           isInt(const char* INT, int defaultValue);
265float         isFloat(const char* FLOAT, float defaultValue);
266//const Vector& isVector(const char* VECTOR, const Vector& defaultValue);
267const char*   isString(const char* STRING, const char* defaultValue);
268
269//TiXmlEmlemnt* isXmlElem(const)
270
271
272/************************
273*** DESCRIPTION STUFF ***
274************************/
275//! A class that handles the description of loadable parameters
276class LoadParamDescription
277{
278  friend class BaseLoadParam;
279  friend class LoadClassDescription;
280 public:
281  LoadParamDescription(const char* paramName);
282  ~LoadParamDescription();
283
284  void setDescription(const char* descriptionText);
285  /** @returns the descriptionString */
286  const char* getDescription() { return this->description; };
287
288  void print() const;
289 private:
290  char*         paramName;             //!< The name of the parameter.
291  int           paramCount;            //!< The count of parameters.
292  char**        types;                 //!< What kind of parameters does this function take ??
293  char*         description;           //!< A longer description about this function.
294  char**        defaultValues;         //!< The 'Default Values'.
295};
296
297//! A class for descriptions of a loadable module
298class LoadClassDescription
299{
300  friend class BaseLoadParam;
301 public:
302  LoadClassDescription(const char* className);
303  ~LoadClassDescription();
304
305  static LoadClassDescription* addClass(const char* className);
306  LoadParamDescription* addParam(const char* paramName);
307
308
309  static void printAll(const char* fileName = NULL);
310
311 private:
312  static bool                          parametersDescription;  //!< if parameter-description should be enabled.
313  static tList<LoadClassDescription>*  classList;              //!< a list, that holds all the loadable classes. (after one instance has been loaded)
314  char*                                className;              //!< name of the class
315  tList<LoadParamDescription>*         paramList;              //!< List of parameters this class knows.
316};
317
318
319/**************************
320**** REAL DECLARATIONS ****
321**************************/
322//! abstract Base class for a Loadable parameter
323class BaseLoadParam : public BaseObject
324{
325 public:
326  BaseLoadParam* describe(const char* descriptionText);
327
328 protected:
329  BaseLoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName, int paramCount, bool multi, const void* pointerToParam, ...);
330
331 protected:
332  LoadClassDescription*    classDesc;            //!< The LoadClassDescription of this LoadParameter
333  LoadParamDescription*    paramDesc;            //!< The LoadParameterDescription of this LoadParameter
334  const char*              loadString;           //!< The string loaded by this LoadParam
335  const void*              pointerToParam;       //!< A Pointer to a Parameter.
336};
337
338
339//! derived template class, so all the Classes can load something.
340template<class T> class LoadParam : public BaseLoadParam
341{
342 public:
343  LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(), bool multi = false)
344    : BaseLoadParam(root, pt2Object, paramName, 0, multi, NULL, "")
345    {
346      if (loadString != NULL && root != NULL)
347        (*pt2Object.*function)();
348      else
349        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName());
350    }
351
352
353  //! makes functions with one string loadable
354  LoadParam1(l_STRING);
355  //! makes functions with two strings loadable
356  LoadParam2(l_STRING, l_STRING);
357  //! makes functions with three strings loadable
358  LoadParam3(l_STRING, l_STRING, l_STRING);
359  //! makes functions with four strings loadable
360  LoadParam4(l_STRING, l_STRING, l_STRING, l_STRING);
361
362  //! makes functions with one bool loadeable
363  LoadParam1(l_BOOL);
364
365  //! makes functions with one int loadable
366  LoadParam1(l_INT);
367  //! makes functions with two ints loadable
368  LoadParam2(l_INT, l_INT);
369  //! makes functions with three ints loadable
370  LoadParam3(l_INT, l_INT, l_INT);
371  //! makes functions with four ints loadable
372  LoadParam4(l_INT, l_INT, l_INT, l_INT);
373
374  //! makes functions with one float loadable
375  LoadParam1(l_FLOAT);
376  //! makes functions with two floats loadable
377  LoadParam2(l_FLOAT, l_FLOAT);
378  //! makes functions with three floats loadable
379  LoadParam3(l_FLOAT, l_FLOAT, l_FLOAT);
380  //! makes functions with four floats loadable
381  LoadParam4(l_FLOAT, l_FLOAT, l_FLOAT, l_FLOAT);
382  //! makes functions with four floats loadable
383  LoadParam5(l_FLOAT, l_FLOAT, l_FLOAT, l_FLOAT, l_FLOAT);
384
385  //! makes functions with one Vector loadable
386  //LoadParam1(l_VECTOR);
387
388  // loads a Ti-XML-element
389  LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(const TiXmlElement*), bool multi = false)
390  : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, "XML")
391  {
392    if (root != NULL)
393    {
394      const TiXmlElement* elem = root->FirstChildElement(paramName);
395      if (likely(elem != NULL))
396        (*pt2Object.*function)(elem);
397      else
398        PRINTF(2)("%s of %s is empty", paramName, pt2Object->getClassName());
399    }
400    else
401      PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName());
402  }
403
404  //LoadParamPT(l_XML_ELEM);
405};
406
407// helper function
408
409const char* grabParameter(const TiXmlElement* root, const char* parameterName);
410
411#endif /* _LOAD_PARAM_H */
Note: See TracBrowser for help on using the repository browser.