Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5553 was 5549, checked in by bensch, 20 years ago

orxonox/trunk: cleanup of LoadParam

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