Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5546 was 5546, checked in by bensch, 18 years ago

orxonox/trunk: taken out LoadParamDescription into a file of its own

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