Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core3/src/core/XMLPort.h @ 1592

Last change on this file since 1592 was 1592, checked in by landauf, 16 years ago
  • Some small changes in XMLPortParam: It's now possible to add functions of another class. an example is setPosition and getPosition that are directly referred to Ogre::Node. Use XMLPortParamExtern if you need this. It takes the object to call the functions on as an additional argument (in our example: this→node_).
  • Removed loadParams functions and reduced the usage of XMLPortParamLoadOnly in several classes
  • Property svn:eol-style set to native
File size: 20.6 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 "CorePrereqs.h"
33
34#include "util/Debug.h"
35#include "util/XMLIncludes.h"
36#include "util/MultiTypeMath.h"
37#include "tinyxml/ticpp.h"
38#include "Executor.h"
39#include "CoreIncludes.h"
40#include "BaseObject.h"
41
42
43#define XMLPortParam(classname, paramname, loadfunction, savefunction, xmlelement, mode) \
44    XMLPortParamGeneric(xmlcontainer##loadfunction##savefunction, classname, this, paramname, orxonox::createExecutor(orxonox::createFunctor(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction), orxonox::createExecutor(orxonox::createFunctor(&classname::savefunction), std::string( #classname ) + "::" + #savefunction), xmlelement, mode)
45#define XMLPortParamTemplate(classname, paramname, loadfunction, savefunction, xmlelement, mode, ...) \
46    XMLPortParamGeneric(xmlcontainer##loadfunction##savefunction, classname, this, paramname, orxonox::createExecutor(orxonox::createFunctor< __VA_ARGS__ >(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction), orxonox::createExecutor(orxonox::createFunctor(&classname::savefunction), std::string( #classname ) + "::" + #savefunction), xmlelement, mode)
47
48#define XMLPortParamLoadOnly(classname, paramname, loadfunction, xmlelement, mode) \
49    XMLPortParamGeneric(xmlcontainer##loadfunction##0, classname, this, paramname, orxonox::createExecutor(orxonox::createFunctor(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction), 0, xmlelement, mode)
50#define XMLPortParamLoadOnlyTemplate(classname, paramname, loadfunction, xmlelement, mode, ...) \
51    XMLPortParamGeneric(xmlcontainer##loadfunction##0, classname, this, paramname, orxonox::createExecutor(orxonox::createFunctor< __VA_ARGS__ >(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction), 0, xmlelement, mode)
52
53#define XMLPortParamExtern(classname, object, paramname, loadfunction, savefunction, xmlelement, mode) \
54    XMLPortParamGeneric(xmlcontainer##loadfunction##savefunction, classname, object, paramname, orxonox::createExecutor(orxonox::createFunctor(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction), orxonox::createExecutor(orxonox::createFunctor(&classname::savefunction), std::string( #classname ) + "::" + #savefunction), xmlelement, mode);
55#define XMLPortParamExternTemplate(classname, object, paramname, loadfunction, savefunction, xmlelement, mode, ...) \
56    XMLPortParamGeneric(xmlcontainer##loadfunction##savefunction, classname, object, paramname, orxonox::createExecutor(orxonox::createFunctor< __VA_ARGS__ >(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction), orxonox::createExecutor(orxonox::createFunctor(&classname::savefunction), std::string( #classname ) + "::" + #savefunction), xmlelement, mode);
57
58#define XMLPortParamGeneric(containername, classname, object, paramname, loadexecutor, saveexecutor, xmlelement, mode) \
59    orxonox::XMLPortClassParamContainer<classname>* containername = (orxonox::XMLPortClassParamContainer<classname>*)(this->getIdentifier()->getXMLPortParamContainer(paramname)); \
60    if (!containername) \
61    { \
62        containername = new orxonox::XMLPortClassParamContainer<classname>(std::string(paramname), loadexecutor, saveexecutor); \
63        this->getIdentifier()->addXMLPortParamContainer(paramname, containername); \
64    } \
65    containername->port(object, xmlelement, mode)
66
67
68#define XMLPortObject(classname, objectclass, sectionname, loadfunction, savefunction, xmlelement, mode, bApplyLoaderMask, bLoadBefore) \
69    XMLPortObjectGeneric(xmlcontainer##loadfunction##savefunction, classname, objectclass, sectionname, orxonox::createExecutor(orxonox::createFunctor(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction), orxonox::createExecutor(orxonox::createFunctor(&classname::savefunction), std::string( #classname ) + "::" + #savefunction), xmlelement, mode, bApplyLoaderMask, bLoadBefore)
70#define XMLPortObjectTemplate(classname, objectclass, sectionname, loadfunction, savefunction, xmlelement, mode, bApplyLoaderMask, bLoadBefore, ...) \
71    XMLPortObjectGeneric(xmlcontainer##loadfunction##savefunction, classname, objectclass, sectionname, orxonox::createExecutor(orxonox::createFunctor< __VA_ARGS__ >(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction), orxonox::createExecutor(orxonox::createFunctor(&classname::savefunction), std::string( #classname ) + "::" + #savefunction), xmlelement, mode, bApplyLoaderMask, bLoadBefore)
72
73#define XMLPortObjectGeneric(containername, classname, objectclass, sectionname, loadexecutor, saveexecutor, xmlelement, mode, bApplyLoaderMask, bLoadBefore) \
74    orxonox::XMLPortClassObjectContainer<classname, objectclass>* containername = (orxonox::XMLPortClassObjectContainer<classname, objectclass>*)(this->getIdentifier()->getXMLPortObjectContainer(sectionname)); \
75    if (!containername) \
76    { \
77        containername = new orxonox::XMLPortClassObjectContainer<classname, objectclass>(std::string(sectionname), loadexecutor, saveexecutor, bApplyLoaderMask, bLoadBefore); \
78        this->getIdentifier()->addXMLPortObjectContainer(sectionname, containername); \
79    } \
80    containername->port(this, xmlelement, mode)
81
82
83namespace orxonox
84{
85    // ###############################
86    // ###  XMLPortParamContainer  ###
87    // ###############################
88    class _CoreExport XMLPortParamContainer
89    {
90    public:
91        enum ParseResult
92        {
93            PR_not_started,
94            PR_finished,
95            PR_waiting_for_default_values
96        };
97
98        public:
99            XMLPortParamContainer()
100                { this->parseResult_ = PR_not_started; }
101            virtual ~XMLPortParamContainer() {}
102
103            inline const std::string& getName() const
104                { return this->paramname_; }
105            inline void setIdentifier(Identifier* identifier)
106                { this->identifier_ = identifier; }
107
108            virtual XMLPortParamContainer& description(const std::string description) = 0;
109            virtual const std::string& getDescription() = 0;
110
111            virtual XMLPortParamContainer& defaultValue(unsigned int index, const MultiTypeMath& param) = 0;
112            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1) = 0;
113            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2) = 0;
114            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) = 0;
115            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) = 0;
116            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) = 0;
117
118        protected:
119            std::string paramname_;
120            ParseResult parseResult_;
121            Identifier* identifier_;
122    };
123
124    template <class T>
125    class XMLPortClassParamContainer : public XMLPortParamContainer
126    {
127        struct ParseParams
128        {
129            T* object;
130            Element* xmlelement;
131            XMLPort::Mode mode;
132        };
133
134        public:
135            XMLPortClassParamContainer(const std::string paramname, ExecutorMember<T>* loadexecutor, ExecutorMember<T>* saveexecutor)
136            {
137                this->paramname_ = paramname;
138                this->loadexecutor_ = loadexecutor;
139                this->saveexecutor_ = saveexecutor;
140            }
141
142            XMLPortParamContainer& port(T* object, Element& xmlelement, XMLPort::Mode mode)
143            {
144                this->parseParams_.object = object;
145                this->parseParams_.xmlelement = &xmlelement;
146                this->parseParams_.mode = mode;
147
148                if (mode == XMLPort::LoadObject)
149                {
150                    try
151                    {
152                        std::string attribute = xmlelement.GetAttribute(this->paramname_);
153                        if ((attribute.size() > 0) || (this->loadexecutor_->allDefaultValuesSet()))
154                        {
155                            COUT(5) << ((BaseObject*)object)->getLoaderIndentation() << "Loading parameter " << this->paramname_ << " in " << this->identifier_->getName() << " (objectname " << ((BaseObject*)object)->getName() << ")." << std::endl << ((BaseObject*)object)->getLoaderIndentation();
156                            if (this->loadexecutor_->parse(object, attribute, ","))
157                                this->parseResult_ = PR_finished;
158                            else
159                                this->parseResult_ = PR_waiting_for_default_values;
160                        }
161                    }
162                    catch (ticpp::Exception& ex)
163                    {
164                        COUT(1) << std::endl;
165                        COUT(1) << "An error occurred in XMLPort.h while loading attribute '" << this->paramname_ << "' of '" << this->identifier_->getName() << "' (objectname: " << ((BaseObject*)object)->getName() << ") in " << ((BaseObject*)object)->getLevelfile() << ":" << std::endl;
166                        COUT(1) << ex.what() << std::endl;
167                    }
168                }
169                else
170                {
171                    if (this->saveexecutor_)
172                    {
173//                        xmlelement.SetAttribute(this->paramname_, "...");
174                    }
175                }
176
177                return (*this);
178            }
179
180            XMLPortParamContainer& port(const ParseParams& parseParams)
181            {
182                return this->port(parseParams.object, *parseParams.xmlelement, parseParams.mode);
183            }
184
185            XMLPortParamContainer& portIfWaitingForDefaultValues(const ParseResult& result, const ParseParams& params)
186            {
187                if (result == PR_waiting_for_default_values)
188                    return this->port(params);
189                else
190                    return (*this);
191            }
192
193            virtual XMLPortParamContainer& description(const std::string description)
194                { this->loadexecutor_->setDescription(description); return (*this); }
195            virtual const std::string& getDescription()
196                { return this->loadexecutor_->getDescription(); }
197
198            virtual XMLPortParamContainer& defaultValue(unsigned int index, const MultiTypeMath& param)
199            {
200                if (!this->loadexecutor_->defaultValueSet(index))
201                    this->loadexecutor_->setDefaultValue(index, param);
202                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
203            }
204            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1)
205            {
206                if (!this->loadexecutor_->defaultValueSet(0))
207                    this->loadexecutor_->setDefaultValues(param1);
208                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
209            }
210            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2)
211            {
212                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)))
213                    this->loadexecutor_->setDefaultValues(param1, param2);
214                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
215            }
216            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3)
217            {
218                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)) || (!this->loadexecutor_->defaultValueSet(2)))
219                    this->loadexecutor_->setDefaultValues(param1, param2, param3);
220                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
221            }
222            virtual XMLPortParamContainer& defaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4)
223            {
224                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)) || (!this->loadexecutor_->defaultValueSet(2)) || (!this->loadexecutor_->defaultValueSet(3)))
225                    this->loadexecutor_->setDefaultValues(param1, param2, param3, param4);
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, const MultiTypeMath& param5)
229            {
230                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)) || (!this->loadexecutor_->defaultValueSet(2)) || (!this->loadexecutor_->defaultValueSet(3)) || (!this->loadexecutor_->defaultValueSet(4)))
231                    this->loadexecutor_->setDefaultValues(param1, param2, param3, param4, param5);
232                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
233            }
234
235        private:
236            ExecutorMember<T>* loadexecutor_;
237            ExecutorMember<T>* saveexecutor_;
238            ParseParams parseParams_;
239    };
240
241
242    // ################################
243    // ###  XMLPortObjectContainer  ###
244    // ################################
245    class _CoreExport XMLPortObjectContainer
246    {
247        public:
248            XMLPortObjectContainer()
249                { this->bApplyLoaderMask_ = false; }
250            virtual ~XMLPortObjectContainer() {}
251
252            inline const std::string& getName() const
253                { return this->sectionname_; }
254
255            virtual XMLPortObjectContainer& description(const std::string description) = 0;
256            virtual const std::string& getDescription() = 0;
257
258            bool identifierIsIncludedInLoaderMask(const Identifier* identifier);
259
260        protected:
261            std::string sectionname_;
262            bool bApplyLoaderMask_;
263            bool bLoadBefore_;
264    };
265
266    template <class T, class O>
267    class XMLPortClassObjectContainer : public XMLPortObjectContainer
268    {
269        public:
270            XMLPortClassObjectContainer(const std::string sectionname, ExecutorMember<T>* loadexecutor, ExecutorMember<T>* saveexecutor, bool bApplyLoaderMask, bool bLoadBefore)
271            {
272                this->sectionname_ = sectionname;
273                this->loadexecutor_ = loadexecutor;
274                this->saveexecutor_ = saveexecutor;
275                this->bApplyLoaderMask_ = bApplyLoaderMask;
276                this->bLoadBefore_ = bLoadBefore;
277            }
278
279            XMLPortObjectContainer& port(T* object, Element& xmlelement, XMLPort::Mode mode)
280            {
281                if (mode == XMLPort::LoadObject)
282                {
283                    try
284                    {
285                        Element* xmlsubelement;
286                        if ((this->sectionname_ != "") && (this->sectionname_.size() > 0))
287                            xmlsubelement = xmlelement.FirstChildElement(this->sectionname_, false);
288                        else
289                            xmlsubelement = &xmlelement;
290
291                        if (xmlsubelement)
292                        {
293                            for (ticpp::Iterator<ticpp::Element> child = xmlsubelement->FirstChildElement(false); child != child.end(); child++)
294                            {
295                                Identifier* identifier = ID(child->Value());
296                                if (identifier)
297                                {
298                                    if (identifier->isA(Class(O)))
299                                    {
300                                        if (this->identifierIsIncludedInLoaderMask(identifier))
301                                        {
302                                            COUT(4) << ((BaseObject*)object)->getLoaderIndentation() << "fabricating " << child->Value() << "..." << std::endl;
303
304                                            BaseObject* newObject = identifier->fabricate();
305                                            newObject->setLoaderIndentation(((BaseObject*)object)->getLoaderIndentation() + "  ");
306                                            newObject->setLevel(((BaseObject*)object)->getLevel());
307                                            newObject->setNamespace(((BaseObject*)object)->getNamespace());
308
309                                            if (this->bLoadBefore_)
310                                            {
311                                                newObject->XMLPort(*child, XMLPort::LoadObject);
312                                                COUT(4) << ((BaseObject*)object)->getLoaderIndentation() << "assigning " << child->Value() << " (objectname " << newObject->getName() << ") to " << object->getIdentifier()->getName() << " (objectname " << ((BaseObject*)object)->getName() << ")" << std::endl;
313                                            }
314                                            else
315                                            {
316                                                COUT(4) << ((BaseObject*)object)->getLoaderIndentation() << "assigning " << child->Value() << " (object not yet loaded) to " << object->getIdentifier()->getName() << " (objectname " << ((BaseObject*)object)->getName() << ")" << std::endl;
317                                            }
318
319                                            COUT(5) << ((BaseObject*)object)->getLoaderIndentation();
320                                            (*this->loadexecutor_)(object, newObject);
321
322                                            if (!this->bLoadBefore_)
323                                                newObject->XMLPort(*child, XMLPort::LoadObject);
324
325                                            COUT(5) << ((BaseObject*)object)->getLoaderIndentation() << "...fabricated " << child->Value() << " (objectname " << newObject->getName() << ")." << std::endl;
326                                        }
327                                    }
328                                    else
329                                    {
330                                        COUT(2) << ((BaseObject*)object)->getLoaderIndentation() << "Warning: '" << child->Value() << "' is not a '" << Class(O)->getName() << "'." << std::endl;
331                                    }
332                                }
333                                else
334                                {
335                                    COUT(2) << object->getLoaderIndentation() << "Warning: '" << child->Value() << "' is not a valid classname." << std::endl;
336                                }
337                            }
338                        }
339                    }
340                    catch (ticpp::Exception& ex)
341                    {
342                        COUT(1) << std::endl;
343                        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;
344                        COUT(1) << ex.what() << std::endl;
345                    }
346                }
347                else
348                {
349                }
350
351                return (*this);
352            }
353
354            virtual XMLPortObjectContainer& description(const std::string description)
355                { this->loadexecutor_->setDescription(description); return (*this); }
356            virtual const std::string& getDescription()
357                { return this->loadexecutor_->getDescription(); }
358
359        private:
360            ExecutorMember<T>* loadexecutor_;
361            ExecutorMember<T>* saveexecutor_;
362    };
363}
364
365#endif /* _XMLPort_H__ */
Note: See TracBrowser for help on using the repository browser.