Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: the most evil for (int i=0; i <'=' …) bug ever…

File size: 11.5 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         = 1,
19  Executor_Static            = 2,
20
21  Executor_NoLoadString      = 8,
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 std::string& parameters = "") = 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_STRING_DEFGRAB(i)       this->defaultValue[i].getString()
90//! where to chek for default CSTRING values
91#define   l_CSTRING_DEFGRAB(i)      this->defaultValue[i].getCString()
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] == t1##_PARAM) \
194    EXECUTOREXECUTER(_1_##t1)(t1##_FUNC(sub[0], 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] == t1##_PARAM && this->defaultValue[1] == t2##_PARAM) \
199    EXECUTOREXECUTER(_2_##t1##_##t2)(t1##_FUNC(sub[0], t1##_DEFGRAB(0)), t2##_FUNC(sub[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] == t1##_PARAM && this->defaultValue[1] == t2##_PARAM && this->defaultValue[2] == t3##_PARAM) \
204    EXECUTOREXECUTER(_3_##t1##_##t2##_##t3)(t1##_FUNC(sub[0], t1##_DEFGRAB(0)), t2##_FUNC(sub[1], t2##_DEFGRAB(1)), t3##_FUNC(sub[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] == t1##_PARAM && this->defaultValue[1] == t2##_PARAM && this->defaultValue[2] == t3##_PARAM && this->defaultValue[3] == t4##_PARAM) \
209    EXECUTOREXECUTER(_4_##t1##_##t2##_##t3##_##t4)(t1##_FUNC(sub[0], t1##_DEFGRAB(0)), t2##_FUNC(sub[1], t2##_DEFGRAB(1)), t3##_FUNC(sub[2], t3##_DEFGRAB(2)), t4##_FUNC(sub[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] == t1##_PARAM && this->defaultValue[1] == t2##_PARAM && this->defaultValue[2] == t3##_PARAM && this->defaultValue[3] == t4##_PARAM && this->defaultValue[4] == t5##_PARAM) \
215    EXECUTOREXECUTER(_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_FUNC(sub[0], t1##_DEFGRAB(0)), t2##_FUNC(sub[1], t2##_DEFGRAB(1)), t3##_FUNC(sub[2], t3##_DEFGRAB(2)), t4##_FUNC(sub[3], t4##_DEFGRAB(3)), t5##_FUNC(sub[4], t5##_DEFGRAB(4)))
216
217
218
219
220/////////////////////
221// DYNAMIC FUNCTOR //
222/////////////////////
223#ifdef FUNCTOR_LIST
224#undef FUNCTOR_LIST
225#endif
226#ifdef EXECUTOR
227#undef EXECUTOR
228#endif
229#define EXECUTOR                       ExecutorObjective
230#ifdef EXECUTOREXECUTER
231#undef EXECUTOREXECUTER
232#endif
233#define EXECUTOREXECUTER(nameExt)      (dynamic_cast<T*>(object)->*(fp.functionPointer##nameExt))
234#ifdef EXECUTORINCLASS
235#undef EXECUTORINCLASS
236#endif
237#define EXECUTORINCLASS(FUNCTION)      (T::FUNCTION)
238#ifdef EXECUTORTYPE
239#undef EXECUTORTYPE
240#endif
241#define EXECUTORTYPE                   Executor_Objective
242//! keeps information about a Executor
243template<class T> class EXECUTOR : public Executor
244{
245  public:
246    EXECUTOR() : Executor() { };
247    // COPY constuctor (virtual version)
248    virtual Executor* clone () const
249    {
250      EXECUTOR<T>* executor = new EXECUTOR<T>();
251      this->cloning(executor);
252      executor->fp = this->fp;
253      return executor;
254    }
255
256//! FUNCTOR_LIST is the List of CommandConstructors
257#define FUNCTOR_LIST(x) ExecutorConstructor ## x
258#include "functor_list.h"
259#undef FUNCTOR_LIST
260
261  private:
262//! FUNCTOR_LIST is the List of FunctionPointers
263    union FunctionPointers {
264#define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
265#include "functor_list.h"
266#undef FUNCTOR_LIST
267    } fp;
268
269    virtual void execute (BaseObject* object, const std::string& parameters = "")
270    {
271      SubString sub;
272      sub.split(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    EXECUTOR() : Executor() { };
309    // COPY constuctor
310    virtual Executor* clone () const
311    {
312      EXECUTOR<T>* executor = new EXECUTOR<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 std::string& parameters = "")
333    {
334      SubString sub;
335      sub.split(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.