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