Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/util/loading/factory.cc @ 9725

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

orxonox/new_class_id: less debug

File size: 4.2 KB
Line 
1/*
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
13   co-programmer: Benjamin Grauer
14*/
15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING
16
17#include "util/loading/factory.h"
18#include "debug.h"
19//#include "shell_command.h"
20
21ObjectListDefinition(Factory);
22
23//SHELL_COMMAND(create, Factory, fabricate);
24
25/**
26 * @brief constructor
27 *
28 * set everything to zero and define factoryName
29 */
30Factory::Factory (const ClassID& classID)
31    : _classID(classID)
32{
33  PRINTF(4)("Factory::create(%s::%d)\n", classID.name().c_str(), classID.id());
34  //this->registerObject(this, Factory::_objectList);
35  this->setName(classID.name());
36
37  if( Factory::_factoryList == NULL)
38    Factory::_factoryList = new std::list<Factory*>;
39
40  Factory::_factoryList->push_back(this);
41}
42
43/** @brief a reference to the First Factory */
44std::list<Factory*>* Factory::_factoryList = NULL;
45
46/**
47 * @brief destructor
48 */
49Factory::~Factory ()
50{
51  //  printf("%s\n", this->factoryName);
52  //  Factory* tmpDel = this->next;
53  //  this->next = NULL;
54}
55
56/**
57 * @brief deletes all the Factories. (cleanup)
58 */
59void 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  }
71}
72
73/**
74 * @param classID match a classID with this classID
75 * @returns true on match, false otherwise
76 */
77bool Factory::operator==(int classID) const
78{
79  return (this->_classID == classID);
80}
81
82
83/**
84 * @brief Compares the Factories Name against a given ClassName
85 * @param className the Name of the Class to Query
86 * @returns true on match, false otherwise.
87 */
88bool Factory::operator==(const std::string& className) const
89{
90  return (this->_classID.name() == className);
91}
92
93
94/**
95 * @brief Creates a new Object of type root->Value() (name)
96 * @param root the XML-Root to match for the newly created Object
97 * @returns a new Object of Type root->Value() on match, NULL otherwise
98 */
99BaseObject* Factory::fabricate(const TiXmlElement* root)
100{
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;
113}
114
115
116/**
117 * @brief Creates a new Object of type className
118 * @param className the ClassName to match for the newly created Object
119 * @returns a new Object of Type className on match, NULL otherwise
120 */
121BaseObject* Factory::fabricate(const std::string& className)
122{
123  if (Factory::_factoryList == NULL)
124    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;
135}
136
137/**
138 * @brief Creates a new Object of type classID
139 * @param classID the ClassID to match for the newly created Object
140 * @returns a new Object of Type classID on match, NULL otherwise
141 */
142BaseObject* Factory::fabricate(const ClassID& classID)
143{
144  if (Factory::_factoryList == NULL)
145    return NULL;
146
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);
153
154  }
155  PRINTF(2)("Could not Fabricate an Object of ClassID '%d'\n", classID.id());
156  return NULL;
157}
Note: See TracBrowser for help on using the repository browser.