Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: it is now possible to execute Commands registered to the ShellCommandBase with no arguments

File size: 13.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 * 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
26#include "factory.h"
27#include "debug.h"
28#include "substring.h"
29#include "tinyxml.h"
30
31// Forward Declaration //
32template<class T> class tList;
33
34//! macro that makes it even more easy to load a Parameter
35/**
36 * @param className the name of the class to load
37 * @param parameterName the name of the parameter to load as written in the XML-file
38 * @param function the function to call
39 */
40#define LOAD_PARAM(className, parameterName, paramFunction) \
41        LoadParam<className>(root, #parameterName, this, &className::paramFunction)
42
43/**
44 * this Starts a Cycle in the Loading Process
45 * be aware, that in the cycle the first parameter of load_param should because
46 * called element, and that you must say true at the Fith parameter, or it will fail
47 * also you will have to close the Cycle again with LOAD_PARAM_END_CYCLE
48 */
49#define LOAD_PARAM_START_CYCLE   const TiXmlElement* element; \
50                                 element = root->FirstChildElement(); \
51                                 while( element != NULL) \
52                                  {
53/**
54 * closes a LoadParam Loop
55 * @see LOAD_PARAM_START_CYCLE
56 */
57#define LOAD_PARAM_END_CYCLE        element = element->NextSiblingElement(); \
58                                  }
59
60
61/*****************************************
62**** MACROS DEFINITIONS OF LOADABLES *****
63*****************************************/
64// 1. TYPE
65/**
66 *  a Macro to easily implement many different Constructors for the LoadParam-Class with 1 argument
67 * @param type1 The type of the first functionParameter
68*/
69#define LoadParam1(type1) \
70 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), \
71           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT) \
72  : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, type1##_NAME, default1) \
73    { \
74      if (loadString != NULL && root != NULL) \
75        (*pt2Object.*function)(type1##_FUNC(loadString, default1)); \
76      else \
77        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
78    }
79
80// 2. TYPES
81/**
82 *  a Macro to easily implement many different Constructors for the LoadParam-Class with 2 arguments
83 * @param type1 The type of the first functionParameter
84 * @param type2 The type of the second functionParameter
85*/
86#define LoadParam2(type1, type2) \
87 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE), \
88           bool multi = false,  type1##_TYPE default1 = type1##_DEFAULT,  type2##_TYPE default2 = type2##_DEFAULT) \
89  : BaseLoadParam(root, pt2Object, paramName, 2, multi, NULL, type1##_NAME, default1, type2##_NAME, default2) \
90    { \
91      if (loadString != NULL && root != NULL) \
92        { \
93          SubString subLoads(loadString); \
94          if (subLoads.getCount() == 2) \
95            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2)); \
96          else \
97            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
98                      paramName, pt2Object->getClassName(), 2, subLoads.getCount()); \
99        } \
100      else \
101        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
102    }
103
104
105// 3. TYPES
106/**
107 *  a Macro to easily implement many different Constructors for the LoadParam-Class with 3 arguments
108 * @param type1 The type of the first functionParameter
109 * @param type2 The type of the second functionParameter
110 * @param type3 The type of the third functionParameter
111*/
112#define LoadParam3(type1, type2, type3) \
113 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE), \
114           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT)\
115  : BaseLoadParam(root, pt2Object, paramName, 3, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3) \
116    { \
117      if (loadString != NULL && root != NULL) \
118        { \
119          SubString subLoads(loadString); \
120          if (subLoads.getCount() == 3) \
121            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3)); \
122          else \
123            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
124                      paramName, pt2Object->getClassName(), 3, subLoads.getCount()); \
125        } \
126      else \
127        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
128    }
129
130
131// 4. TYPES
132/**
133 *  a Macro to easily implement many different Constructors for the LoadParam-Class with 4 arguments
134 * @param type1 The type of the first functionParameter
135 * @param type2 The type of the second functionParameter
136 * @param type3 The type of the third functionParameter
137 * @param type4 The type of the forth functionParameter
138*/
139#define LoadParam4(type1, type2, type3, type4) \
140 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE), \
141           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \
142           type4##_TYPE default4 = type4##_DEFAULT) \
143  : BaseLoadParam(root, pt2Object, paramName, 4, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3, \
144                  type4##_NAME, default4) \
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), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3), type4##_FUNC(subLoads.getString(3), default4)); \
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 *  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, \
171           void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE, type5##_TYPE), \
172           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \
173           type4##_TYPE default4 = type4##_DEFAULT, type5##_TYPE default5 = type5##_DEFAULT ) \
174  : BaseLoadParam(root, pt2Object, paramName, 5, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3, \
175                  type4##_NAME, default4, type5##_NAME, default5) \
176    { \
177      if (loadString != NULL && root != NULL) \
178        { \
179          SubString subLoads(loadString); \
180          if (subLoads.getCount() == 5) \
181            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3), type4##_FUNC(subLoads.getString(3), default4), type5##_FUNC(subLoads.getString(4), default5)); \
182          else \
183            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
184                      paramName, pt2Object->getClassName(), 5, subLoads.getCount()); \
185        } \
186      else \
187        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
188    }
189
190// Pointer TYPE
191/**
192 *  a Macro to easily implement many different Constructors for the LoadParam-Class with one Pointer argument
193 * @param type1 The type of the Pointer
194 */
195#define LoadParamPT(type1) \
196 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), type1##_TYPE pointerToParam, bool multi = false) \
197  : BaseLoadParam(root, pt2Object, paramName, 1, multi, pointerToParam, type1##_NAME) \
198{ \
199      if (pointerToParam != NULL && root != NULL) \
200        (*pt2Object.*function)((type1##_TYPE) pointerToParam); \
201      else \
202        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
203}
204
205
206/***********************
207*** HELPER FUNCTIONS ***
208***********************/
209bool          isBool(const char* BOOL, bool defaultValue);
210int           isInt(const char* INT, int defaultValue);
211float         isFloat(const char* FLOAT, float defaultValue);
212//const Vector& isVector(const char* VECTOR, const Vector& defaultValue);
213const char*   isString(const char* STRING, const char* defaultValue);
214
215//TiXmlEmlemnt* isXmlElem(const)
216
217
218/************************
219*** DESCRIPTION STUFF ***
220************************/
221//! A class that handles the description of loadable parameters
222class LoadParamDescription
223{
224  friend class BaseLoadParam;
225  friend class LoadClassDescription;
226 public:
227  LoadParamDescription(const char* paramName);
228  ~LoadParamDescription();
229
230  void setDescription(const char* descriptionText);
231  /** @returns the descriptionString */
232  const char* getDescription() { return this->description; };
233
234  void print() const;
235 private:
236  char*         paramName;             //!< The name of the parameter.
237  int           paramCount;            //!< The count of parameters.
238  char**        types;                 //!< What kind of parameters does this function take ??
239  char*         description;           //!< A longer description about this function.
240  char**        defaultValues;         //!< The 'Default Values'.
241};
242
243//! A class for descriptions of a loadable module
244class LoadClassDescription
245{
246  friend class BaseLoadParam;
247 public:
248  LoadClassDescription(const char* className);
249  ~LoadClassDescription();
250
251  static LoadClassDescription* addClass(const char* className);
252  LoadParamDescription* addParam(const char* paramName);
253
254  static void printAll(const char* fileName = NULL);
255  static tList<const char>* searchClassWithShort(const char* classNameBegin);
256//  static const LoadParamDescription* getClass(const char* className);
257
258 private:
259  static bool                          parametersDescription;  //!< if parameter-description should be enabled.
260  static tList<LoadClassDescription>*  classList;              //!< a list, that holds all the loadable classes. (after one instance has been loaded)
261  char*                                className;              //!< name of the class
262  tList<LoadParamDescription>*         paramList;              //!< List of parameters this class knows.
263};
264
265
266/**************************
267**** REAL DECLARATIONS ****
268**************************/
269//! abstract Base class for a Loadable parameter
270class BaseLoadParam : public BaseObject
271{
272 public:
273  BaseLoadParam* describe(const char* descriptionText);
274
275 protected:
276  BaseLoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName, int paramCount, bool multi, const void* pointerToParam, ...);
277
278 protected:
279  LoadClassDescription*    classDesc;            //!< The LoadClassDescription of this LoadParameter
280  LoadParamDescription*    paramDesc;            //!< The LoadParameterDescription of this LoadParameter
281  const char*              loadString;           //!< The string loaded by this LoadParam
282  const void*              pointerToParam;       //!< A Pointer to a Parameter.
283};
284
285
286//! derived template class, so all the Classes can load something.
287template<class T> class LoadParam : public BaseLoadParam
288{
289 public:
290  LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(), bool multi = false)
291    : BaseLoadParam(root, pt2Object, paramName, 0, multi, NULL, "")
292    {
293      if (loadString != NULL && root != NULL)
294        (*pt2Object.*function)();
295      else
296        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName());
297    }
298
299
300#define FUNCTOR_LIST(x)    LoadParam ## x
301#include "functor_list.h"
302#undef FUNCTOR_LIST
303
304  //! makes functions with one Vector loadable
305  //LoadParam1(l_VECTOR);
306
307  // loads a Ti-XML-element
308  LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(const TiXmlElement*), bool multi = false)
309  : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, "XML")
310  {
311    if (root != NULL)
312    {
313      const TiXmlElement* elem = root->FirstChildElement(paramName);
314      if (likely(elem != NULL))
315        (*pt2Object.*function)(elem);
316      else
317        PRINTF(2)("%s of %s is empty", paramName, pt2Object->getClassName());
318    }
319    else
320      PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName());
321  }
322
323  //LoadParamPT(l_XML_ELEM);
324};
325
326// helper function
327
328const char* grabParameter(const TiXmlElement* root, const char* parameterName);
329
330#endif /* _LOAD_PARAM_H */
Note: See TracBrowser for help on using the repository browser.