Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7903 was 7725, checked in by bensch, 19 years ago

trunk: const Executor introduced

File size: 16.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
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#endif
68
69#ifdef EXECUTOR_FUNCTIONAL_USE_STATIC
70 #ifdef EXECUTOR_FUNCTIONAL_USE_CONST
71  #error you obviously do not know what you are doing !! ask the bensch
72 #endif /* EXECUTOR_FUNCTIONAL_USE_CONST */
73
74#undef __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC
75 #define __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC       functionPointer
76 #undef __EXECUTOR_FUNCTIONAL_NAME
77 #define __EXECUTOR_FUNCTIONAL_NAME(ParamCount)    Executor##ParamCount##Params_static
78 #undef __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER
79 #define __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER    *functionPointer
80
81#endif /* EXECUTOR_FUNCTIONAL_USE_STATIC */
82
83//! @brief ExecutorClass, that can execute Functions without any parameters.
84template<class T> class __EXECUTOR_FUNCTIONAL_NAME(0) : public Executor
85{
86private:
87  /** @brief the FunctioPointer. */
88  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST;
89
90public:
91  /**
92   * @brief constructs the Executor.
93   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
94   */
95  __EXECUTOR_FUNCTIONAL_NAME(0) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST )
96      : Executor()
97  {
98    this->functorType = Executor_Objective;
99    this->functionPointer = functionPointer;
100  };
101
102  /**
103   * @brief executes the Functional
104   * @param object the Object the action should be executed on.
105   * @param sub the SubString to get the Parameters from.
106   */
107  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
108  {
109    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)();
110  };
111
112  /**
113   * @brief copies the Executor
114   * @returns a new Executor that's a copy of this one.
115   */
116  virtual Executor* clone() const
117  {
118    return new __EXECUTOR_FUNCTIONAL_NAME(0)<T>(this->functionPointer);
119  };
120};
121
122//! @brief ExecutorClass, that can execute Functions with one parameter.
123template<class T, typename type0> class __EXECUTOR_FUNCTIONAL_NAME(1) : public Executor
124{
125private:
126  /** @brief the FunctioPointer. */
127  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST;
128
129public:
130  /**
131   * @brief constructs the Executor.
132   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
133   */
134  __EXECUTOR_FUNCTIONAL_NAME(1) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST)
135      : Executor(ExecutorParamType<type0>())
136  {
137    this->functorType = Executor_Objective;
138    this->functionPointer = functionPointer;
139  };
140
141  /**
142   * @brief executes the Functional
143   * @param object the Object the action should be executed on.
144   * @param sub the SubString to get the Parameters from.
145   */
146  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
147  {
148
149    /* // THE VERY COOL DEBUG
150      printf("SUB[0] : %s\n", sub[0].c_str());
151      printf("getDefault<type0>(this->defaultValue, 0):::: %d\n", getDefault<type0>(this->defaultValue, 0));
152      printf("VALUE: %d\n", fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)));
153    */
154    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
155      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)) );
156  };
157
158  /**
159   * @brief copies the Executor
160   * @returns a new Executor that's a copy of this one.
161   */
162  virtual Executor* clone() const
163  {
164    return  new __EXECUTOR_FUNCTIONAL_NAME(1)<T, type0>(this->functionPointer);
165  };
166};
167
168//! @brief ExecutorClass, that can execute Functions with two parameters.
169template<class T, typename type0, typename type1> class __EXECUTOR_FUNCTIONAL_NAME(2) : public Executor
170{
171private:
172  /** @brief the FunctioPointer. */
173  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST;
174
175public:
176  /**
177   * @brief constructs the Executor.
178   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
179   */
180  __EXECUTOR_FUNCTIONAL_NAME(2) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST)
181      : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>())
182  {
183    this->functorType = Executor_Objective;
184    this->functionPointer = functionPointer;
185  };
186
187  /**
188   * @brief executes the Functional
189   * @param object the Object the action should be executed on.
190   * @param sub the SubString to get the Parameters from.
191   */
192  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
193  {
194    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
195      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
196      fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)));
197  };
198
199  /**
200   * @brief copies the Executor
201   * @returns a new Executor that's a copy of this one.
202   */
203  virtual Executor* clone() const
204  {
205    return new __EXECUTOR_FUNCTIONAL_NAME(2)<T, type0, type1>(this->functionPointer);
206  };
207};
208
209
210//! @brief ExecutorClass, that can execute Functions with three parameters.
211template<class T, typename type0, typename type1, typename type2> class __EXECUTOR_FUNCTIONAL_NAME(3) : public Executor
212{
213private:
214  /** @brief the FunctioPointer. */
215  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2) __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(3) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2) __EXECUTOR_FUNCTIONAL_CONST)
223      : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>())
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      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
238      fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)),
239      fromString<type2>(sub[2], getDefault<type2>(this->defaultValue, 2)));
240  };
241
242  /**
243   * @brief copies the Executor
244   * @returns a new Executor that's a copy of this one.
245   */
246  virtual Executor* clone() const
247  {
248    return new __EXECUTOR_FUNCTIONAL_NAME(3)<T, type0, type1, type2>(this->functionPointer);
249  };
250};
251
252
253
254//! @brief ExecutorClass, that can execute Functions with four parameters.
255template<class T, typename type0, typename type1, typename type2, typename type3> class __EXECUTOR_FUNCTIONAL_NAME(4) : public Executor
256{
257private:
258  /** @brief the FunctioPointer. */
259  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST;
260
261public:
262  /**
263   * @brief constructs the Executor.
264   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
265   */
266  __EXECUTOR_FUNCTIONAL_NAME(4) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST)
267      : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>())
268  {
269    this->functorType = Executor_Objective;
270    this->functionPointer = functionPointer;
271  };
272
273  /**
274  * @brief executes the Functional
275  * @param object the Object the action should be executed on.
276  * @param sub the SubString to get the Parameters from.
277   */
278  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
279  {
280    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
281      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
282      fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)),
283      fromString<type2>(sub[2], getDefault<type2>(this->defaultValue, 2)),
284      fromString<type3>(sub[3], getDefault<type3>(this->defaultValue, 3)));
285  };
286
287  /**
288   * @brief copies the Executor
289   * @returns a new Executor that's a copy of this one.
290   */
291  virtual Executor* clone() const
292  {
293    return new __EXECUTOR_FUNCTIONAL_NAME(4)<T, type0, type1, type2, type3>(this->functionPointer);
294  };
295};
296
297//! @brief ExecutorClass, that can execute Functions with five parameters.
298template<class T, typename type0, typename type1, typename type2, typename type3, typename type4> class __EXECUTOR_FUNCTIONAL_NAME(5) : public Executor
299{
300private:
301  /** @brief the FunctioPointer. */
302  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3, type4) __EXECUTOR_FUNCTIONAL_CONST;
303
304public:
305  /**
306   * @brief constructs the Executor.
307   * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function.
308   */
309  __EXECUTOR_FUNCTIONAL_NAME(5) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3, type4) __EXECUTOR_FUNCTIONAL_CONST)
310      : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>(), ExecutorParamType<type4>())
311  {
312    this->functorType = Executor_Objective;
313    this->functionPointer = functionPointer;
314  };
315
316  /**
317  * @brief executes the Functional
318  * @param object the Object the action should be executed on.
319  * @param sub the SubString to get the Parameters from.
320   */
321  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
322  {
323    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
324      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
325      fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)),
326      fromString<type2>(sub[2], getDefault<type2>(this->defaultValue, 2)),
327      fromString<type3>(sub[3], getDefault<type3>(this->defaultValue, 3)),
328      fromString<type4>(sub[4], getDefault<type4>(this->defaultValue, 4)));
329  };
330
331  /**
332   * @brief copies the Executor
333   * @returns a new Executor that's a copy of this one.
334   */
335  virtual Executor* clone() const
336  {
337    return new __EXECUTOR_FUNCTIONAL_NAME(5)<T, type0, type1, type2, type3, type4>(this->functionPointer);
338  };
339};
340
341
342
343/**
344 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
345 */
346#define EXECUTOR_FUNCTIONAL_CREATOR0() \
347template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST) \
348{ \
349  return new __EXECUTOR_FUNCTIONAL_NAME(0)<T>(functionPointer); \
350}
351
352/**
353 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
354 * @param type0 for internal usage: the first Argument
355 */
356#define EXECUTOR_FUNCTIONAL_CREATOR1(type0) \
357template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
358{ \
359  return new __EXECUTOR_FUNCTIONAL_NAME(1)<T, type0##_TYPE>(functionPointer); \
360}
361
362/**
363 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
364 * @param type0 for internal usage: the first Argument
365 * @param type1 for internal usage: the second Argument
366 */
367#define EXECUTOR_FUNCTIONAL_CREATOR2(type0, type1) \
368template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
369{ \
370  return new __EXECUTOR_FUNCTIONAL_NAME(2)<T, type0##_TYPE, type1##_TYPE>(functionPointer); \
371}
372
373/**
374 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
375 * @param type0 for internal usage: the first Argument
376 * @param type1 for internal usage: the second Argument
377 * @param type2 for internal usage: the third Argument
378 */
379#define EXECUTOR_FUNCTIONAL_CREATOR3(type0, type1, type2) \
380template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE, type2##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
381{ \
382  return new __EXECUTOR_FUNCTIONAL_NAME(3)<T, type0##_TYPE, type1##_TYPE, type2##_TYPE>(functionPointer); \
383}
384
385/**
386 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
387 * @param type0 for internal usage: the first Argument
388 * @param type1 for internal usage: the second Argument
389 * @param type2 for internal usage: the third Argument
390 * @param type3 for internal usage: the fourth Argument
391 */
392#define EXECUTOR_FUNCTIONAL_CREATOR4(type0, type1, type2, type3) \
393template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
394{ \
395  return new __EXECUTOR_FUNCTIONAL_NAME(4)<T, type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE>(functionPointer); \
396}
397
398/**
399 * @brief enables us to easily retrieve an Executor of Class T with modular Argument-count, (without thinking about args at all)
400 * @param type0 for internal usage: the first Argument
401 * @param type1 for internal usage: the second Argument
402 * @param type2 for internal usage: the third Argument
403 * @param type3 for internal usage: the fourth Argument
404 * @param type4 for internal usage: the fifth Argument
405 */
406#define EXECUTOR_FUNCTIONAL_CREATOR5(type0, type1, type2, type3, type4) \
407template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE) __EXECUTOR_FUNCTIONAL_CONST) \
408{ \
409    return new __EXECUTOR_FUNCTIONAL_NAME(5)<T, type0##_TYPE, type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE>(functionPointer); \
410}
411
412
413/**
414 * Creates the FunctionCallers
415 */
416#define FUNCTOR_LIST(x) EXECUTOR_FUNCTIONAL_CREATOR ## x
417#include "functor_list.h"
418#undef FUNCTOR_LIST
419
420
421
422#undef __EXECUTOR_FUNCTIONAL_CONST
423#undef __EXECUTOR_FUNCTIONAL_NAME
424#undef __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC
425#undef __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER
426
427#ifdef EXECUTOR_FUNCTIONAL_USE_CONST
428 #undef EXECUTOR_FUNCTIONAL_USE_CONST
429#endif
430#ifdef EXECUTOR_FUNCTIONAL_USE_STATIC
431 #undef EXECUTOR_FUNCTIONAL_USE_STATIC
432#endif
Note: See TracBrowser for help on using the repository browser.