1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Fabian 'x3n' Landau |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #ifndef _XMLPort_H__ |
---|
29 | #define _XMLPort_H__ |
---|
30 | |
---|
31 | #include "util/XMLIncludes.h" |
---|
32 | #include "util/MultiTypeMath.h" |
---|
33 | #include "util/tinyxml/ticpp.h" |
---|
34 | #include "util/SubString.h" |
---|
35 | #include "Executor.h" |
---|
36 | #include "Debug.h" |
---|
37 | #include "CoreIncludes.h" |
---|
38 | #include "BaseObject.h" |
---|
39 | |
---|
40 | #include "CorePrereqs.h" |
---|
41 | |
---|
42 | |
---|
43 | #define XMLPortParam(classname, paramname, loadfunction, savefunction, xmlelement, mode) \ |
---|
44 | XMLPortParamGeneric(xmlcontainer##loadfunction##savefunction, classname, paramname, createExecutor(createFunctor(&classname::loadfunction), #loadfunction), createExecutor(createFunctor(&classname::savefunction), #savefunction), xmlelement, mode) |
---|
45 | #define XMLPortParam_Template(classname, paramname, loadtemplate, loadfunction, savetemplate, savefunction, xmlelement, mode) \ |
---|
46 | XMLPortParamGeneric(xmlcontainer##loadfunction##savefunction, classname, paramname, createExecutor(createFunctor loadtemplate (&classname::loadfunction), #loadfunction), createExecutor(createFunctor savetemplate (&classname::savefunction), #savefunction), xmlelement, mode) |
---|
47 | |
---|
48 | #define XMLPortParamLoadOnly(classname, paramname, loadfunction, xmlelement, mode) \ |
---|
49 | XMLPortParamGeneric(xmlcontainer##loadfunction##0, classname, paramname, createExecutor(createFunctor(&classname::loadfunction), #loadfunction), 0, xmlelement, mode) |
---|
50 | #define XMLPortParamLoadOnly_Template(classname, paramname, loadtemplate, loadfunction, xmlelement, mode) \ |
---|
51 | XMLPortParamGeneric(xmlcontainer##loadfunction##0, classname, paramname, createExecutor(createFunctor loadtemplate (&classname::loadfunction), #loadfunction), 0, xmlelement, mode) |
---|
52 | |
---|
53 | #define XMLPortParamGeneric(containername, classname, paramname, loadexecutor, saveexecutor, xmlelement, mode) \ |
---|
54 | orxonox::XMLPortClassParamContainer<classname>* containername = (orxonox::XMLPortClassParamContainer<classname>*)(this->getIdentifier()->getXMLPortParamContainer(paramname)); \ |
---|
55 | if (!containername) \ |
---|
56 | { \ |
---|
57 | containername = new orxonox::XMLPortClassParamContainer<classname>(std::string(paramname), loadexecutor, saveexecutor); \ |
---|
58 | this->getIdentifier()->addXMLPortParamContainer(paramname, containername); \ |
---|
59 | } \ |
---|
60 | containername->port(this, xmlelement, mode) |
---|
61 | |
---|
62 | |
---|
63 | #define XMLPortObject(classname, objectclass, sectionname, loadfunction, savefunction, xmlelement, mode, bApplyLoaderMask, bLoadBefore) \ |
---|
64 | XMLPortObjectGeneric(xmlcontainer##loadfunction##savefunction, classname, objectclass, sectionname, createExecutor(createFunctor(&classname::loadfunction), #loadfunction), createExecutor(createFunctor(&classname::savefunction), #savefunction), xmlelement, mode, bApplyLoaderMask, bLoadBefore) |
---|
65 | #define XMLPortObject_Template(classname, objectclass, sectionname, loadtemplate, loadfunction, savetemplate, savefunction, xmlelement, mode, bApplyLoaderMask, bLoadBefore) \ |
---|
66 | XMLPortObjectGeneric(xmlcontainer##loadfunction##savefunction, classname, objectclass, sectionname, createExecutor(createFunctor loadtemplate (&classname::loadfunction), #loadfunction), createExecutor(createFunctor savetemplate (&classname::savefunction), #savefunction), xmlelement, mode, bApplyLoaderMask, bLoadBefore) |
---|
67 | |
---|
68 | #define XMLPortObjectGeneric(containername, classname, objectclass, sectionname, loadexecutor, saveexecutor, xmlelement, mode, bApplyLoaderMask, bLoadBefore) \ |
---|
69 | orxonox::XMLPortClassObjectContainer<classname, objectclass>* containername = (orxonox::XMLPortClassObjectContainer<classname, objectclass>*)(this->getIdentifier()->getXMLPortObjectContainer(sectionname)); \ |
---|
70 | if (!containername) \ |
---|
71 | { \ |
---|
72 | containername = new orxonox::XMLPortClassObjectContainer<classname, objectclass>(std::string(sectionname), loadexecutor, saveexecutor, bApplyLoaderMask, bLoadBefore); \ |
---|
73 | this->getIdentifier()->addXMLPortObjectContainer(sectionname, containername); \ |
---|
74 | } \ |
---|
75 | containername->port(this, xmlelement, mode) |
---|
76 | |
---|
77 | |
---|
78 | namespace orxonox |
---|
79 | { |
---|
80 | |
---|
81 | #ifndef _XMLPort_Mode__ |
---|
82 | #define _XMLPort_Mode__ |
---|
83 | namespace XMLPort |
---|
84 | { |
---|
85 | enum Mode |
---|
86 | { |
---|
87 | LoadObject, |
---|
88 | SaveObject |
---|
89 | }; |
---|
90 | } |
---|
91 | #endif |
---|
92 | |
---|
93 | // ############################### |
---|
94 | // ### XMLPortParamContainer ### |
---|
95 | // ############################### |
---|
96 | class _CoreExport XMLPortParamContainer |
---|
97 | { |
---|
98 | enum ParseResult |
---|
99 | { |
---|
100 | PR_not_started, |
---|
101 | PR_finished, |
---|
102 | PR_waiting_for_default_values |
---|
103 | }; |
---|
104 | |
---|
105 | public: |
---|
106 | XMLPortParamContainer() |
---|
107 | { this->parseResult_ = PR_not_started; } |
---|
108 | virtual ~XMLPortParamContainer() {} |
---|
109 | |
---|
110 | inline const std::string& getName() const |
---|
111 | { return this->paramname_; } |
---|
112 | |
---|
113 | virtual XMLPortParamContainer& description(const std::string description) = 0; |
---|
114 | virtual const std::string& getDescription() = 0; |
---|
115 | |
---|
116 | virtual XMLPortParamContainer& defaultValue(unsigned int index, const MultiTypeMath& param) = 0; |
---|
117 | virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1) = 0; |
---|
118 | virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2) = 0; |
---|
119 | virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) = 0; |
---|
120 | virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) = 0; |
---|
121 | virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) = 0; |
---|
122 | |
---|
123 | protected: |
---|
124 | std::string paramname_; |
---|
125 | ParseResult parseResult_; |
---|
126 | |
---|
127 | }; |
---|
128 | |
---|
129 | template <class T> |
---|
130 | class XMLPortClassParamContainer : public XMLPortParamContainer |
---|
131 | { |
---|
132 | struct ParseParams |
---|
133 | { |
---|
134 | T* object; |
---|
135 | Element* xmlelement; |
---|
136 | XMLPort::Mode mode; |
---|
137 | }; |
---|
138 | |
---|
139 | public: |
---|
140 | XMLPortClassParamContainer(const std::string paramname, ExecutorMember<T>* loadexecutor, ExecutorMember<T>* saveexecutor) |
---|
141 | { |
---|
142 | this->paramname_ = paramname; |
---|
143 | this->loadexecutor_ = loadexecutor; |
---|
144 | this->saveexecutor_ = saveexecutor; |
---|
145 | |
---|
146 | this->setDefaultValue_[0] = false; |
---|
147 | this->setDefaultValue_[1] = false; |
---|
148 | this->setDefaultValue_[2] = false; |
---|
149 | this->setDefaultValue_[3] = false; |
---|
150 | this->setDefaultValue_[4] = false; |
---|
151 | } |
---|
152 | |
---|
153 | XMLPortParamContainer& port(T* object, Element& xmlelement, XMLPort::Mode mode) |
---|
154 | { |
---|
155 | this->parseParams_.object = object; |
---|
156 | this->parseParams_.xmlelement = &xmlelement; |
---|
157 | this->parseParams_.mode = mode; |
---|
158 | |
---|
159 | if (mode == XMLPort::LoadObject) |
---|
160 | { |
---|
161 | try |
---|
162 | { |
---|
163 | std::string attribute = xmlelement.GetAttribute(this->paramname_); |
---|
164 | if ((attribute.size() > 0) || (this->loadexecutor_->allDefaultValuesSet())) |
---|
165 | { |
---|
166 | COUT(5) << object->getLoaderIndentation() << "Loading parameter " << this->paramname_ << " in " << object->getIdentifier()->getName() << " (objectname " << object->getName() << ")." << std::endl << object->getLoaderIndentation(); |
---|
167 | if (this->loadexecutor_->parse(object, attribute, ",")) |
---|
168 | this->parseResult_ = PR_finished; |
---|
169 | else |
---|
170 | this->parseResult_ = PR_waiting_for_default_values; |
---|
171 | } |
---|
172 | } |
---|
173 | catch(ticpp::Exception& ex) |
---|
174 | { |
---|
175 | COUT(1) << std::endl; |
---|
176 | COUT(1) << "An error occurred in XMLPort.h while loading attribute '" << this->paramname_ << "' of '" << object->getIdentifier()->getName() << "' (objectname: " << object->getName() << ") in " << object->getLevelfile() << ":" << std::endl; |
---|
177 | COUT(1) << ex.what() << std::endl; |
---|
178 | } |
---|
179 | } |
---|
180 | else |
---|
181 | { |
---|
182 | if (this->saveexecutor_) |
---|
183 | { |
---|
184 | // xmlelement.SetAttribute(this->paramname_, "..."); |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | return (*this); |
---|
189 | } |
---|
190 | |
---|
191 | XMLPortParamContainer& port(const ParseParams& parseParams) |
---|
192 | { |
---|
193 | return this->port(parseParams.object, *parseParams.xmlelement, parseParams.mode); |
---|
194 | } |
---|
195 | |
---|
196 | XMLPortParamContainer& portIfWaitingForDefaultValues(const ParseResult& result, const ParseParams& params) |
---|
197 | { |
---|
198 | if (result == PR_waiting_for_default_values) |
---|
199 | return this->port(params); |
---|
200 | else |
---|
201 | return (*this); |
---|
202 | } |
---|
203 | |
---|
204 | virtual XMLPortParamContainer& description(const std::string description) |
---|
205 | { this->loadexecutor_->setDescription(description); return (*this); } |
---|
206 | virtual const std::string& getDescription() |
---|
207 | { return this->loadexecutor_->getDescription(); } |
---|
208 | |
---|
209 | virtual XMLPortParamContainer& defaultValue(unsigned int index, const MultiTypeMath& param) |
---|
210 | { |
---|
211 | if (!this->setDefaultValue_[index]) |
---|
212 | { |
---|
213 | this->setDefaultValue_[index] = true; |
---|
214 | this->loadexecutor_->setDefaultValue(index, param); |
---|
215 | } |
---|
216 | return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_); |
---|
217 | } |
---|
218 | virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1) |
---|
219 | { |
---|
220 | if (!this->setDefaultValue_[0]) |
---|
221 | { |
---|
222 | this->setDefaultValue_[0] = true; |
---|
223 | this->loadexecutor_->setDefaultValues(param1); |
---|
224 | } |
---|
225 | return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_); |
---|
226 | } |
---|
227 | virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2) |
---|
228 | { |
---|
229 | if ((!this->setDefaultValue_[0]) || (!this->setDefaultValue_[1])) |
---|
230 | { |
---|
231 | this->setDefaultValue_[0] = true; |
---|
232 | this->setDefaultValue_[1] = true; |
---|
233 | this->loadexecutor_->setDefaultValues(param1, param2); |
---|
234 | } |
---|
235 | return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_); |
---|
236 | } |
---|
237 | virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) |
---|
238 | { |
---|
239 | if ((!this->setDefaultValue_[0]) || (!this->setDefaultValue_[1]) || (!this->setDefaultValue_[2])) |
---|
240 | { |
---|
241 | this->setDefaultValue_[0] = true; |
---|
242 | this->setDefaultValue_[1] = true; |
---|
243 | this->setDefaultValue_[3] = true; |
---|
244 | this->loadexecutor_->setDefaultValues(param1, param2, param3); |
---|
245 | } |
---|
246 | return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_); |
---|
247 | } |
---|
248 | virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) |
---|
249 | { |
---|
250 | if ((!this->setDefaultValue_[0]) || (!this->setDefaultValue_[1]) || (!this->setDefaultValue_[2]) || (!this->setDefaultValue_[3])) |
---|
251 | { |
---|
252 | this->setDefaultValue_[0] = true; |
---|
253 | this->setDefaultValue_[1] = true; |
---|
254 | this->setDefaultValue_[3] = true; |
---|
255 | this->setDefaultValue_[4] = true; |
---|
256 | this->loadexecutor_->setDefaultValues(param1, param2, param3, param4); |
---|
257 | } |
---|
258 | return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_); |
---|
259 | } |
---|
260 | virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) |
---|
261 | { |
---|
262 | if ((!this->setDefaultValue_[0]) || (!this->setDefaultValue_[1]) || (!this->setDefaultValue_[2]) || (!this->setDefaultValue_[3]) || (!this->setDefaultValue_[4])) |
---|
263 | { |
---|
264 | this->setDefaultValue_[0] = true; |
---|
265 | this->setDefaultValue_[1] = true; |
---|
266 | this->setDefaultValue_[3] = true; |
---|
267 | this->setDefaultValue_[4] = true; |
---|
268 | this->setDefaultValue_[5] = true; |
---|
269 | this->loadexecutor_->setDefaultValues(param1, param2, param3, param4, param5); |
---|
270 | } |
---|
271 | return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_); |
---|
272 | } |
---|
273 | |
---|
274 | private: |
---|
275 | ExecutorMember<T>* loadexecutor_; |
---|
276 | ExecutorMember<T>* saveexecutor_; |
---|
277 | ParseParams parseParams_; |
---|
278 | bool setDefaultValue_[5]; |
---|
279 | }; |
---|
280 | |
---|
281 | |
---|
282 | // ################################ |
---|
283 | // ### XMLPortObjectContainer ### |
---|
284 | // ################################ |
---|
285 | class _CoreExport XMLPortObjectContainer |
---|
286 | { |
---|
287 | public: |
---|
288 | XMLPortObjectContainer() |
---|
289 | { this->bApplyLoaderMask_ = false; } |
---|
290 | virtual ~XMLPortObjectContainer() {} |
---|
291 | |
---|
292 | inline const std::string& getName() const |
---|
293 | { return this->sectionname_; } |
---|
294 | |
---|
295 | virtual XMLPortObjectContainer& description(const std::string description) = 0; |
---|
296 | virtual const std::string& getDescription() = 0; |
---|
297 | |
---|
298 | bool identifierIsIncludedInLoaderMask(const Identifier* identifier); |
---|
299 | |
---|
300 | protected: |
---|
301 | std::string sectionname_; |
---|
302 | bool bApplyLoaderMask_; |
---|
303 | bool bLoadBefore_; |
---|
304 | }; |
---|
305 | |
---|
306 | template <class T, class O> |
---|
307 | class XMLPortClassObjectContainer : public XMLPortObjectContainer |
---|
308 | { |
---|
309 | public: |
---|
310 | XMLPortClassObjectContainer(const std::string sectionname, ExecutorMember<T>* loadexecutor, ExecutorMember<T>* saveexecutor, bool bApplyLoaderMask, bool bLoadBefore) |
---|
311 | { |
---|
312 | this->sectionname_ = sectionname; |
---|
313 | this->loadexecutor_ = loadexecutor; |
---|
314 | this->saveexecutor_ = saveexecutor; |
---|
315 | this->bApplyLoaderMask_ = bApplyLoaderMask; |
---|
316 | this->bLoadBefore_ = bLoadBefore; |
---|
317 | } |
---|
318 | |
---|
319 | XMLPortObjectContainer& port(T* object, Element& xmlelement, XMLPort::Mode mode) |
---|
320 | { |
---|
321 | if (mode == XMLPort::LoadObject) |
---|
322 | { |
---|
323 | try |
---|
324 | { |
---|
325 | Element* xmlsubelement; |
---|
326 | if ((this->sectionname_ != "") && (this->sectionname_.size() > 0)) |
---|
327 | xmlsubelement = xmlelement.FirstChildElement(this->sectionname_, false); |
---|
328 | else |
---|
329 | xmlsubelement = &xmlelement; |
---|
330 | |
---|
331 | if (xmlsubelement) |
---|
332 | { |
---|
333 | for (ticpp::Iterator<ticpp::Element> child = xmlsubelement->FirstChildElement(false); child != child.end(); child++) |
---|
334 | { |
---|
335 | Identifier* identifier = ID(child->Value()); |
---|
336 | if (identifier) |
---|
337 | { |
---|
338 | if (identifier->isA(Class(O))) |
---|
339 | { |
---|
340 | if (this->identifierIsIncludedInLoaderMask(identifier)) |
---|
341 | { |
---|
342 | COUT(4) << object->getLoaderIndentation() << "fabricating " << child->Value() << "..." << std::endl; |
---|
343 | |
---|
344 | O* newObject = (O*)identifier->fabricate(); |
---|
345 | newObject->setLoaderIndentation(object->getLoaderIndentation() + " "); |
---|
346 | newObject->setLevel(object->getLevel()); |
---|
347 | newObject->setNamespace(object->getNamespace()); |
---|
348 | |
---|
349 | if (this->bLoadBefore_) |
---|
350 | { |
---|
351 | newObject->XMLPort(*child, XMLPort::LoadObject); |
---|
352 | COUT(4) << object->getLoaderIndentation() << "assigning " << child->Value() << " (objectname " << newObject->getName() << ") to " << object->getIdentifier()->getName() << " (objectname " << object->getName() << ")" << std::endl; |
---|
353 | } |
---|
354 | else |
---|
355 | { |
---|
356 | COUT(4) << object->getLoaderIndentation() << "assigning " << child->Value() << " (object not yet loaded) to " << object->getIdentifier()->getName() << " (objectname " << object->getName() << ")" << std::endl; |
---|
357 | } |
---|
358 | |
---|
359 | COUT(5) << object->getLoaderIndentation(); |
---|
360 | (*this->loadexecutor_)(object, newObject); |
---|
361 | |
---|
362 | if (!this->bLoadBefore_) |
---|
363 | newObject->XMLPort(*child, XMLPort::LoadObject); |
---|
364 | |
---|
365 | COUT(5) << object->getLoaderIndentation() << "...fabricated " << child->Value() << " (objectname " << newObject->getName() << ")." << std::endl; |
---|
366 | } |
---|
367 | } |
---|
368 | else |
---|
369 | { |
---|
370 | COUT(2) << object->getLoaderIndentation() << "Warning: '" << child->Value() << "' is not a '" << Class(O)->getName() << "'." << std::endl; |
---|
371 | } |
---|
372 | } |
---|
373 | else |
---|
374 | { |
---|
375 | COUT(2) << object->getLoaderIndentation() << "Warning: '" << child->Value() << "' is not a valid classname." << std::endl; |
---|
376 | } |
---|
377 | } |
---|
378 | } |
---|
379 | } |
---|
380 | catch(ticpp::Exception& ex) |
---|
381 | { |
---|
382 | COUT(1) << std::endl; |
---|
383 | COUT(1) << "An error occurred in XMLPort.h while loading a '" << Class(O)->getName() << "' in '" << this->sectionname_ << "' of '" << object->getIdentifier()->getName() << "' (objectname: " << object->getName() << ") in " << object->getLevelfile() << ":" << std::endl; |
---|
384 | COUT(1) << ex.what() << std::endl; |
---|
385 | } |
---|
386 | } |
---|
387 | else |
---|
388 | { |
---|
389 | } |
---|
390 | |
---|
391 | return (*this); |
---|
392 | } |
---|
393 | |
---|
394 | virtual XMLPortObjectContainer& description(const std::string description) |
---|
395 | { this->loadexecutor_->setDescription(description); return (*this); } |
---|
396 | virtual const std::string& getDescription() |
---|
397 | { return this->loadexecutor_->getDescription(); } |
---|
398 | |
---|
399 | private: |
---|
400 | ExecutorMember<T>* loadexecutor_; |
---|
401 | ExecutorMember<T>* saveexecutor_; |
---|
402 | }; |
---|
403 | } |
---|
404 | |
---|
405 | #endif /* _XMLPort_H__ */ |
---|