Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5651 in orxonox.OLD for trunk/src/lib/util/executor


Ignore:
Timestamp:
Nov 20, 2005, 5:14:27 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: new (LoadParam && ExecutorSpecials)-Functionality that enables the Loading of XML-elements.
The new Executor really works out, and makes the understanding of loadParam way easier (given you won't read Executor :)

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/util/executor/executor_specials.h

    r5650 r5651  
    44 */
    55
    6 #ifndef _EXECUTOR_H
    7 #define _EXECUTOR_H
     6#ifndef _EXECUTOR_SPECIALS_H
     7#define _EXECUTOR_SPECIALS_H
    88
    9 #include "base_object.h"
     9#include "executor.h"
    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#include "tinyxml.h"
     12// FORWARD DECLARATION
    1513
    16 #include <stdarg.h>
    17 
    18 // FORWARD DECLARATION
    19 template<class T> class tList;
    20 
    21 //! an enumerator for the definition of the Type.
    22 typedef enum {
    23   Executor_Objective   = 1,
    24   Executor_Static      = 2,
    25 } Executor_Type;
    26 
    27 ////////////////
    28 // BASE CLASS //
    29 ////////////////
    30 //! a baseClass for all possible Executors
    31 class Executor: public BaseObject
     14//! Executes a Function with a const TiXmlElement* parameter.
     15/**
     16 * This is a Special Executor, that finds ParamName in root as XML-sub-element
     17 * This is especially usefull, if you have to Load a SubElement from root in a new Function
     18 * What must be defined is a XML-root to search the ParameterName under an  a Function to call.
     19 */
     20template<class T> class ExecutorXML : public Executor
    3221{
    3322  public:
    34     virtual ~Executor();
     23    /**
     24     * Constructor of a ExecutorXML
     25     * @param function a Function to call
     26     * @param root The XML root to search paramName under
     27     * @param paramName the ParameterName the search in root, and lookup the TiXmlElement from
     28     */
     29    ExecutorXML(void(T::*function)(const TiXmlElement*), const TiXmlElement* root, const char* paramName)
     30      : Executor(1)
     31    {
     32      PRINTF(4)("Loading %s from XML-element %p\n", paramName, root);
    3533
    36     virtual Executor* clone () const = 0;
     34      if (likely(root != NULL && paramName != NULL))
     35        this->element = root->FirstChildElement(paramName);
     36      else
     37        this->element = NULL;
    3738
    38     Executor* defaultValues(unsigned int count, ...);
    39     Executor* defaultValues(unsigned int count, va_list values);
     39      this->functionPointer = function;
     40    }
    4041
    41     /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes */
    42     virtual void execute (BaseObject* object, const char* parameters) = 0;
    43 
    44     /** @returns the Type of this Function (either static or objective) */
    45     inline Executor_Type getType() const { return this->functorType; };
    46     /** @returns the Count of Parameters this Executor takes */
    47     inline unsigned int getParamCount() const { return this->paramCount; };
    48 
    49     static void debug();
    50 
    51   protected:
    52     Executor();
    53     Executor(unsigned int paramCount, ...);
    54 
    55     void cloning(Executor* executor) const;
    56 
    57   protected:
    58     Executor_Type                    functorType;      //!< The type of Function we've got (either static or objective).
    59     unsigned int                     paramCount;       //!< the count of parameters.
    60     MultiType*                       defaultValue;     //!< Default Values.
    61 };
    62 
    63 ///////////////////////////////////////////////////
    64 ///////////////////////////////////////////////////
    65 
    66 /////////////////////////////////
    67 // MACRO DEFINITION EXTENSIONS //
    68 /////////////////////////////////
    69 //! where to chek for default BOOL values
    70 #define   l_BOOL_DEFGRAB(i)         this->defaultValue[i].getBool()
    71 //! where to chek for default INT values
    72 #define   l_INT_DEFGRAB(i)          this->defaultValue[i].getInt()
    73 //! where to chek for default UINT values
    74 #define   l_UINT_DEFGRAB(i)         (unsigned int)this->defaultValue[i].getInt()
    75 //! where to chek for default LONG values
    76 #define   l_LONG_DEFGRAB(i)         (long)this->defaultValue[i].getInt()
    77 //! where to chek for default FLOAT values
    78 #define   l_FLOAT_DEFGRAB(i)        this->defaultValue[i].getFloat()
    79 //! where to chek for default STRING values
    80 #define   l_STRING_DEFGRAB(i)       this->defaultValue[i].getString()
    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
    89 
    90 
    91 ///////////////////////
    92 // FUNCTION POINTERS //
    93 ///////////////////////
    94 #define ExecutorFunctionPoiter0() \
    95   void EXECUTORINCLASS(*functionPointer_0)();
    96 
    97 #define ExecutorFunctionPoiter1(t1) \
    98   void EXECUTORINCLASS(*functionPointer_1_##t1)(t1##_TYPE);
    99 
    100 #define ExecutorFunctionPoiter2(t1, t2) \
    101   void EXECUTORINCLASS(*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE);
    102 
    103 
    104 #define ExecutorFunctionPoiter3(t1, t2, t3) \
    105   void EXECUTORINCLASS(*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE);
    106 
    107 #define ExecutorFunctionPoiter4(t1, t2, t3, t4) \
    108   void EXECUTORINCLASS(*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE);
    109 
    110 
    111 #define ExecutorFunctionPoiter5(t1, t2, t3, t4, t5) \
    112   void EXECUTORINCLASS(*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); \
    113 
    114 
    115 //////////////////
    116 // CONSTRUCTORS //
    117 /////////////////
    118 //! creates a command that takes no parameters
    119 #define ExecutorConstructor0() \
    120   EXECUTOR(void EXECUTORINCLASS(*function)()) \
    121   : Executor(0) \
    122   { \
    123     this->functorType = EXECUTORTYPE; \
    124     this->fp.functionPointer_0 = function; \
    125   }
    126 
    127 //! creates a command that takes one parameter
    128 #define ExecutorConstructor1(t1) \
    129   EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE)) \
    130   : Executor(1, t1##_PARAM) \
    131   { \
    132     this->functorType = EXECUTORTYPE; \
    133     this->fp.functionPointer_1_##t1 = function; \
    134   }
    135 
    136 //! creates a command that takes two parameters
    137 #define ExecutorConstructor2(t1,t2) \
    138   EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE)) \
    139   : Executor(2, t1##_PARAM, t2##_PARAM) \
    140   { \
    141     this->functorType = EXECUTORTYPE; \
    142     this->fp.functionPointer_2_##t1##_##t2 = function; \
    143   }
    144 
    145 //! creates a command that takes three parameter
    146 #define ExecutorConstructor3(t1,t2,t3) \
    147   EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE)) \
    148   : Executor(3, t1##_PARAM, t2##_PARAM, t3##_PARAM) \
    149   { \
    150     this->functorType = EXECUTORTYPE; \
    151     this->fp.functionPointer_3_##t1##_##t2##_##t3 = function; \
    152   }
    153 
    154 //! creates a command that takes four parameter
    155 #define ExecutorConstructor4(t1,t2,t3,t4) \
    156   EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE)) \
    157   : Executor(4, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM) \
    158   { \
    159     this->functorType = EXECUTORTYPE; \
    160     this->fp.functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \
    161   }
    162 
    163 //! creates a command that takes five parameter
    164 #define ExecutorConstructor5(t1,t2,t3,t4,t5) \
    165   EXECUTOR(void EXECUTORINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE)) \
    166   : Executor(5, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM) \
    167   { \
    168     this->functorType = EXECUTORTYPE; \
    169     this->fp.functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \
    170   }
    171 
    172 ///////////////
    173 // EXECUTION //
    174 ///////////////
    175 //! execute-macro for functions with no parameters
    176 #define ExecutorExecute0() \
    177   if (this->paramCount == 0) \
    178     EXECUTOREXECUTER(_0)()
    179 
    180 //! execute-macro for functions with one parameter
    181 #define ExecutorExecute1(t1) \
    182    else if (this->paramCount == 1 && this->defaultValue[0].getType() == t1##_PARAM) \
    183     EXECUTOREXECUTER(_1_##t1)(t1##_FUNC(parameters, t1##_DEFGRAB(0)))
    184 
    185 //! execute-macro for functions with two parameters
    186 #define ExecutorExecute2(t1,t2) \
    187    else if (this->paramCount == 2 && this->defaultValue[0].getType() == t1##_PARAM && this->defaultValue[1].getType() == t2##_PARAM) \
    188     EXECUTOREXECUTER(_2_##t1##_##t2)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)))
    189 
    190 //! execute-macro for functions with three parameters
    191 #define ExecutorExecute3(t1,t2,t3) \
    192    else if (this->paramCount == 3 && this->defaultValue[0].getType() == t1##_PARAM && this->defaultValue[1].getType() == t2##_PARAM && this->defaultValue[2].getType() == t3##_PARAM) \
    193     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)))
    194 
    195 //! execute-macro for functions with four parameters
    196 #define ExecutorExecute4(t1,t2,t3,t4) \
    197    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) \
    198     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)))
    199 
    200 //! execute-macro for functions with five parameters
    201 #define ExecutorExecute5(t1,t2,t3,t4,t5) \
    202    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) \
    203     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)))
    204 
    205 
    206 
    207 
    208 
    209 //////////\//////////
    210 // DYNAMIC FUNCTOR //
    211 ///////////\/////////
    212 #ifdef FUNCTOR_LIST
    213 #undef FUNCTOR_LIST
    214 #endif
    215 #ifdef EXECUTOR
    216 #undef EXECUTOR
    217 #endif
    218 #define EXECUTOR                       ExecutorObjective
    219 #ifdef EXECUTOREXECUTER
    220 #undef EXECUTOREXECUTER
    221 #endif
    222 #define EXECUTOREXECUTER(nameExt)      (dynamic_cast<T*>(object)->*(fp.functionPointer##nameExt))
    223 #ifdef EXECUTORINCLASS
    224 #undef EXECUTORINCLASS
    225 #endif
    226 #define EXECUTORINCLASS(FUNCTION)      (T::FUNCTION)
    227 #ifdef EXECUTORTYPE
    228 #undef EXECUTORTYPE
    229 #endif
    230 #define EXECUTORTYPE                   Executor_Objective
    231 //! keeps information about a Executor
    232 template<class T> class ExecutorObjective : public Executor
    233 {
    234   public:
    235     ExecutorObjective() : Executor() { };
    236     // COPY constuctor (virtual version)
     42    /**
     43     * clones an ExecutorXML, used to copy this Element.
     44     * @returns a _new_ Copy of this Executor
     45     */
    23746    virtual Executor* clone () const
    23847    {
    239       ExecutorObjective<T>* executor = new ExecutorObjective<T>();
     48      ExecutorXML<T>* executor = new ExecutorXML<T>();
    24049      this->cloning(executor);
    241       executor->fp = this->fp;
     50      executor->functionPointer = this->functionPointer;
     51      executor->element = this->element;
    24252      return executor;
    24353    }
    24454
    245 //! FUNCTOR_LIST is the List of CommandConstructors
    246 #define FUNCTOR_LIST(x) ExecutorConstructor ## x
    247 #include "functor_list.h"
    248 #undef FUNCTOR_LIST
     55    /**
     56     * executes the Command on BaseObject
     57     * @param object the BaseObject to execute this Executor on
     58     * @param loadString ignored in this case
     59     */
     60    virtual void execute(BaseObject* object, const char* loadString)
     61    {
     62      if (object != NULL && this->element != NULL)
     63        (dynamic_cast<T*>(object)->*(functionPointer))(this->element);
     64    }
    24965
    25066  private:
    251 //! FUNCTOR_LIST is the List of FunctionPointers
    252     union FunctionPointers {
    253 #define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
    254 #include "functor_list.h"
    255 #undef FUNCTOR_LIST
    256     } fp;
     67    /**
     68     * used for the copy-constructor
     69     */
     70    ExecutorXML() : Executor() { };
    25771
    258     virtual void execute (BaseObject* object, const char* parameters)
    259     {
    260       SubString sub(parameters, true);
    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     }
     72
     73  private:
     74    void    (T::*functionPointer)(const TiXmlElement*);  //!< The functionPointer to the function to be called
     75    const   TiXmlElement* element;                       //!< The XML-element to call.
    26676};
    26777
    268 
    269 ////////////////////
    270 // STATIC FUNCTOR //
    271 ////////////////////
    272 #ifdef FUNCTOR_LIST
    273 #undef FUNCTOR_LIST
    274 #endif
    275 #ifdef EXECUTOR
    276 #undef EXECUTOR
    277 #endif
    278 #define EXECUTOR                      ExecutorStatic
    279 #ifdef EXECUTOREXECUTER
    280 #undef EXECUTOREXECUTER
    281 #endif
    282 #define EXECUTOREXECUTER(nameExt)     fp.functionPointer##nameExt
    283 #ifdef EXECUTORINCLASS
    284 #undef EXECUTORINCLASS
    285 #endif
    286 #define EXECUTORINCLASS(FUNCTION)     (FUNCTION)
    287 #ifdef EXECUTORTYPE
    288 #undef EXECUTORTYPE
    289 #endif
    290 #define EXECUTORTYPE                   Executor_Static
    291 
    292 //! keeps information about a Executor, that points to a Static Function
    293 template<class T> class ExecutorStatic : public Executor
    294 {
    295   public:
    296     ExecutorStatic() : Executor() { };
    297     // COPY constuctor
    298     virtual Executor* clone () const
    299     {
    300       ExecutorStatic<T>* executor = new ExecutorStatic<T>();
    301       this->cloning(executor);
    302       executor->fp = this->fp;
    303       return executor;
    304     }
    305 
    306 //! FUNCTOR_LIST is the List of CommandConstructors
    307 #define FUNCTOR_LIST(x) ExecutorConstructor ## x
    308 #include "functor_list.h"
    309 #undef FUNCTOR_LIST
    310 
    311   private:
    312 //! FUNCTOR_LIST is the List of FunctionPointers
    313     union FunctionPointers {
    314 #define FUNCTOR_LIST(x) ExecutorFunctionPoiter ## x
    315 #include "functor_list.h"
    316 #undef FUNCTOR_LIST
    317     } fp;
    318 
    319 
    320     virtual void execute (BaseObject* object, const char* parameters)
    321     {
    322   SubString sub(parameters, true);
    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 #endif /* _EXECUTOR_H */
     78#endif /* _EXECUTOR_SPECIALS_H */
Note: See TracChangeset for help on using the changeset viewer.