Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7448 was 7419, checked in by bensch, 19 years ago

orxonox/trunk: Test in Execute (first implementation)

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