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 | using namespace std; |
---|
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 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 */ |
---|
43 | std::list<Factory*>* Factory::factoryList = NULL; |
---|
44 | |
---|
45 | /** |
---|
46 | * @brief destructor |
---|
47 | */ |
---|
48 | Factory::~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 | */ |
---|
58 | void 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 | */ |
---|
76 | bool 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 | */ |
---|
86 | bool 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 | */ |
---|
96 | bool 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 | */ |
---|
107 | BaseObject* Factory::fabricate(const TiXmlElement* root) |
---|
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()) |
---|
118 | { |
---|
119 | PRINTF(2)("Create a new Object of type %s\n", (*factory)->getName()); |
---|
120 | return (*factory)->fabricateObject(root); |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value()); |
---|
125 | return NULL; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | /** |
---|
131 | * Creates a new Object of type className |
---|
132 | * @param className the ClassName to match for the newly created Object |
---|
133 | * @returns a new Object of Type className on match, NULL otherwise |
---|
134 | */ |
---|
135 | BaseObject* Factory::fabricate(const std::string& className) |
---|
136 | { |
---|
137 | if (Factory::factoryList == NULL) |
---|
138 | return NULL; |
---|
139 | |
---|
140 | std::list<Factory*>::const_iterator factory; |
---|
141 | for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++) |
---|
142 | if (*(*factory) == className) |
---|
143 | break; |
---|
144 | |
---|
145 | if (factory != Factory::factoryList->end()) |
---|
146 | { |
---|
147 | PRINTF(4)("Create a new Object of type %s\n", (*factory)->getName()); |
---|
148 | return (*factory)->fabricateObject(NULL); |
---|
149 | } |
---|
150 | else |
---|
151 | { |
---|
152 | PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className.c_str()); |
---|
153 | return NULL; |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | /** |
---|
159 | * Creates a new Object of type classID |
---|
160 | * @param classID the ClassID to match for the newly created Object |
---|
161 | * @returns a new Object of Type classID on match, NULL otherwise |
---|
162 | */ |
---|
163 | BaseObject* Factory::fabricate(ClassID classID) |
---|
164 | { |
---|
165 | if (Factory::factoryList == NULL) |
---|
166 | return NULL; |
---|
167 | |
---|
168 | std::list<Factory*>::const_iterator factory; |
---|
169 | for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++) |
---|
170 | if (*(*factory) == classID) |
---|
171 | break; |
---|
172 | |
---|
173 | if (factory != Factory::factoryList->end()) |
---|
174 | { |
---|
175 | PRINTF(4)("Create a new Object of type %s\n", (*factory)->getName()); |
---|
176 | return (*factory)->fabricateObject(NULL); |
---|
177 | } |
---|
178 | else |
---|
179 | { |
---|
180 | PRINTF(2)("Could not Fabricate an Object of ClassID '0x%h'\n", classID); |
---|
181 | return NULL; |
---|
182 | } |
---|
183 | } |
---|