Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: minor cleanup (speed-issue) in LoadClassDescription, using enum insted of String, faster, more reliable

File size: 13.5 KB
RevLine 
[4592]1/*
[4250]2   orxonox - the future of 3D-vertical-scrollers
[4233]3
[4250]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
[4592]16/*!
[5039]17 * @file load_param.h
[5129]18 * A Class and macro-functions, that makes our lives easy to load-in parameters
19 */
[4250]20
[4233]21#ifndef _LOAD_PARAM_H
22#define _LOAD_PARAM_H
23
[5332]24#include "functor_list.h"
[4597]25#include "base_object.h"
[5129]26
[4239]27#include "factory.h"
28#include "debug.h"
[4241]29#include "substring.h"
[4598]30#include "tinyxml.h"
[5141]31#include "helper_functions.h"
[4233]32
[4251]33// Forward Declaration //
34template<class T> class tList;
35
[5332]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
[4495]106//! macro that makes it even more easy to load a Parameter
[4249]107/**
[4834]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 */
[4495]112#define LOAD_PARAM(className, parameterName, paramFunction) \
113        LoadParam<className>(root, #parameterName, this, &className::paramFunction)
114
115/**
[4834]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) \
[5332]124{
[4834]125/**
[5332]126   * closes a LoadParam Loop
127   * @see LOAD_PARAM_START_CYCLE
[4834]128 */
129#define LOAD_PARAM_END_CYCLE        element = element->NextSiblingElement(); \
[5332]130}
[4834]131
132
[4623]133/*****************************************
134**** MACROS DEFINITIONS OF LOADABLES *****
135*****************************************/
[5137]136// 0. TYPES
137/**
[5332]138 * a Macro to easily implement many different Constructors for the LoadParam-Class with no argument
[5137]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
[4487]150// 1. TYPE
[4249]151/**
[5332]152 * a Macro to easily implement many different Constructors for the LoadParam-Class with 1 argument
[4836]153 * @param type1 The type of the first functionParameter
[5332]154 */
[4249]155#define LoadParam1(type1) \
[4623]156 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), \
157           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT) \
[5332]158  : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, type1##_PARAM, default1) \
159{ \
[4252]160      if (loadString != NULL && root != NULL) \
[4624]161        (*pt2Object.*function)(type1##_FUNC(loadString, default1)); \
[4249]162      else \
[4592]163        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[5332]164}
[4241]165
[4249]166// 2. TYPES
[4487]167/**
[5332]168 * a Macro to easily implement many different Constructors for the LoadParam-Class with 2 arguments
[4836]169 * @param type1 The type of the first functionParameter
170 * @param type2 The type of the second functionParameter
[5332]171 */
[4250]172#define LoadParam2(type1, type2) \
[4623]173 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE), \
174           bool multi = false,  type1##_TYPE default1 = type1##_DEFAULT,  type2##_TYPE default2 = type2##_DEFAULT) \
[5332]175  : BaseLoadParam(root, pt2Object, paramName, 2, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2) \
176{ \
[4252]177      if (loadString != NULL && root != NULL) \
[5332]178{ \
[4592]179          SubString subLoads(loadString); \
180          if (subLoads.getCount() == 2) \
[4624]181            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2)); \
[4592]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(), 2, subLoads.getCount()); \
[5332]185} \
[4249]186      else \
[4592]187        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[5332]188}
[4241]189
[4243]190
[4249]191// 3. TYPES
[4487]192/**
[5332]193 * a Macro to easily implement many different Constructors for the LoadParam-Class with 3 arguments
[4836]194 * @param type1 The type of the first functionParameter
195 * @param type2 The type of the second functionParameter
196 * @param type3 The type of the third functionParameter
[5332]197 */
[4250]198#define LoadParam3(type1, type2, type3) \
[4623]199 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE), \
200           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT)\
[5332]201  : BaseLoadParam(root, pt2Object, paramName, 3, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3) \
202{ \
[4252]203      if (loadString != NULL && root != NULL) \
[5332]204{ \
[4592]205          SubString subLoads(loadString); \
206          if (subLoads.getCount() == 3) \
[4624]207            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3)); \
[4592]208          else \
209            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
210                      paramName, pt2Object->getClassName(), 3, subLoads.getCount()); \
[5332]211} \
[4249]212      else \
[4592]213        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[5332]214}
[4241]215
[4249]216
217// 4. TYPES
[4487]218/**
[5332]219 * a Macro to easily implement many different Constructors for the LoadParam-Class with 4 arguments
[4836]220 * @param type1 The type of the first functionParameter
221 * @param type2 The type of the second functionParameter
222 * @param type3 The type of the third functionParameter
223 * @param type4 The type of the forth functionParameter
[5332]224 */
[4250]225#define LoadParam4(type1, type2, type3, type4) \
[4623]226 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE), \
227           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \
228           type4##_TYPE default4 = type4##_DEFAULT) \
[5332]229  : BaseLoadParam(root, pt2Object, paramName, 4, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3, \
230                  type4##_PARAM, default4) \
231{ \
[4252]232      if (loadString != NULL && root != NULL) \
[5332]233{ \
[4592]234          SubString subLoads(loadString); \
235          if (subLoads.getCount() == 4) \
[4624]236            (*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)); \
[4592]237          else \
238            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
239                      paramName, pt2Object->getClassName(), 4, subLoads.getCount()); \
[5332]240} \
[4249]241      else \
[4592]242        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[5332]243}
[4241]244
[4250]245
246// 5. TYPES
[4487]247/**
[5332]248 * a Macro to easily implement many different Constructors for the LoadParam-Class with 5 arguments
[4836]249 * @param type1 The type of the first functionParameter
250 * @param type2 The type of the second functionParameter
251 * @param type3 The type of the third functionParameter
252 * @param type4 The type of the forth functionParameter
253 * @param type5 The type of the fifth functionParameter
[5332]254 */
[4250]255#define LoadParam5(type1, type2, type3, type4, type5) \
[4623]256 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, \
257           void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE, type5##_TYPE), \
258           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \
[4725]259           type4##_TYPE default4 = type4##_DEFAULT, type5##_TYPE default5 = type5##_DEFAULT ) \
[5332]260  : BaseLoadParam(root, pt2Object, paramName, 5, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3, \
261                  type4##_PARAM, default4, type5##_PARAM, default5) \
262{ \
[4252]263      if (loadString != NULL && root != NULL) \
[5332]264{ \
[4592]265          SubString subLoads(loadString); \
266          if (subLoads.getCount() == 5) \
[4624]267            (*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)); \
[4592]268          else \
269            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
270                      paramName, pt2Object->getClassName(), 5, subLoads.getCount()); \
[5332]271} \
[4250]272      else \
[4592]273        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[5332]274}
[4250]275
[4598]276// Pointer TYPE
277/**
[5332]278 * a Macro to easily implement many different Constructors for the LoadParam-Class with one Pointer argument
[4836]279 * @param type1 The type of the Pointer
[4598]280 */
281#define LoadParamPT(type1) \
282 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), type1##_TYPE pointerToParam, bool multi = false) \
[5332]283  : BaseLoadParam(root, pt2Object, paramName, 1, multi, pointerToParam, type1##_PARAM) \
[4598]284{ \
285      if (pointerToParam != NULL && root != NULL) \
286        (*pt2Object.*function)((type1##_TYPE) pointerToParam); \
287      else \
288        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
289}
[4250]290
[4256]291//! derived template class, so all the Classes can load something.
[4250]292template<class T> class LoadParam : public BaseLoadParam
[4249]293{
[5137]294  public:
[4501]295
[5133]296#define FUNCTOR_LIST(x)    LoadParam ## x
297#include "functor_list.h"
298#undef FUNCTOR_LIST
[4243]299
[4734]300  //! makes functions with one Vector loadable
301  //LoadParam1(l_VECTOR);
302
[4726]303  // loads a Ti-XML-element
[4599]304  LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(const TiXmlElement*), bool multi = false)
[4625]305  : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, "XML")
[4599]306  {
307    if (root != NULL)
308    {
309      const TiXmlElement* elem = root->FirstChildElement(paramName);
[5279]310      if (elem != NULL)
[4599]311        (*pt2Object.*function)(elem);
312      else
[5301]313        PRINTF(4)("%s of %s is empty\n", paramName, pt2Object->getClassName());
[4599]314    }
315    else
316      PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName());
317  }
318
319  //LoadParamPT(l_XML_ELEM);
[4233]320};
321
[4492]322// helper function
[4233]323
[4492]324const char* grabParameter(const TiXmlElement* root, const char* parameterName);
325
[4233]326#endif /* _LOAD_PARAM_H */
Note: See TracBrowser for help on using the repository browser.