Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new LoadParam procedure with all NON-cycling load-options, and it works perfectly (on first sight :))

now going to make the same for cycling LoadOptions

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