Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Reimplementation of the Executor started in orxonox.cc

File size: 10.9 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{
40public:
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 SubString& sub = SubString()) 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
66protected:
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
73protected:
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///////////////////////
93// FUNCTION POINTERS //
94///////////////////////
95#define ExecutorFunctionPoiter0() \
96  void EXECUTORINCLASS(*functionPointer_0)();
97
98#define ExecutorFunctionPoiter1(t1) \
99  void EXECUTORINCLASS(*functionPointer_1_##t1)(t1##_TYPE);
100
101#define ExecutorFunctionPoiter2(t1, t2) \
102  void EXECUTORINCLASS(*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE);
103
104
105#define ExecutorFunctionPoiter3(t1, t2, t3) \
106  void EXECUTORINCLASS(*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE);
107
108#define ExecutorFunctionPoiter4(t1, t2, t3, t4) \
109  void EXECUTORINCLASS(*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE);
110
111
112#define ExecutorFunctionPoiter5(t1, t2, t3, t4, t5) \
113  void EXECUTORINCLASS(*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE);
114
115
116//////////////////
117// CONSTRUCTORS //
118/////////////////
119//! creates a command that takes no parameters
120#define ExecutorConstructor0() \
121  EXECUTOR(void EXECUTORINCLASS(*function)()) \
122  : Executor(0) \
123  { \
124    this->functorType = EXECUTORTYPE; \
125    this->fp.functionPointer_0 = function; \
126  }
127
128//! creates a command that takes one parameter
129#define ExecutorConstructor1(t1) \
130  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE)) \
131  : Executor(t1##_PARAM) \
132  { \
133    this->functorType = EXECUTORTYPE; \
134    this->fp.functionPointer_1_##t1 = function; \
135  }
136
137//! creates a command that takes two parameters
138#define ExecutorConstructor2(t1,t2) \
139  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE)) \
140  : Executor(t1##_PARAM, t2##_PARAM) \
141  { \
142    this->functorType = EXECUTORTYPE; \
143    this->fp.functionPointer_2_##t1##_##t2 = function; \
144  }
145
146//! creates a command that takes three parameter
147#define ExecutorConstructor3(t1,t2,t3) \
148  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE)) \
149  : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM) \
150  { \
151    this->functorType = EXECUTORTYPE; \
152    this->fp.functionPointer_3_##t1##_##t2##_##t3 = function; \
153  }
154
155//! creates a command that takes four parameter
156#define ExecutorConstructor4(t1,t2,t3,t4) \
157  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE)) \
158  : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM) \
159  { \
160    this->functorType = EXECUTORTYPE; \
161    this->fp.functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \
162  }
163
164//! creates a command that takes five parameter
165#define ExecutorConstructor5(t1,t2,t3,t4,t5) \
166  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE)) \
167  : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM) \
168  { \
169    this->functorType = EXECUTORTYPE; \
170    this->fp.functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \
171  }
172
173///////////////
174// EXECUTION //
175///////////////
176//! execute-macro for functions with no parameters
177#define ExecutorExecute0() \
178  if (this->paramCount == 0) \
179    EXECUTOREXECUTER(_0)()
180
181//! execute-macro for functions with one parameter
182#define ExecutorExecute1(t1) \
183   else if (this->paramCount == 1 && this->defaultValue[0] == t1##_PARAM) \
184    EXECUTOREXECUTER(_1_##t1)(t1##_FUNC(sub[0], t1##_DEFGRAB(0)))
185
186//! execute-macro for functions with two parameters
187#define ExecutorExecute2(t1,t2) \
188   else if (this->paramCount == 2 && this->defaultValue[0] == t1##_PARAM && this->defaultValue[1] == t2##_PARAM) \
189    EXECUTOREXECUTER(_2_##t1##_##t2)(t1##_FUNC(sub[0], t1##_DEFGRAB(0)), t2##_FUNC(sub[1], t2##_DEFGRAB(1)))
190
191//! execute-macro for functions with three parameters
192#define ExecutorExecute3(t1,t2,t3) \
193   else if (this->paramCount == 3 && this->defaultValue[0] == t1##_PARAM && this->defaultValue[1] == t2##_PARAM && this->defaultValue[2] == t3##_PARAM) \
194    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)))
195
196//! execute-macro for functions with four parameters
197#define ExecutorExecute4(t1,t2,t3,t4) \
198   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) \
199    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))) \
200
201
202//! execute-macro for functions with five parameters
203#define ExecutorExecute5(t1,t2,t3,t4,t5) \
204   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) \
205    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)))
206
207
208
209
210/////////////////////
211// DYNAMIC FUNCTOR //
212/////////////////////
213#ifdef FUNCTOR_LIST
214#undef FUNCTOR_LIST
215#endif
216#ifdef EXECUTOR
217#undef EXECUTOR
218#endif
219#define EXECUTOR                       ExecutorObjective
220#ifdef EXECUTOREXECUTER
221#undef EXECUTOREXECUTER
222#endif
223#define EXECUTOREXECUTER(nameExt)      (dynamic_cast<T*>(object)->*(fp.functionPointer##nameExt))
224#ifdef EXECUTORINCLASS
225#undef EXECUTORINCLASS
226#endif
227#define EXECUTORINCLASS(FUNCTION)      (T::FUNCTION)
228#ifdef EXECUTORTYPE
229#undef EXECUTORTYPE
230#endif
231#define EXECUTORTYPE                   Executor_Objective
232//! keeps information about a Executor
233template<class T> class EXECUTOR : public Executor
234{
235public:
236  EXECUTOR() : Executor() { };
237  // COPY constuctor (virtual version)
238  virtual Executor* clone () const
239  {
240    EXECUTOR<T>* executor = new EXECUTOR<T>();
241    this->cloning(executor);
242    executor->fp = this->fp;
243    return executor;
244  }
245
246  //! FUNCTOR_LIST is the List of CommandConstructors
247#define FUNCTOR_LIST(x) ExecutorConstructor ## x
248#include "functor_list.h"
249#undef FUNCTOR_LIST
250
251private:
252  //! FUNCTOR_LIST is the List of FunctionPointers
253  union FunctionPointers {
254#define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
255#include "functor_list.h"
256#undef FUNCTOR_LIST
257  } fp;
258
259  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
260  {
261    //! FUNCTOR_LIST is the List of Executive Functions
262#define FUNCTOR_LIST(x) ExecutorExecute ## x
263#include "functor_list.h"
264#undef FUNCTOR_LIST
265
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{
296public:
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
312private:
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 SubString& sub = SubString()) const
322  {
323    //! FUNCTOR_LIST is the List of Executive Functions
324#define FUNCTOR_LIST(x) ExecutorExecute ## x
325#include "functor_list.h"
326#undef FUNCTOR_LIST
327
328  }
329};
330
331#endif /* _EXECUTOR_H */
Note: See TracBrowser for help on using the repository browser.