Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: derivations work.
now the only thing to do is specify all the classes, and DO it CLEAN.

@patrick: is it ok, how i treated your ObjectManager??

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 "factory.h"
25#include "debug.h"
26#include "substring.h"
27
28// Forward Declaration //
29template<class T> class tList;
30
31//! macro that makes it even more easy to load a Parameter
32/**
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/**
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*/
48
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
52
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
56
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
60
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
64
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
68
69// 1. TYPE
70/**
71   \brief a Macro to easily implement many different Constructors for the LoadParam-Class with 1 argument
72   \param type1 The type of the first functionParameter
73*/
74#define LoadParam1(type1) \
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)          \
77    { \
78      if (loadString != NULL && root != NULL) \
79        (*pt2Object.*function)(type1##_FUNC(loadString)); \
80      else \
81        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
82    }
83
84
85// 2. TYPES
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*/
91#define LoadParam2(type1, type2) \
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) \
94    { \
95      if (loadString != NULL && root != NULL) \
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 \
105        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
106    }
107
108
109// 3. TYPES
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*/
116#define LoadParam3(type1, type2, type3) \
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) \
119    { \
120      if (loadString != NULL && root != NULL) \
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 \
130        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
131    }
132
133
134// 4. TYPES
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*/
142#define LoadParam4(type1, type2, type3, type4) \
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) \
145    { \
146      if (loadString != NULL && root != NULL) \
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 \
156        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
157    }
158
159
160// 5. TYPES
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*/
169#define LoadParam5(type1, type2, type3, type4, type5) \
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) \
172    { \
173      if (loadString != NULL && root != NULL) \
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 \
183        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
184    }
185
186
187//! A class that handles the description of loadable parameters
188class LoadParamDescription
189{
190  friend class BaseLoadParam;
191  friend class LoadClassDescription;
192 public:
193  LoadParamDescription(const char* paramName);
194  ~LoadParamDescription(void);
195
196  void setDescription(const char* descriptionText);
197  /** \returns the descriptionString */
198  const char* getDescription(void) { return this->description; };
199
200  void print(void) const;
201 private:
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
206};
207
208//! A class for descriptions of a loadable module
209class LoadClassDescription
210{
211  friend class BaseLoadParam;
212 public:
213  LoadClassDescription(const char* className);
214  ~LoadClassDescription(void);
215
216  static LoadClassDescription* addClass(const char* className);
217  LoadParamDescription* addParam(const char* paramName);
218
219
220  static void printAll(const char* fileName = NULL);
221
222 private:
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.
227};
228
229//! abstract Base class for a Loadable parameter
230class BaseLoadParam
231{
232 public:
233  BaseLoadParam* describe(const char* descriptionText);
234
235 protected:
236  BaseLoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName, int paramCount, bool multi, ...);
237
238 protected:
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
242};
243
244
245//! derived template class, so all the Classes can load something.
246template<class T> class LoadParam : public BaseLoadParam
247{
248 public:
249  LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(), bool multi = false)
250    : BaseLoadParam(root, pt2Object, paramName, 0, multi, "")
251    {
252      if (loadString != NULL && root != NULL)
253        (*pt2Object.*function)();
254      else
255        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName());
256    }
257
258
259  //! makes functions with one string loadable
260  LoadParam1(l_STRING);
261  //! makes functions with two strings loadable
262  LoadParam2(l_STRING, l_STRING);
263  //! makes functions with three strings loadable
264  LoadParam3(l_STRING, l_STRING, l_STRING);
265  //! makes functions with four strings loadable
266  LoadParam4(l_STRING, l_STRING, l_STRING, l_STRING);
267
268  //! makes functions with one int loadable
269  LoadParam1(l_INT);
270  //! makes functions with two ints loadable
271  LoadParam2(l_INT, l_INT);
272  //! makes functions with three ints loadable
273  LoadParam3(l_INT, l_INT, l_INT);
274  //! makes functions with four ints loadable
275  LoadParam4(l_INT, l_INT, l_INT, l_INT);
276
277  //! makes functions with one float loadable
278  LoadParam1(l_FLOAT);
279  //! makes functions with two floats loadable
280  LoadParam2(l_FLOAT, l_FLOAT);
281  //! makes functions with three floats loadable
282  LoadParam3(l_FLOAT, l_FLOAT, l_FLOAT);
283  //! makes functions with four floats loadable
284  LoadParam4(l_FLOAT, l_FLOAT, l_FLOAT, l_FLOAT);
285};
286
287// helper function
288
289const char* grabParameter(const TiXmlElement* root, const char* parameterName);
290
291#endif /* _LOAD_PARAM_H */
Note: See TracBrowser for help on using the repository browser.