Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9869 in orxonox.OLD for trunk/src/lib/util/loading/factory.cc


Ignore:
Timestamp:
Oct 3, 2006, 12:19:30 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File:
1 edited

Legend:

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

    r9675 r9869  
    1515#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING
    1616
    17 #include "util/loading/factory.h"
     17#include "factory.h"
    1818#include "debug.h"
    1919//#include "shell_command.h"
    2020
    21 
     21ObjectListDefinition(Factory);
    2222
    2323//SHELL_COMMAND(create, Factory, fabricate);
     
    2828 * set everything to zero and define factoryName
    2929 */
    30 Factory::Factory (const std::string& factoryName, ClassID classID)
    31     : classID(classID), className(factoryName)
     30Factory::Factory (const ClassID& classID)
     31    : _classID(classID)
    3232{
    33   this->setClassID(CL_FACTORY, "Factory");
    34   this->setName(factoryName);
     33  PRINTF(4)("Factory::create(%s::%d)\n", classID.name().c_str(), classID.id());
     34  //this->registerObject(this, Factory::_objectList);
     35  this->setName(classID.name());
    3536
    36   if( Factory::factoryList == NULL)
    37     Factory::factoryList = new std::list<Factory*>;
    38 
    39   Factory::factoryList->push_back(this);
     37  Factory::_factoryIDMap[classID] = this;
     38  Factory::_factoryStringMap[classID.name()] = this;
    4039}
    4140
    42 /** @brief a reference to the First Factory */
    43 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;
    4446
    4547/**
     
    4850Factory::~Factory ()
    4951{
    50   //  printf("%s\n", this->factoryName);
    51   //  Factory* tmpDel = this->next;
    52   //  this->next = NULL;
    53 }
     52  FactoryIDMap::iterator it = Factory::_factoryIDMap.find(this->_classID);
     53  if (it != Factory::_factoryIDMap.end() && (*it).second == this)
     54    Factory::_factoryIDMap.erase(it);
    5455
    55 /**
    56  * @brief deletes all the Factories. (cleanup)
    57  */
    58 void Factory::deleteFactories()
    59 {
    60   if (Factory::factoryList != NULL)
    61   {
    62     while(!Factory::factoryList->empty())
    63     {
    64       delete Factory::factoryList->front();
    65       Factory::factoryList->pop_front();
    66     }
    67     delete Factory::factoryList;
    68     Factory::factoryList = NULL;
    69   }
     56  FactoryStringMap::iterator stringIt = Factory::_factoryStringMap.find(this->_classID.name());
     57  if (stringIt != Factory::_factoryStringMap.end() && (*stringIt).second == this)
     58    Factory::_factoryStringMap.erase(stringIt);
    7059}
    7160
     
    7463 * @returns true on match, false otherwise
    7564 */
    76 bool Factory::operator==(ClassID classID) const
     65bool Factory::operator==(int classID) const
    7766{
    78   return (this->classID == classID);
     67  return (this->_classID == classID);
    7968}
    8069
    81 /**
    82  * @brief Compares the Factories Name against a given ClassName
    83  * @param className the Name of the Class to Query
    84  * @returns true on match, false otherwise.
    85  */
    86 bool Factory::operator==(const char* className) const
    87 {
    88   return (className != NULL && this->className == className);
    89 }
    9070
    9171/**
     
    9676bool Factory::operator==(const std::string& className) const
    9777{
    98   return (this->className == className);
     78  return (this->_classID.name() == className);
    9979}
    10080
     
    10787BaseObject* Factory::fabricate(const TiXmlElement* root)
    10888{
    109   assert (root != NULL && Factory::factoryList != NULL);
    110 
    111   std::list<Factory*>::const_iterator factory;
    112   for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
    113     if (*(*factory) == root->Value())
    114     {
    115       PRINTF(2)("Create a new Object of type %s\n", (*factory)->getCName());
    116       return (*factory)->fabricateObject(root);
    117     }
    118 
    119   PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value());
    120   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  }
    121100}
    122101
     
    129108BaseObject* Factory::fabricate(const std::string& className)
    130109{
    131   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());
    132119    return NULL;
    133 
    134   std::list<Factory*>::const_iterator factory;
    135   for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
    136     if (*(*factory) == className)
    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;
     120  }
    143121}
    144 
    145122
    146123/**
     
    149126 * @returns a new Object of Type classID on match, NULL otherwise
    150127 */
    151 BaseObject* Factory::fabricate(ClassID classID)
     128BaseObject* Factory::fabricate(const ClassID& classID)
    152129{
    153   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());
    154139    return NULL;
     140  }
     141}
    155142
    156   std::list<Factory*>::const_iterator factory;
    157   for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
    158     if (*(*factory) == classID)
    159     {
    160       PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName());
    161       return (*factory)->fabricateObject(NULL);
    162143
    163     }
    164   PRINTF(2)("Could not Fabricate an Object of ClassID '0x%h'\n", classID);
    165   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());
    166150}
     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.