Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

split it up

File size: 7.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 "multi_type.h"
12
13//! The maximum Count of Arguments of the Executor
14/** This is Hardcoded, for each Executor. */
15#define EXECUTOR_MAX_ARGUMENTS                7
16
17
18
19template<typename type> MT_Type ExecutorParamType() { return MT_EXT1; };
20template<> MT_Type ExecutorParamType<bool>();
21template<> MT_Type ExecutorParamType<int>();
22template<> MT_Type ExecutorParamType<unsigned int>();
23template<> MT_Type ExecutorParamType<float>();
24template<> MT_Type ExecutorParamType<char>();
25template<> MT_Type ExecutorParamType<const std::string&>();
26
27////////////////
28// BASE CLASS //
29////////////////
30//! a BaseClass for all possible Executors
31/**
32 * An Executor is an Object, that is able to call Objects of Any type (class)
33 * and execute a function with given parameters on it.
34 *
35 * The Executor is able to handle:
36 *  Objects of any Class (Templated)
37 *  Default Values
38 *  Functions with up to 5 parameters (more seems overhead, split up the function)
39 *  Functions with many types (@see functor_list.h)
40 */
41template <typename CallType, class BaseClass = BaseObject> class Executor
42{
43public:
44  //! an enumerator for the definition of the Type.
45  typedef enum {
46    FunctionMember,      //!< The function is neither Static nor Constant
47    FunctionStatic,      //!< The Function is Static and pointing to either a Static Member or a C-style function.
48    FunctionConstMember, //!< The Function is Constant and pointing to a Member that does not change the Object.
49  } FunctionType;
50
51public:
52  virtual ~Executor() {};
53
54  // RETRIEVE INFORMATION
55  /** @param i the i'th defaultValue, @returns reference to the MultiType */
56  inline MultiType& getDefaultValue(unsigned int i) { return defaultValue[i]; };
57  /** @returns the default Values as a List */
58  inline const MultiType* const getDefaultValues() { return defaultValue; };
59
60  /** @returns the Type of this Function (either static or objective) */
61  inline FunctionType getType() const { return this->functionType; };
62
63  /** @returns the Count of Parameters this Executor takes */
64  inline unsigned int getParamCount() const { return this->paramCount; };
65  /** @returns true if the Executor has a return Value. */
66  inline bool hasRetVal() const { return bRetVal; };
67
68  /** executes a Command. @param objec the Object, @param count how many values, @param values the Values */
69  virtual void operator()(BaseClass* object, CallType& values) const = 0;
70
71  /**
72   * @brief set the default values of the executor
73   * @param value0 the first default value
74   * @param value1 the second default value
75   * @param value2 the third default value
76   * @param value3 the fourth default value
77   * @param value4 the fifth default value
78   * @returns itself
79   * @note: THIS FUNCTION WILL BE REPLACED BY A CONFIGURATOR (most probably).
80  */
81  Executor* defaultValues(const MultiType& value0 = MT_NULL, const MultiType& value1 = MT_NULL,
82                          const MultiType& value2 = MT_NULL, const MultiType& value3 = MT_NULL,
83                          const MultiType& value4 = MT_NULL, const MultiType& value5 = MT_NULL,
84                          const MultiType& value6 = MT_NULL)
85  {
86    const MultiType* value[5];
87    value[0] = &value0;
88    value[1] = &value1;
89    value[2] = &value2;
90    value[3] = &value3;
91    value[4] = &value4;
92    value[5] = &value5;
93    value[6] = &value6;
94    for (unsigned int i = 0; i < this->paramCount; i++)
95    {
96      if (*value[i] != MT_NULL)
97      {
98        this->defaultValue[i].setValueOf(*value[i]);
99        this->defaultValue[i].storeString();
100      }
101    }
102    return this;
103  }
104
105  /** @returns the Clone as a new Copy of the Executor. */
106  virtual Executor<CallType>* clone () const = 0;
107
108
109protected:
110  //! Now follows a List of Executor Constructors, to be fast in creating.
111  Executor(bool hasRetVal, FunctionType functionType = FunctionMember)
112  : bRetVal(hasRetVal), paramCount(0), functionType(functionType)
113  { };
114
115  Executor(bool hasRetVal, const MultiType& param0,
116           FunctionType functionType = FunctionMember)
117  : bRetVal(hasRetVal), paramCount(1), functionType(functionType)
118  {
119    this->defaultValue[0] = param0;
120  };
121
122  Executor(bool hasRetVal, const MultiType& param0, const MultiType& param1,
123           FunctionType functionType = FunctionMember)
124  : bRetVal(hasRetVal), paramCount(2), functionType(functionType)
125  {
126    this->defaultValue[0] = param0;
127    this->defaultValue[1] = param1;
128  };
129
130  Executor(bool hasRetVal, const MultiType& param0, const MultiType& param1,
131           const MultiType& param2,
132           FunctionType functionType = FunctionMember)
133  : bRetVal(hasRetVal), paramCount(3), functionType(functionType)
134  {
135    this->defaultValue[0] = param0;
136    this->defaultValue[1] = param1;
137    this->defaultValue[2] = param2;
138  };
139
140  Executor(bool hasRetVal, const MultiType& param0, const MultiType& param1,
141           const MultiType& param2, const MultiType& param3,
142           FunctionType functionType = FunctionMember)
143  : bRetVal(hasRetVal), paramCount(4), functionType(functionType)
144  {
145    this->defaultValue[0] = param0;
146    this->defaultValue[1] = param1;
147    this->defaultValue[2] = param2;
148    this->defaultValue[3] = param3;
149  };
150
151  Executor(bool hasRetVal, const MultiType& param0, const MultiType& param1,
152           const MultiType& param2, const MultiType& param3,
153           const MultiType& param4,
154           FunctionType functionType = FunctionMember)
155  : bRetVal(hasRetVal), paramCount(5), functionType(functionType)
156  {
157    this->defaultValue[0] = param0;
158    this->defaultValue[1] = param1;
159    this->defaultValue[2] = param2;
160    this->defaultValue[3] = param3;
161    this->defaultValue[4] = param4;
162  };
163
164  Executor(bool hasRetVal, const MultiType& param0, const MultiType& param1,
165           const MultiType& param2, const MultiType& param3,
166           const MultiType& param4, const MultiType& param5,
167           FunctionType functionType = FunctionMember)
168  : bRetVal(hasRetVal), paramCount(6), functionType(functionType)
169  {
170    this->defaultValue[0] = param0;
171    this->defaultValue[1] = param1;
172    this->defaultValue[2] = param2;
173    this->defaultValue[3] = param3;
174    this->defaultValue[4] = param4;
175    this->defaultValue[5] = param5;
176  };
177
178  Executor(bool hasRetVal, const MultiType& param0, const MultiType& param1,
179           const MultiType& param2, const MultiType& param3,
180           const MultiType& param4, const MultiType& param5,
181           const MultiType& param6,
182           FunctionType functionType = FunctionMember)
183  : bRetVal(hasRetVal), paramCount(7), functionType(functionType)
184  {
185    this->defaultValue[0] = param0;
186    this->defaultValue[1] = param1;
187    this->defaultValue[2] = param2;
188    this->defaultValue[3] = param3;
189    this->defaultValue[4] = param4;
190    this->defaultValue[5] = param5;
191    this->defaultValue[6] = param6;
192  };
193
194protected:
195  const bool                  bRetVal;          //!< True if the Executor has a return Value.
196  const unsigned int          paramCount;       //!< the count of parameters.
197  MultiType                   defaultValue[7];  //!< Default Values.
198
199  const FunctionType          functionType;     //!< What Type of Function it is.
200};
201
202#endif /* _EXECUTOR_H */
Note: See TracBrowser for help on using the repository browser.