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