Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/util/loading/load_param.h @ 5631

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

orxonox/trunk: Load_Param2 changed, to load Models with, or without scale.

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