Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

corrected directory name

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