Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6225 was 5691, checked in by bensch, 19 years ago

orxonox/trunk: moved functor_list.h

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