Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Background for the Shell

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