Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5332 in orxonox.OLD for trunk


Ignore:
Timestamp:
Oct 9, 2005, 12:02:13 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: minor cleanup (speed-issue) in LoadClassDescription, using enum insted of String, faster, more reliable

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/defs/functor_list.h

    r5331 r5332  
    4242  ParameterFloat   = 32,
    4343  ParameterLong    = 64,
     44  ParameterXML     = 128,
    4445  /* ... */
    4546} ParameterType;
  • trunk/src/orxonox.cc

    r5303 r5332  
    4949
    5050#include "class_list.h"
    51 #include "substring.h"
    52 #include "shell.h"
    5351#include "shell_command.h"
    5452#include "shell_buffer.h"
     
    9593  FastFactory::deleteAll();
    9694  ShellCommandClass::unregisterAllCommands();
     95
    9796  LoadClassDescription::deleteAllDescriptions();
    9897
  • trunk/src/util/loading/load_param.cc

    r5227 r5332  
    1313   co-programmer: ...
    1414*/
     15
     16#include "functor_list.h"
    1517
    1618#include "load_param.h"
     
    6466
    6567      this->paramDesc->paramCount = paramCount;
    66       this->paramDesc->types = new char*[paramCount];
     68      this->paramDesc->types = new int[paramCount];
    6769      this->paramDesc->defaultValues = new char*[paramCount];
    6870
     
    7375      {
    7476          // parameters parsed
    75         const char* tmpTypeName = va_arg (types, const char*);
    76         this->paramDesc->types[i] = new char[strlen(tmpTypeName)+1];
    77         strcpy(this->paramDesc->types[i], tmpTypeName);
    78 
    79         //! @todo SWITCH CASE WITH l_[TYPE]_TYPE -> much faster
    80           // default value description
    81         if (!strcmp(tmpTypeName, l_INT_NAME))
     77        int tmpType = va_arg (types, int);
     78        this->paramDesc->types[i] = tmpType;
     79
     80        //! @todo SWITCH CASE WITH l_[TYPE]_TYPE -> much faster (more usefull)
     81        switch (tmpType)
    8282        {
    83           sprintf(defaultVal, "%d", va_arg(types, l_INT_TYPE));
    84         }
    85         else if (!strcmp(tmpTypeName, l_LONG_NAME))
    86         {
    87           sprintf(defaultVal, "%0.3f", va_arg(types, l_LONG_TYPE));
    88         }
    89           /*          else if (!strcmp(tmpTypeName, l_SHORT_NAME))
    90         {
    91         sprintf(defaultVal, "%d", va_arg(types, l_SHORT_TYPE));
    92       }*/
    93         else if (!strcmp(tmpTypeName, l_FLOAT_NAME))
    94         {
    95           sprintf(defaultVal, "%0.3f", va_arg(types, double));
    96         }
    97         else if (!strcmp(tmpTypeName, l_STRING_NAME))
    98         {
    99           sprintf(defaultVal, "%s", va_arg(types, l_STRING_TYPE));
    100         }
    101         else if (!strcmp(tmpTypeName, l_XML_ELEM_NAME))
    102         {
    103           sprintf(defaultVal, "");
     83          case ParameterInt:
     84            sprintf(defaultVal, "%d", va_arg(types, l_INT_TYPE));
     85            break;
     86          case ParameterLong:
     87            sprintf(defaultVal, "%0.3f", va_arg(types, l_LONG_TYPE));
     88            break;
     89          case ParameterFloat:
     90            sprintf(defaultVal, "%0.3f", va_arg(types, double));
     91            break;
     92          case ParameterString:
     93            sprintf(defaultVal, "%s", va_arg(types, l_STRING_TYPE));
     94            break;
     95          case ParameterXML:
     96            sprintf(defaultVal, "");
     97            break;
    10498        }
    10599
     
    144138LoadParamDescription::~LoadParamDescription()
    145139{
    146   if (this->types != NULL)
    147   {
    148     for(int i = 0; i < this->paramCount; i++)
    149     {
    150       delete []this->types[i];
    151     }
    152   }
    153140  if (this->defaultValues != NULL)
    154141  {
     
    184171      if (i > 0)
    185172        PRINT(3)(",");
    186       PRINT(3)("%s", this->types[i]);
     173      switch (this->types[i])
     174      {
     175        default:
     176          PRINTF(3)("none");
     177          break;
     178        case ParameterBool:
     179          PRINT(3)("bool");
     180          break;
     181        case ParameterChar:
     182          PRINT(3)("char");
     183          break;
     184        case ParameterString:
     185          PRINT(3)("string");
     186          break;
     187        case ParameterInt:
     188          PRINT(3)("int");
     189          break;
     190        case ParameterUInt:
     191          PRINT(3)("Uint");
     192          break;
     193        case ParameterFloat:
     194          PRINT(3)("float");
     195          break;
     196        case ParameterLong:
     197          PRINT(3)("long");
     198          break;
     199        case ParameterXML:
     200          PRINT(3)("XML");
     201          break;
     202      }
    187203    }
    188204  PRINT(3)("</%s>", this->paramName);
     
    197213      if (i > 0)
    198214        PRINT(3)(", ");
    199       if (!strcmp(this->types[i], l_STRING_NAME))
     215      if (this->types[i] & ParameterString)
    200216      { // leave brackets !!
    201217        PRINT(3)("\"%s\"", this->defaultValues[i]);
  • trunk/src/util/loading/load_param.h

    r5301 r5332  
    2222#define _LOAD_PARAM_H
    2323
     24#include "functor_list.h"
    2425#include "base_object.h"
    2526
     
    3334template<class T> class tList;
    3435
     36/************************
     37*** DESCRIPTION STUFF ***
     38************************/
     39//! A class that handles the description of loadable parameters
     40class LoadParamDescription
     41{
     42  friend class BaseLoadParam;
     43  friend class LoadClassDescription;
     44 public:
     45  LoadParamDescription(const char* paramName);
     46  ~LoadParamDescription();
     47
     48  void setDescription(const char* descriptionText);
     49  /** @returns the descriptionString */
     50  const char* getDescription() { return this->description; };
     51
     52  void print() const;
     53 private:
     54  char*         paramName;             //!< The name of the parameter.
     55  int           paramCount;            //!< The count of parameters.
     56  int*          types;                 //!< What kind of parameters does this function take ??
     57  char*         description;           //!< A longer description about this function.
     58  char**        defaultValues;         //!< The 'Default Values'.
     59};
     60
     61//! A class for descriptions of a loadable module
     62class LoadClassDescription
     63{
     64  friend class BaseLoadParam;
     65 public:
     66  LoadClassDescription(const char* className);
     67  ~LoadClassDescription();
     68
     69  static LoadClassDescription* addClass(const char* className);
     70  LoadParamDescription* addParam(const char* paramName);
     71
     72  static void deleteAllDescriptions();
     73
     74  static void printAll(const char* fileName = NULL);
     75  static tList<const char>* searchClassWithShort(const char* classNameBegin);
     76//  static const LoadParamDescription* getClass(const char* className);
     77
     78 private:
     79  static bool                          parametersDescription;  //!< if parameter-description should be enabled.
     80  static tList<LoadClassDescription>*  classList;              //!< a list, that holds all the loadable classes. (after one instance has been loaded)
     81  char*                                className;              //!< name of the class
     82  tList<LoadParamDescription>*         paramList;              //!< List of parameters this class knows.
     83};
     84
     85
     86/**************************
     87**** REAL DECLARATIONS ****
     88**************************/
     89//! abstract Base class for a Loadable parameter
     90class BaseLoadParam : public BaseObject
     91{
     92 public:
     93  BaseLoadParam* describe(const char* descriptionText);
     94
     95 protected:
     96  BaseLoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName, int paramCount, bool multi, const void* pointerToParam, ...);
     97
     98 protected:
     99  LoadClassDescription*    classDesc;            //!< The LoadClassDescription of this LoadParameter
     100  LoadParamDescription*    paramDesc;            //!< The LoadParameterDescription of this LoadParameter
     101  const char*              loadString;           //!< The string loaded by this LoadParam
     102  const void*              pointerToParam;       //!< A Pointer to a Parameter.
     103};
     104
     105
    35106//! macro that makes it even more easy to load a Parameter
    36107/**
     
    51122                                 element = root->FirstChildElement(); \
    52123                                 while( element != NULL) \
    53                                   {
    54 /**
    55  * closes a LoadParam Loop
    56  * @see LOAD_PARAM_START_CYCLE
     124{
     125/**
     126   * closes a LoadParam Loop
     127   * @see LOAD_PARAM_START_CYCLE
    57128 */
    58129#define LOAD_PARAM_END_CYCLE        element = element->NextSiblingElement(); \
    59                                   }
     130}
    60131
    61132
     
    65136// 0. TYPES
    66137/**
    67  *  a Macro to easily implement many different Constructors for the LoadParam-Class with no argument
     138 * a Macro to easily implement many different Constructors for the LoadParam-Class with no argument
    68139 */
    69140#define LoadParam0() \
     
    79150// 1. TYPE
    80151/**
    81  *  a Macro to easily implement many different Constructors for the LoadParam-Class with 1 argument
    82  * @param type1 The type of the first functionParameter
    83 */
     152 * a Macro to easily implement many different Constructors for the LoadParam-Class with 1 argument
     153 * @param type1 The type of the first functionParameter
     154 */
    84155#define LoadParam1(type1) \
    85156 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), \
    86157           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT) \
    87   : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, type1##_NAME, default1) \
    88     { \
     158  : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, type1##_PARAM, default1) \
     159{ \
    89160      if (loadString != NULL && root != NULL) \
    90161        (*pt2Object.*function)(type1##_FUNC(loadString, default1)); \
    91162      else \
    92163        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
    93     }
     164}
    94165
    95166// 2. TYPES
    96167/**
    97  *  a Macro to easily implement many different Constructors for the LoadParam-Class with 2 arguments
     168 * a Macro to easily implement many different Constructors for the LoadParam-Class with 2 arguments
    98169 * @param type1 The type of the first functionParameter
    99170 * @param type2 The type of the second functionParameter
    100 */
     171 */
    101172#define LoadParam2(type1, type2) \
    102173 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE), \
    103174           bool multi = false,  type1##_TYPE default1 = type1##_DEFAULT,  type2##_TYPE default2 = type2##_DEFAULT) \
    104   : BaseLoadParam(root, pt2Object, paramName, 2, multi, NULL, type1##_NAME, default1, type2##_NAME, default2) \
    105     { \
    106       if (loadString != NULL && root != NULL) \
    107         { \
     175  : BaseLoadParam(root, pt2Object, paramName, 2, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2) \
     176{ \
     177      if (loadString != NULL && root != NULL) \
     178{ \
    108179          SubString subLoads(loadString); \
    109180          if (subLoads.getCount() == 2) \
     
    112183            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
    113184                      paramName, pt2Object->getClassName(), 2, subLoads.getCount()); \
    114         } \
    115       else \
    116         PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
    117     }
     185} \
     186      else \
     187        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
     188}
    118189
    119190
    120191// 3. TYPES
    121192/**
    122  *  a Macro to easily implement many different Constructors for the LoadParam-Class with 3 arguments
     193 * a Macro to easily implement many different Constructors for the LoadParam-Class with 3 arguments
    123194 * @param type1 The type of the first functionParameter
    124195 * @param type2 The type of the second functionParameter
    125196 * @param type3 The type of the third functionParameter
    126 */
     197 */
    127198#define LoadParam3(type1, type2, type3) \
    128199 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE), \
    129200           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT)\
    130   : BaseLoadParam(root, pt2Object, paramName, 3, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3) \
    131     { \
    132       if (loadString != NULL && root != NULL) \
    133         { \
     201  : BaseLoadParam(root, pt2Object, paramName, 3, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3) \
     202{ \
     203      if (loadString != NULL && root != NULL) \
     204{ \
    134205          SubString subLoads(loadString); \
    135206          if (subLoads.getCount() == 3) \
     
    138209            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
    139210                      paramName, pt2Object->getClassName(), 3, subLoads.getCount()); \
    140         } \
    141       else \
    142         PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
    143     }
     211} \
     212      else \
     213        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
     214}
    144215
    145216
    146217// 4. TYPES
    147218/**
    148  *  a Macro to easily implement many different Constructors for the LoadParam-Class with 4 arguments
     219 * a Macro to easily implement many different Constructors for the LoadParam-Class with 4 arguments
    149220 * @param type1 The type of the first functionParameter
    150221 * @param type2 The type of the second functionParameter
    151222 * @param type3 The type of the third functionParameter
    152223 * @param type4 The type of the forth functionParameter
    153 */
     224 */
    154225#define LoadParam4(type1, type2, type3, type4) \
    155226 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE), \
    156227           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \
    157228           type4##_TYPE default4 = type4##_DEFAULT) \
    158   : BaseLoadParam(root, pt2Object, paramName, 4, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3, \
    159                   type4##_NAME, default4) \
    160     { \
    161       if (loadString != NULL && root != NULL) \
    162         { \
     229  : BaseLoadParam(root, pt2Object, paramName, 4, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3, \
     230                  type4##_PARAM, default4) \
     231{ \
     232      if (loadString != NULL && root != NULL) \
     233{ \
    163234          SubString subLoads(loadString); \
    164235          if (subLoads.getCount() == 4) \
     
    167238            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
    168239                      paramName, pt2Object->getClassName(), 4, subLoads.getCount()); \
    169         } \
    170       else \
    171         PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
    172     }
     240} \
     241      else \
     242        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
     243}
    173244
    174245
    175246// 5. TYPES
    176247/**
    177  *  a Macro to easily implement many different Constructors for the LoadParam-Class with 5 arguments
     248 * a Macro to easily implement many different Constructors for the LoadParam-Class with 5 arguments
    178249 * @param type1 The type of the first functionParameter
    179250 * @param type2 The type of the second functionParameter
     
    181252 * @param type4 The type of the forth functionParameter
    182253 * @param type5 The type of the fifth functionParameter
    183 */
     254 */
    184255#define LoadParam5(type1, type2, type3, type4, type5) \
    185256 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, \
     
    187258           bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \
    188259           type4##_TYPE default4 = type4##_DEFAULT, type5##_TYPE default5 = type5##_DEFAULT ) \
    189   : BaseLoadParam(root, pt2Object, paramName, 5, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3, \
    190                   type4##_NAME, default4, type5##_NAME, default5) \
    191     { \
    192       if (loadString != NULL && root != NULL) \
    193         { \
     260  : BaseLoadParam(root, pt2Object, paramName, 5, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3, \
     261                  type4##_PARAM, default4, type5##_PARAM, default5) \
     262{ \
     263      if (loadString != NULL && root != NULL) \
     264{ \
    194265          SubString subLoads(loadString); \
    195266          if (subLoads.getCount() == 5) \
     
    198269            PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \
    199270                      paramName, pt2Object->getClassName(), 5, subLoads.getCount()); \
    200         } \
    201       else \
    202         PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
    203     }
     271} \
     272      else \
     273        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
     274}
    204275
    205276// Pointer TYPE
    206277/**
    207  *  a Macro to easily implement many different Constructors for the LoadParam-Class with one Pointer argument
     278 * a Macro to easily implement many different Constructors for the LoadParam-Class with one Pointer argument
    208279 * @param type1 The type of the Pointer
    209280 */
    210281#define LoadParamPT(type1) \
    211282 LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), type1##_TYPE pointerToParam, bool multi = false) \
    212   : BaseLoadParam(root, pt2Object, paramName, 1, multi, pointerToParam, type1##_NAME) \
     283  : BaseLoadParam(root, pt2Object, paramName, 1, multi, pointerToParam, type1##_PARAM) \
    213284{ \
    214285      if (pointerToParam != NULL && root != NULL) \
     
    217288        PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \
    218289}
    219 
    220 /************************
    221 *** DESCRIPTION STUFF ***
    222 ************************/
    223 //! A class that handles the description of loadable parameters
    224 class LoadParamDescription
    225 {
    226   friend class BaseLoadParam;
    227   friend class LoadClassDescription;
    228  public:
    229   LoadParamDescription(const char* paramName);
    230   ~LoadParamDescription();
    231 
    232   void setDescription(const char* descriptionText);
    233   /** @returns the descriptionString */
    234   const char* getDescription() { return this->description; };
    235 
    236   void print() const;
    237  private:
    238   char*         paramName;             //!< The name of the parameter.
    239   int           paramCount;            //!< The count of parameters.
    240   char**        types;                 //!< What kind of parameters does this function take ??
    241   char*         description;           //!< A longer description about this function.
    242   char**        defaultValues;         //!< The 'Default Values'.
    243 };
    244 
    245 //! A class for descriptions of a loadable module
    246 class LoadClassDescription
    247 {
    248   friend class BaseLoadParam;
    249  public:
    250   LoadClassDescription(const char* className);
    251   ~LoadClassDescription();
    252 
    253   static LoadClassDescription* addClass(const char* className);
    254   LoadParamDescription* addParam(const char* paramName);
    255 
    256   static void deleteAllDescriptions();
    257 
    258   static void printAll(const char* fileName = NULL);
    259   static tList<const char>* searchClassWithShort(const char* classNameBegin);
    260 //  static const LoadParamDescription* getClass(const char* className);
    261 
    262  private:
    263   static bool                          parametersDescription;  //!< if parameter-description should be enabled.
    264   static tList<LoadClassDescription>*  classList;              //!< a list, that holds all the loadable classes. (after one instance has been loaded)
    265   char*                                className;              //!< name of the class
    266   tList<LoadParamDescription>*         paramList;              //!< List of parameters this class knows.
    267 };
    268 
    269 
    270 /**************************
    271 **** REAL DECLARATIONS ****
    272 **************************/
    273 //! abstract Base class for a Loadable parameter
    274 class BaseLoadParam : public BaseObject
    275 {
    276  public:
    277   BaseLoadParam* describe(const char* descriptionText);
    278 
    279  protected:
    280   BaseLoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName, int paramCount, bool multi, const void* pointerToParam, ...);
    281 
    282  protected:
    283   LoadClassDescription*    classDesc;            //!< The LoadClassDescription of this LoadParameter
    284   LoadParamDescription*    paramDesc;            //!< The LoadParameterDescription of this LoadParameter
    285   const char*              loadString;           //!< The string loaded by this LoadParam
    286   const void*              pointerToParam;       //!< A Pointer to a Parameter.
    287 };
    288 
    289290
    290291//! derived template class, so all the Classes can load something.
Note: See TracChangeset for help on using the changeset viewer.