Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7716 in orxonox.OLD for trunk/src


Ignore:
Timestamp:
May 19, 2006, 2:43:22 AM (19 years ago)
Author:
bensch
Message:

trunk: static and const calls all work

Location:
trunk/src
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/Makefile.am

    r7661 r7716  
    4141                util/helper_functions.h \
    4242                util/executor/executor.h \
     43                util/executor/executor_functional.h \
    4344                util/executor/executor_specials.h \
    4445                util/executor/functor_list.h \
  • trunk/src/lib/util/executor/executor_functional.h

    r7714 r7716  
    11/*!
    2  * @file executor.h
     2 * @file executor_functional.h
    33 * Definition of an Executor
    44 */
    55
    6 #ifndef _EXECUTOR_H
    7 #define _EXECUTOR_H
     6/*
     7   orxonox - the future of 3D-vertical-scrollers
    88
    9 #include "base_object.h"
     9   Copyright (C) 2004 orx
    1010
    11 #include "helper_functions.h"
    12 #include "multi_type.h"
    13 #include "substring.h"
    14 #include "functor_list.h" //< MUST BE INCLUDED HERE AT LEAST ONCE.
     11   This program is free software; you can redistribute it and/or modify
     12   it under the terms of the GNU General Public License as published by
     13   the Free Software Foundation; either version 2, or (at your option)
     14   any later version.
    1515
    16 //! an enumerator for the definition of the Type.
    17 typedef enum {
    18   Executor_Objective         = 1,
    19   Executor_Static            = 2,
     16### File Specific:
     17   main-programmer: Benjamin Grauer
     18   co-programmer: ...
     19*/
    2020
    21   Executor_NoLoadString      = 8,
    22 } Executor_Type;
    2321
    24 ////////////////
    25 // BASE CLASS //
    26 ////////////////
    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  */
    38 class Executor : public BaseObject
    39 {
    40 public:
    41   virtual ~Executor();
    42 
    43   virtual Executor* clone () const = 0;
    44 
    45   // SETTING up the EXECUTOR
    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);
    49   /** @param i the i'th defaultValue, @returns reference to the MultiType */
    50   inline MultiType& getDefaultValue(unsigned int i) { return defaultValue[i]; };
    51 
    52   // EXECUTE
    53   /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes */
    54   virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const = 0;
    55   /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes @brief here for your convenience*/
    56   void execute (BaseObject* object, const std::string& parameters = "") const { (*this)(object, parameters); };
    57 
    58   // RETRIEVE INFORMATION
    59   /** @returns the Type of this Function (either static or objective) */
    60   inline long getType() const { return this->functorType; };
    61   /** @returns the Count of Parameters this Executor takes */
    62   inline unsigned int getParamCount() const { return this->paramCount; };
    63 
    64   static void debug();
    65 
    66 protected:
    67   Executor(const MultiType& param0 = MT_NULL, const MultiType& param1 = MT_NULL,
    68            const MultiType& param2 = MT_NULL, const MultiType& param3 = MT_NULL,
    69            const MultiType& param4 = MT_NULL);
    70 
    71   void cloning(Executor* executor) const;
    72 
    73 protected:
    74   short                       functorType;      //!< The type of Function we've got (either static or objective).
    75   unsigned int                paramCount;       //!< the count of parameters.
    76   MultiType                   defaultValue[5];  //!< Default Values.
    77 };
    78 
    79 ///////////////////////////////////////////////////
    80 ///////////////////////////////////////////////////
    81 
    82 //////////////////////////
    83 // COMMAND REGISTRATION //
    84 //////////////////////////
    85 // EXECUTOR             can be redefined as Executor or ExecutorStatic
    86 // EXECUTOREXECUTER     can be redefined too.
    87 // EXECUTORINCLASS
    88 // EXECUTORTYPE
     22#ifndef __EXECUTOR_FUNCTIONAL_H_
     23#define __EXECUTOR_FUNCTIONAL_H_
    8924
    9025
    9126
    92 ///////////////////////
    93 // FUNCTION POINTERS //
    94 ///////////////////////
    95 #define ExecutorFunctionPoiter0() \
    96   void EXECUTORINCLASS(*functionPointer_0)();
     27template<typename type> MT_Type ExecutorParamType() { return MT_EXT1; };
     28template<> MT_Type ExecutorParamType<bool>() { return MT_EXT1; };
     29template<> MT_Type ExecutorParamType<int>() { return MT_INT; };
     30template<> MT_Type ExecutorParamType<unsigned int>() { return MT_UINT; };
     31template<> MT_Type ExecutorParamType<float>() { return MT_FLOAT; };
     32template<> MT_Type ExecutorParamType<char>() { return MT_CHAR; };
     33template<> MT_Type ExecutorParamType<const std::string&>() { return MT_STRING; };
    9734
    98 #define ExecutorFunctionPoiter1(t1) \
    99   void EXECUTORINCLASS(*functionPointer_1_##t1)(t1##_TYPE);
     35template<typename type> type fromString(const std::string& input, type defaultValue) {return defaultValue; };
     36template<> bool fromString<bool>(const std::string& input, bool defaultValue) { return isBool(input, defaultValue); };
     37template<> int fromString<int>(const std::string& input, int defaultValue) { return isInt(input, defaultValue); };
     38template<> unsigned int fromString<unsigned int>(const std::string& input, unsigned int defaultValue) { return isInt(input, defaultValue); };
     39template<> float fromString<float>(const std::string& input, float defaultValue) { return isFloat(input, defaultValue); };
     40template<> char fromString<char>(const std::string& input, char defaultValue) { return isInt(input, defaultValue); };
     41template<> const std::string& fromString<const std::string&>(const std::string& input, const std::string& defaultValue) { return isString(input, defaultValue); };
    10042
    101 #define ExecutorFunctionPoiter2(t1, t2) \
    102   void EXECUTORINCLASS(*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE);
     43template<typename type> type getDefault(const MultiType* const defaultValues, unsigned int i) { return (type)0; };
     44template<> bool getDefault<bool>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getBool(); };
     45template<> int getDefault<int>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getInt(); };
     46template<> unsigned int getDefault<unsigned int>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getInt(); };
     47template<> float getDefault<float>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getFloat(); };
     48template<> char getDefault<char>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getChar(); };
     49template<> std::string getDefault<std::string>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getString(); };
     50
     51#endif /* __EXECUTOR_FUNCTIONAL_H_ */
    10352
    10453
    105 #define ExecutorFunctionPoiter3(t1, t2, t3) \
    106   void EXECUTORINCLASS(*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE);
     54#define __EXECUTOR_FUNCTIONAL_CONST
     55#define __EXECUTOR_FUNCTIONAL_NAME(ParamCount)   Executor##ParamCount##Params
     56#define __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC      dynamic_cast<T*>(object)->*(functionPointer)
     57#define __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER   T::*functionPointer
    10758
    108 #define ExecutorFunctionPoiter4(t1, t2, t3, t4) \
    109   void EXECUTORINCLASS(*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE);
     59#ifdef EXECUTOR_FUNCTIONAL_USE_CONST
     60 #undef __EXECUTOR_FUNCTIONAL_CONST
     61 #define __EXECUTOR_FUNCTIONAL_CONST const
     62 #undef __EXECUTOR_FUNCTIONAL_NAME
     63 #define __EXECUTOR_FUNCTIONAL_NAME(ParamCount) Executor##ParamCount##Params_const
     64 #undef EXECUTOR_FUNCTIONAL_USE_CONST
     65#endif
     66
     67#ifdef EXECUTOR_FUNCTIONAL_USE_STATIC
     68 #ifdef EXECUTOR_FUNCTIONAL_USE_CONST
     69  #error you obviously do not know what you are doing !! ask the bensch
     70 #endif /* EXECUTOR_FUNCTIONAL_USE_CONST */
     71
     72 #undef __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC
     73 #define __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC       functionPointer
     74 #undef __EXECUTOR_FUNCTIONAL_NAME
     75 #define __EXECUTOR_FUNCTIONAL_NAME(ParamCount)    Executor##ParamCount##Params_static
     76 #undef __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER
     77 #define __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER    *functionPointer
     78
     79 #undef EXECUTOR_FUNCTIONAL_USE_STATIC
     80#endif /* EXECUTOR_FUNCTIONAL_USE_STATIC */
     81
     82template<class T> class __EXECUTOR_FUNCTIONAL_NAME(0) : public Executor
     83{
     84private:
     85  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST;
     86
     87public:
     88  __EXECUTOR_FUNCTIONAL_NAME(0) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST )
     89      : Executor()
     90  {
     91    this->functorType = Executor_Objective;
     92    this->functionPointer = functionPointer;
     93  }
     94  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
     95  {
     96    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)();
     97  };
     98
     99  Executor* clone() const { };
     100};
    110101
    111102
    112 #define ExecutorFunctionPoiter5(t1, t2, t3, t4, t5) \
    113   void EXECUTORINCLASS(*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE);
     103template<class T, typename type0> class __EXECUTOR_FUNCTIONAL_NAME(1) : public Executor
     104{
     105private:
     106  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST;
    114107
     108public:
     109  __EXECUTOR_FUNCTIONAL_NAME(1) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0) __EXECUTOR_FUNCTIONAL_CONST)
     110      : Executor(ExecutorParamType<type0>())
     111  {
     112    this->functorType = Executor_Objective;
     113    this->functionPointer = functionPointer;
     114  }
     115  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
     116  {
    115117
    116 //////////////////
    117 // CONSTRUCTORS //
    118 /////////////////
    119 //! creates a command that takes no parameters
    120 #define ExecutorConstructor0() \
    121   EXECUTOR(void EXECUTORINCLASS(*function)()) \
    122   : Executor(0) \
    123   { \
    124     this->functorType = EXECUTORTYPE; \
    125     this->fp.functionPointer_0 = function; \
     118    /* // THE VERY COOL DEBUG
     119      printf("SUB[0] : %s\n", sub[0].c_str());
     120      printf("getDefault<type0>(this->defaultValue, 0):::: %d\n", getDefault<type0>(this->defaultValue, 0));
     121      printf("VALUE: %d\n", fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)));
     122    */
     123    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
     124      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)) );
     125  };
     126
     127  virtual Executor* clone() const {};
     128};
     129
     130/// DOUBLE PENETRATION
     131template<class T, typename type0, typename type1> class __EXECUTOR_FUNCTIONAL_NAME(2) : public Executor
     132{
     133private:
     134  void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST;
     135
     136public:
     137  __EXECUTOR_FUNCTIONAL_NAME(2) (void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(type0, type1) __EXECUTOR_FUNCTIONAL_CONST)
     138      : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>())
     139  {
     140    this->functorType = Executor_Objective;
     141    this->functionPointer = functionPointer;
    126142  }
     143  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
     144  {
     145    (__EXECUTOR_FUNCTIONAL_FUNCTION_EXEC)(
     146      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
     147      fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)));
     148  };
    127149
    128 //! creates a command that takes one parameter
    129 #define ExecutorConstructor1(t1) \
    130   EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE)) \
    131   : Executor(t1##_PARAM) \
    132   { \
    133     this->functorType = EXECUTORTYPE; \
    134     this->fp.functionPointer_1_##t1 = function; \
    135   }
    136 
    137 //! creates a command that takes two parameters
    138 #define ExecutorConstructor2(t1,t2) \
    139   EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE)) \
    140   : Executor(t1##_PARAM, t2##_PARAM) \
    141   { \
    142     this->functorType = EXECUTORTYPE; \
    143     this->fp.functionPointer_2_##t1##_##t2 = function; \
    144   }
    145 
    146 //! creates a command that takes three parameter
    147 #define ExecutorConstructor3(t1,t2,t3) \
    148   EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE)) \
    149   : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM) \
    150   { \
    151     this->functorType = EXECUTORTYPE; \
    152     this->fp.functionPointer_3_##t1##_##t2##_##t3 = function; \
    153   }
    154 
    155 //! creates a command that takes four parameter
    156 #define ExecutorConstructor4(t1,t2,t3,t4) \
    157   EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE)) \
    158   : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM) \
    159   { \
    160     this->functorType = EXECUTORTYPE; \
    161     this->fp.functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \
    162   }
    163 
    164 //! creates a command that takes five parameter
    165 #define ExecutorConstructor5(t1,t2,t3,t4,t5) \
    166   EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE)) \
    167   : Executor(t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM) \
    168   { \
    169     this->functorType = EXECUTORTYPE; \
    170     this->fp.functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \
    171   }
    172 
    173 ///////////////
    174 // EXECUTION //
    175 ///////////////
    176 //! execute-macro for functions with no parameters
    177 #define ExecutorExecute0() \
    178   if (this->paramCount == 0) \
    179     EXECUTOREXECUTER(_0)()
    180 
    181 //! execute-macro for functions with one parameter
    182 #define ExecutorExecute1(t1) \
    183    else if (this->paramCount == 1 && this->defaultValue[0] == t1##_PARAM) \
    184     EXECUTOREXECUTER(_1_##t1)(t1##_FUNC(sub[0], t1##_DEFGRAB(0)))
    185 
    186 //! execute-macro for functions with two parameters
    187 #define ExecutorExecute2(t1,t2) \
    188    else if (this->paramCount == 2 && this->defaultValue[0] == t1##_PARAM && this->defaultValue[1] == t2##_PARAM) \
    189     EXECUTOREXECUTER(_2_##t1##_##t2)(t1##_FUNC(sub[0], t1##_DEFGRAB(0)), t2##_FUNC(sub[1], t2##_DEFGRAB(1)))
    190 
    191 //! execute-macro for functions with three parameters
    192 #define ExecutorExecute3(t1,t2,t3) \
    193    else if (this->paramCount == 3 && this->defaultValue[0] == t1##_PARAM && this->defaultValue[1] == t2##_PARAM && this->defaultValue[2] == t3##_PARAM) \
    194     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)))
    195 
    196 //! execute-macro for functions with four parameters
    197 #define ExecutorExecute4(t1,t2,t3,t4) \
    198    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) \
    199     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))) \
    200 
    201 
    202 //! execute-macro for functions with five parameters
    203 #define ExecutorExecute5(t1,t2,t3,t4,t5) \
    204    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) \
    205     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)))
     150  virtual Executor* clone() const {};
     151};
    206152
    207153
    208154
     155/// HACK !! THESE WILL BE RESET AGAIN !!
    209156
    210 /////////////////////
    211 // DYNAMIC FUNCTOR //
    212 /////////////////////
    213 #ifdef FUNCTOR_LIST
    214 #undef FUNCTOR_LIST
    215 #endif
    216 #ifdef EXECUTOR
    217 #undef EXECUTOR
    218 #endif
    219 #define EXECUTOR                       ExecutorObjective
    220 #ifdef EXECUTOREXECUTER
    221 #undef EXECUTOREXECUTER
    222 #endif
    223 #define EXECUTOREXECUTER(nameExt)      (dynamic_cast<T*>(object)->*(fp.functionPointer##nameExt))
    224 #ifdef EXECUTORINCLASS
    225 #undef EXECUTORINCLASS
    226 #endif
    227 #define EXECUTORINCLASS(FUNCTION)      (T::FUNCTION)
    228 #ifdef EXECUTORTYPE
    229 #undef EXECUTORTYPE
    230 #endif
    231 #define EXECUTORTYPE                   Executor_Objective
    232 //! keeps information about a Executor
    233 template<class T> class EXECUTOR : public Executor
     157template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)() __EXECUTOR_FUNCTIONAL_CONST)
    234158{
    235 public:
    236   EXECUTOR() : Executor() { };
    237   // COPY constuctor (virtual version)
    238   virtual Executor* clone () const
    239   {
    240     EXECUTOR<T>* executor = new EXECUTOR<T>();
    241     this->cloning(executor);
    242     executor->fp = this->fp;
    243     return executor;
    244   }
    245 
    246   //! FUNCTOR_LIST is the List of CommandConstructors
    247 #define FUNCTOR_LIST(x) ExecutorConstructor ## x
    248 #include "functor_list.h"
    249 #undef FUNCTOR_LIST
    250 
    251 private:
    252   //! FUNCTOR_LIST is the List of FunctionPointers
    253   union FunctionPointers {
    254 #define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
    255 #include "functor_list.h"
    256 #undef FUNCTOR_LIST
    257   } fp;
    258 
    259   virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
    260   {
    261     //! FUNCTOR_LIST is the List of Executive Functions
    262 #define FUNCTOR_LIST(x) ExecutorExecute ## x
    263 #include "functor_list.h"
    264 #undef FUNCTOR_LIST
    265 
    266   }
    267 };
     159  return new __EXECUTOR_FUNCTIONAL_NAME(0)<T>(functionPointer);
     160}
     161template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(bool) __EXECUTOR_FUNCTIONAL_CONST)
     162{
     163  return new __EXECUTOR_FUNCTIONAL_NAME(1)<T, bool>(functionPointer);
     164}
     165template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(int) __EXECUTOR_FUNCTIONAL_CONST)
     166{
     167  return new __EXECUTOR_FUNCTIONAL_NAME(1)<T, int>(functionPointer);
     168}
     169template<class T> Executor* createExecutor(void (__EXECUTOR_FUNCTIONAL_FUNCTION_POINTER)(bool, int) __EXECUTOR_FUNCTIONAL_CONST)
     170{
     171  return new __EXECUTOR_FUNCTIONAL_NAME(2)<T, bool, int>(functionPointer);
     172}
    268173
    269174
    270 ////////////////////
    271 // STATIC FUNCTOR //
    272 ////////////////////
    273 #ifdef FUNCTOR_LIST
    274 #undef FUNCTOR_LIST
    275 #endif
    276 #ifdef EXECUTOR
    277 #undef EXECUTOR
    278 #endif
    279 #define EXECUTOR                      ExecutorStatic
    280 #ifdef EXECUTOREXECUTER
    281 #undef EXECUTOREXECUTER
    282 #endif
    283 #define EXECUTOREXECUTER(nameExt)     fp.functionPointer##nameExt
    284 #ifdef EXECUTORINCLASS
    285 #undef EXECUTORINCLASS
    286 #endif
    287 #define EXECUTORINCLASS(FUNCTION)     (FUNCTION)
    288 #ifdef EXECUTORTYPE
    289 #undef EXECUTORTYPE
    290 #endif
    291 #define EXECUTORTYPE                   Executor_Static
    292 
    293 //! keeps information about a Executor, that points to a Static Function
    294 template<class T> class ExecutorStatic : public Executor
    295 {
    296 public:
    297   EXECUTOR() : Executor() { };
    298   // COPY constuctor
    299   virtual Executor* clone () const
    300   {
    301     EXECUTOR<T>* executor = new EXECUTOR<T>();
    302     this->cloning(executor);
    303     executor->fp = this->fp;
    304     return executor;
    305   }
    306 
    307   //! FUNCTOR_LIST is the List of CommandConstructors
    308 #define FUNCTOR_LIST(x) ExecutorConstructor ## x
    309 #include "functor_list.h"
    310 #undef FUNCTOR_LIST
    311 
    312 private:
    313   //! FUNCTOR_LIST is the List of FunctionPointers
    314   union FunctionPointers {
    315 #define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
    316 #include "functor_list.h"
    317 #undef FUNCTOR_LIST
    318   } fp;
    319 
    320 
    321   virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
    322   {
    323     //! FUNCTOR_LIST is the List of Executive Functions
    324 #define FUNCTOR_LIST(x) ExecutorExecute ## x
    325 #include "functor_list.h"
    326 #undef FUNCTOR_LIST
    327 
    328   }
    329 };
    330 
    331 #endif /* _EXECUTOR_H */
     175#undef __EXECUTOR_FUNCTIONAL_CONST
     176#undef __EXECUTOR_FUNCTIONAL_NAME
     177#undef __EXECUTOR_FUNCTIONAL_FUNCTION_EXEC
     178#undef __EXECUTOR_FUNCTIONAL_FUNCTION_POINTER
  • trunk/src/orxonox.cc

    r7715 r7716  
    417417
    418418
    419 template<typename type> MT_Type ExecutorParamType() { return MT_EXT1; };
    420 template<> MT_Type ExecutorParamType<bool>() { return MT_EXT1; };
    421 template<> MT_Type ExecutorParamType<int>() { return MT_INT; };
    422 template<> MT_Type ExecutorParamType<unsigned int>() { return MT_UINT; };
    423 template<> MT_Type ExecutorParamType<float>() { return MT_FLOAT; };
    424 template<> MT_Type ExecutorParamType<char>() { return MT_CHAR; };
    425 template<> MT_Type ExecutorParamType<const std::string&>() { return MT_STRING; };
    426 
    427 template<typename type> type fromString(const std::string& input, type defaultValue) {return defaultValue; };
    428 template<> bool fromString<bool>(const std::string& input, bool defaultValue) { return isBool(input, defaultValue); };
    429 template<> int fromString<int>(const std::string& input, int defaultValue) { return isInt(input, defaultValue); };
    430 template<> unsigned int fromString<unsigned int>(const std::string& input, unsigned int defaultValue) { return isInt(input, defaultValue); };
    431 template<> float fromString<float>(const std::string& input, float defaultValue) { return isFloat(input, defaultValue); };
    432 template<> char fromString<char>(const std::string& input, char defaultValue) { return isInt(input, defaultValue); };
    433 template<> const std::string& fromString<const std::string&>(const std::string& input, const std::string& defaultValue) { return isString(input, defaultValue); };
    434 
    435 template<typename type> type getDefault(const MultiType* const defaultValues, unsigned int i) { return (type)0; };
    436 template<> bool getDefault<bool>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getBool(); };
    437 template<> int getDefault<int>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getInt(); };
    438 template<> unsigned int getDefault<unsigned int>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getInt(); };
    439 template<> float getDefault<float>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getFloat(); };
    440 template<> char getDefault<char>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getChar(); };
    441 template<> std::string getDefault<std::string>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getString(); };
    442 
    443 
    444 template<class T> class Executor0Params : public Executor
    445 {
    446 private:
    447   void (T::*functionPointer)();
    448 
    449 public:
    450   Executor0Params (void (T::*functionPointer)())
    451       : Executor()
    452   {
    453     this->functorType = Executor_Objective;
    454     this->functionPointer = functionPointer;
    455   }
    456   virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
    457   {
    458     (dynamic_cast<T*>(object)->*functionPointer)();
    459   };
    460 
    461   Executor* clone() const {};
    462 };
    463 
    464 
    465 template<class T, typename type0> class Executor1Params : public Executor
    466 {
    467 private:
    468   void (T::*functionPointer)(type0);
    469 
    470 public:
    471   Executor1Params (void (T::*functionPointer)(type0))
    472       : Executor(ExecutorParamType<type0>())
    473   {
    474     this->functorType = Executor_Objective;
    475     this->functionPointer = functionPointer;
    476   }
    477   virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
    478   {
    479 
    480     /* // THE VERY COOL DEBUG
    481     printf("SUB[0] : %s\n", sub[0].c_str());
    482     printf("getDefault<type0>(this->defaultValue, 0):::: %d\n", getDefault<type0>(this->defaultValue, 0));
    483     printf("VALUE: %d\n", fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)));
    484     */
    485     (dynamic_cast<T*>(object)->*functionPointer)( fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)) );
    486   };
    487 
    488   virtual Executor* clone() const {};
    489 };
    490 
    491 /// DOUBLE PENETRATION
    492 template<class T, typename type0, typename type1> class Executor2Params : public Executor
    493 {
    494 private:
    495   void (T::*functionPointer)(type0, type1);
    496 
    497 public:
    498   Executor2Params (void (T::*functionPointer)(type0, type1))
    499       : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>())
    500   {
    501     this->functorType = Executor_Objective;
    502     this->functionPointer = functionPointer;
    503   }
    504   virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
    505   {
    506     (dynamic_cast<T*>(object)->*functionPointer)(
    507       fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
    508       fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)));
    509   };
    510 
    511   virtual Executor* clone() const {};
    512 };
    513 
    514 
    515 template<class T> Executor* createExecutor(void (T::*functionPointer)())
    516 {
    517   return new Executor0Params<T>(functionPointer);
    518 }
    519 template<class T> Executor* createExecutor(void (T::*functionPointer)(bool))
    520 {
    521   return new Executor1Params<T, bool>(functionPointer);
    522 }
    523 template<class T> Executor* createExecutor(void (T::*functionPointer)(int))
    524 {
    525   return new Executor1Params<T, int>(functionPointer);
    526 }
    527 template<class T> Executor* createExecutor(void (T::*functionPointer)(bool, int))
    528 {
    529   return new Executor2Params<T, bool, int>(functionPointer);
    530 }
    531 
     419#include "executor/executor_functional.h"
     420
     421#define EXECUTOR_FUNCTIONAL_USE_STATIC
     422#include "executor/executor_functional.h"
    532423
    533424class TestClass : public BaseObject
     
    540431  void printTestBool(bool b) { printf("%d\n", (int)b); };
    541432  void printTwoVals(bool b, int i) { printf ("%d %d\n", b, i); };
     433
     434  static void printStatic() { printf("STATIC\n");};
    542435};
     436
     437template<class T> Executor* createExecutorStatic(void (*functionPointer)())
     438 {
     439   return new Executor0Params_static<TestClass>(functionPointer);
     440 }
    543441
    544442void TEST()
     
    546444  TestClass test;
    547445  SubString testStrings("1, 2, 3", ",", SubString::WhiteSpaces, false);
    548   (*createExecutor(&TestClass::printTest))(&test, testStrings);
    549   (*createExecutor(&TestClass::printTestInt))(&test, testStrings);
    550   (*createExecutor(&TestClass::printTestBool))(&test, testStrings);
    551   (*createExecutor(&TestClass::printTwoVals))(&test, testStrings);
     446  (*createExecutor<TestClass>(&TestClass::printTest))(&test, testStrings);
     447  (*createExecutor<TestClass>(&TestClass::printTestInt))(&test, testStrings);
     448  (*createExecutor<TestClass>(&TestClass::printTestBool))(&test, testStrings);
     449  (*createExecutor<TestClass>(&TestClass::printTwoVals))(&test, testStrings);
     450  (*createExecutor<TestClass>(&TestClass::printStatic))(&test, testStrings);
    552451
    553452}
Note: See TracChangeset for help on using the changeset viewer.