Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new LoadParam procedure with all NON-cycling load-options, and it works perfectly (on first sight :))

now going to make the same for cycling LoadOptions

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