Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: changed (void) → ()

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