Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/XMLPort.h @ 7186

Last change on this file since 7186 was 7186, checked in by landauf, 14 years ago

Moved ability to possess descriptions from Executor to ConsoleCommand, since no other executors use this feature. Also simplified this code a little by introducing a new shortcut in Language.h. XMLPort has to use a temporary solution for descriptions without Language support atm.

  • Property svn:eol-style set to native
File size: 33.4 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/**
30    @file
31    @brief Declaration of the XMLPort helper classes and macros.
32
33    XMLPort is a virtual function of every BaseObject. Every object can change this function.
34    The XMLPort function gets called with an XMLElement, containing all attributes and
35    subclasses the object gets from the levelfile.
36
37    This file declares classes and macros to simplify XML-parsing.
38*/
39
40#ifndef _XMLPort_H__
41#define _XMLPort_H__
42
43#include "CorePrereqs.h"
44
45#include <cassert>
46#include <string>
47#include <tinyxml/ticpp.h>
48
49#include "util/Debug.h"
50#include "util/Exception.h"
51#include "util/MultiType.h"
52#include "util/OrxAssert.h"
53#include "util/StringUtils.h"
54#include "Identifier.h"
55#include "Executor.h"
56#include "BaseObject.h"
57
58// ------------
59// XMLPortParam
60
61/**
62    @brief Declares an XML attribute with a name, which will be set through load- and savefunctions.
63    @param classname The name of the class owning this param
64    @param paramname The name of the attribute
65    @param loadfunction A function to set the param in the object with a given value (~a set-function)
66    @param savefunction A function to get the value of the param from the object (~a get-function)
67    @param xmlelement The XMLElement, you get this from the XMLPort function
68    @param mode The mode (load or save), you get this from the XMLPort function
69
70    In the XML file, a param or attribute will be set like this:
71    <classname paramname="value" />
72
73    The macro will then call loadfunction(value) to set the given value (or call savefunction() to
74    write an existing value to an XML file).
75*/
76#define XMLPortParam(classname, paramname, loadfunction, savefunction, xmlelement, mode) \
77    static ExecutorMember<classname>* xmlcontainer##loadfunction##savefunction##loadexecutor = orxonox::createExecutor(orxonox::createFunctor(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction); \
78    static ExecutorMember<classname>* xmlcontainer##loadfunction##savefunction##saveexecutor = orxonox::createExecutor(orxonox::createFunctor(&classname::savefunction), std::string( #classname ) + "::" + #savefunction); \
79    XMLPortParamGeneric(xmlcontainer##loadfunction##savefunction, classname, classname, this, paramname, xmlcontainer##loadfunction##savefunction##loadexecutor, xmlcontainer##loadfunction##savefunction##saveexecutor, xmlelement, mode)
80
81/**
82    @brief Declares an XML attribute with a name, which will be set through a variable.
83    @param classname The name of the class owning this param
84    @param paramname The name of the attribute
85    @param variable Name of the variable used to save and load the value
86    @param xmlelement The XMLElement, you get this from the XMLPort function
87    @param mode The mode (load or save), you get this from the XMLPort function
88
89    In the XML file, a param or attribute will be set like this:
90    <classname paramname="value" />
91
92    The macro will then store "value" in the variable or read it when saving.
93*/
94#define XMLPortParamVariable(classname, paramname, variable, xmlelement, mode) \
95    XMLPortVariableHelperClass xmlcontainer##variable##dummy(static_cast<void*>(&variable)); \
96    static ExecutorMember<orxonox::XMLPortVariableHelperClass>* xmlcontainer##variable##loadexecutor = static_cast<ExecutorMember<orxonox::XMLPortVariableHelperClass>*>(orxonox::createExecutor(orxonox::createFunctor(orxonox::XMLPortVariableHelperClass::getLoader(variable)), std::string( #classname ) + "::" + #variable + "loader")); \
97    static ExecutorMember<orxonox::XMLPortVariableHelperClass>* xmlcontainer##variable##saveexecutor = static_cast<ExecutorMember<orxonox::XMLPortVariableHelperClass>*>(orxonox::createExecutor(orxonox::createFunctor(orxonox::XMLPortVariableHelperClass::getSaver (variable)), std::string( #classname ) + "::" + #variable + "saver" )); \
98    XMLPortParamGeneric(xmlcontainer##variable, classname, orxonox::XMLPortVariableHelperClass, &xmlcontainer##variable##dummy, paramname, xmlcontainer##variable##loadexecutor, xmlcontainer##variable##saveexecutor, xmlelement, mode)
99
100/**
101    @brief This is the same as XMLPortParam, but you can set the template arguments needed to store the loadfunction.
102
103    Sometimes the name of the loadfunction is ambiguous (example: setPosition(Vector3) or
104    setPosition(float, float, float)). In this case, you can choose the right function by
105    telling the types of the functionparams. In our example, this would be either
106    > XMLPortParamTemplate(classname, paramname, loadfunction, savefunction, xmlelement, mode, Vector3);
107    or
108    > XMLPortParamTemplate(classname, paramname, loadfunction, savefunction, xmlelement, mode, float, float, float);
109    You don't have to use this, if there exist only one function with the given name.
110*/
111#define XMLPortParamTemplate(classname, paramname, loadfunction, savefunction, xmlelement, mode, ...) \
112    static ExecutorMember<classname>* xmlcontainer##loadfunction##savefunction##loadexecutor = orxonox::createExecutor(orxonox::createFunctor<classname, __VA_ARGS__ >(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction); \
113    static ExecutorMember<classname>* xmlcontainer##loadfunction##savefunction##saveexecutor = orxonox::createExecutor(orxonox::createFunctor(&classname::savefunction), std::string( #classname ) + "::" + #savefunction); \
114    XMLPortParamGeneric(xmlcontainer##loadfunction##savefunction, classname, classname, this, paramname, xmlcontainer##loadfunction##savefunction##loadexecutor, xmlcontainer##loadfunction##savefunction##saveexecutor, xmlelement, mode)
115
116// --------------------
117// XMLPortParamLoadOnly
118
119/**
120    @brief Declares an XML attribute with a name, which can be set through a loadfunction.
121
122    This is the same as XMLPortParam, but you don't need a savefunction (get-function). Therefore,
123    this param won't be saved in an XML file, but you can add the attribute manually an it will be
124    loaded.
125
126    This might be helpful in cases, when you have several options to set a value, for example the
127    rotation. You can set the rotation with a quaternion, but you could also use three angles.
128    When saving the object, only one of both options has to be saved; this is, where this macro helps.
129*/
130#define XMLPortParamLoadOnly(classname, paramname, loadfunction, xmlelement, mode) \
131    static ExecutorMember<classname>* xmlcontainer##loadfunction##0##loadexecutor = orxonox::createExecutor(orxonox::createFunctor(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction); \
132    XMLPortParamGeneric(xmlcontainer##loadfunction##0, classname, classname, this, paramname, xmlcontainer##loadfunction##0##loadexecutor, 0, xmlelement, mode)
133/**
134    @brief This is the same as XMLPortParamTemplate, but for load-only attributes (see XMLPortParamLoadOnly).
135*/
136#define XMLPortParamLoadOnlyTemplate(classname, paramname, loadfunction, xmlelement, mode, ...) \
137    static ExecutorMember<classname>* xmlcontainer##loadfunction##0##loadexecutor = orxonox::createExecutor(orxonox::createFunctor<classname, __VA_ARGS__ >(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction); \
138    XMLPortParamGeneric(xmlcontainer##loadfunction##0, classname, classname, this, paramname, xmlcontainer##loadfunction##0##loadexecutor, 0, xmlelement, mode)
139
140// ------------------
141// XMLPortParamExtern
142
143/**
144    @brief This is the same as XMLPortParam, but for attributes in an extern class.
145    @param classname The name of the class owning the object owning the attribute
146    @param externclass The name of the extern class (the objects class)
147    @param object The name of the object of the extern class (a member of the main class)
148    @param paramname The name of the attribute
149    @param loadfunction The function to set the attribute inside of the member object.
150    @param loadfunction The function to get the attribute from the member object
151
152    Sometimes you'll have a member object in your class, which has it's own load- and savefunctions.
153    With this macro, you can simply use them instead of writing your own functions.
154
155    @example
156    Your class is called SpaceShip and this class has an object (myPilot_) of class Pilot. Pilot has a name
157    and two functions, setName(name) and getName(). Now you want an attribute "pilotname" in your
158    SpaceShip class. Instead of writing wrapper functions, you can simply use the XMLPortParamExtern
159    macro:
160    > XMLPortParamExtern(SpaceShip, Pilot, myPilot_, "pilotname", setName, getName, xmlelement, mode);
161*/
162#define XMLPortParamExtern(classname, externclass, object, paramname, loadfunction, savefunction, xmlelement, mode) \
163    static ExecutorMember<externclass>* xmlcontainer##loadfunction##savefunction##loadexecutor = orxonox::createExecutor(orxonox::createFunctor(&externclass::loadfunction), std::string( #externclass ) + "::" + #loadfunction); \
164    static ExecutorMember<externclass>* xmlcontainer##loadfunction##savefunction##saveexecutor = orxonox::createExecutor(orxonox::createFunctor(&externclass::savefunction), std::string( #externclass ) + "::" + #savefunction); \
165    XMLPortParamGeneric(xmlcontainer##loadfunction##savefunction, classname, externclass, object, paramname, xmlcontainer##loadfunction##savefunction##loadexecutor, xmlcontainer##loadfunction##savefunction##saveexecutor, xmlelement, mode);
166/**
167    @brief This is the same as XMLPortParamTemplate, but for extern attributes (see XMLPortParamExtern).
168*/
169#define XMLPortParamExternTemplate(classname, externclass, object, paramname, loadfunction, savefunction, xmlelement, mode, ...) \
170    static ExecutorMember<externclass>* xmlcontainer##loadfunction##savefunction##loadexecutor = orxonox::createExecutor(orxonox::createFunctor<externclass, __VA_ARGS__ >(&externclass::loadfunction), std::string( #externclass ) + "::" + #loadfunction); \
171    static ExecutorMember<externclass>* xmlcontainer##loadfunction##savefunction##saveexecutor = orxonox::createExecutor(orxonox::createFunctor(&externclass::savefunction), std::string( #externclass ) + "::" + #savefunction); \
172    XMLPortParamGeneric(xmlcontainer##loadfunction##savefunction, classname, externclass, object, paramname, xmlcontainer##loadfunction##savefunction##loadexecutor, xmlcontainer##loadfunction##savefunction##saveexecutor, xmlelement, mode);
173
174// -------------------
175// XMLPortParamGeneric
176
177/**
178    @brief This is the generic XMLPort param macro, which is used by all six specialized macros above.
179*/
180#define XMLPortParamGeneric(containername, classname, objectclass, object, paramname, loadexecutor, saveexecutor, xmlelement, mode) \
181    orxonox::XMLPortClassParamContainer<objectclass>* containername = static_cast<orxonox::XMLPortClassParamContainer<objectclass>*>(ClassIdentifier<classname>::getIdentifier()->getXMLPortParamContainer(paramname)); \
182    if (!containername) \
183    { \
184        containername = new orxonox::XMLPortClassParamContainer<objectclass>(std::string(paramname), ClassIdentifier<classname>::getIdentifier(), loadexecutor, saveexecutor); \
185        ClassIdentifier<classname>::getIdentifier()->addXMLPortParamContainer(paramname, containername); \
186    } \
187    containername->port(dynamic_cast<BaseObject*>(this), object, xmlelement, mode)
188
189// --------------------
190// XMLPortObjectExtended
191
192/**
193    @brief Declares a possible sub-object that can be added in the XML file.
194    @param classname The name of the class that uses this macro
195    @param objectclass The baseclass of objects that can be added
196    @param sectionname The name of the subsection in the XML file that encloses the sub-objects ("" means no subsection)
197    @param loadfunction The function to add a new object to the class
198    @param loadfunction The function to get all added objects from the class
199    @param xmlelement The XMLElement (received through the XMLPort function)
200    @param mode The mode (load/save) (received through the XMLPort function)
201    @param bApplyLoaderMask If this is true, an added sub-object gets loaded only if it's class is included in the Loaders ClassTreeMask (this is usually false)
202    @param bLoadBefore If this is true, the sub-object gets loaded (through XMLPort) BEFORE it gets added to the main class (this is usually true)
203
204    bApplyLoaderMask is usually false for the following reason:
205    If the loaders mask says, for example, "load only SpaceShips" and you added weapons to the
206    SpaceShips, then the Weapons will still be loaded (which is most probably what you want).
207    Of course, if there are "standalone" weapons in the level, they wont be loaded.
208
209    If bLoadBefore is true, an added object already has all attributes set (like it's name). This is most
210    likely the best option, so this is usually true.
211
212    @note
213    The load- and savefunctions have to follow an exactly defined protocol.
214    Loadfunction:
215      The loadfunction gets a pointer to the object.
216      > void loadfunction(objectclass* pointer);
217
218    Savefunction:
219      The savefunction gets an index, starting with 0. For every returnvalue != 0, the savefunction
220      gets called again, but with index + 1. It's the functions responsibility to do something smart
221      with the index and to return 0 if all objects were returned.
222      > objectclass* savefunction(unsigned int index) const;
223
224      Possible implementation:
225        objectclass* savefunction(unsigned int index) const
226        {
227          if (index < number_of_added_objects_)
228            return my_added_objects[index];
229          else
230            return 0;
231        }
232
233    @example
234    Possible usage of the macro:
235    > XMLPortObject(SpaceShip, Weapon, "weapons", addWeapon, getWeapon, xmlelement, mode, false, true);
236
237    Now you can add weapons through the XML file:
238    <SpaceShip someattribute="..." ...>
239      <weapons>
240        <Weapon someattribute="..." ... />
241        <Weapon someattribute="..." ... />
242        <Weapon someattribute="..." ... />
243      </weapons>
244    </SpaceShip>
245
246    Note that "weapons" is the subsection. This allows you to add more types of sub-objects. In our example,
247    you could add pilots, blinking lights or other stuff. If you don't want a subsection, just use "" (an
248    empty string). Then you can add sub-objects directly into the mainclass.
249*/
250#define XMLPortObjectExtended(classname, objectclass, sectionname, loadfunction, savefunction, xmlelement, mode, bApplyLoaderMask, bLoadBefore) \
251    static ExecutorMember<classname>* xmlcontainer##loadfunction##savefunction##loadexecutor = orxonox::createExecutor(orxonox::createFunctor(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction); \
252    static ExecutorMember<classname>* xmlcontainer##loadfunction##savefunction##saveexecutor = orxonox::createExecutor(orxonox::createFunctor(&classname::savefunction), std::string( #classname ) + "::" + #savefunction); \
253    XMLPortObjectGeneric(xmlcontainer##loadfunction##savefunction, classname, objectclass, sectionname, xmlcontainer##loadfunction##savefunction##loadexecutor, xmlcontainer##loadfunction##savefunction##saveexecutor, xmlelement, mode, bApplyLoaderMask, bLoadBefore)
254/**
255    @brief This is the same as XMLPortObjectExtended, but you can specify the loadfunction by adding the param types. See XMLPortParamTemplate for more details about the types.
256*/
257#define XMLPortObjectExtendedTemplate(classname, objectclass, sectionname, loadfunction, savefunction, xmlelement, mode, bApplyLoaderMask, bLoadBefore, ...) \
258    static ExecutorMember<classname>* xmlcontainer##loadfunction##savefunction##loadexecutor = orxonox::createExecutor(orxonox::createFunctor<classname, __VA_ARGS__ >(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction); \
259    static ExecutorMember<classname>* xmlcontainer##loadfunction##savefunction##saveexecutor = orxonox::createExecutor(orxonox::createFunctor(&classname::savefunction), std::string( #classname ) + "::" + #savefunction); \
260    XMLPortObjectGeneric(xmlcontainer##loadfunction##savefunction, classname, objectclass, sectionname, xmlcontainer##loadfunction##savefunction##loadexecutor, xmlcontainer##loadfunction##savefunction##saveexecutor, xmlelement, mode, bApplyLoaderMask, bLoadBefore)
261
262// -------------
263// XMLPortObject
264
265/**
266    @brief This is the same as XMLPortObjectExtended, but bApplyLoaderMask is false and bLoadBefore is true by default.
267*/
268#define XMLPortObject(classname, objectclass, sectionname, loadfunction, savefunction, xmlelement, mode) \
269    static ExecutorMember<classname>* xmlcontainer##loadfunction##savefunction##loadexecutor = orxonox::createExecutor(orxonox::createFunctor(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction); \
270    static ExecutorMember<classname>* xmlcontainer##loadfunction##savefunction##saveexecutor = orxonox::createExecutor(orxonox::createFunctor(&classname::savefunction), std::string( #classname ) + "::" + #savefunction); \
271    XMLPortObjectGeneric(xmlcontainer##loadfunction##savefunction, classname, objectclass, sectionname, xmlcontainer##loadfunction##savefunction##loadexecutor, xmlcontainer##loadfunction##savefunction##saveexecutor, xmlelement, mode, false, true)
272/**
273    @brief This is the same as XMLPortObject, but you can specify the loadfunction by adding the param types. See XMLPortParamTemplate for more details about the types.
274*/
275#define XMLPortObjectTemplate(classname, objectclass, sectionname, loadfunction, savefunction, xmlelement, mode, ...) \
276    static ExecutorMember<classname>* xmlcontainer##loadfunction##savefunction##loadexecutor = orxonox::createExecutor(orxonox::createFunctor<classname, __VA_ARGS__ >(&classname::loadfunction), std::string( #classname ) + "::" + #loadfunction); \
277    static ExecutorMember<classname>* xmlcontainer##loadfunction##savefunction##saveexecutor = orxonox::createExecutor(orxonox::createFunctor(&classname::savefunction), std::string( #classname ) + "::" + #savefunction); \
278    XMLPortObjectGeneric(xmlcontainer##loadfunction##savefunction, classname, objectclass, sectionname, xmlcontainer##loadfunction##savefunction##loadexecutor, xmlcontainer##loadfunction##savefunction##saveexecutor, xmlelement, mode, false, true)
279
280// --------------------
281// XMLPortObjectGeneric
282
283/**
284    @brief Generic XMLPortObject macro, that gets called by all other XMLPortObject macros above.
285*/
286#define XMLPortObjectGeneric(containername, classname, objectclass, sectionname, loadexecutor, saveexecutor, xmlelement, mode, bApplyLoaderMask, bLoadBefore) \
287    orxonox::XMLPortClassObjectContainer<classname, objectclass>* containername = (orxonox::XMLPortClassObjectContainer<classname, objectclass>*)(ClassIdentifier<classname>::getIdentifier()->getXMLPortObjectContainer(sectionname)); \
288    if (!containername) \
289    { \
290        containername = new orxonox::XMLPortClassObjectContainer<classname, objectclass>(std::string(sectionname), ClassIdentifier<classname>::getIdentifier(), loadexecutor, saveexecutor, bApplyLoaderMask, bLoadBefore); \
291        ClassIdentifier<classname>::getIdentifier()->addXMLPortObjectContainer(sectionname, containername); \
292    } \
293    containername->port(this, xmlelement, mode)
294
295
296namespace orxonox
297{
298    // ###############################
299    // ###  XMLPortParamContainer  ###
300    // ###############################
301    class _CoreExport XMLPortParamContainer
302    {
303    public:
304        enum ParseResult
305        {
306            PR_not_started,
307            PR_finished,
308            PR_waiting_for_default_values
309        };
310
311        public:
312            XMLPortParamContainer()
313                { this->parseResult_ = PR_not_started; }
314            virtual ~XMLPortParamContainer() {}
315
316            inline const std::string& getName() const
317                { return this->paramname_; }
318
319            inline XMLPortParamContainer& description(const std::string& description)
320                { this->description_ = description; return *this; }
321            inline const std::string& getDescription() const
322                { return this->description_; }
323
324            virtual XMLPortParamContainer& defaultValue(unsigned int index, const MultiType& param) = 0;
325            virtual XMLPortParamContainer& defaultValues(const MultiType& param1) = 0;
326            virtual XMLPortParamContainer& defaultValues(const MultiType& param1, const MultiType& param2) = 0;
327            virtual XMLPortParamContainer& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3) = 0;
328            virtual XMLPortParamContainer& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) = 0;
329            virtual XMLPortParamContainer& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) = 0;
330
331        protected:
332            std::string paramname_;
333            ParseResult parseResult_;
334            Identifier* identifier_;
335            BaseObject* owner_;
336            std::string description_;
337    };
338
339    template <class T>
340    class XMLPortClassParamContainer : public XMLPortParamContainer
341    {
342        struct ParseParams
343        {
344            T* object;
345            Element* xmlelement;
346            XMLPort::Mode mode;
347        };
348
349        public:
350            XMLPortClassParamContainer(const std::string& paramname, Identifier* identifier, ExecutorMember<T>* loadexecutor, ExecutorMember<T>* saveexecutor)
351            {
352                this->paramname_ = paramname;
353                this->identifier_ = identifier;
354                this->loadexecutor_ = loadexecutor;
355                this->saveexecutor_ = saveexecutor;
356            }
357
358            ~XMLPortClassParamContainer()
359            {
360                assert(this->loadexecutor_);
361                delete this->loadexecutor_;
362                if (this->saveexecutor_)
363                    delete this->saveexecutor_;
364            }
365
366            XMLPortParamContainer& port(BaseObject* owner, T* object, Element& xmlelement, XMLPort::Mode mode)
367            {
368                OrxAssert(owner, "XMLPortParamContainer must have a BaseObject as owner.");
369                this->owner_ = owner;
370                this->parseParams_.object = object;
371                this->parseParams_.xmlelement = &xmlelement;
372                this->parseParams_.mode = mode;
373
374                if ((mode == XMLPort::LoadObject) || (mode == XMLPort::ExpandObject))
375                {
376                    try
377                    {
378                        if (this->owner_->lastLoadedXMLElement_ != &xmlelement)
379                        {
380                            this->owner_->xmlAttributes_.clear();
381                            // Iterate through the attributes manually in order to make them case insensitive
382                            ticpp::Attribute* attribute = xmlelement.FirstAttribute(false);
383                            while (attribute != 0)
384                            {
385                                this->owner_->xmlAttributes_[getLowercase(attribute->Name())] = attribute->Value();
386                                attribute = attribute->Next(false);
387                            }
388                            this->owner_->lastLoadedXMLElement_ = &xmlelement;
389                        }
390                        std::map<std::string, std::string>::const_iterator it = this->owner_->xmlAttributes_.find(getLowercase(this->paramname_));
391                        std::string attributeValue;
392                        if (it != this->owner_->xmlAttributes_.end())
393                            attributeValue = it->second;
394
395                        // TODO: Checking the iterator would be better since then we can have strings with value "" as well.
396                        //       Unfortunately this does not seem to work with the Executor parser yet.
397                        if ((!attributeValue.empty()) || ((mode != XMLPort::ExpandObject) && this->loadexecutor_->allDefaultValuesSet()))
398                        {
399                            COUT(5) << this->owner_->getLoaderIndentation() << "Loading parameter " << this->paramname_ << " in " << this->identifier_->getName() << " (objectname " << this->owner_->getName() << ")." << std::endl << this->owner_->getLoaderIndentation();
400                            if (this->loadexecutor_->parse(object, attributeValue, ",") || (mode  == XMLPort::ExpandObject))
401                                this->parseResult_ = PR_finished;
402                            else
403                                this->parseResult_ = PR_waiting_for_default_values;
404                        }
405                        else if (mode == XMLPort::ExpandObject)
406                            this->parseResult_ = PR_finished;
407                        else
408                            this->parseResult_ = PR_waiting_for_default_values;
409                    }
410                    catch (ticpp::Exception& ex)
411                    {
412                        COUT(1) << std::endl;
413                        COUT(1) << "An error occurred in XMLPort.h while loading attribute '" << this->paramname_ << "' of '" << this->identifier_->getName() << "' (objectname: " << this->owner_->getName() << ") in " << this->owner_->getFilename() << ':' << std::endl;
414                        COUT(1) << ex.what() << std::endl;
415                    }
416                }
417                else if (mode == XMLPort::SaveObject)
418                {
419                    if (this->saveexecutor_)
420                    {
421//                        xmlelement.SetAttribute(this->paramname_, "...");
422                    }
423                }
424
425                return (*this);
426            }
427
428            XMLPortParamContainer& port(BaseObject* owner, const ParseParams& parseParams)
429            {
430                return this->port(owner, parseParams.object, *parseParams.xmlelement, parseParams.mode);
431            }
432
433            XMLPortParamContainer& portIfWaitingForDefaultValues(const ParseResult& result, const ParseParams& params)
434            {
435                if (result == PR_waiting_for_default_values)
436                    return this->port(this->owner_, params);
437                else
438                    return (*this);
439            }
440
441            virtual XMLPortParamContainer& defaultValue(unsigned int index, const MultiType& param)
442            {
443                if (!this->loadexecutor_->defaultValueSet(index))
444                    this->loadexecutor_->setDefaultValue(index, param);
445                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
446            }
447            virtual XMLPortParamContainer& defaultValues(const MultiType& param1)
448            {
449                if (!this->loadexecutor_->defaultValueSet(0))
450                    this->loadexecutor_->setDefaultValues(param1);
451                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
452            }
453            virtual XMLPortParamContainer& defaultValues(const MultiType& param1, const MultiType& param2)
454            {
455                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)))
456                    this->loadexecutor_->setDefaultValues(param1, param2);
457                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
458            }
459            virtual XMLPortParamContainer& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3)
460            {
461                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)) || (!this->loadexecutor_->defaultValueSet(2)))
462                    this->loadexecutor_->setDefaultValues(param1, param2, param3);
463                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
464            }
465            virtual XMLPortParamContainer& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4)
466            {
467                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)) || (!this->loadexecutor_->defaultValueSet(2)) || (!this->loadexecutor_->defaultValueSet(3)))
468                    this->loadexecutor_->setDefaultValues(param1, param2, param3, param4);
469                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
470            }
471            virtual XMLPortParamContainer& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5)
472            {
473                if ((!this->loadexecutor_->defaultValueSet(0)) || (!this->loadexecutor_->defaultValueSet(1)) || (!this->loadexecutor_->defaultValueSet(2)) || (!this->loadexecutor_->defaultValueSet(3)) || (!this->loadexecutor_->defaultValueSet(4)))
474                    this->loadexecutor_->setDefaultValues(param1, param2, param3, param4, param5);
475                return this->portIfWaitingForDefaultValues(this->parseResult_, this->parseParams_);
476            }
477
478        private:
479            ExecutorMember<T>* loadexecutor_;
480            ExecutorMember<T>* saveexecutor_;
481            ParseParams parseParams_;
482    };
483
484
485    // ################################
486    // ###  XMLPortObjectContainer  ###
487    // ################################
488    class _CoreExport XMLPortObjectContainer
489    {
490        public:
491            XMLPortObjectContainer()
492                { this->bApplyLoaderMask_ = false; }
493            virtual ~XMLPortObjectContainer() {}
494
495            XMLPortObjectContainer& port(BaseObject* object, Element& xmlelement, XMLPort::Mode mode);
496
497            virtual void callLoadExecutor(BaseObject* object, BaseObject* newObject) = 0;
498
499            inline const std::string& getName() const
500                { return this->sectionname_; }
501
502            inline XMLPortObjectContainer& description(const std::string& description)
503                { this->description_ = description; return *this; }
504            const std::string& getDescription() const
505                { return this->description_; }
506
507            bool identifierIsIncludedInLoaderMask(const Identifier* identifier);
508
509        protected:
510            std::string sectionname_;
511            bool bApplyLoaderMask_;
512            bool bLoadBefore_;
513            Identifier* identifier_;
514            Identifier* objectIdentifier_;
515            std::string description_;
516    };
517
518    template <class T, class O>
519    class XMLPortClassObjectContainer : public XMLPortObjectContainer
520    {
521        public:
522            XMLPortClassObjectContainer(const std::string& sectionname, Identifier* identifier, ExecutorMember<T>* loadexecutor, ExecutorMember<T>* saveexecutor, bool bApplyLoaderMask, bool bLoadBefore)
523            {
524                this->sectionname_ = sectionname;
525                this->identifier_ = identifier;
526                assert(identifier->isA(ClassIdentifier<T>::getIdentifier()));
527                this->objectIdentifier_ = ClassIdentifier<O>::getIdentifier();
528                this->loadexecutor_ = loadexecutor;
529                this->saveexecutor_ = saveexecutor;
530                this->bApplyLoaderMask_ = bApplyLoaderMask;
531                this->bLoadBefore_ = bLoadBefore;
532            }
533
534            ~XMLPortClassObjectContainer()
535            {
536                assert(this->loadexecutor_);
537                delete this->loadexecutor_;
538                if (this->saveexecutor_)
539                    delete this->saveexecutor_;
540            }
541
542            void callLoadExecutor(BaseObject* object, BaseObject* newObject)
543            {
544                T* castedObject = orxonox_cast<T*>(object);
545                assert(castedObject);
546                O* castedNewObject = orxonox_cast<O*>(newObject);
547                assert(castedNewObject);
548
549                (*this->loadexecutor_)(castedObject, castedNewObject);
550            }
551
552        private:
553            ExecutorMember<T>* loadexecutor_;
554            ExecutorMember<T>* saveexecutor_;
555    };
556
557
558    // ####################################
559    // ###  XMLPortVariableHelperClass  ###
560    // ####################################
561    /**
562    @brief
563        Helper class to load and save simple variables with XMLPort.
564
565        getLoader and getSaver were necessary to get the type T with
566        the help of template function type deduction (const T& is unused).
567        These functions return the adress of save<T> or load<T>.
568    */
569    class XMLPortVariableHelperClass
570    {
571        public:
572            XMLPortVariableHelperClass(void* var)
573                : variable_(var)
574                { }
575
576            template <class T>
577            void load(const T& value)
578                { *static_cast<T*>(this->variable_) = value; }
579
580            template <class T>
581            const T& save()
582                { return *static_cast<T*>(this->variable_); }
583
584            template <class T>
585            static void (XMLPortVariableHelperClass::*getLoader(const T& var))(const T& value)
586                { return &XMLPortVariableHelperClass::load<T>; }
587
588            template <class T>
589            static const T& (XMLPortVariableHelperClass::*getSaver(const T& var))()
590                { return &XMLPortVariableHelperClass::save<T>; }
591
592        private:
593            void* variable_;
594    };
595}
596
597#endif /* _XMLPort_H__ */
Note: See TracBrowser for help on using the repository browser.