Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new Default Values… they now too are in MultiType instead of (count, …) or (count, va_arg) style

File size: 11.8 KB
Line 
1/*!
2 * @file executor.h
3 * Definition of an Executor
4 */
5
6#ifndef _EXECUTOR_H
7#define _EXECUTOR_H
8
9#include "base_object.h"
10
11#include "helper_functions.h"
12#include "multi_type.h"
13#include "substring.h"
14#include "functor_list.h" //< MUST BE INCLUDED HERE AT LEAST ONCE.
15
16#include <stdarg.h>
17
18//! an enumerator for the definition of the Type.
19typedef enum {
20  Executor_Objective         = 0x00000001,
21  Executor_Static            = 0x00000002,
22
23  Executor_NoLoadString      = 0x00000010,
24} Executor_Type;
25
26////////////////
27// BASE CLASS //
28////////////////
29//! a BaseClass for all possible Executors
30/**
31 * An Executor is an Object, that is able to call Objects of Any type (class)
32 * and execute a function with given parameters on it.
33 *
34 * The Executor is able to handle:
35 *  Objects of any Class (Templated)
36 *  Default Values
37 *  Functions with up to 5 parameters (more seems useless)
38 *  Functions with many types (see functor_list.h)
39 */
40class Executor: public BaseObject
41{
42  public:
43    virtual ~Executor();
44
45    virtual Executor* clone () const = 0;
46
47    Executor* defaultValues(const MultiType& value0 = MT_NULL, const MultiType& value1 = MT_NULL,
48                            const MultiType& value2 = MT_NULL, const MultiType& value3 = MT_NULL,
49                            const MultiType& value4 = MT_NULL);
50
51    /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes */
52    virtual void execute (BaseObject* object, const void* parameters = NULL) = 0;
53
54    /** @returns the Type of this Function (either static or objective) */
55    inline long getType() const { return this->functorType; };
56    /** @returns the Count of Parameters this Executor takes */
57    inline unsigned int getParamCount() const { return this->paramCount; };
58
59    static void debug();
60
61  protected:
62    Executor(const MultiType& param0 = MT_NULL, const MultiType& param1 = MT_NULL,
63             const MultiType& param2 = MT_NULL, const MultiType& param3 = MT_NULL,
64             const MultiType& param4 = MT_NULL);
65
66    void cloning(Executor* executor) const;
67
68  protected:
69    long                        functorType;      //!< The type of Function we've got (either static or objective).
70    unsigned int                paramCount;       //!< the count of parameters.
71    MultiType                   defaultValue[5];  //!< Default Values.
72};
73
74///////////////////////////////////////////////////
75///////////////////////////////////////////////////
76
77/////////////////////////////////
78// MACRO DEFINITION EXTENSIONS //
79/////////////////////////////////
80//! where to chek for default BOOL values
81#define   l_BOOL_DEFGRAB(i)         this->defaultValue[i].getBool()
82//! where to chek for default INT values
83#define   l_INT_DEFGRAB(i)          this->defaultValue[i].getInt()
84//! where to chek for default UINT values
85#define   l_UINT_DEFGRAB(i)         (unsigned int)this->defaultValue[i].getInt()
86//! where to chek for default LONG values
87#define   l_LONG_DEFGRAB(i)         (long)this->defaultValue[i].getInt()
88//! where to chek for default FLOAT values
89#define   l_FLOAT_DEFGRAB(i)        this->defaultValue[i].getFloat()
90//! where to chek for default STRING values
91#define   l_STRING_DEFGRAB(i)       this->defaultValue[i].getString()
92
93//////////////////////////
94// COMMAND REGISTRATION //
95//////////////////////////
96// EXECUTOR             can be redefined as Executor or ExecutorStatic
97// EXECUTOREXECUTER     can be redefined too.
98// EXECUTORINCLASS
99// EXECUTORTYPE
100
101
102///////////////////////
103// FUNCTION POINTERS //
104///////////////////////
105#define ExecutorFunctionPoiter0() \
106  void EXECUTORINCLASS(*functionPointer_0)();
107
108#define ExecutorFunctionPoiter1(t1) \
109  void EXECUTORINCLASS(*functionPointer_1_##t1)(t1##_TYPE);
110
111#define ExecutorFunctionPoiter2(t1, t2) \
112  void EXECUTORINCLASS(*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE);
113
114
115#define ExecutorFunctionPoiter3(t1, t2, t3) \
116  void EXECUTORINCLASS(*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE);
117
118#define ExecutorFunctionPoiter4(t1, t2, t3, t4) \
119  void EXECUTORINCLASS(*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE);
120
121
122#define ExecutorFunctionPoiter5(t1, t2, t3, t4, t5) \
123  void EXECUTORINCLASS(*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); \
124
125
126//////////////////
127// CONSTRUCTORS //
128/////////////////
129//! creates a command that takes no parameters
130#define ExecutorConstructor0() \
131  EXECUTOR(void EXECUTORINCLASS(*function)()) \
132  : Executor(0) \
133  { \
134    this->functorType = EXECUTORTYPE; \
135    this->fp.functionPointer_0 = function; \
136  }
137
138//! creates a command that takes one parameter
139#define ExecutorConstructor1(t1) \
140  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE)) \
141  : Executor(t1##_PARAM) \
142  { \
143    this->functorType = EXECUTORTYPE; \
144    this->fp.functionPointer_1_##t1 = function; \
145  }
146
147//! creates a command that takes two parameters
148#define ExecutorConstructor2(t1,t2) \
149  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE)) \
150  : Executor(t1##_PARAM, t2##_PARAM) \
151  { \
152    this->functorType = EXECUTORTYPE; \
153    this->fp.functionPointer_2_##t1##_##t2 = function; \
154  }
155
156//! creates a command that takes three parameter
157#define ExecutorConstructor3(t1,t2,t3) \
158  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE)) \
159  : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM) \
160  { \
161    this->functorType = EXECUTORTYPE; \
162    this->fp.functionPointer_3_##t1##_##t2##_##t3 = function; \
163  }
164
165//! creates a command that takes four parameter
166#define ExecutorConstructor4(t1,t2,t3,t4) \
167  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE)) \
168  : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM) \
169  { \
170    this->functorType = EXECUTORTYPE; \
171    this->fp.functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \
172  }
173
174//! creates a command that takes five parameter
175#define ExecutorConstructor5(t1,t2,t3,t4,t5) \
176  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE)) \
177  : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM) \
178  { \
179    this->functorType = EXECUTORTYPE; \
180    this->fp.functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \
181  }
182
183///////////////
184// EXECUTION //
185///////////////
186//! execute-macro for functions with no parameters
187#define ExecutorExecute0() \
188  if (this->paramCount == 0) \
189    EXECUTOREXECUTER(_0)()
190
191//! execute-macro for functions with one parameter
192#define ExecutorExecute1(t1) \
193   else if (this->paramCount == 1 && this->defaultValue[0].getType() == t1##_PARAM) \
194    EXECUTOREXECUTER(_1_##t1)(t1##_FUNC((const char*)parameters, t1##_DEFGRAB(0)))
195
196//! execute-macro for functions with two parameters
197#define ExecutorExecute2(t1,t2) \
198   else if (this->paramCount == 2 && this->defaultValue[0].getType() == t1##_PARAM && this->defaultValue[1].getType() == t2##_PARAM) \
199    EXECUTOREXECUTER(_2_##t1##_##t2)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)))
200
201//! execute-macro for functions with three parameters
202#define ExecutorExecute3(t1,t2,t3) \
203   else if (this->paramCount == 3 && this->defaultValue[0].getType() == t1##_PARAM && this->defaultValue[1].getType() == t2##_PARAM && this->defaultValue[2].getType() == t3##_PARAM) \
204    EXECUTOREXECUTER(_3_##t1##_##t2##_##t3)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)), t3##_FUNC(sub.getString(2), t3##_DEFGRAB(2)))
205
206//! execute-macro for functions with four parameters
207#define ExecutorExecute4(t1,t2,t3,t4) \
208   else if (this->paramCount == 4 && this->defaultValue[0].getType() == t1##_PARAM && this->defaultValue[1].getType() == t2##_PARAM && this->defaultValue[2].getType() == t3##_PARAM && this->defaultValue[3].getType() == t4##_PARAM) \
209    EXECUTOREXECUTER(_4_##t1##_##t2##_##t3##_##t4)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)), t3##_FUNC(sub.getString(2), t3##_DEFGRAB(2)), t4##_FUNC(sub.getString(3), t4##_DEFGRAB(3))) \
210
211
212//! execute-macro for functions with five parameters
213#define ExecutorExecute5(t1,t2,t3,t4,t5) \
214   else if (this->paramCount == 5 && this->defaultValue[0].getType() == t1##_PARAM && this->defaultValue[1].getType() == t2##_PARAM && this->defaultValue[2].getType() == t3##_PARAM && this->defaultValue[3].getType() == t4##_PARAM && this->defaultValue[4].getType() == t5##_PARAM) \
215    EXECUTOREXECUTER(_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)), t3##_FUNC(sub.getString(2), t3##_DEFGRAB(2)), t4##_FUNC(sub.getString(3), t4##_DEFGRAB(3)), t5##_FUNC(sub.getString(4), t5##_DEFGRAB(4)))
216
217
218
219
220
221//////////\//////////
222// DYNAMIC FUNCTOR //
223///////////\/////////
224#ifdef FUNCTOR_LIST
225#undef FUNCTOR_LIST
226#endif
227#ifdef EXECUTOR
228#undef EXECUTOR
229#endif
230#define EXECUTOR                       ExecutorObjective
231#ifdef EXECUTOREXECUTER
232#undef EXECUTOREXECUTER
233#endif
234#define EXECUTOREXECUTER(nameExt)      (dynamic_cast<T*>(object)->*(fp.functionPointer##nameExt))
235#ifdef EXECUTORINCLASS
236#undef EXECUTORINCLASS
237#endif
238#define EXECUTORINCLASS(FUNCTION)      (T::FUNCTION)
239#ifdef EXECUTORTYPE
240#undef EXECUTORTYPE
241#endif
242#define EXECUTORTYPE                   Executor_Objective
243//! keeps information about a Executor
244template<class T> class ExecutorObjective : public Executor
245{
246  public:
247    ExecutorObjective() : Executor() { };
248    // COPY constuctor (virtual version)
249    virtual Executor* clone () const
250    {
251      ExecutorObjective<T>* executor = new ExecutorObjective<T>();
252      this->cloning(executor);
253      executor->fp = this->fp;
254      return executor;
255    }
256
257//! FUNCTOR_LIST is the List of CommandConstructors
258#define FUNCTOR_LIST(x) ExecutorConstructor ## x
259#include "functor_list.h"
260#undef FUNCTOR_LIST
261
262  private:
263//! FUNCTOR_LIST is the List of FunctionPointers
264    union FunctionPointers {
265#define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
266#include "functor_list.h"
267#undef FUNCTOR_LIST
268    } fp;
269
270    virtual void execute (BaseObject* object, const void* parameters = NULL)
271    {
272      SubString sub((const char*) parameters, " \n\t,", '\\');
273//! FUNCTOR_LIST is the List of Executive Functions
274#define FUNCTOR_LIST(x) ExecutorExecute ## x
275#include "functor_list.h"
276#undef FUNCTOR_LIST
277    }
278};
279
280
281////////////////////
282// STATIC FUNCTOR //
283////////////////////
284#ifdef FUNCTOR_LIST
285#undef FUNCTOR_LIST
286#endif
287#ifdef EXECUTOR
288#undef EXECUTOR
289#endif
290#define EXECUTOR                      ExecutorStatic
291#ifdef EXECUTOREXECUTER
292#undef EXECUTOREXECUTER
293#endif
294#define EXECUTOREXECUTER(nameExt)     fp.functionPointer##nameExt
295#ifdef EXECUTORINCLASS
296#undef EXECUTORINCLASS
297#endif
298#define EXECUTORINCLASS(FUNCTION)     (FUNCTION)
299#ifdef EXECUTORTYPE
300#undef EXECUTORTYPE
301#endif
302#define EXECUTORTYPE                   Executor_Static
303
304//! keeps information about a Executor, that points to a Static Function
305template<class T> class ExecutorStatic : public Executor
306{
307  public:
308    ExecutorStatic() : Executor() { };
309    // COPY constuctor
310    virtual Executor* clone () const
311    {
312      ExecutorStatic<T>* executor = new ExecutorStatic<T>();
313      this->cloning(executor);
314      executor->fp = this->fp;
315      return executor;
316    }
317
318//! FUNCTOR_LIST is the List of CommandConstructors
319#define FUNCTOR_LIST(x) ExecutorConstructor ## x
320#include "functor_list.h"
321#undef FUNCTOR_LIST
322
323  private:
324//! FUNCTOR_LIST is the List of FunctionPointers
325    union FunctionPointers {
326#define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
327#include "functor_list.h"
328#undef FUNCTOR_LIST
329    } fp;
330
331
332    virtual void execute (BaseObject* object, const void* parameters = NULL)
333    {
334  SubString sub((const char*)parameters, " \n\t,");
335//! FUNCTOR_LIST is the List of Executive Functions
336#define FUNCTOR_LIST(x) ExecutorExecute ## x
337#include "functor_list.h"
338#undef FUNCTOR_LIST
339    }
340};
341
342#endif /* _EXECUTOR_H */
Note: See TracBrowser for help on using the repository browser.