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 | Executor* defaultValues(const MultiType& value0 = MT_NULL, const MultiType& value1 = MT_NULL, |
---|
46 | const MultiType& value2 = MT_NULL, const MultiType& value3 = MT_NULL, |
---|
47 | const MultiType& value4 = MT_NULL); |
---|
48 | |
---|
49 | /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes */ |
---|
50 | virtual void execute (BaseObject* object, const std::string& parameters = "") = 0; |
---|
51 | |
---|
52 | /** @returns the Type of this Function (either static or objective) */ |
---|
53 | inline long getType() const { return this->functorType; }; |
---|
54 | /** @returns the Count of Parameters this Executor takes */ |
---|
55 | inline unsigned int getParamCount() const { return this->paramCount; }; |
---|
56 | |
---|
57 | static void debug(); |
---|
58 | |
---|
59 | protected: |
---|
60 | Executor(const MultiType& param0 = MT_NULL, const MultiType& param1 = MT_NULL, |
---|
61 | const MultiType& param2 = MT_NULL, const MultiType& param3 = MT_NULL, |
---|
62 | const MultiType& param4 = MT_NULL); |
---|
63 | |
---|
64 | void cloning(Executor* executor) const; |
---|
65 | |
---|
66 | protected: |
---|
67 | long functorType; //!< The type of Function we've got (either static or objective). |
---|
68 | unsigned int paramCount; //!< the count of parameters. |
---|
69 | MultiType defaultValue[5]; //!< Default Values. |
---|
70 | }; |
---|
71 | |
---|
72 | /////////////////////////////////////////////////// |
---|
73 | /////////////////////////////////////////////////// |
---|
74 | |
---|
75 | ///////////////////////////////// |
---|
76 | // MACRO DEFINITION EXTENSIONS // |
---|
77 | ///////////////////////////////// |
---|
78 | //! where to chek for default BOOL values |
---|
79 | #define l_BOOL_DEFGRAB(i) this->defaultValue[i].getBool() |
---|
80 | //! where to chek for default INT values |
---|
81 | #define l_INT_DEFGRAB(i) this->defaultValue[i].getInt() |
---|
82 | //! where to chek for default UINT values |
---|
83 | #define l_UINT_DEFGRAB(i) (unsigned int)this->defaultValue[i].getInt() |
---|
84 | //! where to chek for default LONG values |
---|
85 | #define l_LONG_DEFGRAB(i) (long)this->defaultValue[i].getInt() |
---|
86 | //! where to chek for default FLOAT values |
---|
87 | #define l_FLOAT_DEFGRAB(i) this->defaultValue[i].getFloat() |
---|
88 | //! where to chek for default STRING values |
---|
89 | #define l_STRING_DEFGRAB(i) this->defaultValue[i].getString() |
---|
90 | //! where to chek for default CSTRING values |
---|
91 | #define l_CSTRING_DEFGRAB(i) this->defaultValue[i].getCString() |
---|
92 | |
---|
93 | ////////////////////////// |
---|
94 | // COMMAND REGISTRATION // |
---|
95 | ////////////////////////// |
---|
96 | // EXECUTOR can be redefined as Executor or ExecutorStatic |
---|
97 | // EXECUTOREXECUTER can be redefined too. |
---|
98 | // EXECUTORINCLASS |
---|
99 | // EXECUTORTYPE |
---|
100 | |
---|
101 | |
---|
102 | /////////////////////// |
---|
103 | // FUNCTION POINTERS // |
---|
104 | /////////////////////// |
---|
105 | #define ExecutorFunctionPoiter0() \ |
---|
106 | void EXECUTORINCLASS(*functionPointer_0)(); |
---|
107 | |
---|
108 | #define ExecutorFunctionPoiter1(t1) \ |
---|
109 | void EXECUTORINCLASS(*functionPointer_1_##t1)(t1##_TYPE); |
---|
110 | |
---|
111 | #define ExecutorFunctionPoiter2(t1, t2) \ |
---|
112 | void EXECUTORINCLASS(*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE); |
---|
113 | |
---|
114 | |
---|
115 | #define ExecutorFunctionPoiter3(t1, t2, t3) \ |
---|
116 | void EXECUTORINCLASS(*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE); |
---|
117 | |
---|
118 | #define ExecutorFunctionPoiter4(t1, t2, t3, t4) \ |
---|
119 | void EXECUTORINCLASS(*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE); |
---|
120 | |
---|
121 | |
---|
122 | #define ExecutorFunctionPoiter5(t1, t2, t3, t4, t5) \ |
---|
123 | void EXECUTORINCLASS(*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); \ |
---|
124 | |
---|
125 | |
---|
126 | ////////////////// |
---|
127 | // CONSTRUCTORS // |
---|
128 | ///////////////// |
---|
129 | //! creates a command that takes no parameters |
---|
130 | #define ExecutorConstructor0() \ |
---|
131 | EXECUTOR(void EXECUTORINCLASS(*function)()) \ |
---|
132 | : Executor(0) \ |
---|
133 | { \ |
---|
134 | this->functorType = EXECUTORTYPE; \ |
---|
135 | this->fp.functionPointer_0 = function; \ |
---|
136 | } |
---|
137 | |
---|
138 | //! creates a command that takes one parameter |
---|
139 | #define ExecutorConstructor1(t1) \ |
---|
140 | EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE)) \ |
---|
141 | : Executor(t1##_PARAM) \ |
---|
142 | { \ |
---|
143 | this->functorType = EXECUTORTYPE; \ |
---|
144 | this->fp.functionPointer_1_##t1 = function; \ |
---|
145 | } |
---|
146 | |
---|
147 | //! creates a command that takes two parameters |
---|
148 | #define ExecutorConstructor2(t1,t2) \ |
---|
149 | EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE)) \ |
---|
150 | : Executor(t1##_PARAM, t2##_PARAM) \ |
---|
151 | { \ |
---|
152 | this->functorType = EXECUTORTYPE; \ |
---|
153 | this->fp.functionPointer_2_##t1##_##t2 = function; \ |
---|
154 | } |
---|
155 | |
---|
156 | //! creates a command that takes three parameter |
---|
157 | #define ExecutorConstructor3(t1,t2,t3) \ |
---|
158 | EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE)) \ |
---|
159 | : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM) \ |
---|
160 | { \ |
---|
161 | this->functorType = EXECUTORTYPE; \ |
---|
162 | this->fp.functionPointer_3_##t1##_##t2##_##t3 = function; \ |
---|
163 | } |
---|
164 | |
---|
165 | //! creates a command that takes four parameter |
---|
166 | #define ExecutorConstructor4(t1,t2,t3,t4) \ |
---|
167 | EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE)) \ |
---|
168 | : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM) \ |
---|
169 | { \ |
---|
170 | this->functorType = EXECUTORTYPE; \ |
---|
171 | this->fp.functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \ |
---|
172 | } |
---|
173 | |
---|
174 | //! creates a command that takes five parameter |
---|
175 | #define ExecutorConstructor5(t1,t2,t3,t4,t5) \ |
---|
176 | EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE)) \ |
---|
177 | : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM) \ |
---|
178 | { \ |
---|
179 | this->functorType = EXECUTORTYPE; \ |
---|
180 | this->fp.functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \ |
---|
181 | } |
---|
182 | |
---|
183 | /////////////// |
---|
184 | // EXECUTION // |
---|
185 | /////////////// |
---|
186 | //! execute-macro for functions with no parameters |
---|
187 | #define ExecutorExecute0() \ |
---|
188 | if (this->paramCount == 0) \ |
---|
189 | EXECUTOREXECUTER(_0)() |
---|
190 | |
---|
191 | //! execute-macro for functions with one parameter |
---|
192 | #define ExecutorExecute1(t1) \ |
---|
193 | else if (this->paramCount == 1 && this->defaultValue[0] == t1##_PARAM) \ |
---|
194 | EXECUTOREXECUTER(_1_##t1)(t1##_FUNC(sub[0], t1##_DEFGRAB(0))) |
---|
195 | |
---|
196 | //! execute-macro for functions with two parameters |
---|
197 | #define ExecutorExecute2(t1,t2) \ |
---|
198 | else if (this->paramCount == 2 && this->defaultValue[0] == t1##_PARAM && this->defaultValue[1] == t2##_PARAM) \ |
---|
199 | EXECUTOREXECUTER(_2_##t1##_##t2)(t1##_FUNC(sub[0], t1##_DEFGRAB(0)), t2##_FUNC(sub[1], t2##_DEFGRAB(1))) |
---|
200 | |
---|
201 | //! execute-macro for functions with three parameters |
---|
202 | #define ExecutorExecute3(t1,t2,t3) \ |
---|
203 | else if (this->paramCount == 3 && this->defaultValue[0] == t1##_PARAM && this->defaultValue[1] == t2##_PARAM && this->defaultValue[2] == t3##_PARAM) \ |
---|
204 | 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))) |
---|
205 | |
---|
206 | //! execute-macro for functions with four parameters |
---|
207 | #define ExecutorExecute4(t1,t2,t3,t4) \ |
---|
208 | 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) \ |
---|
209 | 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))) \ |
---|
210 | |
---|
211 | |
---|
212 | //! execute-macro for functions with five parameters |
---|
213 | #define ExecutorExecute5(t1,t2,t3,t4,t5) \ |
---|
214 | 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) \ |
---|
215 | 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))) |
---|
216 | |
---|
217 | |
---|
218 | |
---|
219 | |
---|
220 | |
---|
221 | //////////\////////// |
---|
222 | // DYNAMIC FUNCTOR // |
---|
223 | ///////////\///////// |
---|
224 | #ifdef FUNCTOR_LIST |
---|
225 | #undef FUNCTOR_LIST |
---|
226 | #endif |
---|
227 | #ifdef EXECUTOR |
---|
228 | #undef EXECUTOR |
---|
229 | #endif |
---|
230 | #define EXECUTOR ExecutorObjective |
---|
231 | #ifdef EXECUTOREXECUTER |
---|
232 | #undef EXECUTOREXECUTER |
---|
233 | #endif |
---|
234 | #define EXECUTOREXECUTER(nameExt) (dynamic_cast<T*>(object)->*(fp.functionPointer##nameExt)) |
---|
235 | #ifdef EXECUTORINCLASS |
---|
236 | #undef EXECUTORINCLASS |
---|
237 | #endif |
---|
238 | #define EXECUTORINCLASS(FUNCTION) (T::FUNCTION) |
---|
239 | #ifdef EXECUTORTYPE |
---|
240 | #undef EXECUTORTYPE |
---|
241 | #endif |
---|
242 | #define EXECUTORTYPE Executor_Objective |
---|
243 | //! keeps information about a Executor |
---|
244 | template<class T> class EXECUTOR : public Executor |
---|
245 | { |
---|
246 | public: |
---|
247 | EXECUTOR() : Executor() { }; |
---|
248 | // COPY constuctor (virtual version) |
---|
249 | virtual Executor* clone () const |
---|
250 | { |
---|
251 | EXECUTOR<T>* executor = new EXECUTOR<T>(); |
---|
252 | this->cloning(executor); |
---|
253 | executor->fp = this->fp; |
---|
254 | return executor; |
---|
255 | } |
---|
256 | |
---|
257 | //! FUNCTOR_LIST is the List of CommandConstructors |
---|
258 | #define FUNCTOR_LIST(x) ExecutorConstructor ## x |
---|
259 | #include "functor_list.h" |
---|
260 | #undef FUNCTOR_LIST |
---|
261 | |
---|
262 | private: |
---|
263 | //! FUNCTOR_LIST is the List of FunctionPointers |
---|
264 | union FunctionPointers { |
---|
265 | #define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x |
---|
266 | #include "functor_list.h" |
---|
267 | #undef FUNCTOR_LIST |
---|
268 | } fp; |
---|
269 | |
---|
270 | virtual void execute (BaseObject* object, const std::string& parameters = "") |
---|
271 | { |
---|
272 | SubString sub; |
---|
273 | sub.split(parameters, " \n\t,", '\\'); |
---|
274 | //! FUNCTOR_LIST is the List of Executive Functions |
---|
275 | #define FUNCTOR_LIST(x) ExecutorExecute ## x |
---|
276 | #include "functor_list.h" |
---|
277 | #undef FUNCTOR_LIST |
---|
278 | } |
---|
279 | }; |
---|
280 | |
---|
281 | |
---|
282 | //////////////////// |
---|
283 | // STATIC FUNCTOR // |
---|
284 | //////////////////// |
---|
285 | #ifdef FUNCTOR_LIST |
---|
286 | #undef FUNCTOR_LIST |
---|
287 | #endif |
---|
288 | #ifdef EXECUTOR |
---|
289 | #undef EXECUTOR |
---|
290 | #endif |
---|
291 | #define EXECUTOR ExecutorStatic |
---|
292 | #ifdef EXECUTOREXECUTER |
---|
293 | #undef EXECUTOREXECUTER |
---|
294 | #endif |
---|
295 | #define EXECUTOREXECUTER(nameExt) fp.functionPointer##nameExt |
---|
296 | #ifdef EXECUTORINCLASS |
---|
297 | #undef EXECUTORINCLASS |
---|
298 | #endif |
---|
299 | #define EXECUTORINCLASS(FUNCTION) (FUNCTION) |
---|
300 | #ifdef EXECUTORTYPE |
---|
301 | #undef EXECUTORTYPE |
---|
302 | #endif |
---|
303 | #define EXECUTORTYPE Executor_Static |
---|
304 | |
---|
305 | //! keeps information about a Executor, that points to a Static Function |
---|
306 | template<class T> class ExecutorStatic : public Executor |
---|
307 | { |
---|
308 | public: |
---|
309 | EXECUTOR() : Executor() { }; |
---|
310 | // COPY constuctor |
---|
311 | virtual Executor* clone () const |
---|
312 | { |
---|
313 | EXECUTOR<T>* executor = new EXECUTOR<T>(); |
---|
314 | this->cloning(executor); |
---|
315 | executor->fp = this->fp; |
---|
316 | return executor; |
---|
317 | } |
---|
318 | |
---|
319 | //! FUNCTOR_LIST is the List of CommandConstructors |
---|
320 | #define FUNCTOR_LIST(x) ExecutorConstructor ## x |
---|
321 | #include "functor_list.h" |
---|
322 | #undef FUNCTOR_LIST |
---|
323 | |
---|
324 | private: |
---|
325 | //! FUNCTOR_LIST is the List of FunctionPointers |
---|
326 | union FunctionPointers { |
---|
327 | #define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x |
---|
328 | #include "functor_list.h" |
---|
329 | #undef FUNCTOR_LIST |
---|
330 | } fp; |
---|
331 | |
---|
332 | |
---|
333 | virtual void execute (BaseObject* object, const std::string& parameters = "") |
---|
334 | { |
---|
335 | SubString sub; |
---|
336 | sub.split(parameters, " \n\t,", '\\'); |
---|
337 | //! FUNCTOR_LIST is the List of Executive Functions |
---|
338 | #define FUNCTOR_LIST(x) ExecutorExecute ## x |
---|
339 | #include "functor_list.h" |
---|
340 | #undef FUNCTOR_LIST |
---|
341 | } |
---|
342 | }; |
---|
343 | |
---|
344 | #endif /* _EXECUTOR_H */ |
---|