Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/new_class_id: removed all the base of Executor.
Now it is purely Templated :), since it is not needed otherwise

@note: The Explicit Specializations may not be Templatet and defined in the header, that is why still there exist some cc-file

File size: 18.8 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->functionType = Executor<CallType>::FunctionDefault;
143    this->functionPointer = functionPointer;
144  };
145
146  /**
147   * @brief executes the Functional
148   * @param object the Object the action should be executed on.
149   * @param sub the SubString to get the Parameters from.
150   */
151  virtual void operator()(BaseObject* object, CallType& sub = SubString()) const
152  {
153    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)();
154  };
155
156  /**
157   * @brief copies the Executor
158   * @returns a new Executor that's a copy of this one.
159   */
160  virtual Executor<CallType>* clone() const
161  {
162    return new __EXECUTOR_FUNCTIONAL_NAME(0)<T, CallType>(this->functionPointer);
163  };
164};
165
166
167
168///////////
169//// 1 ////
170///////////
171//! @brief ExecutorClass, that can execute Functions with one parameter.
172template<class T, typename CallType, typename type0, template<typename, typename, int> class Evaluater = ExecutorEvaluater>
173class __EXECUTOR_FUNCTIONAL_NAME(1) : public Executor<CallType>
174{
175private:
176  /** @brief the FunctioPointer. */
177  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST;
178
179public:
180  /**
181   * @brief constructs the Executor.
182   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
183   */
184  __EXECUTOR_FUNCTIONAL_NAME(1) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST)
185      : Executor<CallType>(false, ExecutorParamType<type0>())
186  {
187    this->functionType = Executor<CallType>::FunctionDefault;
188    this->functionPointer = functionPointer;
189  };
190
191  /**
192   * @brief executes the Functional
193   * @param object the Object the action should be executed on.
194   * @param sub the SubString to get the Parameters from.
195   */
196  virtual void operator()(BaseObject* object, CallType& sub = SubString()) const
197  {
198    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
199      Evaluater<CallType, type0, 0>()(sub, this->defaultValue));
200  };
201
202  /**
203   * @brief copies the Executor
204   * @returns a new Executor that's a copy of this one.
205   */
206  virtual Executor<CallType>* clone() const
207  {
208    return  new __EXECUTOR_FUNCTIONAL_NAME(1)<T, CallType, type0>(this->functionPointer);
209  };
210};
211
212///////////
213//// 2 ////
214///////////
215//! @brief ExecutorClass, that can execute Functions with two parameters.
216template<class T, typename CallType, typename type0, typename type1, template<typename, typename, int> class Evaluater = ExecutorEvaluater>
217class __EXECUTOR_FUNCTIONAL_NAME(2) : public Executor<CallType>
218{
219private:
220  /** @brief the FunctioPointer. */
221  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST;
222
223public:
224  /**
225   * @brief constructs the Executor.
226   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
227   */
228  __EXECUTOR_FUNCTIONAL_NAME(2) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST)
229      : Executor<CallType>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>())
230  {
231    this->functionType = Executor<CallType>::FunctionDefault;
232    this->functionPointer = functionPointer;
233  };
234
235  /**
236   * @brief executes the Functional
237   * @param object the Object the action should be executed on.
238   * @param sub the SubString to get the Parameters from.
239   */
240  virtual void operator()(BaseObject* object, CallType& sub = SubString()) const
241  {
242    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
243      Evaluater<CallType, type0, 0>()(sub, this->defaultValue),
244      Evaluater<CallType, type1, 1>()(sub, this->defaultValue));
245  };
246
247  /**
248   * @brief copies the Executor
249   * @returns a new Executor that's a copy of this one.
250   */
251  virtual Executor<CallType>* clone() const
252  {
253    return new __EXECUTOR_FUNCTIONAL_NAME(2)<T, CallType, type0, type1>(this->functionPointer);
254  };
255};
256
257
258///////////
259//// 3 ////
260///////////
261//! @brief ExecutorClass, that can execute Functions with three parameters.
262template<class T, typename CallType, typename type0, typename type1, typename type2, template<typename, typename, int> class Evaluater = ExecutorEvaluater>
263class __EXECUTOR_FUNCTIONAL_NAME(3) : public Executor<CallType>
264{
265private:
266  /** @brief the FunctioPointer. */
267  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2) __EXECUTOR_FUNCTIONAL_CONST;
268
269public:
270  /**
271   * @brief constructs the Executor.
272   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
273   */
274  __EXECUTOR_FUNCTIONAL_NAME(3) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2) __EXECUTOR_FUNCTIONAL_CONST)
275      : Executor<CallType>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>())
276  {
277    this->functionType = Executor<CallType>::FunctionDefault;
278    this->functionPointer = functionPointer;
279  };
280
281  /**
282   * @brief executes the Functional
283   * @param object the Object the action should be executed on.
284   * @param sub the SubString to get the Parameters from.
285   */
286  virtual void operator()(BaseObject* object, CallType& sub = SubString()) const
287  {
288    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
289      Evaluater<CallType, type0, 0>()(sub, this->defaultValue),
290      Evaluater<CallType, type1, 1>()(sub, this->defaultValue),
291      Evaluater<CallType, type2, 2>()(sub, this->defaultValue));
292  };
293
294  /**
295   * @brief copies the Executor
296   * @returns a new Executor that's a copy of this one.
297   */
298  virtual Executor<CallType>* clone() const
299  {
300    return new __EXECUTOR_FUNCTIONAL_NAME(3)<T, CallType, type0, type1, type2>(this->functionPointer);
301  };
302};
303
304
305
306///////////
307//// 4 ////
308///////////
309//! @brief ExecutorClass, that can execute Functions with four parameters.
310template<class T, typename CallType, typename type0, typename type1, typename type2, typename type3, template<typename, typename, int> class Evaluater = ExecutorEvaluater>
311class __EXECUTOR_FUNCTIONAL_NAME(4) : public Executor<CallType>
312{
313private:
314  /** @brief the FunctioPointer. */
315  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST;
316
317public:
318  /**
319   * @brief constructs the Executor.
320   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
321   */
322  __EXECUTOR_FUNCTIONAL_NAME(4) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST)
323      : Executor<CallType>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>())
324  {
325    this->functionType = Executor<CallType>::FunctionDefault;
326    this->functionPointer = functionPointer;
327  };
328
329  /**
330  * @brief executes the Functional
331  * @param object the Object the action should be executed on.
332  * @param sub the SubString to get the Parameters from.
333   */
334  virtual void operator()(BaseObject* object, CallType& sub = SubString()) const
335  {
336    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
337      Evaluater<CallType, type0, 0>()(sub, this->defaultValue),
338      Evaluater<CallType, type1, 1>()(sub, this->defaultValue),
339      Evaluater<CallType, type2, 2>()(sub, this->defaultValue),
340      Evaluater<CallType, type3, 3>()(sub, this->defaultValue));
341  };
342
343  /**
344   * @brief copies the Executor
345   * @returns a new Executor that's a copy of this one.
346   */
347  virtual Executor<CallType>* clone() const
348  {
349    return new __EXECUTOR_FUNCTIONAL_NAME(4)<T, CallType, type0, type1, type2, type3>(this->functionPointer);
350  };
351};
352
353
354
355///////////
356//// 5 ////
357///////////
358//! @brief ExecutorClass, that can execute Functions with five parameters.
359template<class T, typename CallType, typename type0, typename type1, typename type2, typename type3, typename type4, template<typename, typename, int> class Evaluater = ExecutorEvaluater>
360class __EXECUTOR_FUNCTIONAL_NAME(5) : public Executor<CallType>
361{
362private:
363  /** @brief the FunctioPointer. */
364  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3, type4) __EXECUTOR_FUNCTIONAL_CONST;
365
366public:
367  /**
368   * @brief constructs the Executor.
369   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
370   */
371  __EXECUTOR_FUNCTIONAL_NAME(5) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3, type4) __EXECUTOR_FUNCTIONAL_CONST)
372      : Executor<CallType>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>(), ExecutorParamType<type4>())
373  {
374    this->functionType = Executor<CallType>::FunctionDefault;
375    this->functionPointer = functionPointer;
376  };
377
378  /**
379  * @brief executes the Functional
380  * @param object the Object the action should be executed on.
381  * @param sub the SubString to get the Parameters from.
382   */
383  virtual void operator()(BaseObject* object, CallType& sub = SubString()) const
384  {
385    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
386      Evaluater<CallType, type0, 0>()(sub, this->defaultValue),
387      Evaluater<CallType, type1, 1>()(sub, this->defaultValue),
388      Evaluater<CallType, type2, 2>()(sub, this->defaultValue),
389      Evaluater<CallType, type3, 3>()(sub, this->defaultValue),
390      Evaluater<CallType, type4, 4>()(sub, this->defaultValue));
391  };
392
393  /**
394   * @brief copies the Executor
395   * @returns a new Executor that's a copy of this one.
396   */
397  virtual Executor<CallType>* clone() const
398  {
399    return new __EXECUTOR_FUNCTIONAL_NAME(5)<T, CallType, type0, type1, type2, type3, type4>(this->functionPointer);
400  };
401};
402
403
404
405
406
407
408
409
410
411// // // // // // // // // // // // //
412//// EXTENSION TO HIDE CONSTRUCT /////
413// // // // // // // // // // // // //
414/**
415 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
416 */
417#define EXECUTOR_FUNCTIONAL_CREATOR0() \
418template<class T> Executor<const SubString>* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST) \
419{ \
420  return new __EXECUTOR_FUNCTIONAL_NAME(0)<T, const SubString>(functionPointer); \
421}
422
423/**
424 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
425 * @param type0 for internal usage: the first Argument
426 */
427#define EXECUTOR_FUNCTIONAL_CREATOR1(type0) \
428template<class T> Executor<const SubString>* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST) \
429{ \
430  return new __EXECUTOR_FUNCTIONAL_NAME(1)<T, const SubString, type0>(functionPointer); \
431}
432
433/**
434 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
435 * @param type0 for internal usage: the first Argument
436 * @param type1 for internal usage: the second Argument
437 */
438#define EXECUTOR_FUNCTIONAL_CREATOR2(type0, type1) \
439template<class T> Executor<const SubString>* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST) \
440{ \
441  return new __EXECUTOR_FUNCTIONAL_NAME(2)<T, const SubString, type0, type1>(functionPointer); \
442}
443
444/**
445 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
446 * @param type0 for internal usage: the first Argument
447 * @param type1 for internal usage: the second Argument
448 * @param type2 for internal usage: the third Argument
449 */
450#define EXECUTOR_FUNCTIONAL_CREATOR3(type0, type1, type2) \
451template<class T> Executor<const SubString>* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2) __EXECUTOR_FUNCTIONAL_CONST) \
452{ \
453  return new __EXECUTOR_FUNCTIONAL_NAME(3)<T, const SubString, type0, type1, type2>(functionPointer); \
454}
455
456/**
457 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
458 * @param type0 for internal usage: the first Argument
459 * @param type1 for internal usage: the second Argument
460 * @param type2 for internal usage: the third Argument
461 * @param type3 for internal usage: the fourth Argument
462 */
463#define EXECUTOR_FUNCTIONAL_CREATOR4(type0, type1, type2, type3) \
464template<class T> Executor<const SubString>* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST) \
465{ \
466  return new __EXECUTOR_FUNCTIONAL_NAME(4)<T, const SubString, type0, type1, type2, type3>(functionPointer); \
467}
468
469/**
470 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
471 * @param type0 for internal usage: the first Argument
472 * @param type1 for internal usage: the second Argument
473 * @param type2 for internal usage: the third Argument
474 * @param type3 for internal usage: the fourth Argument
475 * @param type4 for internal usage: the fifth Argument
476 */
477#define EXECUTOR_FUNCTIONAL_CREATOR5(type0, type1, type2, type3, type4) \
478template<class T> Executor<const SubString>* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3, type4) __EXECUTOR_FUNCTIONAL_CONST) \
479{ \
480    return new __EXECUTOR_FUNCTIONAL_NAME(5)<T, const SubString, type0, type1, type2, type3, type4>(functionPointer); \
481}
482
483
484/**
485 * Creates the FunctionCallers
486 */
487#define FUNCTOR_LIST(x) EXECUTOR_FUNCTIONAL_CREATOR ## x
488#include "functor_list.h"
489#undef FUNCTOR_LIST
490
491
492
493#undef __EXECUTOR_FUNCTIONAL_CONST
494#undef __EXECUTOR_FUNCTIONAL_NAME
495#undef __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC
496#undef __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER
497
498#ifdef EXECUTOR_FUNCTIONAL_USE_CONST
499 #undef EXECUTOR_FUNCTIONAL_USE_CONST
500#endif
501#ifdef EXECUTOR_FUNCTIONAL_USE_STATIC
502 #undef EXECUTOR_FUNCTIONAL_USE_STATIC
503#endif
504
505
506#endif /* __EXECUTOR_FUNCTIONAL_NOT_INCLUDE_THIS */
507#undef __EXECUTOR_FUNCTIONAL_NOT_INCLUDE_THIS
Note: See TracBrowser for help on using the repository browser.