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 "factory.h" |
---|
18 | #include "debug.h" |
---|
19 | //#include "shell_command.h" |
---|
20 | |
---|
21 | ObjectListDefinition(Factory); |
---|
22 | |
---|
23 | //SHELL_COMMAND(create, Factory, fabricate); |
---|
24 | |
---|
25 | /** |
---|
26 | * @brief constructor |
---|
27 | * |
---|
28 | * set everything to zero and define factoryName |
---|
29 | */ |
---|
30 | Factory::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 | Factory::_factoryIDMap[classID] = this; |
---|
38 | Factory::_factoryStringMap[classID.name()] = this; |
---|
39 | } |
---|
40 | |
---|
41 | /** @brief A Map of all Factories ordered by ID. */ |
---|
42 | Factory::FactoryIDMap Factory::_factoryIDMap; |
---|
43 | |
---|
44 | /** @brief A Map of all Factories ordered by Name. */ |
---|
45 | Factory::FactoryStringMap Factory::_factoryStringMap; |
---|
46 | |
---|
47 | /** |
---|
48 | * @brief destructor |
---|
49 | */ |
---|
50 | Factory::~Factory () |
---|
51 | { |
---|
52 | FactoryIDMap::iterator it = Factory::_factoryIDMap.find(this->_classID); |
---|
53 | if (it != Factory::_factoryIDMap.end() && (*it).second == this) |
---|
54 | Factory::_factoryIDMap.erase(it); |
---|
55 | |
---|
56 | FactoryStringMap::iterator stringIt = Factory::_factoryStringMap.find(this->_classID.name()); |
---|
57 | if (stringIt != Factory::_factoryStringMap.end() && (*stringIt).second == this) |
---|
58 | Factory::_factoryStringMap.erase(stringIt); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * @param classID match a classID with this classID |
---|
63 | * @returns true on match, false otherwise |
---|
64 | */ |
---|
65 | bool Factory::operator==(int classID) const |
---|
66 | { |
---|
67 | return (this->_classID == classID); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | /** |
---|
72 | * @brief Compares the Factories Name against a given ClassName |
---|
73 | * @param className the Name of the Class to Query |
---|
74 | * @returns true on match, false otherwise. |
---|
75 | */ |
---|
76 | bool Factory::operator==(const std::string& className) const |
---|
77 | { |
---|
78 | return (this->_classID.name() == className); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | /** |
---|
83 | * @brief Creates a new Object of type root->Value() (name) |
---|
84 | * @param root the XML-Root to match for the newly created Object |
---|
85 | * @returns a new Object of Type root->Value() on match, NULL otherwise |
---|
86 | */ |
---|
87 | BaseObject* Factory::fabricate(const TiXmlElement* root) |
---|
88 | { |
---|
89 | FactoryStringMap::const_iterator it = Factory::_factoryStringMap.find(root->Value()); |
---|
90 | if (it != Factory::_factoryStringMap.end()) |
---|
91 | { |
---|
92 | PRINTF(2)("Create a new Object of type %s\n", (*it).second->getCName()); |
---|
93 | return (*it).second->fabricateObject(root); |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value()); |
---|
98 | return NULL; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | /** |
---|
104 | * @brief Creates a new Object of type className |
---|
105 | * @param className the ClassName to match for the newly created Object |
---|
106 | * @returns a new Object of Type className on match, NULL otherwise |
---|
107 | */ |
---|
108 | BaseObject* Factory::fabricate(const std::string& className) |
---|
109 | { |
---|
110 | FactoryStringMap::const_iterator it = Factory::_factoryStringMap.find(className); |
---|
111 | if (it != Factory::_factoryStringMap.end()) |
---|
112 | { |
---|
113 | PRINTF(2)("Create a new Object of type %s\n", (*it).second->getCName()); |
---|
114 | return (*it).second->fabricateObject(NULL); |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className.c_str()); |
---|
119 | return NULL; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * @brief Creates a new Object of type classID |
---|
125 | * @param classID the ClassID to match for the newly created Object |
---|
126 | * @returns a new Object of Type classID on match, NULL otherwise |
---|
127 | */ |
---|
128 | BaseObject* Factory::fabricate(const ClassID& classID) |
---|
129 | { |
---|
130 | FactoryIDMap::const_iterator it = Factory::_factoryIDMap.find(classID); |
---|
131 | if (it != Factory::_factoryIDMap.end()) |
---|
132 | { |
---|
133 | PRINTF(4)("Create a new Object of type %s\n", (*it).second->getCName()); |
---|
134 | return (*it).second->fabricateObject(NULL); |
---|
135 | } |
---|
136 | else |
---|
137 | { |
---|
138 | PRINTF(2)("Could not Fabricate an Object of ClassID '%d'\n", classID.id()); |
---|
139 | return NULL; |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | /** |
---|
145 | * @brief print out some nice litte debug information about the Factory. |
---|
146 | */ |
---|
147 | void Factory::debug() const |
---|
148 | { |
---|
149 | PRINTF(0)("Factory of class '%s' with ClassID: %d\n", this->_classID.name().c_str(), this->_classID.id()); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * @brief Prints out some nice Debug information about all factories |
---|
154 | */ |
---|
155 | void Factory::debugAll() |
---|
156 | { |
---|
157 | PRINTF(0)("Debugging all %d Factories\n", Factory::_factoryStringMap.size()); |
---|
158 | Factory::FactoryStringMap::const_iterator it; |
---|
159 | for (it = Factory::_factoryStringMap.begin(); it != Factory::_factoryStringMap.end(); ++it) |
---|
160 | (*it).second->debug(); |
---|
161 | } |
---|