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. |
---|
17 | typedef 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 | */ |
---|
38 | class 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 | |
---|
50 | // EXECUTE |
---|
51 | /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes */ |
---|
52 | virtual void operator()(BaseObject* object, const std::string& parameters = "") = 0; |
---|
53 | /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes @brief here for your convenience*/ |
---|
54 | void execute (BaseObject* object, const std::string& parameters = "") { (*this)(object, parameters); }; |
---|
55 | |
---|
56 | // RETRIEVE INFORMATION |
---|
57 | /** @returns the Type of this Function (either static or objective) */ |
---|
58 | inline long getType() const { return this->functorType; }; |
---|
59 | /** @returns the Count of Parameters this Executor takes */ |
---|
60 | inline unsigned int getParamCount() const { return this->paramCount; }; |
---|
61 | |
---|
62 | static void debug(); |
---|
63 | |
---|
64 | protected: |
---|
65 | Executor(const MultiType& param0 = MT_NULL, const MultiType& param1 = MT_NULL, |
---|
66 | const MultiType& param2 = MT_NULL, const MultiType& param3 = MT_NULL, |
---|
67 | const MultiType& param4 = MT_NULL); |
---|
68 | |
---|
69 | void cloning(Executor* executor) const; |
---|
70 | |
---|
71 | protected: |
---|
72 | short functorType; //!< The type of Function we've got (either static or objective). |
---|
73 | unsigned int paramCount; //!< the count of parameters. |
---|
74 | MultiType defaultValue[5]; //!< Default Values. |
---|
75 | }; |
---|
76 | |
---|
77 | /////////////////////////////////////////////////// |
---|
78 | /////////////////////////////////////////////////// |
---|
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 | /////////////////////// |
---|
90 | // FUNCTION POINTERS // |
---|
91 | /////////////////////// |
---|
92 | #define ExecutorFunctionPoiter0() \ |
---|
93 | void EXECUTORINCLASS(*functionPointer_0)(); |
---|
94 | |
---|
95 | #define ExecutorFunctionPoiter1(t1) \ |
---|
96 | void EXECUTORINCLASS(*functionPointer_1_##t1)(t1##_TYPE); |
---|
97 | |
---|
98 | #define ExecutorFunctionPoiter2(t1, t2) \ |
---|
99 | void EXECUTORINCLASS(*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE); |
---|
100 | |
---|
101 | |
---|
102 | #define ExecutorFunctionPoiter3(t1, t2, t3) \ |
---|
103 | void EXECUTORINCLASS(*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE); |
---|
104 | |
---|
105 | #define ExecutorFunctionPoiter4(t1, t2, t3, t4) \ |
---|
106 | void EXECUTORINCLASS(*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE); |
---|
107 | |
---|
108 | |
---|
109 | #define ExecutorFunctionPoiter5(t1, t2, t3, t4, t5) \ |
---|
110 | void EXECUTORINCLASS(*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); |
---|
111 | |
---|
112 | |
---|
113 | ////////////////// |
---|
114 | // CONSTRUCTORS // |
---|
115 | ///////////////// |
---|
116 | //! creates a command that takes no parameters |
---|
117 | #define ExecutorConstructor0() \ |
---|
118 | EXECUTOR(void EXECUTORINCLASS(*function)()) \ |
---|
119 | : Executor(0) \ |
---|
120 | { \ |
---|
121 | this->functorType = EXECUTORTYPE; \ |
---|
122 | this->fp.functionPointer_0 = function; \ |
---|
123 | } |
---|
124 | |
---|
125 | //! creates a command that takes one parameter |
---|
126 | #define ExecutorConstructor1(t1) \ |
---|
127 | EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE)) \ |
---|
128 | : Executor(t1##_PARAM) \ |
---|
129 | { \ |
---|
130 | this->functorType = EXECUTORTYPE; \ |
---|
131 | this->fp.functionPointer_1_##t1 = function; \ |
---|
132 | } |
---|
133 | |
---|
134 | //! creates a command that takes two parameters |
---|
135 | #define ExecutorConstructor2(t1,t2) \ |
---|
136 | EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE)) \ |
---|
137 | : Executor(t1##_PARAM, t2##_PARAM) \ |
---|
138 | { \ |
---|
139 | this->functorType = EXECUTORTYPE; \ |
---|
140 | this->fp.functionPointer_2_##t1##_##t2 = function; \ |
---|
141 | } |
---|
142 | |
---|
143 | //! creates a command that takes three parameter |
---|
144 | #define ExecutorConstructor3(t1,t2,t3) \ |
---|
145 | EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE)) \ |
---|
146 | : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM) \ |
---|
147 | { \ |
---|
148 | this->functorType = EXECUTORTYPE; \ |
---|
149 | this->fp.functionPointer_3_##t1##_##t2##_##t3 = function; \ |
---|
150 | } |
---|
151 | |
---|
152 | //! creates a command that takes four parameter |
---|
153 | #define ExecutorConstructor4(t1,t2,t3,t4) \ |
---|
154 | EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE)) \ |
---|
155 | : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM) \ |
---|
156 | { \ |
---|
157 | this->functorType = EXECUTORTYPE; \ |
---|
158 | this->fp.functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \ |
---|
159 | } |
---|
160 | |
---|
161 | //! creates a command that takes five parameter |
---|
162 | #define ExecutorConstructor5(t1,t2,t3,t4,t5) \ |
---|
163 | EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE)) \ |
---|
164 | : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM) \ |
---|
165 | { \ |
---|
166 | this->functorType = EXECUTORTYPE; \ |
---|
167 | this->fp.functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \ |
---|
168 | } |
---|
169 | |
---|
170 | /////////////// |
---|
171 | // EXECUTION // |
---|
172 | /////////////// |
---|
173 | //! execute-macro for functions with no parameters |
---|
174 | #define ExecutorExecute0() \ |
---|
175 | if (this->paramCount == 0) \ |
---|
176 | EXECUTOREXECUTER(_0)() |
---|
177 | |
---|
178 | //! execute-macro for functions with one parameter |
---|
179 | #define ExecutorExecute1(t1) \ |
---|
180 | else if (this->paramCount == 1 && this->defaultValue[0] == t1##_PARAM) \ |
---|
181 | EXECUTOREXECUTER(_1_##t1)(t1##_FUNC(sub[0], t1##_DEFGRAB(0))) |
---|
182 | |
---|
183 | //! execute-macro for functions with two parameters |
---|
184 | #define ExecutorExecute2(t1,t2) \ |
---|
185 | else if (this->paramCount == 2 && this->defaultValue[0] == t1##_PARAM && this->defaultValue[1] == t2##_PARAM) \ |
---|
186 | EXECUTOREXECUTER(_2_##t1##_##t2)(t1##_FUNC(sub[0], t1##_DEFGRAB(0)), t2##_FUNC(sub[1], t2##_DEFGRAB(1))) |
---|
187 | |
---|
188 | //! execute-macro for functions with three parameters |
---|
189 | #define ExecutorExecute3(t1,t2,t3) \ |
---|
190 | else if (this->paramCount == 3 && this->defaultValue[0] == t1##_PARAM && this->defaultValue[1] == t2##_PARAM && this->defaultValue[2] == t3##_PARAM) \ |
---|
191 | 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))) |
---|
192 | |
---|
193 | //! execute-macro for functions with four parameters |
---|
194 | #define ExecutorExecute4(t1,t2,t3,t4) \ |
---|
195 | 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) \ |
---|
196 | 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))) \ |
---|
197 | |
---|
198 | |
---|
199 | //! execute-macro for functions with five parameters |
---|
200 | #define ExecutorExecute5(t1,t2,t3,t4,t5) \ |
---|
201 | 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) \ |
---|
202 | 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))) |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | |
---|
207 | ///////////////////// |
---|
208 | // DYNAMIC FUNCTOR // |
---|
209 | ///////////////////// |
---|
210 | #ifdef FUNCTOR_LIST |
---|
211 | #undef FUNCTOR_LIST |
---|
212 | #endif |
---|
213 | #ifdef EXECUTOR |
---|
214 | #undef EXECUTOR |
---|
215 | #endif |
---|
216 | #define EXECUTOR ExecutorObjective |
---|
217 | #ifdef EXECUTOREXECUTER |
---|
218 | #undef EXECUTOREXECUTER |
---|
219 | #endif |
---|
220 | #define EXECUTOREXECUTER(nameExt) (dynamic_cast<T*>(object)->*(fp.functionPointer##nameExt)) |
---|
221 | #ifdef EXECUTORINCLASS |
---|
222 | #undef EXECUTORINCLASS |
---|
223 | #endif |
---|
224 | #define EXECUTORINCLASS(FUNCTION) (T::FUNCTION) |
---|
225 | #ifdef EXECUTORTYPE |
---|
226 | #undef EXECUTORTYPE |
---|
227 | #endif |
---|
228 | #define EXECUTORTYPE Executor_Objective |
---|
229 | //! keeps information about a Executor |
---|
230 | template<class T> class EXECUTOR : public Executor |
---|
231 | { |
---|
232 | public: |
---|
233 | EXECUTOR() : Executor() { }; |
---|
234 | // COPY constuctor (virtual version) |
---|
235 | virtual Executor* clone () const |
---|
236 | { |
---|
237 | EXECUTOR<T>* executor = new EXECUTOR<T>(); |
---|
238 | this->cloning(executor); |
---|
239 | executor->fp = this->fp; |
---|
240 | return executor; |
---|
241 | } |
---|
242 | |
---|
243 | //! FUNCTOR_LIST is the List of CommandConstructors |
---|
244 | #define FUNCTOR_LIST(x) ExecutorConstructor ## x |
---|
245 | #include "functor_list.h" |
---|
246 | #undef FUNCTOR_LIST |
---|
247 | |
---|
248 | private: |
---|
249 | //! FUNCTOR_LIST is the List of FunctionPointers |
---|
250 | union FunctionPointers { |
---|
251 | #define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x |
---|
252 | #include "functor_list.h" |
---|
253 | #undef FUNCTOR_LIST |
---|
254 | } fp; |
---|
255 | |
---|
256 | virtual void operator()(BaseObject* object, const std::string& parameters = "") |
---|
257 | { |
---|
258 | SubString sub; |
---|
259 | sub.split(parameters, " \n\t,", '\\'); |
---|
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 |
---|
292 | template<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 std::string& parameters = "") |
---|
320 | { |
---|
321 | SubString sub; |
---|
322 | sub.split(parameters, " \n\t,", '\\'); |
---|
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 | #endif /* _EXECUTOR_H */ |
---|