Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: comments

File size: 16.7 KB
RevLine 
[4838]1/*!
[7716]2 * @file executor_functional.h
[7197]3 * Definition of an Executor
[5391]4 */
[1853]5
[7716]6/*
7   orxonox - the future of 3D-vertical-scrollers
[1853]8
[7716]9   Copyright (C) 2004 orx
[1853]10
[7716]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.
[5141]15
[7716]16### File Specific:
17   main-programmer: Benjamin Grauer
18   co-programmer: ...
19*/
[5652]20
[5328]21
[7716]22#ifndef __EXECUTOR_FUNCTIONAL_H_
23#define __EXECUTOR_FUNCTIONAL_H_
[5641]24
[7721]25#include "executor.h"
[5641]26
[7716]27template<typename type> MT_Type ExecutorParamType() { return MT_EXT1; };
[7721]28template<> MT_Type ExecutorParamType<bool>();
29template<> MT_Type ExecutorParamType<int>();
30template<> MT_Type ExecutorParamType<unsigned int>();
31template<> MT_Type ExecutorParamType<float>();
32template<> MT_Type ExecutorParamType<char>();
33template<> MT_Type ExecutorParamType<const std::string&>();
[5161]34
[7716]35template<typename type> type fromString(const std::string& input, type defaultValue) {return defaultValue; };
[7721]36template<> bool fromString<bool>(const std::string& input, bool defaultValue);
37template<> int fromString<int>(const std::string& input, int defaultValue);
38template<> unsigned int fromString<unsigned int>(const std::string& input, unsigned int defaultValue);
39template<> float fromString<float>(const std::string& input, float defaultValue);
40template<> char fromString<char>(const std::string& input, char defaultValue);
41template<> const std::string& fromString<const std::string&>(const std::string& input, const std::string& defaultValue);
[5641]42
[7716]43template<typename type> type getDefault(const MultiType* const defaultValues, unsigned int i) { return (type)0; };
[7721]44template<> bool getDefault<bool>(const MultiType* const defaultValues, unsigned int i);
45template<> int getDefault<int>(const MultiType* const defaultValues, unsigned int i);
46template<> unsigned int getDefault<unsigned int>(const MultiType* const defaultValues, unsigned int i);
47template<> float getDefault<float>(const MultiType* const defaultValues, unsigned int i);
48template<> char getDefault<char>(const MultiType* const defaultValues, unsigned int i);
49template<> const std::string& getDefault<const std::string&>(const MultiType* const defaultValues, unsigned int i);
[5161]50
[7716]51#endif /* __EXECUTOR_FUNCTIONAL_H_ */
[5161]52
[7724]53//! if Functional is constant calling
[7716]54#define __EXECUTOR_FUNCTIONAL_CONST
[7724]55//! The Name to be attached to the functional (for normal, static, and const modes)
[7716]56#define __EXECUTOR_FUNCTIONAL_NAME(ParamCount)   Executor##ParamCount##Params
[7724]57//! The Execution-mode (either static or objective)
[7716]58#define __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC      dynamic_cast<T*>(object)->*(functionPointer)
[7724]59//! The Function-Pointer, and how to save it internally.
[7716]60#define __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER   T::*functionPointer
[5161]61
[7716]62#ifdef EXECUTOR_FUNCTIONAL_USE_CONST
63 #undef __EXECUTOR_FUNCTIONAL_CONST
64 #define __EXECUTOR_FUNCTIONAL_CONST const
65 #undef __EXECUTOR_FUNCTIONAL_NAME
66 #define __EXECUTOR_FUNCTIONAL_NAME(ParamCount) Executor##ParamCount##Params_const
67 #undef EXECUTOR_FUNCTIONAL_USE_CONST
68#endif
[5161]69
[7716]70#ifdef EXECUTOR_FUNCTIONAL_USE_STATIC
71 #ifdef EXECUTOR_FUNCTIONAL_USE_CONST
72  #error you obviously do not know what you are doing !! ask the bensch
73 #endif /* EXECUTOR_FUNCTIONAL_USE_CONST */
[5135]74
[7719]75#undef __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC
[7716]76 #define __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC       functionPointer
77 #undef __EXECUTOR_FUNCTIONAL_NAME
78 #define __EXECUTOR_FUNCTIONAL_NAME(ParamCount)    Executor##ParamCount##Params_static
79 #undef __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER
80 #define __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER    *functionPointer
[5636]81
[7719]82#undef EXECUTOR_FUNCTIONAL_USE_STATIC
[7716]83#endif /* EXECUTOR_FUNCTIONAL_USE_STATIC */
[7714]84
[7724]85//! @brief ExecutorClass, that can execute Functions without any parameters.
[7716]86template<class T> class __EXECUTOR_FUNCTIONAL_NAME(0) : public Executor
87{
88private:
[7724]89  /** @brief the FunctioPointer. */
[7716]90  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST;
[5551]91
[7716]92public:
[7724]93  /**
94   * @brief constructs the Executor.
95   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
96   */
[7716]97  __EXECUTOR_FUNCTIONAL_NAME(0) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST )
98      : Executor()
99  {
100    this->functorType = Executor_Objective;
101    this->functionPointer = functionPointer;
[7717]102  };
103
[7724]104  /**
105   * @brief executes the Functional
106   * @param object the Object the action should be executed on.
107   * @param sub the SubString to get the Parameters from.
108   */
[7716]109  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
110  {
111    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)();
112  };
[5142]113
[7724]114  /**
115   * @brief copies the Executor
116   * @returns a new Executor that's a copy of this one.
117   */
[7719]118  virtual Executor* clone() const
119  {
[7717]120    return new __EXECUTOR_FUNCTIONAL_NAME(0)<T>(this->functionPointer);
121  };
[7716]122};
[5135]123
[7724]124//! @brief ExecutorClass, that can execute Functions with one parameter.
[7716]125template<class T, typename type0> class __EXECUTOR_FUNCTIONAL_NAME(1) : public Executor
126{
127private:
[7724]128  /** @brief the FunctioPointer. */
[7716]129  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST;
[5141]130
[7716]131public:
[7724]132  /**
133   * @brief constructs the Executor.
134   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
135   */
[7716]136  __EXECUTOR_FUNCTIONAL_NAME(1) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST)
137      : Executor(ExecutorParamType<type0>())
138  {
139    this->functorType = Executor_Objective;
140    this->functionPointer = functionPointer;
[7717]141  };
142
[7724]143  /**
144   * @brief executes the Functional
145   * @param object the Object the action should be executed on.
146   * @param sub the SubString to get the Parameters from.
147   */
[7716]148  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
149  {
[5153]150
[7716]151    /* // THE VERY COOL DEBUG
152      printf("SUB[0] : %s\n", sub[0].c_str());
153      printf("getDefault<type0>(this->defaultValue, 0):::: %d\n", getDefault<type0>(this->defaultValue, 0));
154      printf("VALUE: %d\n", fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)));
155    */
156    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
157      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)) );
158  };
[5153]159
[7724]160  /**
161   * @brief copies the Executor
162   * @returns a new Executor that's a copy of this one.
163   */
[7719]164  virtual Executor* clone() const
165  {
[7717]166    return  new __EXECUTOR_FUNCTIONAL_NAME(1)<T, type0>(this->functionPointer);
167  };
[7716]168};
[5145]169
[7724]170//! @brief ExecutorClass, that can execute Functions with two parameters.
[7716]171template<class T, typename type0, typename type1> class __EXECUTOR_FUNCTIONAL_NAME(2) : public Executor
172{
173private:
[7724]174  /** @brief the FunctioPointer. */
[7716]175  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST;
[5145]176
[7714]177public:
[7724]178  /**
179   * @brief constructs the Executor.
180   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
181   */
[7716]182  __EXECUTOR_FUNCTIONAL_NAME(2) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST)
183      : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>())
[7714]184  {
[7716]185    this->functorType = Executor_Objective;
186    this->functionPointer = functionPointer;
[7717]187  };
188
[7724]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   */
[7714]194  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
195  {
[7716]196    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
197      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
198      fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)));
199  };
[7714]200
[7724]201  /**
202   * @brief copies the Executor
203   * @returns a new Executor that's a copy of this one.
204   */
[7719]205  virtual Executor* clone() const
206  {
[7717]207    return new __EXECUTOR_FUNCTIONAL_NAME(2)<T, type0, type1>(this->functionPointer);
208  };
[5129]209};
[5113]210
[5632]211
[7724]212//! @brief ExecutorClass, that can execute Functions with three parameters.
[7719]213template<class T, typename type0, typename type1, typename type2> class __EXECUTOR_FUNCTIONAL_NAME(3) : public Executor
214{
215private:
[7724]216  /** @brief the FunctioPointer. */
[7719]217  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2) __EXECUTOR_FUNCTIONAL_CONST;
[5326]218
[7719]219public:
[7724]220  /**
221   * @brief constructs the Executor.
222   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
223   */
[7719]224  __EXECUTOR_FUNCTIONAL_NAME(3) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2) __EXECUTOR_FUNCTIONAL_CONST)
225      : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>())
226  {
227    this->functorType = Executor_Objective;
228    this->functionPointer = functionPointer;
229  };
[7716]230
[7724]231  /**
232   * @brief executes the Functional
233   * @param object the Object the action should be executed on.
234   * @param sub the SubString to get the Parameters from.
235   */
[7719]236  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
237  {
238    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
239      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
240      fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)),
241      fromString<type2>(sub[2], getDefault<type2>(this->defaultValue, 2)));
242  };
[7718]243
[7724]244  /**
245   * @brief copies the Executor
246   * @returns a new Executor that's a copy of this one.
247   */
[7719]248  virtual Executor* clone() const
249  {
250    return new __EXECUTOR_FUNCTIONAL_NAME(3)<T, type0, type1, type2>(this->functionPointer);
251  };
252};
253
254
255
[7724]256//! @brief ExecutorClass, that can execute Functions with four parameters.
[7719]257template<class T, typename type0, typename type1, typename type2, typename type3> class __EXECUTOR_FUNCTIONAL_NAME(4) : public Executor
258{
259private:
[7724]260  /** @brief the FunctioPointer. */
[7719]261  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST;
262
263public:
[7724]264  /**
265   * @brief constructs the Executor.
266   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
267   */
[7719]268  __EXECUTOR_FUNCTIONAL_NAME(4) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST)
[7724]269      : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>())
[7719]270  {
271    this->functorType = Executor_Objective;
272    this->functionPointer = functionPointer;
273  };
274
[7724]275  /**
276  * @brief executes the Functional
277  * @param object the Object the action should be executed on.
278  * @param sub the SubString to get the Parameters from.
279   */
[7719]280  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
281  {
282    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
283      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
284      fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)),
285      fromString<type2>(sub[2], getDefault<type2>(this->defaultValue, 2)),
286      fromString<type3>(sub[3], getDefault<type3>(this->defaultValue, 3)));
287  };
288
[7724]289  /**
290   * @brief copies the Executor
291   * @returns a new Executor that's a copy of this one.
292   */
[7719]293  virtual Executor* clone() const
294  {
295    return new __EXECUTOR_FUNCTIONAL_NAME(4)<T, type0, type1, type2, type3>(this->functionPointer);
296  };
297};
298
[7724]299//! @brief ExecutorClass, that can execute Functions with five parameters.
[7719]300template<class T, typename type0, typename type1, typename type2, typename type3, typename type4> class __EXECUTOR_FUNCTIONAL_NAME(5) : public Executor
301{
[7724]302private:
303  /** @brief the FunctioPointer. */
304  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3, type4) __EXECUTOR_FUNCTIONAL_CONST;
[7719]305
[7724]306public:
307  /**
308   * @brief constructs the Executor.
309   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
310   */
311  __EXECUTOR_FUNCTIONAL_NAME(5) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3, type4) __EXECUTOR_FUNCTIONAL_CONST)
312      : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>(), ExecutorParamType<type4>())
313  {
314    this->functorType = Executor_Objective;
315    this->functionPointer = functionPointer;
316  };
[7719]317
[7724]318  /**
319  * @brief executes the Functional
320  * @param object the Object the action should be executed on.
321  * @param sub the SubString to get the Parameters from.
322   */
323  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
324  {
325    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
326      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
[7719]327      fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)),
328      fromString<type2>(sub[2], getDefault<type2>(this->defaultValue, 2)),
329      fromString<type3>(sub[3], getDefault<type3>(this->defaultValue, 3)),
330      fromString<type4>(sub[4], getDefault<type4>(this->defaultValue, 4)));
[7724]331  };
[7719]332
[7724]333  /**
334   * @brief copies the Executor
335   * @returns a new Executor that's a copy of this one.
336   */
337  virtual Executor* clone() const
338  {
339    return new __EXECUTOR_FUNCTIONAL_NAME(5)<T, type0, type1, type2, type3, type4>(this->functionPointer);
340  };
[7719]341};
342
343
344
[7724]345/**
346 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
347 */
[7718]348#define EXECUTOR_FUNCTIONAL_CREATOR0() \
349template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST) \
350{ \
351  return new __EXECUTOR_FUNCTIONAL_NAME(0)<T>(functionPointer); \
[7716]352}
[7718]353
[7724]354/**
355 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
356 * @param type0 for internal usage: the first Argument
357 */
[7718]358#define EXECUTOR_FUNCTIONAL_CREATOR1(type0) \
359template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
360{ \
361  return new __EXECUTOR_FUNCTIONAL_NAME(1)<T, type0##_TYPE>(functionPointer); \
[7716]362}
[7718]363
[7724]364/**
365 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
366 * @param type0 for internal usage: the first Argument
367 * @param type1 for internal usage: the second Argument
368 */
[7718]369#define EXECUTOR_FUNCTIONAL_CREATOR2(type0, type1) \
370template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
371{ \
372  return new __EXECUTOR_FUNCTIONAL_NAME(2)<T, type0##_TYPE, type1##_TYPE>(functionPointer); \
[7716]373}
[5641]374
[7724]375/**
376 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
377 * @param type0 for internal usage: the first Argument
378 * @param type1 for internal usage: the second Argument
379 * @param type2 for internal usage: the third Argument
380 */
[7719]381#define EXECUTOR_FUNCTIONAL_CREATOR3(type0, type1, type2) \
382template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE, type2##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
383{ \
384  return new __EXECUTOR_FUNCTIONAL_NAME(3)<T, type0##_TYPE, type1##_TYPE, type2##_TYPE>(functionPointer); \
385}
[5326]386
[7724]387/**
388 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
389 * @param type0 for internal usage: the first Argument
390 * @param type1 for internal usage: the second Argument
391 * @param type2 for internal usage: the third Argument
392 * @param type3 for internal usage: the fourth Argument
393 */
[7719]394#define EXECUTOR_FUNCTIONAL_CREATOR4(type0, type1, type2, type3) \
395template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
396{ \
397  return new __EXECUTOR_FUNCTIONAL_NAME(4)<T, type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE>(functionPointer); \
398}
[7718]399
[7724]400/**
401 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
402 * @param type0 for internal usage: the first Argument
403 * @param type1 for internal usage: the second Argument
404 * @param type2 for internal usage: the third Argument
405 * @param type3 for internal usage: the fourth Argument
406 * @param type4 for internal usage: the fifth Argument
407 */
[7719]408#define EXECUTOR_FUNCTIONAL_CREATOR5(type0, type1, type2, type3, type4) \
409template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
410{ \
411    return new __EXECUTOR_FUNCTIONAL_NAME(5)<T, type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE>(functionPointer); \
412}
[7718]413
[7719]414
[7724]415/**
416 * Creates the FunctionCallers
417 */
[7718]418#define FUNCTOR_LIST(x) EXECUTOR_FUNCTIONAL_CREATOR ## x
419#include "functor_list.h"
420#undef FUNCTOR_LIST
421
422
423
[7716]424#undef __EXECUTOR_FUNCTIONAL_CONST
425#undef __EXECUTOR_FUNCTIONAL_NAME
426#undef __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC
427#undef __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER
Note: See TracBrowser for help on using the repository browser.