Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Executor now uses MultiType instead of va_arg

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