Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Executor now uses MultiType instead of va_arg

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