Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/executor/executor_functional.h @ 7721

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

orxonox/trunk: introduced the absolutely new, much faster, more templated, and easier to look at Executor, and use it directly inside the LoadParam-Class

File size: 11.9 KB
Line 
1/*!
2 * @file executor_functional.h
3 * Definition of an Executor
4 */
5
6/*
7   orxonox - the future of 3D-vertical-scrollers
8
9   Copyright (C) 2004 orx
10
11   This program is free software; you can redistribute it and/or modify
12   it under the terms of the GNU General Public License as published by
13   the Free Software Foundation; either version 2, or (at your option)
14   any later version.
15
16### File Specific:
17   main-programmer: Benjamin Grauer
18   co-programmer: ...
19*/
20
21
22#ifndef __EXECUTOR_FUNCTIONAL_H_
23#define __EXECUTOR_FUNCTIONAL_H_
24
25#include "base_object.h"
26#include "multi_type.h"
27#include "executor.h"
28
29template<typename type> MT_Type ExecutorParamType() { return MT_EXT1; };
30template<> MT_Type ExecutorParamType<bool>();
31template<> MT_Type ExecutorParamType<int>();
32template<> MT_Type ExecutorParamType<unsigned int>();
33template<> MT_Type ExecutorParamType<float>();
34template<> MT_Type ExecutorParamType<char>();
35template<> MT_Type ExecutorParamType<const std::string&>();
36
37template<typename type> type fromString(const std::string& input, type defaultValue) {return defaultValue; };
38template<> bool fromString<bool>(const std::string& input, bool defaultValue);
39template<> int fromString<int>(const std::string& input, int defaultValue);
40template<> unsigned int fromString<unsigned int>(const std::string& input, unsigned int defaultValue);
41template<> float fromString<float>(const std::string& input, float defaultValue);
42template<> char fromString<char>(const std::string& input, char defaultValue);
43template<> const std::string& fromString<const std::string&>(const std::string& input, const std::string& defaultValue);
44
45template<typename type> type getDefault(const MultiType* const defaultValues, unsigned int i) { return (type)0; };
46template<> bool getDefault<bool>(const MultiType* const defaultValues, unsigned int i);
47template<> int getDefault<int>(const MultiType* const defaultValues, unsigned int i);
48template<> unsigned int getDefault<unsigned int>(const MultiType* const defaultValues, unsigned int i);
49template<> float getDefault<float>(const MultiType* const defaultValues, unsigned int i);
50template<> char getDefault<char>(const MultiType* const defaultValues, unsigned int i);
51template<> const std::string& getDefault<const std::string&>(const MultiType* const defaultValues, unsigned int i);
52
53#endif /* __EXECUTOR_FUNCTIONAL_H_ */
54
55
56#define __EXECUTOR_FUNCTIONAL_CONST
57#define __EXECUTOR_FUNCTIONAL_NAME(ParamCount)   Executor##ParamCount##Params
58#define __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC      dynamic_cast<T*>(object)->*(functionPointer)
59#define __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER   T::*functionPointer
60
61#ifdef EXECUTOR_FUNCTIONAL_USE_CONST
62 #undef __EXECUTOR_FUNCTIONAL_CONST
63 #define __EXECUTOR_FUNCTIONAL_CONST const
64 #undef __EXECUTOR_FUNCTIONAL_NAME
65 #define __EXECUTOR_FUNCTIONAL_NAME(ParamCount) Executor##ParamCount##Params_const
66 #undef EXECUTOR_FUNCTIONAL_USE_CONST
67#endif
68
69#ifdef EXECUTOR_FUNCTIONAL_USE_STATIC
70 #ifdef EXECUTOR_FUNCTIONAL_USE_CONST
71  #error you obviously do not know what you are doing !! ask the bensch
72 #endif /* EXECUTOR_FUNCTIONAL_USE_CONST */
73
74#undef __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC
75 #define __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC       functionPointer
76 #undef __EXECUTOR_FUNCTIONAL_NAME
77 #define __EXECUTOR_FUNCTIONAL_NAME(ParamCount)    Executor##ParamCount##Params_static
78 #undef __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER
79 #define __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER    *functionPointer
80
81#undef EXECUTOR_FUNCTIONAL_USE_STATIC
82#endif /* EXECUTOR_FUNCTIONAL_USE_STATIC */
83
84template<class T> class __EXECUTOR_FUNCTIONAL_NAME(0) : public Executor
85{
86private:
87  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST;
88
89public:
90  __EXECUTOR_FUNCTIONAL_NAME(0) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST )
91      : Executor()
92  {
93    this->functorType = Executor_Objective;
94    this->functionPointer = functionPointer;
95  };
96
97  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
98  {
99    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)();
100  };
101
102  virtual Executor* clone() const
103  {
104    return new __EXECUTOR_FUNCTIONAL_NAME(0)<T>(this->functionPointer);
105  };
106};
107
108//! SINGLE VALUE
109template<class T, typename type0> class __EXECUTOR_FUNCTIONAL_NAME(1) : public Executor
110{
111private:
112  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST;
113
114public:
115  __EXECUTOR_FUNCTIONAL_NAME(1) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST)
116      : Executor(ExecutorParamType<type0>())
117  {
118    this->functorType = Executor_Objective;
119    this->functionPointer = functionPointer;
120  };
121
122  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
123  {
124
125    /* // THE VERY COOL DEBUG
126      printf("SUB[0] : %s\n", sub[0].c_str());
127      printf("getDefault<type0>(this->defaultValue, 0):::: %d\n", getDefault<type0>(this->defaultValue, 0));
128      printf("VALUE: %d\n", fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)));
129    */
130    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
131      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)) );
132  };
133
134  virtual Executor* clone() const
135  {
136    return  new __EXECUTOR_FUNCTIONAL_NAME(1)<T, type0>(this->functionPointer);
137  };
138};
139
140//! DOUBLE VALUE
141template<class T, typename type0, typename type1> class __EXECUTOR_FUNCTIONAL_NAME(2) : public Executor
142{
143private:
144  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST;
145
146public:
147  __EXECUTOR_FUNCTIONAL_NAME(2) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST)
148      : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>())
149  {
150    this->functorType = Executor_Objective;
151    this->functionPointer = functionPointer;
152  };
153
154  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
155  {
156    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
157      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
158      fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)));
159  };
160
161  virtual Executor* clone() const
162  {
163    return new __EXECUTOR_FUNCTIONAL_NAME(2)<T, type0, type1>(this->functionPointer);
164  };
165};
166
167
168//! TRIPPLE VALUE
169template<class T, typename type0, typename type1, typename type2> class __EXECUTOR_FUNCTIONAL_NAME(3) : public Executor
170{
171private:
172  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2) __EXECUTOR_FUNCTIONAL_CONST;
173
174public:
175  __EXECUTOR_FUNCTIONAL_NAME(3) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2) __EXECUTOR_FUNCTIONAL_CONST)
176      : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>())
177  {
178    this->functorType = Executor_Objective;
179    this->functionPointer = functionPointer;
180  };
181
182  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
183  {
184    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
185      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
186      fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)),
187      fromString<type2>(sub[2], getDefault<type2>(this->defaultValue, 2)));
188  };
189
190  virtual Executor* clone() const
191  {
192    return new __EXECUTOR_FUNCTIONAL_NAME(3)<T, type0, type1, type2>(this->functionPointer);
193  };
194};
195
196
197
198//! QUADRUPPEL VALUE
199template<class T, typename type0, typename type1, typename type2, typename type3> class __EXECUTOR_FUNCTIONAL_NAME(4) : public Executor
200{
201private:
202  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST;
203
204public:
205  __EXECUTOR_FUNCTIONAL_NAME(4) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST)
206  : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>())
207  {
208    this->functorType = Executor_Objective;
209    this->functionPointer = functionPointer;
210  };
211
212  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
213  {
214    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
215      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
216      fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)),
217      fromString<type2>(sub[2], getDefault<type2>(this->defaultValue, 2)),
218      fromString<type3>(sub[3], getDefault<type3>(this->defaultValue, 3)));
219  };
220
221  virtual Executor* clone() const
222  {
223    return new __EXECUTOR_FUNCTIONAL_NAME(4)<T, type0, type1, type2, type3>(this->functionPointer);
224  };
225};
226
227//! CHINQUE VALUE
228template<class T, typename type0, typename type1, typename type2, typename type3, typename type4> class __EXECUTOR_FUNCTIONAL_NAME(5) : public Executor
229{
230  private:
231    void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3, type4) __EXECUTOR_FUNCTIONAL_CONST;
232
233  public:
234    __EXECUTOR_FUNCTIONAL_NAME(5) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3, type4) __EXECUTOR_FUNCTIONAL_CONST)
235  : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>(), ExecutorParamType<type4>())
236    {
237      this->functorType = Executor_Objective;
238      this->functionPointer = functionPointer;
239    };
240
241    virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
242    {
243      (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
244          fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
245      fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)),
246      fromString<type2>(sub[2], getDefault<type2>(this->defaultValue, 2)),
247      fromString<type3>(sub[3], getDefault<type3>(this->defaultValue, 3)),
248      fromString<type4>(sub[4], getDefault<type4>(this->defaultValue, 4)));
249    };
250
251    virtual Executor* clone() const
252    {
253      return new __EXECUTOR_FUNCTIONAL_NAME(5)<T, type0, type1, type2, type3, type4>(this->functionPointer);
254    };
255};
256
257
258
259#define EXECUTOR_FUNCTIONAL_CREATOR0() \
260template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST) \
261{ \
262  return new __EXECUTOR_FUNCTIONAL_NAME(0)<T>(functionPointer); \
263}
264
265#define EXECUTOR_FUNCTIONAL_CREATOR1(type0) \
266template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
267{ \
268  return new __EXECUTOR_FUNCTIONAL_NAME(1)<T, type0##_TYPE>(functionPointer); \
269}
270
271#define EXECUTOR_FUNCTIONAL_CREATOR2(type0, type1) \
272template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
273{ \
274  return new __EXECUTOR_FUNCTIONAL_NAME(2)<T, type0##_TYPE, type1##_TYPE>(functionPointer); \
275}
276
277#define EXECUTOR_FUNCTIONAL_CREATOR3(type0, type1, type2) \
278template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE, type2##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
279{ \
280  return new __EXECUTOR_FUNCTIONAL_NAME(3)<T, type0##_TYPE, type1##_TYPE, type2##_TYPE>(functionPointer); \
281}
282
283#define EXECUTOR_FUNCTIONAL_CREATOR4(type0, type1, type2, type3) \
284template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
285{ \
286  return new __EXECUTOR_FUNCTIONAL_NAME(4)<T, type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE>(functionPointer); \
287}
288
289#define EXECUTOR_FUNCTIONAL_CREATOR5(type0, type1, type2, type3, type4) \
290template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
291{ \
292    return new __EXECUTOR_FUNCTIONAL_NAME(5)<T, type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE>(functionPointer); \
293}
294
295
296
297#define FUNCTOR_LIST(x) EXECUTOR_FUNCTIONAL_CREATOR ## x
298#include "functor_list.h"
299#undef FUNCTOR_LIST
300
301
302
303#undef __EXECUTOR_FUNCTIONAL_CONST
304#undef __EXECUTOR_FUNCTIONAL_NAME
305#undef __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC
306#undef __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER
Note: See TracBrowser for help on using the repository browser.