Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/powerups/src/util/loading/factory.cc @ 6110

Last change on this file since 6110 was 5985, checked in by manuel, 19 years ago

merge: factory has now create from class name string function (svn merge -r 5955:HEAD ../trunk/ powerups/)

File size: 3.7 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*/
[5985]15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING
[3940]16
17#include "factory.h"
[5155]18
[5985]19//#include "shell_command.h"
20
[3940]21using namespace std;
22
[5985]23//SHELL_COMMAND(create, Factory, fabricate);
[5162]24
25
[3940]26/*  --------------------------------------------------
[4597]27 *               Factory
[4003]28 *   --------------------------------------------------
29 */
[3940]30
31/**
[4836]32 *  constructor
[4597]33
[4020]34   set everything to zero and define factoryName
[3940]35*/
[5750]36Factory::Factory (const char* factoryName, ClassID classID)
[3940]37{
[4597]38  this->setClassID(CL_FACTORY, "Factory");
39  this->setName(factoryName);
40
[5750]41  this->classID = classID;
[5985]42  this->className = factoryName;
[4597]43
[5985]44  if( Factory::factoryList == NULL)
45    Factory::factoryList = new std::list<Factory*>;
46
47  Factory::factoryList->push_back(this);
[3940]48}
49
[4739]50/** a reference to the First Factory */
[5985]51std::list<Factory*>* Factory::factoryList = NULL;
[4730]52
[3940]53/**
[4836]54 *  destructor
[5985]55 *
56 * clear the Q
57 */
[3940]58Factory::~Factory ()
59{
[4020]60  //  printf("%s\n", this->factoryName);
[4004]61  //  Factory* tmpDel = this->next;
62  //  this->next = NULL;
[3940]63}
64
[5985]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
[4487]80/**
[5985]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
[3940]86{
[5985]87  return(className != NULL && !strcmp(className, this->className));
88}
[3940]89
[4739]90
[5985]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())
[5155]102  {
[5985]103    PRINTF(4)("Create a new Object of type %s\n", (*factory)->getName());
104    return (*factory)->fabricateObject(root);
[5155]105  }
[4739]106  else
107  {
[5985]108    PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value());
109    return NULL;
[4739]110  }
[3940]111}
[5155]112
[5985]113BaseObject* Factory::fabricate(const char* className)
[5155]114{
[5985]115  if (className == NULL || Factory::factoryList == NULL)
116    return NULL;
[5155]117
[5985]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())
[5155]124  {
[5985]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;
132  }
133}
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)
[5155]144      break;
[5985]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);
[5155]150  }
[5985]151  else
152  {
153    PRINTF(2)("Could not Fabricate an Object of ClassID '%h'\n", classID);
154    return NULL;
155  }
[5155]156}
Note: See TracBrowser for help on using the repository browser.