Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: added Aim-class

File size: 11.8 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"
[5129]25
[5549]26#include "debug.h"
27
[4239]28#include "factory.h"
[4241]29#include "substring.h"
[4598]30#include "tinyxml.h"
[5549]31
[5141]32#include "helper_functions.h"
[4233]33
[4251]34// Forward Declaration //
35template<class T> class tList;
[5546]36class LoadClassDescription;
37class LoadParamDescription;
[5556]38class MultiType;
[4251]39
[5332]40/**************************
41**** REAL DECLARATIONS ****
42**************************/
43//! abstract Base class for a Loadable parameter
[5545]44class LoadParamBase : public BaseObject
[5332]45{
46 public:
[5545]47  LoadParamBase* describe(const char* descriptionText);
[5556]48  LoadParamBase* defaultValues(unsigned int count, ...);
[5332]49
50 protected:
[5545]51  LoadParamBase(const TiXmlElement* root, BaseObject* object, const char* paramName, int paramCount, bool multi, const void* pointerToParam, ...);
[5332]52
53 protected:
54  LoadClassDescription*    classDesc;            //!< The LoadClassDescription of this LoadParameter
55  LoadParamDescription*    paramDesc;            //!< The LoadParameterDescription of this LoadParameter
56  const char*              loadString;           //!< The string loaded by this LoadParam
57  const void*              pointerToParam;       //!< A Pointer to a Parameter.
[5556]58
59  MultiType*               defaultValue;
[5332]60};
61
62
[4495]63//! macro that makes it even more easy to load a Parameter
[4249]64/**
[4834]65 * @param className the name of the class to load
66 * @param parameterName the name of the parameter to load as written in the XML-file
67 * @param function the function to call
68 */
[4495]69#define LOAD_PARAM(className, parameterName, paramFunction) \
70        LoadParam<className>(root, #parameterName, this, &className::paramFunction)
71
72/**
[4834]73 * this Starts a Cycle in the Loading Process
74 * be aware, that in the cycle the first parameter of load_param should because
75 * called element, and that you must say true at the Fith parameter, or it will fail
76 * also you will have to close the Cycle again with LOAD_PARAM_END_CYCLE
77 */
78#define LOAD_PARAM_START_CYCLE   const TiXmlElement* element; \
79                                 element = root->FirstChildElement(); \
80                                 while( element != NULL) \
[5332]81{
[4834]82/**
[5332]83   * closes a LoadParam Loop
84   * @see LOAD_PARAM_START_CYCLE
[4834]85 */
86#define LOAD_PARAM_END_CYCLE        element = element->NextSiblingElement(); \
[5332]87}
[4834]88
89
[4623]90/*****************************************
91**** MACROS DEFINITIONS OF LOADABLES *****
92*****************************************/
[5137]93// 0. TYPES
94/**
[5332]95 * a Macro to easily implement many different Constructors for the LoadParam-Class with no argument
[5137]96 */
97#define LoadParam0() \
98LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(), bool multi = false) \
[5556]99  : LoadParamBase(root, pt2Object, paramName, 0, multi, NULL) \
[5137]100{ \
101  if (loadString != NULL && root != NULL) \
102    (*pt2Object.*function)(); \
103  else \
104    PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName());\
105}
106
[4487]107// 1. TYPE
[4249]108/**
[5332]109 * a Macro to easily implement many different Constructors for the LoadParam-Class with 1 argument
[4836]110 * @param type1 The type of the first functionParameter
[5332]111 */
[4249]112#define LoadParam1(type1) \
[4623]113 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), \
114           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT) \
[5545]115  : LoadParamBase(root, pt2Object, paramName, 1, multi, NULL, type1##_PARAM, default1) \
[5332]116{ \
[4252]117      if (loadString != NULL && root != NULL) \
[4624]118        (*pt2Object.*function)(type1##_FUNC(loadString, default1)); \
[4249]119      else \
[4592]120        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[5332]121}
[4241]122
[4249]123// 2. TYPES
[4487]124/**
[5332]125 * a Macro to easily implement many different Constructors for the LoadParam-Class with 2 arguments
[4836]126 * @param type1 The type of the first functionParameter
127 * @param type2 The type of the second functionParameter
[5499]128 *
129 * @TODO DEFAULT VALUES HACK
[5332]130 */
[4250]131#define LoadParam2(type1, type2) \
[4623]132 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE), \
133           bool multi = false,  type1##_TYPE default1 = type1##_DEFAULT,  type2##_TYPE default2 = type2##_DEFAULT) \
[5545]134  : LoadParamBase(root, pt2Object, paramName, 2, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2) \
[5332]135{ \
[4252]136      if (loadString != NULL && root != NULL) \
[5332]137{ \
[4592]138          SubString subLoads(loadString); \
[5499]139          if (subLoads.getCount() >= 1) \
[4624]140            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2)); \
[4592]141          else \
142            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
143                      paramName, pt2Object->getClassName(), 2, subLoads.getCount()); \
[5332]144} \
[4249]145      else \
[4592]146        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[5332]147}
[4241]148
[4243]149
[4249]150// 3. TYPES
[4487]151/**
[5332]152 * a Macro to easily implement many different Constructors for the LoadParam-Class with 3 arguments
[4836]153 * @param type1 The type of the first functionParameter
154 * @param type2 The type of the second functionParameter
155 * @param type3 The type of the third functionParameter
[5332]156 */
[4250]157#define LoadParam3(type1, type2, type3) \
[4623]158 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE), \
159           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT)\
[5545]160  : LoadParamBase(root, pt2Object, paramName, 3, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3) \
[5332]161{ \
[4252]162      if (loadString != NULL && root != NULL) \
[5332]163{ \
[4592]164          SubString subLoads(loadString); \
165          if (subLoads.getCount() == 3) \
[4624]166            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3)); \
[4592]167          else \
168            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
169                      paramName, pt2Object->getClassName(), 3, subLoads.getCount()); \
[5332]170} \
[4249]171      else \
[4592]172        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[5332]173}
[4241]174
[4249]175
176// 4. TYPES
[4487]177/**
[5332]178 * a Macro to easily implement many different Constructors for the LoadParam-Class with 4 arguments
[4836]179 * @param type1 The type of the first functionParameter
180 * @param type2 The type of the second functionParameter
181 * @param type3 The type of the third functionParameter
182 * @param type4 The type of the forth functionParameter
[5332]183 */
[4250]184#define LoadParam4(type1, type2, type3, type4) \
[4623]185 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE), \
186           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \
187           type4##_TYPE default4 = type4##_DEFAULT) \
[5545]188  : LoadParamBase(root, pt2Object, paramName, 4, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3, \
[5332]189                  type4##_PARAM, default4) \
190{ \
[4252]191      if (loadString != NULL && root != NULL) \
[5332]192{ \
[4592]193          SubString subLoads(loadString); \
194          if (subLoads.getCount() == 4) \
[4624]195            (*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]196          else \
197            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
198                      paramName, pt2Object->getClassName(), 4, subLoads.getCount()); \
[5332]199} \
[4249]200      else \
[4592]201        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[5332]202}
[4241]203
[4250]204
205// 5. TYPES
[4487]206/**
[5332]207 * a Macro to easily implement many different Constructors for the LoadParam-Class with 5 arguments
[4836]208 * @param type1 The type of the first functionParameter
209 * @param type2 The type of the second functionParameter
210 * @param type3 The type of the third functionParameter
211 * @param type4 The type of the forth functionParameter
212 * @param type5 The type of the fifth functionParameter
[5332]213 */
[4250]214#define LoadParam5(type1, type2, type3, type4, type5) \
[4623]215 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, \
216           void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE, type5##_TYPE), \
217           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \
[4725]218           type4##_TYPE default4 = type4##_DEFAULT, type5##_TYPE default5 = type5##_DEFAULT ) \
[5545]219  : LoadParamBase(root, pt2Object, paramName, 5, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3, \
[5332]220                  type4##_PARAM, default4, type5##_PARAM, default5) \
221{ \
[4252]222      if (loadString != NULL && root != NULL) \
[5332]223{ \
[4592]224          SubString subLoads(loadString); \
225          if (subLoads.getCount() == 5) \
[4624]226            (*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]227          else \
228            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
229                      paramName, pt2Object->getClassName(), 5, subLoads.getCount()); \
[5332]230} \
[4250]231      else \
[4592]232        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
[5332]233}
[4250]234
[4598]235// Pointer TYPE
236/**
[5332]237 * a Macro to easily implement many different Constructors for the LoadParam-Class with one Pointer argument
[4836]238 * @param type1 The type of the Pointer
[4598]239 */
240#define LoadParamPT(type1) \
241 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), type1##_TYPE pointerToParam, bool multi = false) \
[5545]242  : LoadParamBase(root, pt2Object, paramName, 1, multi, pointerToParam, type1##_PARAM) \
[4598]243{ \
244      if (pointerToParam != NULL && root != NULL) \
245        (*pt2Object.*function)((type1##_TYPE) pointerToParam); \
246      else \
247        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
248}
[4250]249
[4256]250//! derived template class, so all the Classes can load something.
[5545]251template<class T> class LoadParam : public LoadParamBase
[4249]252{
[5137]253  public:
[4501]254
[5133]255#define FUNCTOR_LIST(x)    LoadParam ## x
256#include "functor_list.h"
257#undef FUNCTOR_LIST
[4243]258
[4734]259  //! makes functions with one Vector loadable
260  //LoadParam1(l_VECTOR);
261
[4726]262  // loads a Ti-XML-element
[4599]263  LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(const TiXmlElement*), bool multi = false)
[5545]264  : LoadParamBase(root, pt2Object, paramName, 1, multi, NULL, "XML")
[4599]265  {
266    if (root != NULL)
267    {
268      const TiXmlElement* elem = root->FirstChildElement(paramName);
[5279]269      if (elem != NULL)
[4599]270        (*pt2Object.*function)(elem);
271      else
[5301]272        PRINTF(4)("%s of %s is empty\n", paramName, pt2Object->getClassName());
[4599]273    }
274    else
275      PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName());
276  }
277
278  //LoadParamPT(l_XML_ELEM);
[4233]279};
280
[4492]281// helper function
[4233]282
[4492]283const char* grabParameter(const TiXmlElement* root, const char* parameterName);
284
[4233]285#endif /* _LOAD_PARAM_H */
Note: See TracBrowser for help on using the repository browser.