Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more doxygen-tags in util

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