Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

new_class_id: better constructs for the Executor.

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