Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/levelLoader/src/util/loading/load_param.h @ 4255

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

orxonox/branches/levelLoader: description works

File size: 7.4 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    \brief 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 "factory.h"
25#include "debug.h"
26#include "substring.h"
27
28// Forward Declaration //
29template<class T> class tList;
30
31/**
32   useable FunctionParameters are:
33   l_INT:    int
34   l_LONG:   long
35   l_SHORT:  short
36   l_FLOAT:  float
37   l_STRING: const char*
38*/
39
40#define l_INT_TYPE int
41#define l_INT_FUNC atoi
42#define l_INT_NAME "int"
43
44#define l_LONG_TYPE long
45#define l_LONG_FUNC atol
46#define l_LONG_NAME "long"
47
48#define l_SHORT_TYPE short
49#define l_SHORT_FUNC atoi
50#define l_SHORT_NAME "short"
51
52#define l_FLOAT_TYPE float
53#define l_FLOAT_FUNC atof
54#define l_FLOAT_NAME "float"
55
56#define l_STRING_TYPE const char*
57#define l_STRING_FUNC
58#define l_STRING_NAME "string"
59
60
61/**
62   \brief a Macro to easily implement many different Constructors for the LoadParam-Class
63   \param type1 The type of the first functionParameter
64*/
65#define LoadParam1(type1) \
66 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE)) \
67  : BaseLoadParam(pt2Object, paramName, 1, type1##_NAME) \
68    { \
69      const char* loadString = grabParameter(root, paramName); \
70      if (loadString != NULL && root != NULL) \
71        (*pt2Object.*function)(type1##_FUNC(loadString)); \
72      else \
73        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
74    }
75
76
77// 2. TYPES
78#define LoadParam2(type1, type2) \
79 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE)) \
80  : BaseLoadParam(pt2Object, paramName, 2, type1##_NAME, type2##_NAME) \
81    { \
82      const char* loadString = grabParameter(root, paramName); \
83      if (loadString != NULL && root != NULL) \
84        { \
85          SubString subLoads(loadString); \
86          if (subLoads.getCount() == 2) \
87            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0)), type2##_FUNC(subLoads.getString(1))); \
88          else \
89            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
90                      paramName, pt2Object->getClassName(), 2, subLoads.getCount()); \
91        } \
92      else \
93        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
94    }
95
96
97// 3. TYPES
98#define LoadParam3(type1, type2, type3) \
99 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE))\
100  : BaseLoadParam(pt2Object, paramName, 3, type1##_NAME, type2##_NAME, type3##_NAME) \
101    { \
102      const char* loadString = grabParameter(root, paramName); \
103      if (loadString != NULL && root != NULL) \
104        { \
105          SubString subLoads(loadString); \
106          if (subLoads.getCount() == 3) \
107            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0)), type2##_FUNC(subLoads.getString(1)), type3##_FUNC(subLoads.getString(2))); \
108          else \
109            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
110                      paramName, pt2Object->getClassName(), 3, subLoads.getCount()); \
111        } \
112      else \
113        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
114    }
115
116
117// 4. TYPES
118#define LoadParam4(type1, type2, type3, type4) \
119 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE)) \
120  : BaseLoadParam(pt2Object, paramName, 4, type1##_NAME, type2##_NAME, type3##_NAME, type2##_NAME, type4##_NAME) \
121    { \
122      const char* loadString = grabParameter(root, paramName); \
123      if (loadString != NULL && root != NULL) \
124        { \
125          SubString subLoads(loadString); \
126          if (subLoads.getCount() == 4) \
127            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0)), type2##_FUNC(subLoads.getString(1)), type3##_FUNC(subLoads.getString(2)), type4##_FUNC(subLoads.getString(3))); \
128          else \
129            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
130                      paramName, pt2Object->getClassName(), 4, subLoads.getCount()); \
131        } \
132      else \
133        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
134    }
135
136
137// 5. TYPES
138#define LoadParam5(type1, type2, type3, type4, type5) \
139 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE, type5##_TYPE)) \
140  : BaseLoadParam(pt2Object, paramName, 5, type1##_NAME, type2##_NAME, type3##_NAME, type2##_NAME, type4##_NAME, type5##_NAME) \
141    { \
142      const char* loadString = grabParameter(root, paramName); \
143      if (loadString != NULL && root != NULL) \
144        { \
145          SubString subLoads(loadString); \
146          if (subLoads.getCount() == 5) \
147            (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0)), type2##_FUNC(subLoads.getString(1)), type3##_FUNC(subLoads.getString(2)), type4##_FUNC(subLoads.getString(3)), type5##_FUNC(subLoads.getString(4))); \
148          else \
149            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
150                      paramName, pt2Object->getClassName(), 5, subLoads.getCount()); \
151        } \
152      else \
153        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
154    }
155
156
157class LoadParamDescription
158{
159  friend class BaseLoadParam;
160  friend class LoadClassDescription;
161 public:
162  LoadParamDescription(const char* paramName);
163  ~LoadParamDescription(void);
164
165  void setDescription(const char* descriptionText);
166  /** \returns the descriptionString */
167  const char* getDescription(void) { return this->description;};
168
169  void print(void) const;
170 private:
171  char* paramName;
172  int paramCount;
173  char** types;
174  const char* description;
175};
176
177class LoadClassDescription
178{
179  friend class BaseLoadParam;
180 public:
181  LoadClassDescription(const char* className);
182  ~LoadClassDescription(void);
183
184  static LoadClassDescription* addClass(const char* className);
185  LoadParamDescription* addParam(const char* paramName);
186
187  static void printAll(void);
188
189  static bool parametersDescription; 
190  static tList<LoadClassDescription>* classList;
191 private:
192  char* className;
193  tList<LoadParamDescription>* paramList;
194};
195
196// abstract Base class
197class BaseLoadParam
198{
199 public:
200  void describe(const char* descriptionText);
201
202 protected:
203  BaseLoadParam(BaseObject* object, const char* paramName, int paramCount, ...);
204
205 protected:
206  LoadClassDescription* classDesc;
207  LoadParamDescription* paramDesc;
208};
209
210
211// derived template class
212template<class T> class LoadParam : public BaseLoadParam
213{
214 public:
215  LoadParam1(l_STRING);
216  LoadParam2(l_STRING, l_STRING);
217  LoadParam3(l_STRING, l_STRING, l_STRING);
218  LoadParam4(l_STRING, l_STRING, l_STRING, l_STRING);
219
220  LoadParam1(l_INT);
221  LoadParam2(l_INT, l_INT);
222  LoadParam3(l_INT, l_INT, l_INT);
223  LoadParam4(l_INT, l_INT, l_INT, l_INT);
224
225  LoadParam1(l_FLOAT);
226  LoadParam2(l_FLOAT, l_FLOAT);
227  LoadParam3(l_FLOAT, l_FLOAT, l_FLOAT);
228  LoadParam4(l_FLOAT, l_FLOAT, l_FLOAT, l_FLOAT);
229};
230
231
232#endif /* _LOAD_PARAM_H */
Note: See TracBrowser for help on using the repository browser.