Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/XMLPort.h @ 1059

Last change on this file since 1059 was 1059, checked in by rgrieder, 16 years ago
  • updated VC++ files
  • 3 tiny bugfixes
File size: 19.8 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#ifndef _XMLPort_H__
30#define _XMLPort_H__
31
32#include "util/XMLIncludes.h"
33#include "util/MultiTypeMath.h"
34#include "util/tinyxml/ticpp.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, orxonox::createExecutor(orxonox::createFunctor(&classname::loadfunction), #loadfunction), orxonox::createExecutor(orxonox::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, orxonox::createExecutor(orxonox::createFunctor loadtemplate (&classname::loadfunction), #loadfunction), orxonox::createExecutor(orxonox::createFunctor savetemplate (&classname::savefunction), #savefunction), xmlelement, mode)
47
48#define XMLPortParamLoadOnly(classname, paramname, loadfunction, xmlelement, mode) \
49    XMLPortParamGeneric(xmlcontainer##loadfunction##0, classname, paramname, orxonox::createExecutor(orxonox::createFunctor(&classname::loadfunction), #loadfunction), 0, xmlelement, mode)
50#define XMLPortParamLoadOnly_Template(classname, paramname, loadtemplate, loadfunction, xmlelement, mode) \
51    XMLPortParamGeneric(xmlcontainer##loadfunction##0, classname, paramname, orxonox::createExecutor(orxonox::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, orxonox::createExecutor(orxonox::createFunctor(&classname::loadfunction), #loadfunction), orxonox::createExecutor(orxonox::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, orxonox::createExecutor(orxonox::createFunctor loadtemplate (&classname::loadfunction), #loadfunction), orxonox::createExecutor(orxonox::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
78namespace 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    public:
99        enum ParseResult
100        {
101            PR_not_started,
102            PR_finished,
103            PR_waiting_for_default_values
104        };
105
106        public:
107            XMLPortParamContainer()
108                { this->parseResult_ = PR_not_started; }
109            virtual ~XMLPortParamContainer() {}
110
111            inline const std::string& getName() const
112                { return this->paramname_; }
113
114            virtual XMLPortParamContainer& description(const std::string description) = 0;
115            virtual const std::string& getDescription() = 0;
116
117            virtual XMLPortParamContainer& defaultValue(unsigned int index, const MultiTypeMath& param) = 0;
118            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1) = 0;
119            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2) = 0;
120            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) = 0;
121            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) = 0;
122            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) = 0;
123
124        protected:
125            std::string paramname_;
126            ParseResult parseResult_;
127
128    };
129
130    template <class T>
131    class XMLPortClassParamContainer : public XMLPortParamContainer
132    {
133        struct ParseParams
134        {
135            T* object;
136            Element* xmlelement;
137            XMLPort::Mode mode;
138        };
139
140        public:
141            XMLPortClassParamContainer(const std::string paramname, ExecutorMember<T>* loadexecutor, ExecutorMember<T>* saveexecutor)
142            {
143                this->paramname_ = paramname;
144                this->loadexecutor_ = loadexecutor;
145                this->saveexecutor_ = saveexecutor;
146            }
147
148            XMLPortParamContainer& port(T* object, Element& xmlelement, XMLPort::Mode mode)
149            {
150                this->parseParams_.object = object;
151                this->parseParams_.xmlelement = &xmlelement;
152                this->parseParams_.mode = mode;
153
154                if (mode == XMLPort::LoadObject)
155                {
156                    try
157                    {
158                        std::string attribute = xmlelement.GetAttribute(this->paramname_);
159                        if ((attribute.size() > 0) || (this->loadexecutor_->allDefaultValuesSet()))
160                        {
161                            COUT(5) << ((BaseObject*)object)->getLoaderIndentation() << "Loading parameter " << this->paramname_ << " in " << object->getIdentifier()->getName() << " (objectname " << ((BaseObject*)object)->getName() << ")." << std::endl << ((BaseObject*)object)->getLoaderIndentation();
162                            if (this->loadexecutor_->parse(object, attribute, ","))
163                                this->parseResult_ = PR_finished;
164                            else
165                                this->parseResult_ = PR_waiting_for_default_values;
166                        }
167                    }
168                    catch(ticpp::Exception& ex)
169                    {
170                        COUT(1) << std::endl;
171                        COUT(1) << "An error occurred in XMLPort.h while loading attribute '" << this->paramname_ << "' of '" << object->getIdentifier()->getName() << "' (objectname: " << ((BaseObject*)object)->getName() << ") in " << ((BaseObject*)object)->getLevelfile() << ":" << std::endl;
172                        COUT(1) << ex.what() << std::endl;
173                    }
174                }
175                else
176                {
177                    if (this->saveexecutor_)
178                    {
179//                        xmlelement.SetAttribute(this->paramname_, "...");
180                    }
181                }
182
183                return (*this);
184            }
185
186            XMLPortParamContainer& port(const ParseParams& parseParams)
187            {
188                return this->port(parseParams.object, *parseParams.xmlelement, parseParams.mode);
189            }
190
191            XMLPortParamContainer& portIfWaitingForDefaultValues(const ParseResult& result, const ParseParams& params)
192            {
193                if (result == PR_waiting_for_default_values)
194                    return this->port(params);
195                else
196                    return (*this);
197            }
198
199            virtual XMLPortParamContainer& description(const std::string description)
200                { this->loadexecutor_->setDescription(description); return (*this); }
201            virtual const std::string& getDescription()
202                { return this->loadexecutor_->getDescription(); }
203
204            virtual XMLPortParamContainer& defaultValue(unsigned int index, const MultiTypeMath& param)
205            {
206                if (!this->loadexecutor_->defaultValueSet(index))
207                    this->loadexecutor_->setDefaultValue(index, param);
208                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
209            }
210            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1)
211            {
212                if (!this->loadexecutor_->defaultValueSet(0))
213                    this->loadexecutor_->setDefaultValues(param1);
214                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
215            }
216            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2)
217            {
218                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)))
219                    this->loadexecutor_->setDefaultValues(param1, param2);
220                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
221            }
222            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3)
223            {
224                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)) || (!this->loadexecutor_->defaultValueSet(2)))
225                    this->loadexecutor_->setDefaultValues(param1, param2, param3);
226                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
227            }
228            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4)
229            {
230                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)) || (!this->loadexecutor_->defaultValueSet(2)) || (!this->loadexecutor_->defaultValueSet(3)))
231                    this->loadexecutor_->setDefaultValues(param1, param2, param3, param4);
232                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
233            }
234            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5)
235            {
236                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)) || (!this->loadexecutor_->defaultValueSet(2)) || (!this->loadexecutor_->defaultValueSet(3)) || (!this->loadexecutor_->defaultValueSet(4)))
237                    this->loadexecutor_->setDefaultValues(param1, param2, param3, param4, param5);
238                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
239            }
240
241        private:
242            ExecutorMember<T>* loadexecutor_;
243            ExecutorMember<T>* saveexecutor_;
244            ParseParams parseParams_;
245    };
246
247
248    // ################################
249    // ###  XMLPortObjectContainer  ###
250    // ################################
251    class _CoreExport XMLPortObjectContainer
252    {
253        public:
254            XMLPortObjectContainer()
255                { this->bApplyLoaderMask_ = false; }
256            virtual ~XMLPortObjectContainer() {}
257
258            inline const std::string& getName() const
259                { return this->sectionname_; }
260
261            virtual XMLPortObjectContainer& description(const std::string description) = 0;
262            virtual const std::string& getDescription() = 0;
263
264            bool identifierIsIncludedInLoaderMask(const Identifier* identifier);
265
266        protected:
267            std::string sectionname_;
268            bool bApplyLoaderMask_;
269            bool bLoadBefore_;
270    };
271
272    template <class T, class O>
273    class XMLPortClassObjectContainer : public XMLPortObjectContainer
274    {
275        public:
276            XMLPortClassObjectContainer(const std::string sectionname, ExecutorMember<T>* loadexecutor, ExecutorMember<T>* saveexecutor, bool bApplyLoaderMask, bool bLoadBefore)
277            {
278                this->sectionname_ = sectionname;
279                this->loadexecutor_ = loadexecutor;
280                this->saveexecutor_ = saveexecutor;
281                this->bApplyLoaderMask_ = bApplyLoaderMask;
282                this->bLoadBefore_ = bLoadBefore;
283            }
284
285            XMLPortObjectContainer& port(T* object, Element& xmlelement, XMLPort::Mode mode)
286            {
287                if (mode == XMLPort::LoadObject)
288                {
289                    try
290                    {
291                        Element* xmlsubelement;
292                        if ((this->sectionname_ != "") && (this->sectionname_.size() > 0))
293                            xmlsubelement = xmlelement.FirstChildElement(this->sectionname_, false);
294                        else
295                            xmlsubelement = &xmlelement;
296
297                        if (xmlsubelement)
298                        {
299                            for (ticpp::Iterator<ticpp::Element> child = xmlsubelement->FirstChildElement(false); child != child.end(); child++)
300                            {
301                                Identifier* identifier = ID(child->Value());
302                                if (identifier)
303                                {
304                                    if (identifier->isA(Class(O)))
305                                    {
306                                        if (this->identifierIsIncludedInLoaderMask(identifier))
307                                        {
308                                            COUT(4) << ((BaseObject*)object)->getLoaderIndentation() << "fabricating " << child->Value() << "..." << std::endl;
309
310                                            BaseObject* newObject = identifier->fabricate();
311                                            newObject->setLoaderIndentation(((BaseObject*)object)->getLoaderIndentation() + "  ");
312                                            newObject->setLevel(((BaseObject*)object)->getLevel());
313                                            newObject->setNamespace(((BaseObject*)object)->getNamespace());
314
315                                            if (this->bLoadBefore_)
316                                            {
317                                                newObject->XMLPort(*child, XMLPort::LoadObject);
318                                                COUT(4) << ((BaseObject*)object)->getLoaderIndentation() << "assigning " << child->Value() << " (objectname " << newObject->getName() << ") to " << object->getIdentifier()->getName() << " (objectname " << ((BaseObject*)object)->getName() << ")" << std::endl;
319                                            }
320                                            else
321                                            {
322                                                COUT(4) << ((BaseObject*)object)->getLoaderIndentation() << "assigning " << child->Value() << " (object not yet loaded) to " << object->getIdentifier()->getName() << " (objectname " << ((BaseObject*)object)->getName() << ")" << std::endl;
323                                            }
324
325                                            COUT(5) << ((BaseObject*)object)->getLoaderIndentation();
326                                            (*this->loadexecutor_)(object, newObject);
327
328                                            if (!this->bLoadBefore_)
329                                                newObject->XMLPort(*child, XMLPort::LoadObject);
330
331                                            COUT(5) << ((BaseObject*)object)->getLoaderIndentation() << "...fabricated " << child->Value() << " (objectname " << newObject->getName() << ")." << std::endl;
332                                        }
333                                    }
334                                    else
335                                    {
336                                        COUT(2) << ((BaseObject*)object)->getLoaderIndentation() << "Warning: '" << child->Value() << "' is not a '" << Class(O)->getName() << "'." << std::endl;
337                                    }
338                                }
339                                else
340                                {
341                                    COUT(2) << object->getLoaderIndentation() << "Warning: '" << child->Value() << "' is not a valid classname." << std::endl;
342                                }
343                            }
344                        }
345                    }
346                    catch(ticpp::Exception& ex)
347                    {
348                        COUT(1) << std::endl;
349                        COUT(1) << "An error occurred in XMLPort.h while loading a '" << Class(O)->getName() << "' in '" << this->sectionname_ << "' of '" << object->getIdentifier()->getName() << "' (objectname: " << ((BaseObject*)object)->getName() << ") in " << object->getLevelfile() << ":" << std::endl;
350                        COUT(1) << ex.what() << std::endl;
351                    }
352                }
353                else
354                {
355                }
356
357                return (*this);
358            }
359
360            virtual XMLPortObjectContainer& description(const std::string description)
361                { this->loadexecutor_->setDescription(description); return (*this); }
362            virtual const std::string& getDescription()
363                { return this->loadexecutor_->getDescription(); }
364
365        private:
366            ExecutorMember<T>* loadexecutor_;
367            ExecutorMember<T>* saveexecutor_;
368    };
369}
370
371#endif /* _XMLPort_H__ */
Note: See TracBrowser for help on using the repository browser.