Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: default Values should work now

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