Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/XMLPort.h @ 932

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

expanded Executor

File size: 15.0 KB
Line 
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 "Functor.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, loading) \
44    XMLPortParamGeneric(classname, paramname, xmlcontainer##loadfunction##savefunction, createFunctor(&classname::loadfunction), createFunctor(&classname::savefunction), xmlelement, loading)
45#define XMLPortParam_Template(classname, paramname, loadtemplate, loadfunction, savetemplate, savefunction, xmlelement, loading) \
46    XMLPortParamGeneric(classname, paramname, xmlcontainer##loadfunction##savefunction, createFunctor loadtemplate (&classname::loadfunction), createFunctor savetemplate (&classname::savefunction), xmlelement, loading)
47
48
49#define XMLPortParamLoadOnly(classname, paramname, loadfunction, xmlelement, loading) \
50    XMLPortParamGeneric(classname, paramname, xmlcontainer##loadfunction##0, createFunctor(&classname::loadfunction), 0, xmlelement, loading)
51#define XMLPortParamLoadOnly_Template(classname, paramname, loadtemplate, loadfunction, xmlelement, loading) \
52    XMLPortParamGeneric(classname, paramname, xmlcontainer##loadfunction##0, createFunctor loadtemplate (&classname::loadfunction), 0, xmlelement, loading)
53
54
55#define XMLPortParamGeneric(classname, paramname, containername, loadfunctor, savefunctor, xmlelement, loading) \
56    orxonox::XMLPortClassParamContainer<classname>* containername = (orxonox::XMLPortClassParamContainer<classname>*)(this->getIdentifier()->getXMLPortParamContainer(paramname)); \
57    if (!containername) \
58    { \
59        containername = new orxonox::XMLPortClassParamContainer<classname>(this->getIdentifier()->getName(), std::string(paramname), loadfunctor, savefunctor); \
60        this->getIdentifier()->addXMLPortParamContainer(paramname, containername); \
61    } \
62    containername->port(this, xmlelement, loading)
63
64
65#define XMLPortObject(classname, objectclass, sectionname, loadfunction, savefunction, xmlelement, loading, bApplyLoaderMask, bLoadBefore) \
66    orxonox::XMLPortClassObjectContainer<classname, objectclass>* xmlcontainer##loadfunction##savefunction = (orxonox::XMLPortClassObjectContainer<classname, objectclass>*)(this->getIdentifier()->getXMLPortObjectContainer(sectionname)); \
67    if (!xmlcontainer##loadfunction##savefunction) \
68    { \
69        xmlcontainer##loadfunction##savefunction = new orxonox::XMLPortClassObjectContainer<classname, objectclass>(this->getIdentifier()->getName(), std::string(sectionname), &classname::loadfunction, &classname::savefunction, bApplyLoaderMask, bLoadBefore); \
70        this->getIdentifier()->addXMLPortObjectContainer(sectionname, xmlcontainer##loadfunction##savefunction); \
71    } \
72    xmlcontainer##loadfunction##savefunction->port(this, xmlelement, loading)
73
74
75namespace orxonox
76{
77
78#ifndef _XMLPort_Mode__
79#define _XMLPort_Mode__
80    namespace XMLPort
81    {
82        enum Mode
83        {
84            LoadObject,
85            SaveObject
86        };
87    }
88#endif
89
90    // ###############################
91    // ###  XMLPortParamContainer  ###
92    // ###############################
93    class _CoreExport XMLPortParamContainer
94    {
95        public:
96            XMLPortParamContainer();
97
98            inline const std::string& getName() const
99                { return this->paramname_; }
100
101            XMLPortParamContainer& description(const std::string description);
102            const std::string& getDescription();
103
104        protected:
105            std::string classname_;
106            std::string paramname_;
107
108        private:
109            LanguageEntryLabel description_;
110            bool bAddedDescription_;
111            bool bAddedDefaultValues_;
112    };
113
114    template <class T>
115    class XMLPortClassParamContainer : public XMLPortParamContainer
116    {
117        public:
118            XMLPortClassParamContainer(const std::string classname, const std::string paramname, FunctorMember<T>* loadfunction, FunctorMember<T>* savefunction)
119            {
120                this->classname_ = classname;
121                this->paramname_ = paramname;
122                this->loadfunction_ = loadfunction;
123                this->savefunction_ = savefunction;
124            }
125
126            XMLPortParamContainer& port(T* object, Element& xmlelement, XMLPort::Mode mode)
127            {
128                if (mode == XMLPort::LoadObject)
129                {
130                    try
131                    {
132                        std::string attribute = xmlelement.GetAttribute(this->paramname_);
133                        if (attribute.size() > 0)
134                        {
135                            SubString tokens(attribute, ",", SubString::WhiteSpaces, false, '\\', '"', '(', ')', '\0');
136                            if ((unsigned int)tokens.size() >= (unsigned int)this->loadfunction_->getParamCount())
137                            {
138                                COUT(5) << object->getLoaderIndentation() << "Loading parameter " << this->paramname_ << " in " << this->classname_ << " (objectname " << object->getName() << ") with ";
139                                if (this->loadfunction_->getParamCount() == 1)
140                                {
141                                    COUT(5) << "1 parameter (using whole string):" << std::endl;
142                                    COUT(5) << object->getLoaderIndentation() << "  " << attribute << std::endl;
143                                    (*this->loadfunction_)(object, MultiTypeMath(attribute));
144                                }
145                                else
146                                {
147                                    COUT(5) << tokens.size() << " parameter (using MultiTypeMath)." << std::endl;
148                                    MultiTypeMath param1 = MT_null, param2 = MT_null, param3 = MT_null, param4 = MT_null, param5 = MT_null;
149                                    if (tokens.size() >= 1) param1 = tokens[0];
150                                    if (tokens.size() >= 2) param2 = tokens[1];
151                                    if (tokens.size() >= 3) param3 = tokens[2];
152                                    if (tokens.size() >= 4) param4 = tokens[3];
153                                    if (tokens.size() >= 5) param5 = tokens[4];
154                                    COUT(5) << object->getLoaderIndentation() << "  " << attribute << std::endl;
155                                    COUT(5) << object->getLoaderIndentation() << "  " << param1 << ", " << param2 << ", " << param3 << ", " << param4 << ", " << param5 << std::endl;
156
157                                    (*this->loadfunction_)(object, param1, param2, param3, param4, param5);
158                                }
159                            }
160                            else
161                            {
162                                COUT(2) << object->getLoaderIndentation() << "Warning: Parameter \"" << this->paramname_ << "\" in \"" << this->classname_ << "\" (objectname: " << object->getName() << ") is incomplete and couln't be loaded." << std::endl;
163                            }
164                        }
165                    }
166                    catch(ticpp::Exception& ex)
167                    {
168                        COUT(1) << std::endl;
169                        COUT(1) << "An error occurred in XMLPort.h while loading attribute '" << this->paramname_ << "' of '" << this->classname_ << "' (objectname: " << object->getName() << ") in " << object->getLevelfile() << ":" << std::endl;
170                        COUT(1) << ex.what() << std::endl;
171                    }
172                }
173                else
174                {
175                    if (this->savefunction_)
176                    {
177//                        xmlelement.SetAttribute(this->paramname_, "...");
178                    }
179                }
180
181                return (*this);
182            }
183
184        private:
185            FunctorMember<T>* loadfunction_;
186            FunctorMember<T>* savefunction_;
187    };
188
189
190    // ################################
191    // ###  XMLPortObjectContainer  ###
192    // ################################
193    class _CoreExport XMLPortObjectContainer
194    {
195        public:
196            XMLPortObjectContainer();
197
198            inline const std::string& getName() const
199                { return this->sectionname_; }
200
201            XMLPortObjectContainer& description(const std::string description);
202            const std::string& getDescription();
203            bool identifierIsIncludedInLoaderMask(const Identifier* identifier);
204
205        protected:
206            std::string classname_;
207            std::string sectionname_;
208            bool bApplyLoaderMask_;
209            bool bLoadBefore_;
210
211        private:
212            LanguageEntryLabel description_;
213            bool bAddedDescription_;
214    };
215
216    template <class T, class O>
217    class XMLPortClassObjectContainer : public XMLPortObjectContainer
218    {
219        public:
220            XMLPortClassObjectContainer(const std::string classname, const std::string sectionname, void (T::*loadfunction)(O*), const O* (T::*savefunction)(unsigned int) const, bool bApplyLoaderMask, bool bLoadBefore)
221            {
222                this->classname_ = classname;
223                this->sectionname_ = sectionname;
224                this->loadfunction_ = loadfunction;
225                this->savefunction_ = savefunction;
226                this->bApplyLoaderMask_ = bApplyLoaderMask;
227                this->bLoadBefore_ = bLoadBefore;
228            }
229
230            XMLPortObjectContainer& port(T* object, Element& xmlelement, XMLPort::Mode mode)
231            {
232                if (mode == XMLPort::LoadObject)
233                {
234                    try
235                    {
236                        Element* xmlsubelement;
237                        if ((this->sectionname_ != "") && (this->sectionname_.size() > 0))
238                            xmlsubelement = xmlelement.FirstChildElement(this->sectionname_, false);
239                        else
240                            xmlsubelement = &xmlelement;
241
242                        if (xmlsubelement)
243                        {
244                            for (ticpp::Iterator<ticpp::Element> child = xmlsubelement->FirstChildElement(false); child != child.end(); child++)
245                            {
246                                Identifier* identifier = ID(child->Value());
247                                if (identifier)
248                                {
249                                    if (identifier->isA(Class(O)))
250                                    {
251                                        if (this->identifierIsIncludedInLoaderMask(identifier))
252                                        {
253                                            COUT(4) << object->getLoaderIndentation() << "fabricating " << child->Value() << "..." << std::endl;
254                                            O* newObject = (O*)identifier->fabricate();
255                                            newObject->setLoaderIndentation(object->getLoaderIndentation() + "  ");
256                                            newObject->setLevel(object->getLevel());
257                                            newObject->setNamespace(object->getNamespace());
258                                            if (this->bLoadBefore_)
259                                            {
260                                                newObject->XMLPort(*child, XMLPort::LoadObject);
261                                                COUT(4) << object->getLoaderIndentation() << "assigning " << child->Value() << " (objectname " << newObject->getName() << ") to " << this->classname_ << " (objectname " << object->getName() << ")" << std::endl;
262                                            }
263                                            else
264                                            {
265                                                COUT(4) << object->getLoaderIndentation() << "assigning " << child->Value() << " (object not yet loaded) to " << this->classname_ << " (objectname " << object->getName() << ")" << std::endl;
266                                            }
267                                            (*object.*this->loadfunction_)(newObject);
268                                            if (!this->bLoadBefore_)
269                                                newObject->XMLPort(*child, XMLPort::LoadObject);
270                                            COUT(5) << object->getLoaderIndentation() << "...fabricated " << child->Value() << " (objectname " << newObject->getName() << ")." << std::endl;
271                                        }
272                                    }
273                                    else
274                                    {
275                                        COUT(2) << object->getLoaderIndentation() << "Warning: '" << child->Value() << "' is not a '" << Class(O)->getName() << "'." << std::endl;
276                                    }
277                                }
278                                else
279                                {
280                                    COUT(2) << object->getLoaderIndentation() << "Warning: '" << child->Value() << "' is not a valid classname." << std::endl;
281                                }
282                            }
283                        }
284                    }
285                    catch(ticpp::Exception& ex)
286                    {
287                        COUT(1) << std::endl;
288                        COUT(1) << "An error occurred in XMLPort.h while loading a '" << Class(O)->getName() << "' in '" << this->sectionname_ << "' of '" << this->classname_ << "' (objectname: " << object->getName() << ") in " << object->getLevelfile() << ":" << std::endl;
289                        COUT(1) << ex.what() << std::endl;
290                    }
291                }
292                else
293                {
294                }
295
296                return (*this);
297            }
298
299        private:
300            void     (T::*loadfunction_)(O*);
301            const O* (T::*savefunction_)(unsigned int) const;
302    };
303}
304
305#endif /* _XMLPort_H__ */
Note: See TracBrowser for help on using the repository browser.