Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/util/loading/factory.cc @ 6239

Last change on this file since 6239 was 6239, checked in by patrick, 18 years ago

network: singleton fix

File size: 4.5 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
17#include "factory.h"
[5155]18
[5982]19//#include "shell_command.h"
20
[3940]21using namespace std;
22
[5982]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;
[5982]42  this->className = factoryName;
[4597]43
[5982]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 */
[5982]51std::list<Factory*>* Factory::factoryList = NULL;
[4730]52
[3940]53/**
[4836]54 *  destructor
[5982]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
[5984]65/**
66 * deletes all the Factories. (cleanup)
67 */
[5982]68void Factory::deleteFactories()
69{
70  if (Factory::factoryList != NULL)
71  {
72    while(!Factory::factoryList->empty())
73    {
74      delete Factory::factoryList->front();
75      Factory::factoryList->pop_front();
76    }
77    delete Factory::factoryList;
78    Factory::factoryList = NULL;
79  }
80}
81
[5984]82/**
83 * @param classID match a classID with this classID
84 * @returns true on match, false otherwise
85 */
86bool Factory::operator==(ClassID classID) const
87{
88  return (this->classID == classID);
89}
[5982]90
[4487]91/**
[5982]92 * 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 char* className) const
[3940]97{
[5982]98  return(className != NULL && !strcmp(className, this->className));
99}
[3940]100
[4739]101
[5984]102/**
103 * Creates a new Object of type root->Value() (name)
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 */
107 BaseObject* Factory::fabricate(const TiXmlElement* root)
[5982]108{
109  if (root == NULL || Factory::factoryList == NULL)
110    return NULL;
111
112  std::list<Factory*>::const_iterator factory;
113  for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
114    if (*(*factory) == root->Value())
115      break;
116
117  if (factory != Factory::factoryList->end())
[5155]118  {
[5982]119    PRINTF(4)("Create a new Object of type %s\n", (*factory)->getName());
120    return (*factory)->fabricateObject(root);
[5155]121  }
[4739]122  else
123  {
[5982]124    PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value());
125    return NULL;
[4739]126  }
[3940]127}
[5155]128
[6239]129BaseObject* Factory::fabricate(ClassID classID)
[5155]130{
[6239]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++)
[6239]136    if (*(*factory) == classID)
[5982]137      break;
138
139  if (factory != Factory::factoryList->end())
[5155]140  {
[5982]141    PRINTF(4)("Create a new Object of type %s\n", (*factory)->getName());
142    return (*factory)->fabricateObject(NULL);
143  }
144  else
145  {
[6239]146    PRINTF(2)("Could not Fabricate an Object of ClassID '%h'\n", classID);
[5982]147    return NULL;
148  }
149}
150
[6239]151
[5984]152/**
[6239]153 * Creates a new Object of type className
154 * @param className the ClassName to match for the newly created Object
155 * @returns a new Object of Type className on match, NULL otherwise
[5984]156 */
[6239]157 BaseObject* Factory::fabricate(const char* className)
[5982]158{
[6239]159  if (className == NULL || Factory::factoryList == NULL)
[5982]160    return NULL;
161
162  std::list<Factory*>::const_iterator factory;
163  for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
[6239]164    if (*(*factory) == className)
[5155]165      break;
[5982]166
167  if (factory != Factory::factoryList->end())
168  {
169    PRINTF(4)("Create a new Object of type %s\n", (*factory)->getName());
170    return (*factory)->fabricateObject(NULL);
[5155]171  }
[5982]172  else
173  {
[6239]174    PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className);
[5982]175    return NULL;
176  }
[5155]177}
[6239]178
179/**
180 * Creates a new Object of type classID
181 * @param classID the ClassID to match for the newly created Object
182 * @returns a new Object of Type classID on match, NULL otherwise
183 */
184
Note: See TracBrowser for help on using the repository browser.