Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/executor/executor.h @ 7199

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

orxonox/trunk: MultiType rework (now uses std::string) this is more compliant, and better to handle

File size: 11.7 KB
RevLine 
[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.
17typedef enum {
[5652]18  Executor_Objective         = 0x00000001,
19  Executor_Static            = 0x00000002,
20
21  Executor_NoLoadString      = 0x00000010,
[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]38class 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 */
[5691]50    virtual void execute (BaseObject* object, const void* parameters = NULL) = 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
[7199]89#define   l_CSTRING_DEFGRAB(i)      this->defaultValue[i].getCString()
[5135]90
[5153]91//////////////////////////
92// COMMAND REGISTRATION //
93//////////////////////////
[5636]94// EXECUTOR             can be redefined as Executor or ExecutorStatic
95// EXECUTOREXECUTER     can be redefined too.
[5632]96// EXECUTORINCLASS
97// EXECUTORTYPE
[5135]98
[5636]99
[5551]100///////////////////////
101// FUNCTION POINTERS //
102///////////////////////
[5632]103#define ExecutorFunctionPoiter0() \
104  void EXECUTORINCLASS(*functionPointer_0)();
[5551]105
[5632]106#define ExecutorFunctionPoiter1(t1) \
107  void EXECUTORINCLASS(*functionPointer_1_##t1)(t1##_TYPE);
[5551]108
[5632]109#define ExecutorFunctionPoiter2(t1, t2) \
110  void EXECUTORINCLASS(*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE);
[5551]111
112
[5632]113#define ExecutorFunctionPoiter3(t1, t2, t3) \
114  void EXECUTORINCLASS(*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE);
[5551]115
[5632]116#define ExecutorFunctionPoiter4(t1, t2, t3, t4) \
117  void EXECUTORINCLASS(*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE);
[5551]118
119
[5632]120#define ExecutorFunctionPoiter5(t1, t2, t3, t4, t5) \
121  void EXECUTORINCLASS(*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); \
[5551]122
123
[5153]124//////////////////
125// CONSTRUCTORS //
126/////////////////
[5166]127//! creates a command that takes no parameters
[5632]128#define ExecutorConstructor0() \
[5636]129  EXECUTOR(void EXECUTORINCLASS(*function)()) \
130  : Executor(0) \
[5142]131  { \
[5632]132    this->functorType = EXECUTORTYPE; \
[5551]133    this->fp.functionPointer_0 = function; \
[5142]134  }
135
[5166]136//! creates a command that takes one parameter
[5632]137#define ExecutorConstructor1(t1) \
[5636]138  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE)) \
[7197]139  : Executor(t1##_PARAM) \
[5135]140  { \
[5632]141    this->functorType = EXECUTORTYPE; \
[5551]142    this->fp.functionPointer_1_##t1 = function; \
[5135]143  }
144
[5166]145//! creates a command that takes two parameters
[5632]146#define ExecutorConstructor2(t1,t2) \
[5636]147  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE)) \
[7197]148  : Executor(t1##_PARAM, t2##_PARAM) \
[5153]149  { \
[5632]150    this->functorType = EXECUTORTYPE; \
[5551]151    this->fp.functionPointer_2_##t1##_##t2 = function; \
[5153]152  }
[5135]153
[5166]154//! creates a command that takes three parameter
[5632]155#define ExecutorConstructor3(t1,t2,t3) \
[5636]156  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE)) \
[7197]157  : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM) \
[5153]158  { \
[5632]159    this->functorType = EXECUTORTYPE; \
[5551]160    this->fp.functionPointer_3_##t1##_##t2##_##t3 = function; \
[5153]161  }
[5141]162
[5166]163//! creates a command that takes four parameter
[5632]164#define ExecutorConstructor4(t1,t2,t3,t4) \
[5636]165  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE)) \
[7197]166  : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM) \
[5153]167  { \
[5632]168    this->functorType = EXECUTORTYPE; \
[5551]169    this->fp.functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \
[5153]170  }
171
[5166]172//! creates a command that takes five parameter
[5632]173#define ExecutorConstructor5(t1,t2,t3,t4,t5) \
[5636]174  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE)) \
[7197]175  : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM) \
[5153]176  { \
[5632]177    this->functorType = EXECUTORTYPE; \
[5551]178    this->fp.functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \
[5153]179  }
180
181///////////////
182// EXECUTION //
183///////////////
[5166]184//! execute-macro for functions with no parameters
[5632]185#define ExecutorExecute0() \
[5145]186  if (this->paramCount == 0) \
[5632]187    EXECUTOREXECUTER(_0)()
[5145]188
[5166]189//! execute-macro for functions with one parameter
[5632]190#define ExecutorExecute1(t1) \
[5633]191   else if (this->paramCount == 1 && this->defaultValue[0].getType() == t1##_PARAM) \
[5690]192    EXECUTOREXECUTER(_1_##t1)(t1##_FUNC((const char*)parameters, t1##_DEFGRAB(0)))
[5145]193
[5166]194//! execute-macro for functions with two parameters
[5632]195#define ExecutorExecute2(t1,t2) \
[5633]196   else if (this->paramCount == 2 && this->defaultValue[0].getType() == t1##_PARAM && this->defaultValue[1].getType() == t2##_PARAM) \
[5632]197    EXECUTOREXECUTER(_2_##t1##_##t2)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)))
[5145]198
[5166]199//! execute-macro for functions with three parameters
[5632]200#define ExecutorExecute3(t1,t2,t3) \
[5633]201   else if (this->paramCount == 3 && this->defaultValue[0].getType() == t1##_PARAM && this->defaultValue[1].getType() == t2##_PARAM && this->defaultValue[2].getType() == t3##_PARAM) \
[5632]202    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)))
[5153]203
[5166]204//! execute-macro for functions with four parameters
[5632]205#define ExecutorExecute4(t1,t2,t3,t4) \
[5633]206   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) \
[5652]207    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))) \
[5153]208
[5652]209
[5166]210//! execute-macro for functions with five parameters
[5632]211#define ExecutorExecute5(t1,t2,t3,t4,t5) \
[5633]212   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) \
[5632]213    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)))
[5153]214
215
[5632]216
217
218
219//////////\//////////
220// DYNAMIC FUNCTOR //
221///////////\/////////
[5153]222#ifdef FUNCTOR_LIST
223#undef FUNCTOR_LIST
224#endif
[5632]225#ifdef EXECUTOR
226#undef EXECUTOR
[5326]227#endif
[5632]228#define EXECUTOR                       ExecutorObjective
229#ifdef EXECUTOREXECUTER
230#undef EXECUTOREXECUTER
[5326]231#endif
[5632]232#define EXECUTOREXECUTER(nameExt)      (dynamic_cast<T*>(object)->*(fp.functionPointer##nameExt))
233#ifdef EXECUTORINCLASS
234#undef EXECUTORINCLASS
[5327]235#endif
[5632]236#define EXECUTORINCLASS(FUNCTION)      (T::FUNCTION)
237#ifdef EXECUTORTYPE
238#undef EXECUTORTYPE
[5328]239#endif
[5632]240#define EXECUTORTYPE                   Executor_Objective
[5633]241//! keeps information about a Executor
242template<class T> class ExecutorObjective : public Executor
243{
[5641]244  public:
245    ExecutorObjective() : Executor() { };
246    // COPY constuctor (virtual version)
247    virtual Executor* clone () const
248    {
249      ExecutorObjective<T>* executor = new ExecutorObjective<T>();
250      this->cloning(executor);
251      executor->fp = this->fp;
252      return executor;
253    }
[5633]254
255//! FUNCTOR_LIST is the List of CommandConstructors
256#define FUNCTOR_LIST(x) ExecutorConstructor ## x
[5153]257#include "functor_list.h"
[5142]258#undef FUNCTOR_LIST
[5136]259
[5068]260  private:
[5551]261//! FUNCTOR_LIST is the List of FunctionPointers
262    union FunctionPointers {
[5632]263#define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
[5551]264#include "functor_list.h"
265#undef FUNCTOR_LIST
266    } fp;
267
[5691]268    virtual void execute (BaseObject* object, const void* parameters = NULL)
[5135]269    {
[5690]270      SubString sub((const char*) parameters, " \n\t,", '\\');
[5166]271//! FUNCTOR_LIST is the List of Executive Functions
[5632]272#define FUNCTOR_LIST(x) ExecutorExecute ## x
[5153]273#include "functor_list.h"
[5135]274#undef FUNCTOR_LIST
[5145]275    }
[5129]276};
[5113]277
[5632]278
279////////////////////
280// STATIC FUNCTOR //
281////////////////////
[5326]282#ifdef FUNCTOR_LIST
283#undef FUNCTOR_LIST
284#endif
[5632]285#ifdef EXECUTOR
286#undef EXECUTOR
[5326]287#endif
[5632]288#define EXECUTOR                      ExecutorStatic
289#ifdef EXECUTOREXECUTER
290#undef EXECUTOREXECUTER
[5326]291#endif
[5632]292#define EXECUTOREXECUTER(nameExt)     fp.functionPointer##nameExt
293#ifdef EXECUTORINCLASS
294#undef EXECUTORINCLASS
[5327]295#endif
[5632]296#define EXECUTORINCLASS(FUNCTION)     (FUNCTION)
297#ifdef EXECUTORTYPE
298#undef EXECUTORTYPE
[5328]299#endif
[5632]300#define EXECUTORTYPE                   Executor_Static
[5326]301
[5633]302//! keeps information about a Executor, that points to a Static Function
303template<class T> class ExecutorStatic : public Executor
304{
305  public:
[5641]306    ExecutorStatic() : Executor() { };
307    // COPY constuctor
308    virtual Executor* clone () const
309    {
310      ExecutorStatic<T>* executor = new ExecutorStatic<T>();
311      this->cloning(executor);
312      executor->fp = this->fp;
313      return executor;
314    }
315
[5633]316//! FUNCTOR_LIST is the List of CommandConstructors
317#define FUNCTOR_LIST(x) ExecutorConstructor ## x
[5326]318#include "functor_list.h"
319#undef FUNCTOR_LIST
320
321  private:
[5551]322//! FUNCTOR_LIST is the List of FunctionPointers
323    union FunctionPointers {
[5632]324#define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
[5551]325#include "functor_list.h"
326#undef FUNCTOR_LIST
327    } fp;
328
[5326]329
[5691]330    virtual void execute (BaseObject* object, const void* parameters = NULL)
[5326]331    {
[5690]332  SubString sub((const char*)parameters, " \n\t,");
[5326]333//! FUNCTOR_LIST is the List of Executive Functions
[5632]334#define FUNCTOR_LIST(x) ExecutorExecute ## x
[5326]335#include "functor_list.h"
336#undef FUNCTOR_LIST
337    }
338};
339
[5632]340#endif /* _EXECUTOR_H */
Note: See TracBrowser for help on using the repository browser.