Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new LoadParam procedure with all NON-cycling load-options, and it works perfectly (on first sight :))

now going to make the same for cycling LoadOptions

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