Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5637 was 5636, checked in by bensch, 20 years ago

orxonox/trunk: adapting ShellCommand to use Executor.
On the go

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   = 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(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///////////////////////
86// FUNCTION POINTERS //
87///////////////////////
88#define ExecutorFunctionPoiter0() \
89  void EXECUTORINCLASS(*functionPointer_0)();
90
91#define ExecutorFunctionPoiter1(t1) \
92  void EXECUTORINCLASS(*functionPointer_1_##t1)(t1##_TYPE);
93
94#define ExecutorFunctionPoiter2(t1, t2) \
95  void EXECUTORINCLASS(*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE);
96
97
98#define ExecutorFunctionPoiter3(t1, t2, t3) \
99  void EXECUTORINCLASS(*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE);
100
101#define ExecutorFunctionPoiter4(t1, t2, t3, t4) \
102  void EXECUTORINCLASS(*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE);
103
104
105#define ExecutorFunctionPoiter5(t1, t2, t3, t4, t5) \
106  void EXECUTORINCLASS(*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); \
107
108
109//////////////////
110// CONSTRUCTORS //
111/////////////////
112//! creates a command that takes no parameters
113#define ExecutorConstructor0() \
114  EXECUTOR(void EXECUTORINCLASS(*function)()) \
115  : Executor(0) \
116  { \
117    this->functorType = EXECUTORTYPE; \
118    this->fp.functionPointer_0 = function; \
119  }
120
121//! creates a command that takes one parameter
122#define ExecutorConstructor1(t1) \
123  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE)) \
124  : Executor(1, t1##_PARAM) \
125  { \
126    this->functorType = EXECUTORTYPE; \
127    this->fp.functionPointer_1_##t1 = function; \
128  }
129
130//! creates a command that takes two parameters
131#define ExecutorConstructor2(t1,t2) \
132  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE)) \
133  : Executor(2, t1##_PARAM, t2##_PARAM) \
134  { \
135    this->functorType = EXECUTORTYPE; \
136    this->fp.functionPointer_2_##t1##_##t2 = function; \
137  }
138
139//! creates a command that takes three parameter
140#define ExecutorConstructor3(t1,t2,t3) \
141  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE)) \
142  : Executor(3, t1##_PARAM, t2##_PARAM, t3##_PARAM) \
143  { \
144    this->functorType = EXECUTORTYPE; \
145    this->fp.functionPointer_3_##t1##_##t2##_##t3 = function; \
146  }
147
148//! creates a command that takes four parameter
149#define ExecutorConstructor4(t1,t2,t3,t4) \
150  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE)) \
151  : Executor(4, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM) \
152  { \
153    this->functorType = EXECUTORTYPE; \
154    this->fp.functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \
155  }
156
157//! creates a command that takes five parameter
158#define ExecutorConstructor5(t1,t2,t3,t4,t5) \
159  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE)) \
160  : Executor(5, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM) \
161  { \
162    this->functorType = EXECUTORTYPE; \
163    this->fp.functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \
164  }
165
166///////////////
167// EXECUTION //
168///////////////
169//! execute-macro for functions with no parameters
170#define ExecutorExecute0() \
171  if (this->paramCount == 0) \
172    EXECUTOREXECUTER(_0)()
173
174//! execute-macro for functions with one parameter
175#define ExecutorExecute1(t1) \
176   else if (this->paramCount == 1 && this->defaultValue[0].getType() == t1##_PARAM) \
177    EXECUTOREXECUTER(_1_##t1)(t1##_FUNC(parameters, t1##_DEFGRAB(0)))
178
179//! execute-macro for functions with two parameters
180#define ExecutorExecute2(t1,t2) \
181   else if (this->paramCount == 2 && this->defaultValue[0].getType() == t1##_PARAM && this->defaultValue[1].getType() == t2##_PARAM) \
182    EXECUTOREXECUTER(_2_##t1##_##t2)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)))
183
184//! execute-macro for functions with three parameters
185#define ExecutorExecute3(t1,t2,t3) \
186   else if (this->paramCount == 3 && this->defaultValue[0].getType() == t1##_PARAM && this->defaultValue[1].getType() == t2##_PARAM && this->defaultValue[2].getType() == t3##_PARAM) \
187    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)))
188
189//! execute-macro for functions with four parameters
190#define ExecutorExecute4(t1,t2,t3,t4) \
191   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) \
192    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)))
193
194//! execute-macro for functions with five parameters
195#define ExecutorExecute5(t1,t2,t3,t4,t5) \
196   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) \
197    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)))
198
199
200
201
202
203//////////\//////////
204// DYNAMIC FUNCTOR //
205///////////\/////////
206#ifdef FUNCTOR_LIST
207#undef FUNCTOR_LIST
208#endif
209#ifdef EXECUTOR
210#undef EXECUTOR
211#endif
212#define EXECUTOR                       ExecutorObjective
213#ifdef EXECUTOREXECUTER
214#undef EXECUTOREXECUTER
215#endif
216#define EXECUTOREXECUTER(nameExt)      (dynamic_cast<T*>(object)->*(fp.functionPointer##nameExt))
217#ifdef EXECUTORINCLASS
218#undef EXECUTORINCLASS
219#endif
220#define EXECUTORINCLASS(FUNCTION)      (T::FUNCTION)
221#ifdef EXECUTORTYPE
222#undef EXECUTORTYPE
223#endif
224#define EXECUTORTYPE                   Executor_Objective
225//! keeps information about a Executor
226template<class T> class ExecutorObjective : public Executor
227{
228
229  public:
230//! FUNCTOR_LIST is the List of CommandConstructors
231#define FUNCTOR_LIST(x) ExecutorConstructor ## x
232#include "functor_list.h"
233#undef FUNCTOR_LIST
234
235  private:
236//! FUNCTOR_LIST is the List of FunctionPointers
237    union FunctionPointers {
238#define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
239#include "functor_list.h"
240#undef FUNCTOR_LIST
241    } fp;
242
243    virtual void execute (BaseObject* object, const char* parameters)
244    {
245      SubString sub(parameters, true);
246//! FUNCTOR_LIST is the List of Executive Functions
247#define FUNCTOR_LIST(x) ExecutorExecute ## x
248#include "functor_list.h"
249#undef FUNCTOR_LIST
250    }
251};
252
253
254////////////////////
255// STATIC FUNCTOR //
256////////////////////
257#ifdef FUNCTOR_LIST
258#undef FUNCTOR_LIST
259#endif
260#ifdef EXECUTOR
261#undef EXECUTOR
262#endif
263#define EXECUTOR                      ExecutorStatic
264#ifdef EXECUTOREXECUTER
265#undef EXECUTOREXECUTER
266#endif
267#define EXECUTOREXECUTER(nameExt)     fp.functionPointer##nameExt
268#ifdef EXECUTORINCLASS
269#undef EXECUTORINCLASS
270#endif
271#define EXECUTORINCLASS(FUNCTION)     (FUNCTION)
272#ifdef EXECUTORTYPE
273#undef EXECUTORTYPE
274#endif
275#define EXECUTORTYPE                   Executor_Static
276
277//! keeps information about a Executor, that points to a Static Function
278template<class T> class ExecutorStatic : public Executor
279{
280  public:
281//! FUNCTOR_LIST is the List of CommandConstructors
282#define FUNCTOR_LIST(x) ExecutorConstructor ## x
283#include "functor_list.h"
284#undef FUNCTOR_LIST
285
286  private:
287//! FUNCTOR_LIST is the List of FunctionPointers
288    union FunctionPointers {
289#define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
290#include "functor_list.h"
291#undef FUNCTOR_LIST
292    } fp;
293
294
295    virtual void execute (BaseObject* object, const char* parameters)
296    {
297  SubString sub(parameters, true);
298//! FUNCTOR_LIST is the List of Executive Functions
299#define FUNCTOR_LIST(x) ExecutorExecute ## x
300#include "functor_list.h"
301#undef FUNCTOR_LIST
302    }
303};
304
305//! A Class, that handles aliases.
306class ExecutorAlias
307{
308  friend class ExecutorBase;
309  public:
310    /** @returns the Name of the Alias. */
311    const char* getName() const { return this->aliasName; };
312    /** @returns the Command, this Alias is asociated with */
313    ExecutorBase* getCommand() const { return this->command; };
314
315  private:
316    /** @param aliasName the name of the Alias @param command the Command, to associate this alias with */
317    ExecutorAlias(const char* aliasName, ExecutorBase* command) { this->aliasName = aliasName; this->command = command; };
318
319  private:
320    const char*         aliasName;       //!< the name of the Alias
321    ExecutorBase*       command;         //!< a pointer to the command, this alias executes.
322};
323
324#endif /* _EXECUTOR_H */
Note: See TracBrowser for help on using the repository browser.