Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more about the executor.

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