Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Test in Execute (first implementation)

File size: 11.1 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 {
[7221]18  Executor_Objective         = 1,
19  Executor_Static            = 2,
[5652]20
[7221]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)
[7331]36 *  Functions with many types (@see functor_list.h)
[7197]37 */
[5632]38class Executor: public BaseObject
[5170]39{
40  public:
[5641]41    virtual ~Executor();
42
43    virtual Executor* clone () const = 0;
44
[7331]45    // SETTING up the EXECUTOR
[7198]46    Executor* defaultValues(const MultiType& value0 = MT_NULL, const MultiType& value1 = MT_NULL,
47                            const MultiType& value2 = MT_NULL, const MultiType& value3 = MT_NULL,
48                            const MultiType& value4 = MT_NULL);
[7407]49    /** @param i the i'th defaultValue, @returns reference to the MultiType */
50    inline MultiType& getDefaultValue(unsigned int i) { return defaultValue[i]; };
[5164]51
[7331]52    // EXECUTE
[5633]53    /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes */
[7419]54    virtual void operator()(BaseObject* object, const std::string& parameters = "") const = 0;
[7331]55    /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes @brief here for your convenience*/
[7419]56    void execute (BaseObject* object, const std::string& parameters = "") const { (*this)(object, parameters); };
[5161]57
[7331]58    // RETRIEVE INFORMATION
[5641]59    /** @returns the Type of this Function (either static or objective) */
[5652]60    inline long getType() const { return this->functorType; };
[5642]61    /** @returns the Count of Parameters this Executor takes */
62    inline unsigned int getParamCount() const { return this->paramCount; };
[5641]63
[5161]64    static void debug();
65
66  protected:
[7198]67    Executor(const MultiType& param0 = MT_NULL, const MultiType& param1 = MT_NULL,
68             const MultiType& param2 = MT_NULL, const MultiType& param3 = MT_NULL,
[7197]69             const MultiType& param4 = MT_NULL);
[5161]70
[5641]71    void cloning(Executor* executor) const;
[5328]72
[5161]73  protected:
[7331]74    short                       functorType;      //!< The type of Function we've got (either static or objective).
[5652]75    unsigned int                paramCount;       //!< the count of parameters.
[7197]76    MultiType                   defaultValue[5];  //!< Default Values.
[5161]77};
78
79///////////////////////////////////////////////////
80///////////////////////////////////////////////////
81
[5153]82//////////////////////////
83// COMMAND REGISTRATION //
84//////////////////////////
[5636]85// EXECUTOR             can be redefined as Executor or ExecutorStatic
86// EXECUTOREXECUTER     can be redefined too.
[5632]87// EXECUTORINCLASS
88// EXECUTORTYPE
[5135]89
[5636]90
[5551]91///////////////////////
92// FUNCTION POINTERS //
93///////////////////////
[5632]94#define ExecutorFunctionPoiter0() \
95  void EXECUTORINCLASS(*functionPointer_0)();
[5551]96
[5632]97#define ExecutorFunctionPoiter1(t1) \
98  void EXECUTORINCLASS(*functionPointer_1_##t1)(t1##_TYPE);
[5551]99
[5632]100#define ExecutorFunctionPoiter2(t1, t2) \
101  void EXECUTORINCLASS(*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE);
[5551]102
103
[5632]104#define ExecutorFunctionPoiter3(t1, t2, t3) \
105  void EXECUTORINCLASS(*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE);
[5551]106
[5632]107#define ExecutorFunctionPoiter4(t1, t2, t3, t4) \
108  void EXECUTORINCLASS(*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE);
[5551]109
110
[5632]111#define ExecutorFunctionPoiter5(t1, t2, t3, t4, t5) \
[7300]112  void EXECUTORINCLASS(*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE);
[5551]113
114
[5153]115//////////////////
116// CONSTRUCTORS //
117/////////////////
[5166]118//! creates a command that takes no parameters
[5632]119#define ExecutorConstructor0() \
[5636]120  EXECUTOR(void EXECUTORINCLASS(*function)()) \
121  : Executor(0) \
[5142]122  { \
[5632]123    this->functorType = EXECUTORTYPE; \
[5551]124    this->fp.functionPointer_0 = function; \
[5142]125  }
126
[5166]127//! creates a command that takes one parameter
[5632]128#define ExecutorConstructor1(t1) \
[5636]129  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE)) \
[7197]130  : Executor(t1##_PARAM) \
[5135]131  { \
[5632]132    this->functorType = EXECUTORTYPE; \
[5551]133    this->fp.functionPointer_1_##t1 = function; \
[5135]134  }
135
[5166]136//! creates a command that takes two parameters
[5632]137#define ExecutorConstructor2(t1,t2) \
[5636]138  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE)) \
[7197]139  : Executor(t1##_PARAM, t2##_PARAM) \
[5153]140  { \
[5632]141    this->functorType = EXECUTORTYPE; \
[5551]142    this->fp.functionPointer_2_##t1##_##t2 = function; \
[5153]143  }
[5135]144
[5166]145//! creates a command that takes three parameter
[5632]146#define ExecutorConstructor3(t1,t2,t3) \
[5636]147  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE)) \
[7197]148  : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM) \
[5153]149  { \
[5632]150    this->functorType = EXECUTORTYPE; \
[5551]151    this->fp.functionPointer_3_##t1##_##t2##_##t3 = function; \
[5153]152  }
[5141]153
[5166]154//! creates a command that takes four parameter
[5632]155#define ExecutorConstructor4(t1,t2,t3,t4) \
[5636]156  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE)) \
[7197]157  : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM) \
[5153]158  { \
[5632]159    this->functorType = EXECUTORTYPE; \
[5551]160    this->fp.functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \
[5153]161  }
162
[5166]163//! creates a command that takes five parameter
[5632]164#define ExecutorConstructor5(t1,t2,t3,t4,t5) \
[5636]165  EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE)) \
[7197]166  : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM) \
[5153]167  { \
[5632]168    this->functorType = EXECUTORTYPE; \
[5551]169    this->fp.functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \
[5153]170  }
171
172///////////////
173// EXECUTION //
174///////////////
[5166]175//! execute-macro for functions with no parameters
[5632]176#define ExecutorExecute0() \
[5145]177  if (this->paramCount == 0) \
[5632]178    EXECUTOREXECUTER(_0)()
[5145]179
[5166]180//! execute-macro for functions with one parameter
[5632]181#define ExecutorExecute1(t1) \
[7200]182   else if (this->paramCount == 1 && this->defaultValue[0] == t1##_PARAM) \
[7221]183    EXECUTOREXECUTER(_1_##t1)(t1##_FUNC(sub[0], t1##_DEFGRAB(0)))
[5145]184
[5166]185//! execute-macro for functions with two parameters
[5632]186#define ExecutorExecute2(t1,t2) \
[7200]187   else if (this->paramCount == 2 && this->defaultValue[0] == t1##_PARAM && this->defaultValue[1] == t2##_PARAM) \
[7221]188    EXECUTOREXECUTER(_2_##t1##_##t2)(t1##_FUNC(sub[0], t1##_DEFGRAB(0)), t2##_FUNC(sub[1], t2##_DEFGRAB(1)))
[5145]189
[5166]190//! execute-macro for functions with three parameters
[5632]191#define ExecutorExecute3(t1,t2,t3) \
[7200]192   else if (this->paramCount == 3 && this->defaultValue[0] == t1##_PARAM && this->defaultValue[1] == t2##_PARAM && this->defaultValue[2] == t3##_PARAM) \
[7221]193    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]194
[5166]195//! execute-macro for functions with four parameters
[5632]196#define ExecutorExecute4(t1,t2,t3,t4) \
[7200]197   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) \
[7221]198    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]199
[5652]200
[5166]201//! execute-macro for functions with five parameters
[5632]202#define ExecutorExecute5(t1,t2,t3,t4,t5) \
[7200]203   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) \
[7221]204    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]205
206
[5632]207
208
[7300]209/////////////////////
[5632]210// DYNAMIC FUNCTOR //
[7300]211/////////////////////
[5153]212#ifdef FUNCTOR_LIST
213#undef FUNCTOR_LIST
214#endif
[5632]215#ifdef EXECUTOR
216#undef EXECUTOR
[5326]217#endif
[5632]218#define EXECUTOR                       ExecutorObjective
219#ifdef EXECUTOREXECUTER
220#undef EXECUTOREXECUTER
[5326]221#endif
[5632]222#define EXECUTOREXECUTER(nameExt)      (dynamic_cast<T*>(object)->*(fp.functionPointer##nameExt))
223#ifdef EXECUTORINCLASS
224#undef EXECUTORINCLASS
[5327]225#endif
[5632]226#define EXECUTORINCLASS(FUNCTION)      (T::FUNCTION)
227#ifdef EXECUTORTYPE
228#undef EXECUTORTYPE
[5328]229#endif
[5632]230#define EXECUTORTYPE                   Executor_Objective
[5633]231//! keeps information about a Executor
[7221]232template<class T> class EXECUTOR : public Executor
[5633]233{
[5641]234  public:
[7221]235    EXECUTOR() : Executor() { };
[5641]236    // COPY constuctor (virtual version)
237    virtual Executor* clone () const
238    {
[7221]239      EXECUTOR<T>* executor = new EXECUTOR<T>();
[5641]240      this->cloning(executor);
241      executor->fp = this->fp;
242      return executor;
243    }
[5633]244
245//! FUNCTOR_LIST is the List of CommandConstructors
246#define FUNCTOR_LIST(x) ExecutorConstructor ## x
[5153]247#include "functor_list.h"
[5142]248#undef FUNCTOR_LIST
[5136]249
[5068]250  private:
[5551]251//! FUNCTOR_LIST is the List of FunctionPointers
252    union FunctionPointers {
[5632]253#define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
[5551]254#include "functor_list.h"
255#undef FUNCTOR_LIST
256    } fp;
257
[7419]258    virtual void operator()(BaseObject* object, const std::string& parameters = "") const
[5135]259    {
[7221]260      SubString sub;
261      sub.split(parameters, " \n\t,", '\\');
[5166]262//! FUNCTOR_LIST is the List of Executive Functions
[5632]263#define FUNCTOR_LIST(x) ExecutorExecute ## x
[5153]264#include "functor_list.h"
[5135]265#undef FUNCTOR_LIST
[5145]266    }
[5129]267};
[5113]268
[5632]269
270////////////////////
271// STATIC FUNCTOR //
272////////////////////
[5326]273#ifdef FUNCTOR_LIST
274#undef FUNCTOR_LIST
275#endif
[5632]276#ifdef EXECUTOR
277#undef EXECUTOR
[5326]278#endif
[5632]279#define EXECUTOR                      ExecutorStatic
280#ifdef EXECUTOREXECUTER
281#undef EXECUTOREXECUTER
[5326]282#endif
[5632]283#define EXECUTOREXECUTER(nameExt)     fp.functionPointer##nameExt
284#ifdef EXECUTORINCLASS
285#undef EXECUTORINCLASS
[5327]286#endif
[5632]287#define EXECUTORINCLASS(FUNCTION)     (FUNCTION)
288#ifdef EXECUTORTYPE
289#undef EXECUTORTYPE
[5328]290#endif
[5632]291#define EXECUTORTYPE                   Executor_Static
[5326]292
[5633]293//! keeps information about a Executor, that points to a Static Function
294template<class T> class ExecutorStatic : public Executor
295{
296  public:
[7221]297    EXECUTOR() : Executor() { };
[5641]298    // COPY constuctor
299    virtual Executor* clone () const
300    {
[7221]301      EXECUTOR<T>* executor = new EXECUTOR<T>();
[5641]302      this->cloning(executor);
303      executor->fp = this->fp;
304      return executor;
305    }
306
[5633]307//! FUNCTOR_LIST is the List of CommandConstructors
308#define FUNCTOR_LIST(x) ExecutorConstructor ## x
[5326]309#include "functor_list.h"
310#undef FUNCTOR_LIST
311
312  private:
[5551]313//! FUNCTOR_LIST is the List of FunctionPointers
314    union FunctionPointers {
[5632]315#define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
[5551]316#include "functor_list.h"
317#undef FUNCTOR_LIST
318    } fp;
319
[5326]320
[7419]321    virtual void operator()(BaseObject* object, const std::string& parameters = "") const
[5326]322    {
[7221]323      SubString sub;
324      sub.split(parameters, " \n\t,", '\\');
[5326]325//! FUNCTOR_LIST is the List of Executive Functions
[5632]326#define FUNCTOR_LIST(x) ExecutorExecute ## x
[5326]327#include "functor_list.h"
328#undef FUNCTOR_LIST
329    }
330};
331
[5632]332#endif /* _EXECUTOR_H */
Note: See TracBrowser for help on using the repository browser.