Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1057 was 1057, checked in by landauf, 16 years ago

i hope this fixes a problem with visual studio

File size: 19.7 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        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
147            XMLPortParamContainer& port(T* object, Element& xmlelement, XMLPort::Mode mode)
148            {
149                this->parseParams_.object = object;
150                this->parseParams_.xmlelement = &xmlelement;
151                this->parseParams_.mode = mode;
152
153                if (mode == XMLPort::LoadObject)
154                {
155                    try
156                    {
157                        std::string attribute = xmlelement.GetAttribute(this->paramname_);
158                        if ((attribute.size() > 0) || (this->loadexecutor_->allDefaultValuesSet()))
159                        {
160                            COUT(5) << ((BaseObject*)object)->getLoaderIndentation() << "Loading parameter " << this->paramname_ << " in " << object->getIdentifier()->getName() << " (objectname " << ((BaseObject*)object)->getName() << ")." << std::endl << ((BaseObject*)object)->getLoaderIndentation();
161                            if (this->loadexecutor_->parse(object, attribute, ","))
162                                this->parseResult_ = PR_finished;
163                            else
164                                this->parseResult_ = PR_waiting_for_default_values;
165                        }
166                    }
167                    catch(ticpp::Exception& ex)
168                    {
169                        COUT(1) << std::endl;
170                        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;
171                        COUT(1) << ex.what() << std::endl;
172                    }
173                }
174                else
175                {
176                    if (this->saveexecutor_)
177                    {
178//                        xmlelement.SetAttribute(this->paramname_, "...");
179                    }
180                }
181
182                return (*this);
183            }
184
185            XMLPortParamContainer& port(const ParseParams& parseParams)
186            {
187                return this->port(parseParams.object, *parseParams.xmlelement, parseParams.mode);
188            }
189
190            XMLPortParamContainer& portIfWaitingForDefaultValues(const ParseResult& result, const ParseParams& params)
191            {
192                if (result == PR_waiting_for_default_values)
193                    return this->port(params);
194                else
195                    return (*this);
196            }
197
198            virtual XMLPortParamContainer& description(const std::string description)
199                { this->loadexecutor_->setDescription(description); return (*this); }
200            virtual const std::string& getDescription()
201                { return this->loadexecutor_->getDescription(); }
202
203            virtual XMLPortParamContainer& defaultValue(unsigned int index, const MultiTypeMath& param)
204            {
205                if (!this->loadexecutor_->defaultValueSet(index))
206                    this->loadexecutor_->setDefaultValue(index, param);
207                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
208            }
209            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1)
210            {
211                if (!this->loadexecutor_->defaultValueSet(0))
212                    this->loadexecutor_->setDefaultValues(param1);
213                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
214            }
215            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2)
216            {
217                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)))
218                    this->loadexecutor_->setDefaultValues(param1, param2);
219                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
220            }
221            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3)
222            {
223                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)) || (!this->loadexecutor_->defaultValueSet(2)))
224                    this->loadexecutor_->setDefaultValues(param1, param2, param3);
225                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
226            }
227            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4)
228            {
229                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)) || (!this->loadexecutor_->defaultValueSet(2)) || (!this->loadexecutor_->defaultValueSet(3)))
230                    this->loadexecutor_->setDefaultValues(param1, param2, param3, param4);
231                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
232            }
233            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5)
234            {
235                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)) || (!this->loadexecutor_->defaultValueSet(2)) || (!this->loadexecutor_->defaultValueSet(3)) || (!this->loadexecutor_->defaultValueSet(4)))
236                    this->loadexecutor_->setDefaultValues(param1, param2, param3, param4, param5);
237                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
238            }
239
240        private:
241            ExecutorMember<T>* loadexecutor_;
242            ExecutorMember<T>* saveexecutor_;
243            ParseParams parseParams_;
244    };
245
246
247    // ################################
248    // ###  XMLPortObjectContainer  ###
249    // ################################
250    class _CoreExport XMLPortObjectContainer
251    {
252        public:
253            XMLPortObjectContainer()
254                { this->bApplyLoaderMask_ = false; }
255            virtual ~XMLPortObjectContainer() {}
256
257            inline const std::string& getName() const
258                { return this->sectionname_; }
259
260            virtual XMLPortObjectContainer& description(const std::string description) = 0;
261            virtual const std::string& getDescription() = 0;
262
263            bool identifierIsIncludedInLoaderMask(const Identifier* identifier);
264
265        protected:
266            std::string sectionname_;
267            bool bApplyLoaderMask_;
268            bool bLoadBefore_;
269    };
270
271    template <class T, class O>
272    class XMLPortClassObjectContainer : public XMLPortObjectContainer
273    {
274        public:
275            XMLPortClassObjectContainer(const std::string sectionname, ExecutorMember<T>* loadexecutor, ExecutorMember<T>* saveexecutor, bool bApplyLoaderMask, bool bLoadBefore)
276            {
277                this->sectionname_ = sectionname;
278                this->loadexecutor_ = loadexecutor;
279                this->saveexecutor_ = saveexecutor;
280                this->bApplyLoaderMask_ = bApplyLoaderMask;
281                this->bLoadBefore_ = bLoadBefore;
282            }
283
284            XMLPortObjectContainer& port(T* object, Element& xmlelement, XMLPort::Mode mode)
285            {
286                if (mode == XMLPort::LoadObject)
287                {
288                    try
289                    {
290                        Element* xmlsubelement;
291                        if ((this->sectionname_ != "") && (this->sectionname_.size() > 0))
292                            xmlsubelement = xmlelement.FirstChildElement(this->sectionname_, false);
293                        else
294                            xmlsubelement = &xmlelement;
295
296                        if (xmlsubelement)
297                        {
298                            for (ticpp::Iterator<ticpp::Element> child = xmlsubelement->FirstChildElement(false); child != child.end(); child++)
299                            {
300                                Identifier* identifier = ID(child->Value());
301                                if (identifier)
302                                {
303                                    if (identifier->isA(Class(O)))
304                                    {
305                                        if (this->identifierIsIncludedInLoaderMask(identifier))
306                                        {
307                                            COUT(4) << ((BaseObject*)object)->getLoaderIndentation() << "fabricating " << child->Value() << "..." << std::endl;
308
309                                            BaseObject* newObject = identifier->fabricate();
310                                            newObject->setLoaderIndentation(((BaseObject*)object)->getLoaderIndentation() + "  ");
311                                            newObject->setLevel(((BaseObject*)object)->getLevel());
312                                            newObject->setNamespace(((BaseObject*)object)->getNamespace());
313
314                                            if (this->bLoadBefore_)
315                                            {
316                                                newObject->XMLPort(*child, XMLPort::LoadObject);
317                                                COUT(4) << ((BaseObject*)object)->getLoaderIndentation() << "assigning " << child->Value() << " (objectname " << newObject->getName() << ") to " << object->getIdentifier()->getName() << " (objectname " << ((BaseObject*)object)->getName() << ")" << std::endl;
318                                            }
319                                            else
320                                            {
321                                                COUT(4) << ((BaseObject*)object)->getLoaderIndentation() << "assigning " << child->Value() << " (object not yet loaded) to " << object->getIdentifier()->getName() << " (objectname " << ((BaseObject*)object)->getName() << ")" << std::endl;
322                                            }
323
324                                            COUT(5) << ((BaseObject*)object)->getLoaderIndentation();
325                                            (*this->loadexecutor_)(object, newObject);
326
327                                            if (!this->bLoadBefore_)
328                                                newObject->XMLPort(*child, XMLPort::LoadObject);
329
330                                            COUT(5) << ((BaseObject*)object)->getLoaderIndentation() << "...fabricated " << child->Value() << " (objectname " << newObject->getName() << ")." << std::endl;
331                                        }
332                                    }
333                                    else
334                                    {
335                                        COUT(2) << ((BaseObject*)object)->getLoaderIndentation() << "Warning: '" << child->Value() << "' is not a '" << Class(O)->getName() << "'." << std::endl;
336                                    }
337                                }
338                                else
339                                {
340                                    COUT(2) << object->getLoaderIndentation() << "Warning: '" << child->Value() << "' is not a valid classname." << std::endl;
341                                }
342                            }
343                        }
344                    }
345                    catch(ticpp::Exception& ex)
346                    {
347                        COUT(1) << std::endl;
348                        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;
349                        COUT(1) << ex.what() << std::endl;
350                    }
351                }
352                else
353                {
354                }
355
356                return (*this);
357            }
358
359            virtual XMLPortObjectContainer& description(const std::string description)
360                { this->loadexecutor_->setDescription(description); return (*this); }
361            virtual const std::string& getDescription()
362                { return this->loadexecutor_->getDescription(); }
363
364        private:
365            ExecutorMember<T>* loadexecutor_;
366            ExecutorMember<T>* saveexecutor_;
367    };
368}
369
370#endif /* _XMLPort_H__ */
Note: See TracBrowser for help on using the repository browser.