Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9828 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
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
21
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 std::string& factoryName, ClassID classID)
31    : classID(classID), className(factoryName)
32{
33  this->setClassID(CL_FACTORY, "Factory");
34  this->setName(factoryName);
35
36  if( Factory::factoryList == NULL)
37    Factory::factoryList = new std::list<Factory*>;
38
39  Factory::factoryList->push_back(this);
40}
41
42/** @brief a reference to the First Factory */
43std::list<Factory*>* Factory::factoryList = NULL;
44
45/**
46 * @brief destructor
47 */
48Factory::~Factory ()
49{
50  //  printf("%s\n", this->factoryName);
51  //  Factory* tmpDel = this->next;
52  //  this->next = NULL;
53}
54
55/**
56 * @brief deletes all the Factories. (cleanup)
57 */
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
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}
80
81/**
82 * @brief Compares the Factories Name against a given ClassName
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
87{
88  return (className != NULL && this->className == className);
89}
90
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{
98  return (this->className == className);
99}
100
101
102/**
103 * @brief 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 */
107BaseObject* Factory::fabricate(const TiXmlElement* root)
108{
109  assert (root != NULL && Factory::factoryList != NULL);
110
111  std::list<Factory*>::const_iterator factory;
112  for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
113    if (*(*factory) == root->Value())
114    {
115      PRINTF(2)("Create a new Object of type %s\n", (*factory)->getCName());
116      return (*factory)->fabricateObject(root);
117    }
118
119  PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value());
120  return NULL;
121}
122
123
124/**
125 * @brief Creates a new Object of type className
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 */
129BaseObject* Factory::fabricate(const std::string& className)
130{
131  if (Factory::factoryList == NULL)
132    return NULL;
133
134  std::list<Factory*>::const_iterator factory;
135  for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
136    if (*(*factory) == className)
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;
143}
144
145
146/**
147 * @brief Creates a new Object of type classID
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 */
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)
159    {
160      PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName());
161      return (*factory)->fabricateObject(NULL);
162
163    }
164  PRINTF(2)("Could not Fabricate an Object of ClassID '0x%h'\n", classID);
165  return NULL;
166}
Note: See TracBrowser for help on using the repository browser.