Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: made bool's loadable

File size: 17.4 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
[4836]18  *  A Class and macro-functions, that makes our lives easy to load-in parameters
[4250]19*/
20
[4233]21#ifndef _LOAD_PARAM_H
22#define _LOAD_PARAM_H
23
[4597]24#include "base_object.h"
[4734]25#include "vector.h"
[4239]26#include "factory.h"
27#include "debug.h"
[4241]28#include "substring.h"
[4598]29#include "tinyxml.h"
[4233]30
[4251]31// Forward Declaration //
32template<class T> class tList;
33
[4495]34//! macro that makes it even more easy to load a Parameter
[4249]35/**
[4834]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 */
[4495]40#define LOAD_PARAM(className, parameterName, paramFunction) \
41        LoadParam<className>(root, #parameterName, this, &className::paramFunction)
42
43/**
[4834]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/**
[4249]63   useable FunctionParameters are:
[4598]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*
[4249]70*/
[4233]71
[4860]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
[4623]78#define l_INT_TYPE         int                  //!< The type of an INT
[4624]79#define l_INT_FUNC         isInt                //!< The function to call to parse INT
[4623]80#define l_INT_NAME         "int"                //!< The name of an INT
[4860]81#define l_INT_DEFAULT      true                 //!< a default Value for an INT
[4233]82
[4623]83#define l_LONG_TYPE        long                 //!< The type of a LONG
[4624]84#define l_LONG_FUNC        isInt                //!< The function to parse a LONG
[4623]85#define l_LONG_NAME        "long"               //!< The name of a LONG
86#define l_LONG_DEFAULT     0                    //!< a default Value for a LONG
[4233]87
[4623]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
[4242]92
[4623]93#define l_FLOAT_TYPE       float                //!< The type of a FLOAT
[4624]94#define l_FLOAT_FUNC       isFloat              //!< The function to parse a FLOAT
[4623]95#define l_FLOAT_NAME       "float"              //!< The name of a FLOAT
96#define l_FLOAT_DEFAULT    0.0                  //!< a default Value for a FLOAT
[4242]97
[4734]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
[4623]103#define l_STRING_TYPE      const char*          //!< The type of a STRING
[4624]104#define l_STRING_FUNC      isString             //!< The function to parse a STRING
[4623]105#define l_STRING_NAME      "string"             //!< The name of a STRING
106#define l_STRING_DEFAULT   ""                   //!< a default Value for an STRING
[4242]107
[4623]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
[4598]112
[4623]113
114/*****************************************
115**** MACROS DEFINITIONS OF LOADABLES *****
116*****************************************/
[4487]117// 1. TYPE
[4249]118/**
[4836]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
[4249]121*/
122#define LoadParam1(type1) \
[4623]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) \
[4249]126    { \
[4252]127      if (loadString != NULL && root != NULL) \
[4624]128        (*pt2Object.*function)(type1##_FUNC(loadString, default1)); \
[4249]129      else \
[4592]130        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[4249]131    }
[4241]132
[4249]133
134// 2. TYPES
[4487]135/**
[4836]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
[4487]139*/
[4250]140#define LoadParam2(type1, type2) \
[4623]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) \
[4249]144    { \
[4252]145      if (loadString != NULL && root != NULL) \
[4592]146        { \
147          SubString subLoads(loadString); \
148          if (subLoads.getCount() == 2) \
[4624]149            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2)); \
[4592]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        } \
[4249]154      else \
[4592]155        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[4251]156    }
[4241]157
[4243]158
[4249]159// 3. TYPES
[4487]160/**
[4836]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
[4487]165*/
[4250]166#define LoadParam3(type1, type2, type3) \
[4623]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) \
[4249]170    { \
[4252]171      if (loadString != NULL && root != NULL) \
[4592]172        { \
173          SubString subLoads(loadString); \
174          if (subLoads.getCount() == 3) \
[4624]175            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3)); \
[4592]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        } \
[4249]180      else \
[4592]181        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[4251]182    }
[4241]183
[4249]184
185// 4. TYPES
[4487]186/**
[4836]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
[4487]192*/
[4250]193#define LoadParam4(type1, type2, type3, type4) \
[4623]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) \
[4249]199    { \
[4252]200      if (loadString != NULL && root != NULL) \
[4592]201        { \
202          SubString subLoads(loadString); \
203          if (subLoads.getCount() == 4) \
[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)); \
[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(), 4, subLoads.getCount()); \
208        } \
[4249]209      else \
[4592]210        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[4251]211    }
[4241]212
[4250]213
214// 5. TYPES
[4487]215/**
[4836]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
[4487]222*/
[4250]223#define LoadParam5(type1, type2, type3, type4, type5) \
[4623]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, \
[4725]227           type4##_TYPE default4 = type4##_DEFAULT, type5##_TYPE default5 = type5##_DEFAULT ) \
[4623]228  : BaseLoadParam(root, pt2Object, paramName, 5, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3, \
229                  type4##_NAME, default4, type5##_NAME, default5) \
[4250]230    { \
[4252]231      if (loadString != NULL && root != NULL) \
[4592]232        { \
233          SubString subLoads(loadString); \
234          if (subLoads.getCount() == 5) \
[4624]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)); \
[4592]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        } \
[4250]240      else \
[4592]241        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[4251]242    }
[4250]243
[4598]244// Pointer TYPE
245/**
[4836]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
[4598]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}
[4250]258
[4624]259
260/***********************
261*** HELPER FUNCTIONS ***
262***********************/
[4860]263bool          isBool(const char* BOOL, bool defaultValue);
[4734]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
[4624]269//TiXmlEmlemnt* isXmlElem(const)
270
271
272/************************
273*** DESCRIPTION STUFF ***
274************************/
[4256]275//! A class that handles the description of loadable parameters
[4250]276class LoadParamDescription
[4249]277{
[4254]278  friend class BaseLoadParam;
279  friend class LoadClassDescription;
280 public:
281  LoadParamDescription(const char* paramName);
[4746]282  ~LoadParamDescription();
[4255]283
284  void setDescription(const char* descriptionText);
[4836]285  /** @returns the descriptionString */
[4746]286  const char* getDescription() { return this->description; };
[4255]287
[4746]288  void print() const;
[4250]289 private:
[4623]290  char*         paramName;             //!< The name of the parameter.
291  int           paramCount;            //!< The count of parameters.
[4487]292  char**        types;                 //!< What kind of parameters does this function take ??
[4623]293  char*         description;           //!< A longer description about this function.
294  char**        defaultValues;         //!< The 'Default Values'.
[4250]295};
296
[4487]297//! A class for descriptions of a loadable module
[4251]298class LoadClassDescription
299{
[4254]300  friend class BaseLoadParam;
[4251]301 public:
[4254]302  LoadClassDescription(const char* className);
[4746]303  ~LoadClassDescription();
[4251]304
[4254]305  static LoadClassDescription* addClass(const char* className);
306  LoadParamDescription* addParam(const char* paramName);
[4251]307
[4592]308
[4260]309  static void printAll(const char* fileName = NULL);
[4255]310
[4251]311 private:
[4496]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.
[4251]316};
317
[4624]318
319/**************************
320**** REAL DECLARATIONS ****
321**************************/
[4487]322//! abstract Base class for a Loadable parameter
[4597]323class BaseLoadParam : public BaseObject
[4250]324{
[4255]325 public:
[4260]326  BaseLoadParam* describe(const char* descriptionText);
[4255]327
[4251]328 protected:
[4598]329  BaseLoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName, int paramCount, bool multi, const void* pointerToParam, ...);
[4251]330
331 protected:
[4487]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
[4598]335  const void*              pointerToParam;       //!< A Pointer to a Parameter.
[4249]336};
[4243]337
[4241]338
[4256]339//! derived template class, so all the Classes can load something.
[4250]340template<class T> class LoadParam : public BaseLoadParam
[4249]341{
342 public:
[4501]343  LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(), bool multi = false)
[4598]344    : BaseLoadParam(root, pt2Object, paramName, 0, multi, NULL, "")
[4592]345    {
[4501]346      if (loadString != NULL && root != NULL)
[4592]347        (*pt2Object.*function)();
348      else
349        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName());
[4501]350    }
351
352
[4487]353  //! makes functions with one string loadable
[4249]354  LoadParam1(l_STRING);
[4487]355  //! makes functions with two strings loadable
[4249]356  LoadParam2(l_STRING, l_STRING);
[4487]357  //! makes functions with three strings loadable
[4249]358  LoadParam3(l_STRING, l_STRING, l_STRING);
[4487]359  //! makes functions with four strings loadable
[4249]360  LoadParam4(l_STRING, l_STRING, l_STRING, l_STRING);
[4243]361
[4860]362  //! makes functions with one bool loadeable
363  LoadParam1(l_BOOL);
364
[4487]365  //! makes functions with one int loadable
[4249]366  LoadParam1(l_INT);
[4487]367  //! makes functions with two ints loadable
[4249]368  LoadParam2(l_INT, l_INT);
[4487]369  //! makes functions with three ints loadable
[4249]370  LoadParam3(l_INT, l_INT, l_INT);
[4487]371  //! makes functions with four ints loadable
[4249]372  LoadParam4(l_INT, l_INT, l_INT, l_INT);
[4241]373
[4487]374  //! makes functions with one float loadable
[4249]375  LoadParam1(l_FLOAT);
[4487]376  //! makes functions with two floats loadable
[4249]377  LoadParam2(l_FLOAT, l_FLOAT);
[4487]378  //! makes functions with three floats loadable
[4249]379  LoadParam3(l_FLOAT, l_FLOAT, l_FLOAT);
[4487]380  //! makes functions with four floats loadable
[4249]381  LoadParam4(l_FLOAT, l_FLOAT, l_FLOAT, l_FLOAT);
[4725]382  //! makes functions with four floats loadable
383  LoadParam5(l_FLOAT, l_FLOAT, l_FLOAT, l_FLOAT, l_FLOAT);
[4598]384
[4734]385  //! makes functions with one Vector loadable
386  //LoadParam1(l_VECTOR);
387
[4726]388  // loads a Ti-XML-element
[4599]389  LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(const TiXmlElement*), bool multi = false)
[4625]390  : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, "XML")
[4599]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);
[4233]405};
406
[4492]407// helper function
[4233]408
[4492]409const char* grabParameter(const TiXmlElement* root, const char* parameterName);
410
[4233]411#endif /* _LOAD_PARAM_H */
Note: See TracBrowser for help on using the repository browser.