Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/util/executor/executor_functional.h @ 9730

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

cleaner executor with evaluate function, that will be made an Object Soon.

File size: 17.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 "executor.h"
26#include "substring.h"
27
28
29template<typename type> type fromString(const std::string& input, type defaultValue) { return defaultValue; };
30template<> bool fromString<bool>(const std::string& input, bool defaultValue);
31template<> int fromString<int>(const std::string& input, int defaultValue);
32template<> unsigned int fromString<unsigned int>(const std::string& input, unsigned int defaultValue);
33template<> float fromString<float>(const std::string& input, float defaultValue);
34template<> char fromString<char>(const std::string& input, char defaultValue);
35template<> const std::string& fromString<const std::string&>(const std::string& input, const std::string& defaultValue);
36
37template<typename type> type fromMulti(const MultiType& multi) { /* return defaultValue; */ };
38template<> bool fromMulti<bool>(const MultiType& multi);
39template<> int fromMulti<int>(const MultiType& multi);
40template<> unsigned int fromMulti<unsigned int>(const MultiType& multi);
41template<> float fromMulti<float>(const MultiType& multi);
42template<> char fromMulti<char>(const MultiType& multi);
43template<> const std::string& fromMulti<const std::string&>(const MultiType& multi);
44
45
46template<typename type> type getDefault(const MultiType* const defaultValues, unsigned int i) { return (type)0; };
47template<> bool getDefault<bool>(const MultiType* const defaultValues, unsigned int i);
48template<> int getDefault<int>(const MultiType* const defaultValues, unsigned int i);
49template<> unsigned int getDefault<unsigned int>(const MultiType* const defaultValues, unsigned int i);
50template<> float getDefault<float>(const MultiType* const defaultValues, unsigned int i);
51template<> char getDefault<char>(const MultiType* const defaultValues, unsigned int i);
52template<> const std::string& getDefault<const std::string&>(const MultiType* const defaultValues, unsigned int i);
53
54
55/**
56 * @brief to remove writing errors, this function is Used.
57 * @param sub The SubString to use
58 * @param default The default Values.
59 */
60template<typename type, int index> type Evaluate(const SubString& sub, const MultiType* const defaults)
61{
62  return (sub.size() > index) ?
63      fromString<type>(sub[index], getDefault<type>(defaults, index)) :
64      fromMulti<type>(defaults[index]);
65};
66
67
68#endif /* __EXECUTOR_FUNCTIONAL_H_ */
69
70//! if Functional is constant calling
71#define __EXECUTOR_FUNCTIONAL_CONST
72//! The Name to be attached to the functional (for normal, static, and const modes)
73#define __EXECUTOR_FUNCTIONAL_NAME(ParamCount)   Executor##ParamCount##Params
74//! The Execution-mode (either static or objective)
75#define __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC      dynamic_cast<T*>(object)->*(functionPointer)
76//! The Function-Pointer, and how to save it internally.
77#define __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER   T::*functionPointer
78
79#ifdef EXECUTOR_FUNCTIONAL_USE_CONST
80 #ifdef __EXECUTOR_FUNCTIONAL_CONST_INCLUDED
81  #define __EXECUTOR_FUNCTIONAL_NOT_INCLUDE_THIS
82 #else
83  #define __EXECUTOR_FUNCTIONAL_CONST_INCLUDED
84 #endif
85 #undef __EXECUTOR_FUNCTIONAL_CONST
86 #define __EXECUTOR_FUNCTIONAL_CONST const
87 #undef __EXECUTOR_FUNCTIONAL_NAME
88 #define __EXECUTOR_FUNCTIONAL_NAME(ParamCount) Executor##ParamCount##Params_const
89//#endif /* EXECUTOR_FUNCTIONAL_USE_CONST */
90
91#elif defined EXECUTOR_FUNCTIONAL_USE_STATIC
92 #ifdef EXECUTOR_FUNCTIONAL_USE_CONST
93  #error you obviously do not know what you are doing !! ask the bensch
94 #endif
95
96#ifdef __EXECUTOR_FUNCTIONAL_STATIC_INCLUDED
97   #define __EXECUTOR_FUNCTIONAL_NOT_INCLUDE_THIS
98  #else
99   #define __EXECUTOR_FUNCTIONAL_STATIC_INCLUDED
100  #endif
101
102#undef __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC
103 #define __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC       functionPointer
104 #undef __EXECUTOR_FUNCTIONAL_NAME
105 #define __EXECUTOR_FUNCTIONAL_NAME(ParamCount)    Executor##ParamCount##Params_static
106 #undef __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER
107 #define __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER    *functionPointer
108
109//#endif /* EXECUTOR_FUNCTIONAL_USE_STATIC */
110#else
111 #ifdef __EXECUTOR_FUNCTIONAL_PLAIN_INCLUDED
112  #define __EXECUTOR_FUNCTIONAL_NOT_INCLUDE_THIS
113 #else
114  #define __EXECUTOR_FUNCTIONAL_PLAIN_INCLUDED
115 #endif
116#endif
117
118#ifndef __EXECUTOR_FUNCTIONAL_NOT_INCLUDE_THIS
119
120///////////
121//// 0 ////
122///////////
123//! @brief ExecutorClass, that can execute Functions without any parameters.
124template<class T> class __EXECUTOR_FUNCTIONAL_NAME(0) : public Executor<const SubString>
125{
126private:
127  /** @brief the FunctioPointer. */
128  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST;
129
130public:
131  /**
132   * @brief constructs the Executor.
133   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
134   */
135  __EXECUTOR_FUNCTIONAL_NAME(0) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST )
136      : Executor<const SubString>(false)
137  {
138    this->functorType = Executor_Objective;
139    this->functionPointer = functionPointer;
140  };
141
142  /**
143   * @brief executes the Functional
144   * @param object the Object the action should be executed on.
145   * @param sub the SubString to get the Parameters from.
146   */
147  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
148  {
149    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)();
150  };
151
152  /**
153   * @brief copies the Executor
154   * @returns a new Executor that's a copy of this one.
155   */
156  virtual Executor<const SubString>* clone() const
157  {
158    return new __EXECUTOR_FUNCTIONAL_NAME(0)<T>(this->functionPointer);
159  };
160};
161
162
163
164///////////
165//// 1 ////
166///////////
167//! @brief ExecutorClass, that can execute Functions with one parameter.
168template<class T, typename type0> class __EXECUTOR_FUNCTIONAL_NAME(1) : public Executor<const SubString>
169{
170private:
171  /** @brief the FunctioPointer. */
172  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST;
173
174public:
175  /**
176   * @brief constructs the Executor.
177   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
178   */
179  __EXECUTOR_FUNCTIONAL_NAME(1) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST)
180      : Executor<const SubString>(false, ExecutorParamType<type0>())
181  {
182    this->functorType = Executor_Objective;
183    this->functionPointer = functionPointer;
184  };
185
186  /**
187   * @brief executes the Functional
188   * @param object the Object the action should be executed on.
189   * @param sub the SubString to get the Parameters from.
190   */
191  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
192  {
193    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
194      Evaluate<type0, 0>(sub, this->defaultValue));
195  };
196
197  /**
198   * @brief copies the Executor
199   * @returns a new Executor that's a copy of this one.
200   */
201  virtual Executor<const SubString>* clone() const
202  {
203    return  new __EXECUTOR_FUNCTIONAL_NAME(1)<T, type0>(this->functionPointer);
204  };
205};
206
207///////////
208//// 2 ////
209///////////
210//! @brief ExecutorClass, that can execute Functions with two parameters.
211template<class T, typename type0, typename type1> class __EXECUTOR_FUNCTIONAL_NAME(2) : public Executor<const SubString>
212{
213private:
214  /** @brief the FunctioPointer. */
215  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST;
216
217public:
218  /**
219   * @brief constructs the Executor.
220   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
221   */
222  __EXECUTOR_FUNCTIONAL_NAME(2) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST)
223      : Executor<const SubString>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>())
224  {
225    this->functorType = Executor_Objective;
226    this->functionPointer = functionPointer;
227  };
228
229  /**
230   * @brief executes the Functional
231   * @param object the Object the action should be executed on.
232   * @param sub the SubString to get the Parameters from.
233   */
234  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
235  {
236    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
237      Evaluate<type0, 0>(sub, this->defaultValue),
238      Evaluate<type1, 1>(sub, this->defaultValue));
239  };
240
241  /**
242   * @brief copies the Executor
243   * @returns a new Executor that's a copy of this one.
244   */
245  virtual Executor<const SubString>* clone() const
246  {
247    return new __EXECUTOR_FUNCTIONAL_NAME(2)<T, type0, type1>(this->functionPointer);
248  };
249};
250
251
252///////////
253//// 3 ////
254///////////
255//! @brief ExecutorClass, that can execute Functions with three parameters.
256template<class T, typename type0, typename type1, typename type2> class __EXECUTOR_FUNCTIONAL_NAME(3) : public Executor<const SubString>
257{
258private:
259  /** @brief the FunctioPointer. */
260  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2) __EXECUTOR_FUNCTIONAL_CONST;
261
262public:
263  /**
264   * @brief constructs the Executor.
265   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
266   */
267  __EXECUTOR_FUNCTIONAL_NAME(3) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2) __EXECUTOR_FUNCTIONAL_CONST)
268      : Executor<const SubString>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>())
269  {
270    this->functorType = Executor_Objective;
271    this->functionPointer = functionPointer;
272  };
273
274  /**
275   * @brief executes the Functional
276   * @param object the Object the action should be executed on.
277   * @param sub the SubString to get the Parameters from.
278   */
279  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
280  {
281    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
282      Evaluate<type0, 0>(sub, this->defaultValue),
283      Evaluate<type1, 1>(sub, this->defaultValue),
284      Evaluate<type2, 2>(sub, this->defaultValue));
285  };
286
287  /**
288   * @brief copies the Executor
289   * @returns a new Executor that's a copy of this one.
290   */
291  virtual Executor<const SubString>* clone() const
292  {
293    return new __EXECUTOR_FUNCTIONAL_NAME(3)<T, type0, type1, type2>(this->functionPointer);
294  };
295};
296
297
298
299///////////
300//// 4 ////
301///////////
302//! @brief ExecutorClass, that can execute Functions with four parameters.
303template<class T, typename type0, typename type1, typename type2, typename type3> class __EXECUTOR_FUNCTIONAL_NAME(4) : public Executor<const SubString>
304{
305private:
306  /** @brief the FunctioPointer. */
307  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST;
308
309public:
310  /**
311   * @brief constructs the Executor.
312   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
313   */
314  __EXECUTOR_FUNCTIONAL_NAME(4) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST)
315      : Executor<const SubString>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>())
316  {
317    this->functorType = Executor_Objective;
318    this->functionPointer = functionPointer;
319  };
320
321  /**
322  * @brief executes the Functional
323  * @param object the Object the action should be executed on.
324  * @param sub the SubString to get the Parameters from.
325   */
326  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
327  {
328    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
329      Evaluate<type0, 0>(sub, this->defaultValue),
330      Evaluate<type1, 1>(sub, this->defaultValue),
331      Evaluate<type2, 2>(sub, this->defaultValue),
332      Evaluate<type3, 3>(sub, this->defaultValue));
333  };
334
335  /**
336   * @brief copies the Executor
337   * @returns a new Executor that's a copy of this one.
338   */
339  virtual Executor<const SubString>* clone() const
340  {
341    return new __EXECUTOR_FUNCTIONAL_NAME(4)<T, type0, type1, type2, type3>(this->functionPointer);
342  };
343};
344
345
346
347///////////
348//// 5 ////
349///////////
350//! @brief ExecutorClass, that can execute Functions with five parameters.
351template<class T, typename type0, typename type1, typename type2, typename type3, typename type4> class __EXECUTOR_FUNCTIONAL_NAME(5) : public Executor<const SubString>
352{
353private:
354  /** @brief the FunctioPointer. */
355  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3, type4) __EXECUTOR_FUNCTIONAL_CONST;
356
357public:
358  /**
359   * @brief constructs the Executor.
360   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
361   */
362  __EXECUTOR_FUNCTIONAL_NAME(5) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3, type4) __EXECUTOR_FUNCTIONAL_CONST)
363      : Executor<const SubString>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>(), ExecutorParamType<type4>())
364  {
365    this->functorType = Executor_Objective;
366    this->functionPointer = functionPointer;
367  };
368
369  /**
370  * @brief executes the Functional
371  * @param object the Object the action should be executed on.
372  * @param sub the SubString to get the Parameters from.
373   */
374  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
375  {
376    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
377      Evaluate<type0, 0>(sub, this->defaultValue),
378      Evaluate<type1, 1>(sub, this->defaultValue),
379      Evaluate<type2, 2>(sub, this->defaultValue),
380      Evaluate<type3, 3>(sub, this->defaultValue),
381      Evaluate<type4, 4>(sub, this->defaultValue));
382  };
383
384  /**
385   * @brief copies the Executor
386   * @returns a new Executor that's a copy of this one.
387   */
388  virtual Executor<const SubString>* clone() const
389  {
390    return new __EXECUTOR_FUNCTIONAL_NAME(5)<T, type0, type1, type2, type3, type4>(this->functionPointer);
391  };
392};
393
394
395
396/**
397 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
398 */
399#define EXECUTOR_FUNCTIONAL_CREATOR0() \
400template<class T> Executor<const SubString>* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST) \
401{ \
402  return new __EXECUTOR_FUNCTIONAL_NAME(0)<T>(functionPointer); \
403}
404
405/**
406 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
407 * @param type0 for internal usage: the first Argument
408 */
409#define EXECUTOR_FUNCTIONAL_CREATOR1(type0) \
410template<class T> Executor<const SubString>* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
411{ \
412  return new __EXECUTOR_FUNCTIONAL_NAME(1)<T, type0##_TYPE>(functionPointer); \
413}
414
415/**
416 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
417 * @param type0 for internal usage: the first Argument
418 * @param type1 for internal usage: the second Argument
419 */
420#define EXECUTOR_FUNCTIONAL_CREATOR2(type0, type1) \
421template<class T> Executor<const SubString>* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
422{ \
423  return new __EXECUTOR_FUNCTIONAL_NAME(2)<T, type0##_TYPE, type1##_TYPE>(functionPointer); \
424}
425
426/**
427 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
428 * @param type0 for internal usage: the first Argument
429 * @param type1 for internal usage: the second Argument
430 * @param type2 for internal usage: the third Argument
431 */
432#define EXECUTOR_FUNCTIONAL_CREATOR3(type0, type1, type2) \
433template<class T> Executor<const SubString>* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE, type2##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
434{ \
435  return new __EXECUTOR_FUNCTIONAL_NAME(3)<T, type0##_TYPE, type1##_TYPE, type2##_TYPE>(functionPointer); \
436}
437
438/**
439 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
440 * @param type0 for internal usage: the first Argument
441 * @param type1 for internal usage: the second Argument
442 * @param type2 for internal usage: the third Argument
443 * @param type3 for internal usage: the fourth Argument
444 */
445#define EXECUTOR_FUNCTIONAL_CREATOR4(type0, type1, type2, type3) \
446template<class T> Executor<const SubString>* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
447{ \
448  return new __EXECUTOR_FUNCTIONAL_NAME(4)<T, type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE>(functionPointer); \
449}
450
451/**
452 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
453 * @param type0 for internal usage: the first Argument
454 * @param type1 for internal usage: the second Argument
455 * @param type2 for internal usage: the third Argument
456 * @param type3 for internal usage: the fourth Argument
457 * @param type4 for internal usage: the fifth Argument
458 */
459#define EXECUTOR_FUNCTIONAL_CREATOR5(type0, type1, type2, type3, type4) \
460template<class T> Executor<const SubString>* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
461{ \
462    return new __EXECUTOR_FUNCTIONAL_NAME(5)<T, type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE>(functionPointer); \
463}
464
465
466/**
467 * Creates the FunctionCallers
468 */
469#define FUNCTOR_LIST(x) EXECUTOR_FUNCTIONAL_CREATOR ## x
470#include "functor_list.h"
471#undef FUNCTOR_LIST
472
473
474
475#undef __EXECUTOR_FUNCTIONAL_CONST
476#undef __EXECUTOR_FUNCTIONAL_NAME
477#undef __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC
478#undef __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER
479
480#ifdef EXECUTOR_FUNCTIONAL_USE_CONST
481 #undef EXECUTOR_FUNCTIONAL_USE_CONST
482#endif
483#ifdef EXECUTOR_FUNCTIONAL_USE_STATIC
484 #undef EXECUTOR_FUNCTIONAL_USE_STATIC
485#endif
486
487
488#endif /* __EXECUTOR_FUNCTIONAL_NOT_INCLUDE_THIS */
489#undef __EXECUTOR_FUNCTIONAL_NOT_INCLUDE_THIS
Note: See TracBrowser for help on using the repository browser.