Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 19, 2006, 5:00:10 PM (18 years ago)
Author:
bensch
Message:

new_class_id: much faster and nicer self-cleaning Factory

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/new_class_id/src/lib/util/loading/factory.cc

    r9725 r9762  
    3535  this->setName(classID.name());
    3636
    37   if( Factory::_factoryList == NULL)
    38     Factory::_factoryList = new std::list<Factory*>;
    39 
    40   Factory::_factoryList->push_back(this);
     37  Factory::_factoryIDMap[classID] = this;
     38  Factory::_factoryStringMap[classID.name()] = this;
    4139}
    4240
    43 /** @brief a reference to the First Factory */
    44 std::list<Factory*>* Factory::_factoryList = NULL;
     41/** @brief A Map of all Factories ordered by ID. */
     42Factory::FactoryIDMap Factory::_factoryIDMap;
     43
     44/** @brief A Map of all Factories ordered by Name. */
     45Factory::FactoryStringMap Factory::_factoryStringMap;
    4546
    4647/**
     
    4950Factory::~Factory ()
    5051{
    51   //  printf("%s\n", this->factoryName);
    52   //  Factory* tmpDel = this->next;
    53   //  this->next = NULL;
    54 }
     52  FactoryIDMap::iterator it = Factory::_factoryIDMap.find(this->_classID);
     53  if (it != Factory::_factoryIDMap.end() && (*it).second == this)
     54    Factory::_factoryIDMap.erase(it);
    5555
    56 /**
    57  * @brief deletes all the Factories. (cleanup)
    58  */
    59 void Factory::deleteFactories()
    60 {
    61   if (Factory::_factoryList != NULL)
    62   {
    63     while(!Factory::_factoryList->empty())
    64     {
    65       delete Factory::_factoryList->front();
    66       Factory::_factoryList->pop_front();
    67     }
    68     delete Factory::_factoryList;
    69     Factory::_factoryList = NULL;
    70   }
     56  FactoryStringMap::iterator stringIt = Factory::_factoryStringMap.find(this->_classID.name());
     57  if (stringIt != Factory::_factoryStringMap.end() && (*stringIt).second == this)
     58    Factory::_factoryStringMap.erase(stringIt);
    7159}
    7260
     
    9987BaseObject* Factory::fabricate(const TiXmlElement* root)
    10088{
    101   assert (Factory::_factoryList != NULL);
    102 
    103   std::list<Factory*>::const_iterator factory;
    104   for (factory = Factory::_factoryList->begin(); factory != Factory::_factoryList->end(); factory++)
    105     if (*(*factory) == root->Value())
    106     {
    107       PRINTF(2)("Create a new Object of type %s\n", (*factory)->getCName());
    108       return (*factory)->fabricateObject(root);
    109     }
    110 
    111   PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value());
    112   return NULL;
     89  FactoryStringMap::const_iterator it = Factory::_factoryStringMap.find(root->Value());
     90  if (it != Factory::_factoryStringMap.end())
     91  {
     92    PRINTF(2)("Create a new Object of type %s\n", (*it).second->getCName());
     93    return (*it).second->fabricateObject(root);
     94  }
     95  else
     96  {
     97    PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value());
     98    return NULL;
     99  }
    113100}
    114101
     
    121108BaseObject* Factory::fabricate(const std::string& className)
    122109{
    123   if (Factory::_factoryList == NULL)
     110  FactoryStringMap::const_iterator it = Factory::_factoryStringMap.find(className);
     111  if (it != Factory::_factoryStringMap.end())
     112  {
     113    PRINTF(2)("Create a new Object of type %s\n", (*it).second->getCName());
     114    return (*it).second->fabricateObject(NULL);
     115  }
     116  else
     117  {
     118    PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className.c_str());
    124119    return NULL;
    125 
    126   std::list<Factory*>::const_iterator factory;
    127   for (factory = Factory::_factoryList->begin(); factory != Factory::_factoryList->end(); factory++)
    128     if (*(*factory) == className)
    129     {
    130       PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName());
    131       return (*factory)->fabricateObject(NULL);
    132     }
    133   PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className.c_str());
    134   return NULL;
     120  }
    135121}
    136122
     
    142128BaseObject* Factory::fabricate(const ClassID& classID)
    143129{
    144   if (Factory::_factoryList == NULL)
     130  FactoryIDMap::const_iterator it = Factory::_factoryIDMap.find(classID);
     131  if (it != Factory::_factoryIDMap.end())
     132  {
     133    PRINTF(4)("Create a new Object of type %s\n", (*it).second->getCName());
     134    return (*it).second->fabricateObject(NULL);
     135  }
     136  else
     137  {
     138    PRINTF(2)("Could not Fabricate an Object of ClassID '%d'\n", classID.id());
    145139    return NULL;
     140  }
     141}
    146142
    147   std::list<Factory*>::const_iterator factory;
    148   for (factory = Factory::_factoryList->begin(); factory != Factory::_factoryList->end(); factory++)
    149     if (*(*factory) == classID)
    150   {
    151     PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName());
    152     return (*factory)->fabricateObject(NULL);
    153143
    154   }
    155   PRINTF(2)("Could not Fabricate an Object of ClassID '%d'\n", classID.id());
    156   return NULL;
     144/**
     145 * @brief print out some nice litte debug information about the Factory.
     146 */
     147void Factory::debug() const
     148{
     149  PRINTF(0)("Factory of class '%s' with ClassID: %d\n", this->_classID.name().c_str(), this->_classID.id());
    157150}
     151
     152/**
     153 * @brief Prints out some nice Debug information about all factories
     154 */
     155void Factory::debugAll()
     156{
     157  PRINTF(0)("Debugging all %d Factories\n", Factory::_factoryStringMap.size());
     158  Factory::FactoryStringMap::const_iterator it;
     159  for (it = Factory::_factoryStringMap.begin(); it != Factory::_factoryStringMap.end(); ++it)
     160    (*it).second->debug();
     161}
Note: See TracChangeset for help on using the changeset viewer.