Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: setClassID implemented in all files

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