1 | /*! |
---|
2 | * @file executor_generic.h |
---|
3 | * Definition of a Generic Executor |
---|
4 | */ |
---|
5 | |
---|
6 | /* |
---|
7 | orxonox - the future of 3D-vertical-scrollers |
---|
8 | |
---|
9 | Copyright (C) 2004 orx |
---|
10 | |
---|
11 | This program is free software; you can redistribute it and/or modify |
---|
12 | it under the terms of the GNU General Public License as published by |
---|
13 | the Free Software Foundation; either version 2, or (at your option) |
---|
14 | any later version. |
---|
15 | |
---|
16 | ### File Specific: |
---|
17 | main-programmer: Benjamin Grauer |
---|
18 | co-programmer: ... |
---|
19 | */ |
---|
20 | |
---|
21 | |
---|
22 | #ifndef __EXECUTOR_GENERIC_H_ |
---|
23 | #define __EXECUTOR_GENERIC_H_ |
---|
24 | |
---|
25 | #include "executor.h" |
---|
26 | #include "substring.h" |
---|
27 | |
---|
28 | |
---|
29 | template<typename type> type fromString(const std::string& input, type defaultValue) { return defaultValue; }; |
---|
30 | template<> bool fromString<bool>(const std::string& input, bool defaultValue); |
---|
31 | template<> int fromString<int>(const std::string& input, int defaultValue); |
---|
32 | template<> unsigned int fromString<unsigned int>(const std::string& input, unsigned int defaultValue); |
---|
33 | template<> float fromString<float>(const std::string& input, float defaultValue); |
---|
34 | template<> char fromString<char>(const std::string& input, char defaultValue); |
---|
35 | template<> const std::string& fromString<const std::string&>(const std::string& input, const std::string& defaultValue); |
---|
36 | |
---|
37 | template<typename type> type fromMulti(const MultiType& multi) { /* return defaultValue; */ }; |
---|
38 | template<> bool fromMulti<bool>(const MultiType& multi); |
---|
39 | template<> int fromMulti<int>(const MultiType& multi); |
---|
40 | template<> unsigned int fromMulti<unsigned int>(const MultiType& multi); |
---|
41 | template<> float fromMulti<float>(const MultiType& multi); |
---|
42 | template<> char fromMulti<char>(const MultiType& multi); |
---|
43 | template<> const std::string& fromMulti<const std::string&>(const MultiType& multi); |
---|
44 | |
---|
45 | |
---|
46 | template<typename type> type getDefault(const MultiType* const defaultValues, unsigned int i) { return (type)0; }; |
---|
47 | template<> bool getDefault<bool>(const MultiType* const defaultValues, unsigned int i); |
---|
48 | template<> int getDefault<int>(const MultiType* const defaultValues, unsigned int i); |
---|
49 | template<> unsigned int getDefault<unsigned int>(const MultiType* const defaultValues, unsigned int i); |
---|
50 | template<> float getDefault<float>(const MultiType* const defaultValues, unsigned int i); |
---|
51 | template<> char getDefault<char>(const MultiType* const defaultValues, unsigned int i); |
---|
52 | template<> const std::string& getDefault<const std::string&>(const MultiType* const defaultValues, unsigned int i); |
---|
53 | |
---|
54 | |
---|
55 | /** |
---|
56 | * @brief this is a Template Class used as an evaluater. |
---|
57 | * |
---|
58 | * Trait to determine a default Value for any Type, |
---|
59 | * and to define the Convertible, and how it is transformed into the |
---|
60 | * corresponding SubTypes over the operator(). |
---|
61 | * |
---|
62 | * This Class must be reimplemented for each Convertible and all of its |
---|
63 | * conversion-members. |
---|
64 | * |
---|
65 | * e.g: Convertible SubSting, that splits up into many Stings |
---|
66 | * conversion-members: (int) can be transformed from a String. |
---|
67 | */ |
---|
68 | template<typename FromType> class ExecutorEvaluater |
---|
69 | { |
---|
70 | public: |
---|
71 | /** @brief Executes the Evaluater |
---|
72 | * @param CallValue the Value that should be converted |
---|
73 | * @param defaults the default Values. |
---|
74 | */ |
---|
75 | template <typename ToType, int index> |
---|
76 | ToType operator()(FromType& CallValue, const MultiType* const defaults) |
---|
77 | { |
---|
78 | return defaultValue; /*(CallValue.size() > index) ? |
---|
79 | fromString<ToType>(CallValue[index], getDefault<ToType>(defaults, index)) : |
---|
80 | fromMulti<ToType>(defaults[index]); */ |
---|
81 | } |
---|
82 | static FromType& defaultValue() { return FromType(); }; |
---|
83 | }; |
---|
84 | |
---|
85 | /** |
---|
86 | * @brief to remove writing errors, this function is Used. |
---|
87 | * @param sub The SubString to use |
---|
88 | * @param default The default Values. |
---|
89 | */ |
---|
90 | template<> class ExecutorEvaluater <const SubString> |
---|
91 | { |
---|
92 | public: |
---|
93 | /** @brief Executes the Evaluater |
---|
94 | * @param CallValue the Value that should be converted |
---|
95 | * @param defaults the default Values. |
---|
96 | */ |
---|
97 | template <typename ToType, int index> |
---|
98 | ToType operator()(const SubString& CallValue, const MultiType* const defaults) |
---|
99 | { |
---|
100 | return (CallValue.size() > index) ? |
---|
101 | fromString<ToType>(CallValue[index], getDefault<ToType>(defaults, index)) : |
---|
102 | fromMulti<ToType>(defaults[index]); |
---|
103 | } |
---|
104 | static const SubString& defaultValue() { return SubString::NullSubString; }; |
---|
105 | }; |
---|
106 | |
---|
107 | |
---|
108 | /////////// |
---|
109 | //// 0 //// |
---|
110 | /////////// |
---|
111 | //! @brief ExecutorClass, that can execute Functions without any parameters. |
---|
112 | template<class T, typename CallType, template<typename> class Evaluater = ExecutorEvaluater, class BaseClass = BaseObject> |
---|
113 | class __EXECUTOR_FUNCTIONAL_NAME(0) :public Executor<CallType, BaseClass> |
---|
114 | { |
---|
115 | private: |
---|
116 | /** @brief the FunctioPointer. */ |
---|
117 | void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST; |
---|
118 | |
---|
119 | public: |
---|
120 | /** |
---|
121 | * @brief constructs the Executor. |
---|
122 | * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function. |
---|
123 | */ |
---|
124 | __EXECUTOR_FUNCTIONAL_NAME(0) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST ) |
---|
125 | : Executor<CallType, BaseClass>(false, __EXECUTOR_FUNCTIONAL_FUNCTIONTYPE) |
---|
126 | { |
---|
127 | this->functionPointer = functionPointer; |
---|
128 | }; |
---|
129 | |
---|
130 | /** |
---|
131 | * @brief executes the Functional |
---|
132 | * @param object the Object the action should be executed on. |
---|
133 | * @param sub the SubString to get the Parameters from. |
---|
134 | */ |
---|
135 | virtual void operator()(BaseObject* object, CallType& sub = Evaluater<CallType>::defaultValue()) const |
---|
136 | { |
---|
137 | (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(); |
---|
138 | }; |
---|
139 | |
---|
140 | /** |
---|
141 | * @brief copies the Executor |
---|
142 | * @returns a new Executor that's a copy of this one. |
---|
143 | */ |
---|
144 | virtual Executor<CallType, BaseClass>* clone() const |
---|
145 | { |
---|
146 | return new __EXECUTOR_FUNCTIONAL_NAME(0)<T, CallType>(this->functionPointer); |
---|
147 | }; |
---|
148 | }; |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | /////////// |
---|
155 | //// 1 //// |
---|
156 | /////////// |
---|
157 | //! @brief ExecutorClass, that can execute Functions with one parameter. |
---|
158 | template<class T, typename CallType, typename type0, template<typename> class Evaluater = ExecutorEvaluater, class BaseClass = BaseObject> |
---|
159 | class __EXECUTOR_FUNCTIONAL_NAME(1) : public Executor<CallType, BaseClass> |
---|
160 | { |
---|
161 | private: |
---|
162 | /** @brief the FunctioPointer. */ |
---|
163 | void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST; |
---|
164 | |
---|
165 | public: |
---|
166 | /** |
---|
167 | * @brief constructs the Executor. |
---|
168 | * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function. |
---|
169 | */ |
---|
170 | __EXECUTOR_FUNCTIONAL_NAME(1) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST) |
---|
171 | : Executor<CallType, BaseClass>(false, ExecutorParamType<type0>(), __EXECUTOR_FUNCTIONAL_FUNCTIONTYPE) |
---|
172 | { |
---|
173 | this->functionPointer = functionPointer; |
---|
174 | }; |
---|
175 | |
---|
176 | /** |
---|
177 | * @brief executes the Functional |
---|
178 | * @param object the Object the action should be executed on. |
---|
179 | * @param sub the SubString to get the Parameters from. |
---|
180 | */ |
---|
181 | virtual void operator()(BaseObject* object, CallType& sub = Evaluater<CallType>::defaultValue()) const |
---|
182 | { |
---|
183 | (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)( |
---|
184 | Evaluater<CallType>().template operator()<type0, 0>(sub, this->defaultValue)); |
---|
185 | }; |
---|
186 | |
---|
187 | /** |
---|
188 | * @brief copies the Executor |
---|
189 | * @returns a new Executor that's a copy of this one. |
---|
190 | */ |
---|
191 | virtual Executor<CallType, BaseClass>* clone() const |
---|
192 | { |
---|
193 | return new __EXECUTOR_FUNCTIONAL_NAME(1)<T, CallType, type0>(this->functionPointer); |
---|
194 | }; |
---|
195 | }; |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | |
---|
201 | /////////// |
---|
202 | //// 2 //// |
---|
203 | /////////// |
---|
204 | //! @brief ExecutorClass, that can execute Functions with two parameters. |
---|
205 | template<class T, typename CallType, typename type0, typename type1, template<typename> class Evaluater = ExecutorEvaluater, class BaseClass = BaseObject> |
---|
206 | class __EXECUTOR_FUNCTIONAL_NAME(2) : public Executor<CallType, BaseClass> |
---|
207 | { |
---|
208 | private: |
---|
209 | /** @brief the FunctioPointer. */ |
---|
210 | void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST; |
---|
211 | |
---|
212 | public: |
---|
213 | /** |
---|
214 | * @brief constructs the Executor. |
---|
215 | * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function. |
---|
216 | */ |
---|
217 | __EXECUTOR_FUNCTIONAL_NAME(2) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST) |
---|
218 | : Executor<CallType, BaseClass>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>(), __EXECUTOR_FUNCTIONAL_FUNCTIONTYPE) |
---|
219 | { |
---|
220 | this->functionPointer = functionPointer; |
---|
221 | }; |
---|
222 | |
---|
223 | /** |
---|
224 | * @brief executes the Functional |
---|
225 | * @param object the Object the action should be executed on. |
---|
226 | * @param sub the SubString to get the Parameters from. |
---|
227 | */ |
---|
228 | virtual void operator()(BaseObject* object, CallType& sub = Evaluater<CallType>::defaultValue()) const |
---|
229 | { |
---|
230 | (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)( |
---|
231 | Evaluater<CallType>().template operator()<type0, 0>(sub, this->defaultValue), |
---|
232 | Evaluater<CallType>().template operator()<type1, 1>(sub, this->defaultValue)); |
---|
233 | }; |
---|
234 | |
---|
235 | /** |
---|
236 | * @brief copies the Executor |
---|
237 | * @returns a new Executor that's a copy of this one. |
---|
238 | */ |
---|
239 | virtual Executor<CallType, BaseClass>* clone() const |
---|
240 | { |
---|
241 | return new __EXECUTOR_FUNCTIONAL_NAME(2)<T, CallType, type0, type1>(this->functionPointer); |
---|
242 | }; |
---|
243 | }; |
---|
244 | |
---|
245 | |
---|
246 | |
---|
247 | |
---|
248 | |
---|
249 | /////////// |
---|
250 | //// 3 //// |
---|
251 | /////////// |
---|
252 | //! @brief ExecutorClass, that can execute Functions with three parameters. |
---|
253 | template<class T, typename CallType, typename type0, typename type1, typename type2, template<typename> class Evaluater = ExecutorEvaluater, class BaseClass = BaseObject> |
---|
254 | class __EXECUTOR_FUNCTIONAL_NAME(3) : public Executor<CallType, BaseClass> |
---|
255 | { |
---|
256 | private: |
---|
257 | /** @brief the FunctioPointer. */ |
---|
258 | void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2) __EXECUTOR_FUNCTIONAL_CONST; |
---|
259 | |
---|
260 | public: |
---|
261 | /** |
---|
262 | * @brief constructs the Executor. |
---|
263 | * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function. |
---|
264 | */ |
---|
265 | __EXECUTOR_FUNCTIONAL_NAME(3) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2) __EXECUTOR_FUNCTIONAL_CONST) |
---|
266 | : Executor<CallType, BaseClass>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), __EXECUTOR_FUNCTIONAL_FUNCTIONTYPE) |
---|
267 | { |
---|
268 | this->functionPointer = functionPointer; |
---|
269 | }; |
---|
270 | |
---|
271 | /** |
---|
272 | * @brief executes the Functional |
---|
273 | * @param object the Object the action should be executed on. |
---|
274 | * @param sub the SubString to get the Parameters from. |
---|
275 | */ |
---|
276 | virtual void operator()(BaseObject* object, CallType& sub = Evaluater<CallType>::defaultValue()) const |
---|
277 | { |
---|
278 | (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)( |
---|
279 | Evaluater<CallType>().template operator()<type0, 0>(sub, this->defaultValue), |
---|
280 | Evaluater<CallType>().template operator()<type1, 1>(sub, this->defaultValue), |
---|
281 | Evaluater<CallType>().template operator()<type2, 2>(sub, this->defaultValue)); |
---|
282 | }; |
---|
283 | |
---|
284 | /** |
---|
285 | * @brief copies the Executor |
---|
286 | * @returns a new Executor that's a copy of this one. |
---|
287 | */ |
---|
288 | virtual Executor<CallType, BaseClass>* clone() const |
---|
289 | { |
---|
290 | return new __EXECUTOR_FUNCTIONAL_NAME(3)<T, CallType, type0, type1, type2>(this->functionPointer); |
---|
291 | }; |
---|
292 | }; |
---|
293 | |
---|
294 | |
---|
295 | |
---|
296 | |
---|
297 | |
---|
298 | /////////// |
---|
299 | //// 4 //// |
---|
300 | /////////// |
---|
301 | //! @brief ExecutorClass, that can execute Functions with four parameters. |
---|
302 | template<class T, typename CallType, typename type0, typename type1, typename type2, typename type3, template<typename> class Evaluater = ExecutorEvaluater, class BaseClass = BaseObject> |
---|
303 | class __EXECUTOR_FUNCTIONAL_NAME(4) : public Executor<CallType, BaseClass> |
---|
304 | { |
---|
305 | private: |
---|
306 | /** @brief the FunctioPointer. */ |
---|
307 | void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST; |
---|
308 | |
---|
309 | public: |
---|
310 | /** |
---|
311 | * @brief constructs the Executor. |
---|
312 | * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function. |
---|
313 | */ |
---|
314 | __EXECUTOR_FUNCTIONAL_NAME(4) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3) __EXECUTOR_FUNCTIONAL_CONST) |
---|
315 | : Executor<CallType, BaseClass>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>(), __EXECUTOR_FUNCTIONAL_FUNCTIONTYPE) |
---|
316 | { |
---|
317 | this->functionPointer = functionPointer; |
---|
318 | }; |
---|
319 | |
---|
320 | /** |
---|
321 | * @brief executes the Functional |
---|
322 | * @param object the Object the action should be executed on. |
---|
323 | * @param sub the SubString to get the Parameters from. |
---|
324 | */ |
---|
325 | virtual void operator()(BaseObject* object, CallType& sub = Evaluater<CallType>::defaultValue()) const |
---|
326 | { |
---|
327 | (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)( |
---|
328 | Evaluater<CallType>().template operator()<type0, 0>(sub, this->defaultValue), |
---|
329 | Evaluater<CallType>().template operator()<type1, 1>(sub, this->defaultValue), |
---|
330 | Evaluater<CallType>().template operator()<type2, 2>(sub, this->defaultValue), |
---|
331 | Evaluater<CallType>().template operator()<type3, 3>(sub, this->defaultValue)); |
---|
332 | }; |
---|
333 | |
---|
334 | /** |
---|
335 | * @brief copies the Executor |
---|
336 | * @returns a new Executor that's a copy of this one. |
---|
337 | */ |
---|
338 | virtual Executor<CallType, BaseClass>* clone() const |
---|
339 | { |
---|
340 | return new __EXECUTOR_FUNCTIONAL_NAME(4)<T, CallType, type0, type1, type2, type3>(this->functionPointer); |
---|
341 | }; |
---|
342 | }; |
---|
343 | |
---|
344 | |
---|
345 | |
---|
346 | |
---|
347 | |
---|
348 | |
---|
349 | /////////// |
---|
350 | //// 5 //// |
---|
351 | /////////// |
---|
352 | //! @brief ExecutorClass, that can execute Functions with five parameters. |
---|
353 | template<class T, typename CallType, typename type0, typename type1, typename type2, typename type3, typename type4, template<typename> class Evaluater = ExecutorEvaluater, class BaseClass = BaseObject> |
---|
354 | class __EXECUTOR_FUNCTIONAL_NAME(5) : public Executor<CallType, BaseClass> |
---|
355 | { |
---|
356 | private: |
---|
357 | /** @brief the FunctioPointer. */ |
---|
358 | void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3, type4) __EXECUTOR_FUNCTIONAL_CONST; |
---|
359 | |
---|
360 | public: |
---|
361 | /** |
---|
362 | * @brief constructs the Executor. |
---|
363 | * @param __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER the FunctionPointer to the Calling Function. |
---|
364 | */ |
---|
365 | __EXECUTOR_FUNCTIONAL_NAME(5) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1, type2, type3, type4) __EXECUTOR_FUNCTIONAL_CONST) |
---|
366 | : Executor<CallType, BaseClass>(false, ExecutorParamType<type0>(), ExecutorParamType<type1>(), ExecutorParamType<type2>(), ExecutorParamType<type3>(), ExecutorParamType<type4>(), __EXECUTOR_FUNCTIONAL_FUNCTIONTYPE) |
---|
367 | { |
---|
368 | this->functionPointer = functionPointer; |
---|
369 | }; |
---|
370 | |
---|
371 | /** |
---|
372 | * @brief executes the Functional |
---|
373 | * @param object the Object the action should be executed on. |
---|
374 | * @param sub the SubString to get the Parameters from. |
---|
375 | */ |
---|
376 | virtual void operator()(BaseObject* object, CallType& sub = Evaluater<CallType>::defaultValue()) const |
---|
377 | { |
---|
378 | (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)( |
---|
379 | Evaluater<CallType>().template operator()<type0, 0>(sub, this->defaultValue), |
---|
380 | Evaluater<CallType>().template operator()<type1, 1>(sub, this->defaultValue), |
---|
381 | Evaluater<CallType>().template operator()<type2, 2>(sub, this->defaultValue), |
---|
382 | Evaluater<CallType>().template operator()<type3, 3>(sub, this->defaultValue), |
---|
383 | Evaluater<CallType>().template operator()<type4, 4>(sub, this->defaultValue)); |
---|
384 | }; |
---|
385 | |
---|
386 | /** |
---|
387 | * @brief copies the Executor |
---|
388 | * @returns a new Executor that's a copy of this one. |
---|
389 | */ |
---|
390 | virtual Executor<CallType, BaseClass>* clone() const |
---|
391 | { |
---|
392 | return new __EXECUTOR_FUNCTIONAL_NAME(5)<T, CallType, type0, type1, type2, type3, type4>(this->functionPointer); |
---|
393 | }; |
---|
394 | }; |
---|
395 | |
---|
396 | #ifndef __EXECUTOR_GENERIC_H_ |
---|