Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9762 in orxonox.OLD


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

Location:
branches/new_class_id/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/new_class_id/src/lib/lang/class_id.h

    r9757 r9762  
    4747  /** @param name the id to compare @returns false on match (match is same Name) @brief compares an ID with a ClassName */
    4848  bool operator!=(const std::string& name) const { return *_name != name; };
     49  /** @param classID: the Id to compare @returns true if this id is smaller than the classID's ID */
     50  bool operator<(const ClassID& classID) const { return *this->_id < *classID._id; };
    4951
    5052private:
  • branches/new_class_id/src/lib/sound/ogg_player.h

    r9715 r9762  
    1313#include <ogg/ogg.h>
    1414#include <vorbis/vorbisfile.h>
    15 #include "threading.h"
     15#include "util/threading.h"
    1616
    1717struct File;
  • 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}
  • branches/new_class_id/src/lib/util/loading/factory.h

    r9757 r9762  
    2727#include "parser/tinyxml/tinyxml.h"
    2828#include "base_object.h"
    29 #include <list>
     29#include <map>
    3030
    3131/**
     
    3434 */
    3535#define CREATE_FACTORY(CLASS_NAME) \
    36     tFactory<CLASS_NAME>* global_##CLASS_NAME##_Factory = new tFactory<CLASS_NAME>(CLASS_NAME::staticClassID())
     36    tFactory<CLASS_NAME> global_##CLASS_NAME##_Factory = tFactory<CLASS_NAME>(CLASS_NAME::staticClassID())
    3737
    3838//! The Factory is a loadable object handler
    3939class Factory : public BaseObject
    4040{
     41  //! Declare the ObjectList at the BaseObject List
    4142  ObjectListDeclaration(Factory);
    4243public:
    4344  virtual ~Factory ();
    44 
    45   static void deleteFactories();
    4645
    4746  static  BaseObject* fabricate(const std::string& className);
     
    4948  static  BaseObject* fabricate(const TiXmlElement* root);
    5049
    51 
    5250  bool operator==(int classID) const;
    5351  bool operator==(const std::string& className) const;
     52  /** @param classID the ID to compare @returns true if the ID's match */
    5453  bool operator==(const ClassID& classID) const { return _classID == classID; };
     54
     55
     56  void debug() const;
     57  static void debugAll();
    5558
    5659protected:
    5760  Factory (const ClassID& id);
     61  /**
     62   * @brief The core function of the Factory. Creates objects of the Factories Type.
     63   * @param root The TiXmlElement of the Factory.
     64   * @returns the created Object in BaseType* format.
     65   */
    5866  virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const = 0;
    5967
    6068private:
     69  //! Copy Constructor is hidden.
    6170  Factory (const Factory&) {};
    6271
    6372protected:
    64   const ClassID              _classID;              //!< The Class-Identifyer of the Factory.
    65   static std::list<Factory*>*   _factoryList;          //!< List of Registered Factories
     73  /** The Type of the FactoryMap that is sorted by ID */
     74  typedef std::map<const ClassID, Factory*>  FactoryIDMap;
     75  /** The Type of the FactoryMap that is sorted by Name */
     76  typedef std::map<std::string, Factory*>    FactoryStringMap;
     77
     78  const ClassID                 _classID;              //!< The Class-Identifyer of the Factory.
     79  static FactoryIDMap           _factoryIDMap;         //!< List of Registered Factories
     80  static FactoryStringMap       _factoryStringMap;     //!< List of Registered Factories
    6681};
    6782
     
    7590  /**
    7691   * @brief creates a new type Factory to enable the loading of T
    77    * @param factoryName the Name of the Factory to load.
    7892   * @param classID the ID of the Class to be created.
    7993   */
     
    8195      : Factory(classID)
    8296  {  }
     97  /**
     98   * @brief copy constructor
     99   * @param factory the Factory to copy
     100   */
     101  tFactory (const tFactory& factory) : Factory(factory._classID) {};
    83102
    84 private:
    85   tFactory (const tFactory&) {};
    86103  /**
    87104   * @brief fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*)
  • branches/new_class_id/src/orxonox.cc

    r9715 r9762  
    4545#include "event_handler.h"
    4646
    47 #include "util/loading/factory.h"
    4847#include "loading/fast_factory.h"
    4948
     
    127126
    128127  // class-less services/factories
    129   Factory::deleteFactories();
    130128  FastFactory::deleteAll();
    131129  OrxShell::ShellCommandClass::unregisterAllCommands();
Note: See TracChangeset for help on using the changeset viewer.