Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Executor works just fine.
Changed: Paramerte* to MT_*, to make things more general

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