Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/util/executor/executor_generic.h @ 9740

Last change on this file since 9740 was 9740, checked in by bensch, 18 years ago

multiple generic inclusion of Executors, better and nicer interface for include

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