Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: MultiType rework (now uses std::string) this is more compliant, and better to handle

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