Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6248 was 5691, checked in by bensch, 19 years ago

orxonox/trunk: moved functor_list.h

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