Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/loading/factory.cc @ 9675

Last change on this file since 9675 was 9675, checked in by bensch, 18 years ago

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

File size: 4.4 KB
RevLine 
[4597]1/*
[3940]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Christian Meyer
[4250]13   co-programmer: Benjamin Grauer
[3940]14*/
[5982]15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING
[3940]16
[7193]17#include "util/loading/factory.h"
[8362]18#include "debug.h"
[5982]19//#include "shell_command.h"
20
[3940]21
[9406]22
[5982]23//SHELL_COMMAND(create, Factory, fabricate);
[5162]24
[7221]25/**
26 * @brief constructor
27 *
28 * set everything to zero and define factoryName
[4003]29 */
[7221]30Factory::Factory (const std::string& factoryName, ClassID classID)
[9675]31    : classID(classID), className(factoryName)
[3940]32{
[4597]33  this->setClassID(CL_FACTORY, "Factory");
34  this->setName(factoryName);
35
[5982]36  if( Factory::factoryList == NULL)
37    Factory::factoryList = new std::list<Factory*>;
38
39  Factory::factoryList->push_back(this);
[3940]40}
41
[7221]42/** @brief a reference to the First Factory */
[5982]43std::list<Factory*>* Factory::factoryList = NULL;
[4730]44
[3940]45/**
[7221]46 * @brief destructor
[5982]47 */
[3940]48Factory::~Factory ()
49{
[4020]50  //  printf("%s\n", this->factoryName);
[4004]51  //  Factory* tmpDel = this->next;
52  //  this->next = NULL;
[3940]53}
54
[5984]55/**
[7221]56 * @brief deletes all the Factories. (cleanup)
[5984]57 */
[5982]58void 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  }
70}
71
[5984]72/**
73 * @param classID match a classID with this classID
74 * @returns true on match, false otherwise
75 */
76bool Factory::operator==(ClassID classID) const
77{
78  return (this->classID == classID);
79}
[5982]80
[4487]81/**
[7221]82 * @brief Compares the Factories Name against a given ClassName
[5982]83 * @param className the Name of the Class to Query
84 * @returns true on match, false otherwise.
85 */
86bool Factory::operator==(const char* className) const
[3940]87{
[9675]88  return (className != NULL && this->className == className);
[5982]89}
[3940]90
[7221]91/**
92 * @brief Compares the Factories Name against a given ClassName
93 * @param className the Name of the Class to Query
94 * @returns true on match, false otherwise.
95 */
96bool Factory::operator==(const std::string& className) const
97{
[9675]98  return (this->className == className);
[7221]99}
[4739]100
[7221]101
[5984]102/**
[7221]103 * @brief Creates a new Object of type root->Value() (name)
[5984]104 * @param root the XML-Root to match for the newly created Object
105 * @returns a new Object of Type root->Value() on match, NULL otherwise
106 */
[9675]107BaseObject* Factory::fabricate(const TiXmlElement* root)
[5982]108{
[9675]109  assert (root != NULL && Factory::factoryList != NULL);
[5982]110
111  std::list<Factory*>::const_iterator factory;
112  for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
113    if (*(*factory) == root->Value())
[9675]114    {
115      PRINTF(2)("Create a new Object of type %s\n", (*factory)->getCName());
116      return (*factory)->fabricateObject(root);
117    }
[5982]118
[9675]119  PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value());
120  return NULL;
[3940]121}
[5155]122
[5984]123
124/**
[9675]125 * @brief Creates a new Object of type className
[5984]126 * @param className the ClassName to match for the newly created Object
127 * @returns a new Object of Type className on match, NULL otherwise
128 */
[9675]129BaseObject* Factory::fabricate(const std::string& className)
[5155]130{
[7221]131  if (Factory::factoryList == NULL)
[5982]132    return NULL;
[5155]133
[5982]134  std::list<Factory*>::const_iterator factory;
135  for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
136    if (*(*factory) == className)
[9675]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;
[5982]143}
144
[6341]145
[5984]146/**
[9675]147 * @brief Creates a new Object of type classID
[5984]148 * @param classID the ClassID to match for the newly created Object
149 * @returns a new Object of Type classID on match, NULL otherwise
150 */
[5982]151BaseObject* Factory::fabricate(ClassID classID)
152{
153  if (Factory::factoryList == NULL)
154    return NULL;
155
156  std::list<Factory*>::const_iterator factory;
157  for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
158    if (*(*factory) == classID)
[9675]159    {
160      PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName());
161      return (*factory)->fabricateObject(NULL);
[5982]162
[9675]163    }
164  PRINTF(2)("Could not Fabricate an Object of ClassID '0x%h'\n", classID);
165  return NULL;
[5155]166}
Note: See TracBrowser for help on using the repository browser.