Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/physics/src/util/loading/load_param.h @ 4295

Last change on this file since 4295 was 4283, checked in by bensch, 20 years ago

orxonox/branches/physics: merged the trunk back to branches/physics
merged with command
svn merge -r 4223:HEAD ../../trunk/ .
conflicts additively included

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