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