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
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
27template<typename type> MT_Type ExecutorParamType() { return MT_EXT1; };
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&>();
34
35template<typename type> type fromString(const std::string& input, type defaultValue) {return defaultValue; };
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);
42
43template<typename type> type getDefault(const MultiType* const defaultValues, unsigned int i) { return (type)0; };
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);
50
51#endif /* __EXECUTOR_FUNCTIONAL_H_ */
52
53//! if Functional is constant calling
54#define __EXECUTOR_FUNCTIONAL_CONST
55//! The Name to be attached to the functional (for normal, static, and const modes)
56#define __EXECUTOR_FUNCTIONAL_NAME(ParamCount)   Executor##ParamCount##Params
57//! The Execution-mode (either static or objective)
58#define __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC      dynamic_cast<T*>(object)->*(functionPointer)
59//! The Function-Pointer, and how to save it internally.
60#define __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER   T::*functionPointer
61
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
69
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 */
74
75#undef __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC
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
81
82#undef EXECUTOR_FUNCTIONAL_USE_STATIC
83#endif /* EXECUTOR_FUNCTIONAL_USE_STATIC */
84
85//! @brief ExecutorClass, that can execute Functions without any parameters.
86template<class T> class __EXECUTOR_FUNCTIONAL_NAME(0) : public Executor
87{
88private:
89  /** @brief the FunctioPointer. */
90  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST;
91
92public:
93  /**
94   * @brief constructs the Executor.
95   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
96   */
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;
102  };
103
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   */
109  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
110  {
111    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)();
112  };
113
114  /**
115   * @brief copies the Executor
116   * @returns a new Executor that's a copy of this one.
117   */
118  virtual Executor* clone() const
119  {
120    return new __EXECUTOR_FUNCTIONAL_NAME(0)<T>(this->functionPointer);
121  };
122};
123
124//! @brief ExecutorClass, that can execute Functions with one parameter.
125template<class T, typename type0> class __EXECUTOR_FUNCTIONAL_NAME(1) : public Executor
126{
127private:
128  /** @brief the FunctioPointer. */
129  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST;
130
131public:
132  /**
133   * @brief constructs the Executor.
134   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
135   */
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;
141  };
142
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   */
148  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
149  {
150
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  };
159
160  /**
161   * @brief copies the Executor
162   * @returns a new Executor that's a copy of this one.
163   */
164  virtual Executor* clone() const
165  {
166    return  new __EXECUTOR_FUNCTIONAL_NAME(1)<T, type0>(this->functionPointer);
167  };
168};
169
170//! @brief ExecutorClass, that can execute Functions with two parameters.
171template<class T, typename type0, typename type1> class __EXECUTOR_FUNCTIONAL_NAME(2) : public Executor
172{
173private:
174  /** @brief the FunctioPointer. */
175  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST;
176
177public:
178  /**
179   * @brief constructs the Executor.
180   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
181   */
182  __EXECUTOR_FUNCTIONAL_NAME(2) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST)
183      : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>())
184  {
185    this->functorType = Executor_Objective;
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, const SubString& sub = SubString()) const
195  {
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  };
200
201  /**
202   * @brief copies the Executor
203   * @returns a new Executor that's a copy of this one.
204   */
205  virtual Executor* clone() const
206  {
207    return new __EXECUTOR_FUNCTIONAL_NAME(2)<T, type0, type1>(this->functionPointer);
208  };
209};
210
211
212//! @brief ExecutorClass, that can execute Functions with three parameters.
213template<class T, typename type0, typename type1, typename type2> class __EXECUTOR_FUNCTIONAL_NAME(3) : public Executor
214{
215private:
216  /** @brief the FunctioPointer. */
217  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2) __EXECUTOR_FUNCTIONAL_CONST;
218
219public:
220  /**
221   * @brief constructs the Executor.
222   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
223   */
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  };
230
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   */
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  };
243
244  /**
245   * @brief copies the Executor
246   * @returns a new Executor that's a copy of this one.
247   */
248  virtual Executor* clone() const
249  {
250    return new __EXECUTOR_FUNCTIONAL_NAME(3)<T, type0, type1, type2>(this->functionPointer);
251  };
252};
253
254
255
256//! @brief ExecutorClass, that can execute Functions with four parameters.
257template<class T, typename type0, typename type1, typename type2, typename type3> class __EXECUTOR_FUNCTIONAL_NAME(4) : public Executor
258{
259private:
260  /** @brief the FunctioPointer. */
261  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST;
262
263public:
264  /**
265   * @brief constructs the Executor.
266   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
267   */
268  __EXECUTOR_FUNCTIONAL_NAME(4) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST)
269      : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>())
270  {
271    this->functorType = Executor_Objective;
272    this->functionPointer = functionPointer;
273  };
274
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   */
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
289  /**
290   * @brief copies the Executor
291   * @returns a new Executor that's a copy of this one.
292   */
293  virtual Executor* clone() const
294  {
295    return new __EXECUTOR_FUNCTIONAL_NAME(4)<T, type0, type1, type2, type3>(this->functionPointer);
296  };
297};
298
299//! @brief ExecutorClass, that can execute Functions with five parameters.
300template<class T, typename type0, typename type1, typename type2, typename type3, typename type4> class __EXECUTOR_FUNCTIONAL_NAME(5) : public Executor
301{
302private:
303  /** @brief the FunctioPointer. */
304  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3, type4) __EXECUTOR_FUNCTIONAL_CONST;
305
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  };
317
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)),
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)));
331  };
332
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  };
341};
342
343
344
345/**
346 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
347 */
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); \
352}
353
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 */
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); \
362}
363
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 */
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); \
373}
374
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 */
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}
386
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 */
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}
399
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 */
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}
413
414
415/**
416 * Creates the FunctionCallers
417 */
418#define FUNCTOR_LIST(x) EXECUTOR_FUNCTIONAL_CREATOR ## x
419#include "functor_list.h"
420#undef FUNCTOR_LIST
421
422
423
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.