Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9675 in orxonox.OLD for trunk/src/lib


Ignore:
Timestamp:
Aug 21, 2006, 10:57:35 PM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: more Implementation of Functionality.
Introduced map for fast search of ID/Name pairs.
This will also be used to speedup the Factory create-process

Location:
trunk/src/lib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/lang/new_object_list.cc

    r9671 r9675  
    2323    : _id(-1), _name(className)
    2424{
    25   if (NewObjectListBase::_classes == NULL)
    26     NewObjectListBase::_classes = new cSet;
     25  if (NewObjectListBase::_classesByID == NULL)
     26  {
     27    NewObjectListBase::_classesByID = new classIDMap;
     28    assert (NewObjectListBase::_classesByName == NULL);
     29    NewObjectListBase::_classesByName = new classNameMap;
     30  }
     31  assert(!NewObjectListBase::classNameExists(className) && "Classes should only be included once, and no two classes should have the same name/ID (key value)");
    2732
    28 
    29   assert(!NewObjectListBase::classNameExists(className) && "Classes should not be included once, and no two classes should have the same name (key value)");
    3033
    3134  this->_id = NewObjectListBase::_idCounter++;
    3235
     36  (*NewObjectListBase::_classesByID)[this->_id] = this;
     37  (*NewObjectListBase::_classesByName)[this->_name] = this;
    3338}
    3439
    3540NewObjectListBase::~NewObjectListBase()
    3641{
     42  NewObjectListBase::_classesByName->erase(this->_name);
     43  NewObjectListBase::_classesByID->erase(this->_id);
    3744
     45  if (NewObjectListBase::_classesByID->empty())
     46  {
     47    delete NewObjectListBase::_classesByID;
     48    NewObjectListBase::_classesByID = NULL;
     49    assert(NewObjectListBase::_classesByName != NULL);
     50    delete NewObjectListBase::_classesByName;
     51    NewObjectListBase::_classesByName = NULL;
     52  }
    3853}
    3954
    4055int NewObjectListBase::_idCounter = 0;
    41 NewObjectListBase::cSet* NewObjectListBase::_classes = NULL;
     56NewObjectListBase::classIDMap* NewObjectListBase::_classesByID = NULL;
     57NewObjectListBase::classNameMap* NewObjectListBase::_classesByName = NULL;
    4258
    4359
     
    5066bool NewObjectListBase::classNameExists(const std::string& name)
    5167{
    52   cSet::iterator it;
    53   for (it = NewObjectListBase::_classes->begin(); it != NewObjectListBase::_classes->end(); it++)
    54     if(*it != NULL && (*it)->name() != name)
    55       return true;
    56   return false;
     68  return (NewObjectListBase::_classesByName->find(name) != NewObjectListBase::_classesByName->end());
     69  //  classNameMap::iterator it;
     70  //   for (it = NewObjectListBase::_classesByID->begin(); it != NewObjectListBase::_classesByID->end(); it++)
     71  //     if(*it != NULL && (*it)->name() != name)
     72  //       return true;
     73  //   return false;
    5774}
     75
     76
     77/**
     78 * @brief Converts an ID into a ClassName String.
     79 * @param classID The ID to convert.
     80 * @return The ClassName or an empty string if the ID was not found.
     81 */
     82const std::string& NewObjectListBase::IDToString(int classID)
     83{
     84  assert (NewObjectListBase::_classesByID != NULL);
     85  NewObjectListBase::classIDMap::iterator it = NewObjectListBase::_classesByID->find(classID);
     86  if (it != NewObjectListBase::_classesByID->end())
     87    return (*it).second->name();
     88  else
     89  {
     90    static std::string empty;
     91    return empty;
     92  }
     93}
     94
     95
     96/**
     97 * @brief Converts a String into an ID
     98 * @param className the Name of the Class to search for
     99 * @return The Classes ID if found, -1 otherwise.
     100 */
     101int NewObjectListBase::StringToID(const std::string& className)
     102{
     103  assert (NewObjectListBase::_classesByName != NULL);
     104  NewObjectListBase::classNameMap::iterator it = NewObjectListBase::_classesByName->find(className);
     105  if (it != NewObjectListBase::_classesByName->end())
     106    return (*it).second->id();
     107  else
     108    return -1;
     109}
     110
  • trunk/src/lib/lang/new_object_list.h

    r9674 r9675  
    99
    1010#include "type_info.h"
    11 #include <set>
     11#include <map>
    1212#include <list>
    13 #include <vector>
    1413#include <string>
    1514
     
    3332
    3433public:
    35   int id() const { return _id; };
    36   const std::string& name() const { return _name; };
     34  inline int id() const { return _id; };
     35  inline const std::string& name() const { return _name; };
    3736  bool operator==(int id) const { return _id == id; };
    3837  bool operator==(const std::string& name) const { return _name == name; };
     
    4039
    4140  /// Comparing operators.
    42   bool compareName(const NewObjectListBase& more) const { return this->_name < more.name(); };
    43   bool compareID(const NewObjectListBase& more) const { return this->_id < more.id(); };
     41//   struct CompareID  {
     42//     bool operator()(const NewObjectListBase* less, const NewObjectListBase* more) { return less->id() < more->id(); };
     43//   };
     44//   struct CompareName{
     45//     bool operator()(const NewObjectListBase* less, const NewObjectListBase* more) { return less->name() < more->name(); };
     46//   };
    4447
    4548  virtual void debug() const = 0;
     
    6366
    6467protected:
    65   typedef std::set<NewObjectListBase*>     cSet;    //!< The Generic Set.
    66   typedef std::vector<NewObjectListBase*>  cVector; //!< The
     68  typedef std::map<int, NewObjectListBase*> classIDMap;    //!< The Generic Map.
     69  typedef std::map<std::string, NewObjectListBase*> classNameMap;//!< The Generic Map.
    6770
    6871  int                           _id;                //!< The ID of the class.
    6972  std::string                   _name;              //!< The Name of the Class.
    7073
    71   cVector                       _typeOfList;        //!< A List of all classes this class is derived of, and the class itself, ordered by age of addition.
    72   cSet                          _typeOfSet;         //!< A Set of all classes this is derived from and the class itself (for isA).
    7374private:
    7475
    7576  static int                    _idCounter;         //!< A counter, that gives all classes a Unique ClassID. Access to this Variable is to be Thread-Safe.
    76   static cSet*                  _classes;           //!< A Set of all the classes in existance.
     77  static classIDMap*            _classesByID;       //!< A Map of all the classes in existance.
     78  static classNameMap*          _classesByName;     //!< A Map of all the classes in existance.
    7779  static std::list<std::string> _classNames;        //!< A list of all the registered ClassNames.
    7880};
  • trunk/src/lib/lang/test_object_list.cc

    r9674 r9675  
    88{
    99public:
    10   void setName(const std::string& name) { this->_objectName = name; };
    11   const std::string& getName() const { return _objectName; };
    12   bool operator==(const std::string& name) const { return _objectName == name; };
     10  //   void setName(const std::string& name) { this->_objectName = name; };
     11     const std::string& getName() const { return _objectName; };
     12  //   bool operator==(const std::string& name) const { return _objectName == name; };
    1313
    14   NewObjectListDeclaration(NewBaseObject);
     14  //  NewObjectListDeclaration(NewBaseObject);
    1515
    1616protected:
    1717  NewBaseObject(const std::string& objectName = "") : _objectName(objectName)
    1818  {
    19     this->registerObject(this, objectList);
     19    //    this->registerObject(this, objectList);
    2020  };
    2121  template<class T>
     
    2727
    2828};
    29 NewObjectListDefinition(NewBaseObject);
     29//NewObjectListDefinition(NewBaseObject);
    3030
    3131
    32 class Test : public NewBaseObject
     32class Test :
     33      public   NewBaseObject
     34//    ,
     35//      public BaseObject
    3336{
    3437public:
     
    4447Test::Test()
    4548{
     49  this->registerObject(this, Test::objectList);
    4650//  this->setClassID(CL_PARENT_NODE, "Test");
    47   this->registerObject(this, Test::objectList);
    4851  // std::cout << "Test()\n";
    4952};
     
    5861  Bone()
    5962  {
    60 //    this->registerObject(this, Bone::objectList);
     63    //    this->registerObject(this, Bone::objectList);
    6164    //std::cout << "Bone()\n";
    6265  };
     
    7578    std::cout<< i << std::endl;
    7679    Test* test = new Test[10000];
    77     delete[]test;
     80    // delete[]test;
    7881  }
    7982  //   char tmp[100];
  • trunk/src/lib/util/loading/factory.cc

    r9406 r9675  
    2929 */
    3030Factory::Factory (const std::string& factoryName, ClassID classID)
    31   : classID(classID), className(factoryName)
     31    : classID(classID), className(factoryName)
    3232{
    3333  this->setClassID(CL_FACTORY, "Factory");
     
    8686bool Factory::operator==(const char* className) const
    8787{
    88   return(className != NULL && this->className == className);
     88  return (className != NULL && this->className == className);
    8989}
    9090
     
    9696bool Factory::operator==(const std::string& className) const
    9797{
    98   return(this->className == className);
     98  return (this->className == className);
    9999}
    100100
     
    105105 * @returns a new Object of Type root->Value() on match, NULL otherwise
    106106 */
    107  BaseObject* Factory::fabricate(const TiXmlElement* root)
     107BaseObject* Factory::fabricate(const TiXmlElement* root)
    108108{
    109   if (root == NULL || Factory::factoryList == NULL)
    110     return NULL;
     109  assert (root != NULL && Factory::factoryList != NULL);
    111110
    112111  std::list<Factory*>::const_iterator factory;
    113112  for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
    114113    if (*(*factory) == root->Value())
    115       break;
     114    {
     115      PRINTF(2)("Create a new Object of type %s\n", (*factory)->getCName());
     116      return (*factory)->fabricateObject(root);
     117    }
    116118
    117   if (factory != Factory::factoryList->end())
    118   {
    119     PRINTF(2)("Create a new Object of type %s\n", (*factory)->getCName());
    120     return (*factory)->fabricateObject(root);
    121   }
    122   else
    123   {
    124     PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value());
    125     return NULL;
    126   }
     119  PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value());
     120  return NULL;
    127121}
    128122
    129123
    130124/**
    131  * Creates a new Object of type className
     125 * @brief Creates a new Object of type className
    132126 * @param className the ClassName to match for the newly created Object
    133127 * @returns a new Object of Type className on match, NULL otherwise
    134128 */
    135  BaseObject* Factory::fabricate(const std::string& className)
     129BaseObject* Factory::fabricate(const std::string& className)
    136130{
    137131  if (Factory::factoryList == NULL)
     
    141135  for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
    142136    if (*(*factory) == className)
    143       break;
    144 
    145   if (factory != Factory::factoryList->end())
    146   {
    147     PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName());
    148     return (*factory)->fabricateObject(NULL);
    149   }
    150   else
    151   {
    152     PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className.c_str());
    153     return NULL;
    154   }
     137    {
     138      PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName());
     139      return (*factory)->fabricateObject(NULL);
     140    }
     141  PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className.c_str());
     142  return NULL;
    155143}
    156144
    157145
    158146/**
    159  * Creates a new Object of type classID
     147 * @brief Creates a new Object of type classID
    160148 * @param classID the ClassID to match for the newly created Object
    161149 * @returns a new Object of Type classID on match, NULL otherwise
     
    169157  for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
    170158    if (*(*factory) == classID)
    171       break;
     159    {
     160      PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName());
     161      return (*factory)->fabricateObject(NULL);
    172162
    173   if (factory != Factory::factoryList->end())
    174   {
    175     PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName());
    176     return (*factory)->fabricateObject(NULL);
    177   }
    178   else
    179   {
    180     PRINTF(2)("Could not Fabricate an Object of ClassID '0x%h'\n", classID);
    181     return NULL;
    182   }
     163    }
     164  PRINTF(2)("Could not Fabricate an Object of ClassID '0x%h'\n", classID);
     165  return NULL;
    183166}
Note: See TracChangeset for help on using the changeset viewer.