Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: cycling read-in of parameters should work now… this is quite tricky, and the TrackManager has to be rewritten in some parts…. :(

File size: 11.1 KB
RevLine 
[4250]1/*
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
16/*!
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
[4239]24#include "factory.h"
25#include "debug.h"
[4241]26#include "substring.h"
[4233]27
[4251]28// Forward Declaration //
29template<class T> class tList;
30
[4495]31//! macro that makes it even more easy to load a Parameter
[4249]32/**
[4495]33   \param className the name of the class to load
34   \param parameterName the name of the parameter to load as written in the XML-file
35   \param function the function to call
36*/
37#define LOAD_PARAM(className, parameterName, paramFunction) \
38        LoadParam<className>(root, #parameterName, this, &className::paramFunction)
39
40/**
[4249]41   useable FunctionParameters are:
42   l_INT:    int
43   l_LONG:   long
44   l_SHORT:  short
45   l_FLOAT:  float
46   l_STRING: const char*
47*/
[4233]48
[4487]49#define l_INT_TYPE       int              //!< The type of an INT
50#define l_INT_FUNC       atoi             //!< the function to call to parse INT
51#define l_INT_NAME       "int"            //!< the name of an INT
[4233]52
[4487]53#define l_LONG_TYPE      long             //!< The type of a LONG
54#define l_LONG_FUNC      atol             //!< The function to parse a LONG
55#define l_LONG_NAME      "long"           //!< The name of a LONG
[4233]56
[4487]57#define l_SHORT_TYPE     short            //!< The type of a SHORT
58#define l_SHORT_FUNC     atoi             //!< The function to parse a SHORT
59#define l_SHORT_NAME     "short"          //!< The name of a SHORT
[4242]60
[4487]61#define l_FLOAT_TYPE     float            //!< The type of a FLOAT
62#define l_FLOAT_FUNC     atof             //!< The function to parse a FLOAT
63#define l_FLOAT_NAME     "float"          //!< The name of a FLOAT
[4242]64
[4487]65#define l_STRING_TYPE    const char*      //!< The type fo a STRING
66#define l_STRING_FUNC                     //!< The function to parse a STRING
67#define l_STRING_NAME    "string"         //!< The name of a STRING
[4242]68
[4487]69// 1. TYPE
[4249]70/**
[4487]71   \brief a Macro to easily implement many different Constructors for the LoadParam-Class with 1 argument
[4249]72   \param type1 The type of the first functionParameter
73*/
74#define LoadParam1(type1) \
[4496]75 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), bool multi = false) \
76   : BaseLoadParam(root, pt2Object, paramName, 1, multi, type1##_NAME)          \
[4249]77    { \
[4252]78      if (loadString != NULL && root != NULL) \
[4249]79        (*pt2Object.*function)(type1##_FUNC(loadString)); \
80      else \
[4251]81        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[4249]82    }
[4241]83
[4249]84
85// 2. TYPES
[4487]86/**
87   \brief a Macro to easily implement many different Constructors for the LoadParam-Class with 2 arguments
88   \param type1 The type of the first functionParameter
89   \param type2 The type of the second functionParameter
90*/
[4250]91#define LoadParam2(type1, type2) \
[4496]92 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE), bool multi = false) \
93   : BaseLoadParam(root, pt2Object, paramName, 2, multi, type1##_NAME, type2##_NAME) \
[4249]94    { \
[4252]95      if (loadString != NULL && root != NULL) \
[4249]96        { \
97          SubString subLoads(loadString); \
98          if (subLoads.getCount() == 2) \
99            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0)), type2##_FUNC(subLoads.getString(1))); \
100          else \
101            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
102                      paramName, pt2Object->getClassName(), 2, subLoads.getCount()); \
103        } \
104      else \
[4251]105        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
106    }
[4241]107
[4243]108
[4249]109// 3. TYPES
[4487]110/**
111   \brief a Macro to easily implement many different Constructors for the LoadParam-Class with 3 arguments
112   \param type1 The type of the first functionParameter
113   \param type2 The type of the second functionParameter
114   \param type3 The type of the third functionParameter
115*/
[4250]116#define LoadParam3(type1, type2, type3) \
[4496]117 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE), bool multi = false)\
118   : BaseLoadParam(root, pt2Object, paramName, 3, multi, type1##_NAME, type2##_NAME, type3##_NAME) \
[4249]119    { \
[4252]120      if (loadString != NULL && root != NULL) \
[4249]121        { \
122          SubString subLoads(loadString); \
123          if (subLoads.getCount() == 3) \
124            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0)), type2##_FUNC(subLoads.getString(1)), type3##_FUNC(subLoads.getString(2))); \
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(), 3, subLoads.getCount()); \
128        } \
129      else \
[4251]130        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
131    }
[4241]132
[4249]133
134// 4. TYPES
[4487]135/**
136   \brief a Macro to easily implement many different Constructors for the LoadParam-Class with 4 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   \param type4 The type of the forth functionParameter
141*/
[4250]142#define LoadParam4(type1, type2, type3, type4) \
[4496]143 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE), bool multi = false) \
144   : BaseLoadParam(root, pt2Object, paramName, 4, multi, type1##_NAME, type2##_NAME, type3##_NAME, type2##_NAME, type4##_NAME) \
[4249]145    { \
[4252]146      if (loadString != NULL && root != NULL) \
[4249]147        { \
148          SubString subLoads(loadString); \
149          if (subLoads.getCount() == 4) \
150            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0)), type2##_FUNC(subLoads.getString(1)), type3##_FUNC(subLoads.getString(2)), type4##_FUNC(subLoads.getString(3))); \
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(), 4, subLoads.getCount()); \
154        } \
155      else \
[4251]156        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
157    }
[4241]158
[4250]159
160// 5. TYPES
[4487]161/**
162   \brief a Macro to easily implement many different Constructors for the LoadParam-Class with 5 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   \param type5 The type of the fifth functionParameter
168*/
[4250]169#define LoadParam5(type1, type2, type3, type4, type5) \
[4496]170 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE, type5##_TYPE), bool multi = false) \
171   : BaseLoadParam(root, pt2Object, paramName, 5, multi, type1##_NAME, type2##_NAME, type3##_NAME, type2##_NAME, type4##_NAME, type5##_NAME) \
[4250]172    { \
[4252]173      if (loadString != NULL && root != NULL) \
[4250]174        { \
175          SubString subLoads(loadString); \
176          if (subLoads.getCount() == 5) \
177            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0)), type2##_FUNC(subLoads.getString(1)), type3##_FUNC(subLoads.getString(2)), type4##_FUNC(subLoads.getString(3)), type5##_FUNC(subLoads.getString(4))); \
178          else \
179            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
180                      paramName, pt2Object->getClassName(), 5, subLoads.getCount()); \
181        } \
182      else \
[4251]183        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
184    }
[4250]185
186
[4256]187//! A class that handles the description of loadable parameters
[4250]188class LoadParamDescription
[4249]189{
[4254]190  friend class BaseLoadParam;
191  friend class LoadClassDescription;
192 public:
193  LoadParamDescription(const char* paramName);
194  ~LoadParamDescription(void);
[4255]195
196  void setDescription(const char* descriptionText);
197  /** \returns the descriptionString */
[4487]198  const char* getDescription(void) { return this->description; };
[4255]199
200  void print(void) const;
[4250]201 private:
[4487]202  char*         paramName;             //!< The name of the parameter
203  int           paramCount;            //!< The count of parameters
204  char**        types;                 //!< What kind of parameters does this function take ??
205  char*         description;           //!< A longer description about this function
[4250]206};
207
[4487]208//! A class for descriptions of a loadable module
[4251]209class LoadClassDescription
210{
[4254]211  friend class BaseLoadParam;
[4251]212 public:
[4254]213  LoadClassDescription(const char* className);
214  ~LoadClassDescription(void);
[4251]215
[4254]216  static LoadClassDescription* addClass(const char* className);
217  LoadParamDescription* addParam(const char* paramName);
[4318]218 
[4251]219
[4260]220  static void printAll(const char* fileName = NULL);
[4255]221
[4251]222 private:
[4496]223  static bool                          parametersDescription;  //!< if parameter-description should be enabled.
224  static tList<LoadClassDescription>*  classList;              //!< a list, that holds all the loadable classes. (after one instance has been loaded)
225  char*                                className;              //!< name of the class
226  tList<LoadParamDescription>*         paramList;              //!< List of parameters this class knows.
[4251]227};
228
[4487]229//! abstract Base class for a Loadable parameter
[4250]230class BaseLoadParam
231{
[4255]232 public:
[4260]233  BaseLoadParam* describe(const char* descriptionText);
[4255]234
[4251]235 protected:
[4496]236  BaseLoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName, int paramCount, bool multi, ...);
[4251]237
238 protected:
[4487]239  LoadClassDescription*    classDesc;            //!< The LoadClassDescription of this LoadParameter
240  LoadParamDescription*    paramDesc;            //!< The LoadParameterDescription of this LoadParameter
241  const char*              loadString;           //!< The string loaded by this LoadParam
[4249]242};
[4243]243
[4241]244
[4256]245//! derived template class, so all the Classes can load something.
[4250]246template<class T> class LoadParam : public BaseLoadParam
[4249]247{
248 public:
[4487]249  //! makes functions with one string loadable
[4249]250  LoadParam1(l_STRING);
[4487]251  //! makes functions with two strings loadable
[4249]252  LoadParam2(l_STRING, l_STRING);
[4487]253  //! makes functions with three strings loadable
[4249]254  LoadParam3(l_STRING, l_STRING, l_STRING);
[4487]255  //! makes functions with four strings loadable
[4249]256  LoadParam4(l_STRING, l_STRING, l_STRING, l_STRING);
[4243]257
[4487]258  //! makes functions with one int loadable
[4249]259  LoadParam1(l_INT);
[4487]260  //! makes functions with two ints loadable
[4249]261  LoadParam2(l_INT, l_INT);
[4487]262  //! makes functions with three ints loadable
[4249]263  LoadParam3(l_INT, l_INT, l_INT);
[4487]264  //! makes functions with four ints loadable
[4249]265  LoadParam4(l_INT, l_INT, l_INT, l_INT);
[4241]266
[4487]267  //! makes functions with one float loadable
[4249]268  LoadParam1(l_FLOAT);
[4487]269  //! makes functions with two floats loadable
[4249]270  LoadParam2(l_FLOAT, l_FLOAT);
[4487]271  //! makes functions with three floats loadable
[4249]272  LoadParam3(l_FLOAT, l_FLOAT, l_FLOAT);
[4487]273  //! makes functions with four floats loadable
[4249]274  LoadParam4(l_FLOAT, l_FLOAT, l_FLOAT, l_FLOAT);
[4233]275};
276
[4492]277// helper function
[4233]278
[4492]279const char* grabParameter(const TiXmlElement* root, const char* parameterName);
280
[4233]281#endif /* _LOAD_PARAM_H */
Note: See TracBrowser for help on using the repository browser.