Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: SubString::split algorithm revisited: now it Splits Strings where Delimitters are, but it ereases the Neighbours of those delimiters, if you want it
also there is now a way to have empty Entries.

File size: 11.0 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 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
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 SubString& sub = SubString()) const
259    {
260//! FUNCTOR_LIST is the List of Executive Functions
261#define FUNCTOR_LIST(x) ExecutorExecute ## x
262#include "functor_list.h"
263#undef FUNCTOR_LIST
264    }
265};
266
267
268////////////////////
269// STATIC FUNCTOR //
270////////////////////
271#ifdef FUNCTOR_LIST
272#undef FUNCTOR_LIST
273#endif
274#ifdef EXECUTOR
275#undef EXECUTOR
276#endif
277#define EXECUTOR                      ExecutorStatic
278#ifdef EXECUTOREXECUTER
279#undef EXECUTOREXECUTER
280#endif
281#define EXECUTOREXECUTER(nameExt)     fp.functionPointer##nameExt
282#ifdef EXECUTORINCLASS
283#undef EXECUTORINCLASS
284#endif
285#define EXECUTORINCLASS(FUNCTION)     (FUNCTION)
286#ifdef EXECUTORTYPE
287#undef EXECUTORTYPE
288#endif
289#define EXECUTORTYPE                   Executor_Static
290
291//! keeps information about a Executor, that points to a Static Function
292template<class T> class ExecutorStatic : public Executor
293{
294  public:
295    EXECUTOR() : Executor() { };
296    // COPY constuctor
297    virtual Executor* clone () const
298    {
299      EXECUTOR<T>* executor = new EXECUTOR<T>();
300      this->cloning(executor);
301      executor->fp = this->fp;
302      return executor;
303    }
304
305//! FUNCTOR_LIST is the List of CommandConstructors
306#define FUNCTOR_LIST(x) ExecutorConstructor ## x
307#include "functor_list.h"
308#undef FUNCTOR_LIST
309
310  private:
311//! FUNCTOR_LIST is the List of FunctionPointers
312    union FunctionPointers {
313#define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
314#include "functor_list.h"
315#undef FUNCTOR_LIST
316    } fp;
317
318
319    virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
320    {
321//! FUNCTOR_LIST is the List of Executive Functions
322#define FUNCTOR_LIST(x) ExecutorExecute ## x
323#include "functor_list.h"
324#undef FUNCTOR_LIST
325    }
326};
327
328#endif /* _EXECUTOR_H */
Note: See TracBrowser for help on using the repository browser.