Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


Ignore:
Timestamp:
Dec 8, 2005, 12:22:53 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: Factory-Redesign on the wish of manuel

File:
1 edited

Legend:

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

    r5750 r5982  
    1313   co-programmer: Benjamin Grauer
    1414*/
    15 
     15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING
    1616
    1717#include "factory.h"
    1818
    19 #include "shell_command.h"
    20 #include "game_loader.h"
     19//#include "shell_command.h"
     20
    2121using namespace std;
    2222
    23 SHELL_COMMAND(create, Factory, fabricate);
     23//SHELL_COMMAND(create, Factory, fabricate);
    2424
    2525
     
    3939  this->setName(factoryName);
    4040
    41   this->next = NULL;
    4241  this->classID = classID;
     42  this->className = factoryName;
    4343
    44   Factory::registerFactory(this);
     44  if( Factory::factoryList == NULL)
     45    Factory::factoryList = new std::list<Factory*>;
     46
     47  Factory::factoryList->push_back(this);
    4548}
    4649
    4750/** a reference to the First Factory */
    48 Factory* Factory::first = NULL;
     51std::list<Factory*>* Factory::factoryList = NULL;
    4952
    5053/**
    5154 *  destructor
    52 
    53    clear the Q
    54 */
     55 *
     56 * clear the Q
     57 */
    5558Factory::~Factory ()
    5659{
     
    5861  //  Factory* tmpDel = this->next;
    5962  //  this->next = NULL;
    60   if (this->next)
    61     delete this->next;
    6263}
    6364
     65void Factory::deleteFactories()
     66{
     67  if (Factory::factoryList != NULL)
     68  {
     69    while(!Factory::factoryList->empty())
     70    {
     71      delete Factory::factoryList->front();
     72      Factory::factoryList->pop_front();
     73    }
     74    delete Factory::factoryList;
     75    Factory::factoryList = NULL;
     76  }
     77}
     78
     79
    6480/**
    65  *  add a Factory to the Factory Queue
    66  * @param factory a Factory to be registered
    67 */
    68 void Factory::registerFactory( Factory* factory)
     81 * Compares the Factories Name against a given ClassName
     82 * @param className the Name of the Class to Query
     83 * @returns true on match, false otherwise.
     84 */
     85bool Factory::operator==(const char* className) const
    6986{
    70   assert( factory != NULL);
     87  return(className != NULL && !strcmp(className, this->className));
     88}
    7189
    72   PRINTF(5)("Registered factory for '%s'\n", factory->getName());
    7390
    74   if( Factory::first == NULL)
     91BaseObject* Factory::fabricate(const TiXmlElement* root)
     92{
     93  if (root == NULL || Factory::factoryList == NULL)
     94    return NULL;
     95
     96  std::list<Factory*>::const_iterator factory;
     97  for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
     98    if (*(*factory) == root->Value())
     99      break;
     100
     101  if (factory != Factory::factoryList->end())
    75102  {
    76     Factory::first = factory;
     103    PRINTF(4)("Create a new Object of type %s\n", (*factory)->getName());
     104    return (*factory)->fabricateObject(root);
    77105  }
    78106  else
    79107  {
    80     Factory* tmpFac = Factory::first;
    81     while( tmpFac->next != NULL)
    82     {
    83       tmpFac = tmpFac->next;
    84     }
    85     tmpFac->setNext(factory);
     108    PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value());
     109    return NULL;
    86110  }
    87111}
    88112
    89 void Factory::fabricate(const char* className, const char* entityName)
     113BaseObject* Factory::fabricate(const char* className)
    90114{
    91   if (className == NULL)
    92     return;
    93   Factory* fac = Factory::first;
     115  if (className == NULL || Factory::factoryList == NULL)
     116    return NULL;
    94117
    95   while (fac != NULL)
     118  std::list<Factory*>::const_iterator factory;
     119  for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
     120    if (*(*factory) == className)
     121      break;
     122
     123  if (factory != Factory::factoryList->end())
    96124  {
    97     if (!strcmp(className, fac->getName()))
    98     {
    99       PRINTF(3)("Create a new Object of type %s\n", fac->getName());
    100       BaseObject* object = fac->fabricateDirect();
    101       if (object != NULL)
    102       {
    103         object->setName(entityName);
    104       }
    105       break;
    106     }
    107     fac = fac->next;
     125    PRINTF(4)("Create a new Object of type %s\n", (*factory)->getName());
     126    return (*factory)->fabricateObject(NULL);
     127  }
     128  else
     129  {
     130    PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className);
     131    return NULL;
    108132  }
    109133}
     134
     135
     136BaseObject* Factory::fabricate(ClassID classID)
     137{
     138  if (Factory::factoryList == NULL)
     139    return NULL;
     140
     141  std::list<Factory*>::const_iterator factory;
     142  for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
     143    if (*(*factory) == classID)
     144      break;
     145
     146  if (factory != Factory::factoryList->end())
     147  {
     148    PRINTF(4)("Create a new Object of type %s\n", (*factory)->getName());
     149    return (*factory)->fabricateObject(NULL);
     150  }
     151  else
     152  {
     153    PRINTF(2)("Could not Fabricate an Object of ClassID '%h'\n", classID);
     154    return NULL;
     155  }
     156}
Note: See TracChangeset for help on using the changeset viewer.