Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 28, 2008, 1:23:17 AM (16 years ago)
Author:
rgrieder
Message:

Added new XMLPort macro: XMLPortParamVariable
It allows to directly load values into a variable or from one. However it is not yet save to use. For instance declaring a Vector3 parameter as "1,2,3" is a very bad idea. "(1,2,3)" works well.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/physics/src/core/XMLPort.h

    r2087 r2291  
    7373    static ExecutorMember<classname>* xmlcontainer##loadfunction##savefunction##saveexecutor = orxonox::createExecutor(orxonox::createFunctor(&classname::savefunction), std::string( #classname ) + "::" + #savefunction); \
    7474    XMLPortParamGeneric(xmlcontainer##loadfunction##savefunction, classname, classname, this, paramname, xmlcontainer##loadfunction##savefunction##loadexecutor, xmlcontainer##loadfunction##savefunction##saveexecutor, xmlelement, mode)
     75
     76/**
     77    @brief Declares an XML attribute with a name, which will be set through a variable.
     78    @param classname The name of the class owning this param
     79    @param paramname The name of the attribute
     80    @param variable Name of the variable used to save and load the value
     81    @param xmlelement The XMLElement, you get this from the XMLPort function
     82    @param mode The mode (load or save), you get this from the XMLPort function
     83
     84    *** EXPERIMENTAL. DO NOT USE UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING! ***
     85
     86    In the XML file, a param or attribute will be set like this:
     87    <classname paramname="value" />
     88
     89    The macro will then store "value" in the variable or read it when saving.
     90*/
     91#define XMLPortParamVariable(classname, paramname, variable, xmlelement, mode) \
     92    static XMLPortVariableHelperClass xmlcontainer##variable##dummy; \
     93    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")->setDefaultValue(1, &variable))); \
     94    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" )->setDefaultValue(0,  variable))); \
     95    XMLPortParamGeneric(xmlcontainer##variable, classname, orxonox::XMLPortVariableHelperClass, &xmlcontainer##variable##dummy, paramname, xmlcontainer##variable##loadexecutor, xmlcontainer##variable##saveexecutor, xmlelement, mode)
     96
    7597/**
    7698    @brief This is the same as XMLPortParam, but you can set the template arguments needed to store the loadfunction.
     
    570592            ExecutorMember<T>* saveexecutor_;
    571593    };
     594
     595
     596    // ####################################
     597    // ###  XMLPortVariableHelperClass  ###
     598    // ####################################
     599    /**
     600    @brief
     601        Helper class to load and save simple variables with XMLPort.
     602
     603        getLoader and getSaver were necessary to get the type T with
     604        the help of template function type deduction (const T& is unused).
     605        These functions return the adress of save<T> or load<T>.
     606    */
     607    class XMLPortVariableHelperClass
     608    {
     609        public:
     610            template <class T>
     611            inline void load(const T& value, T* var)
     612                { *var = value; }
     613
     614            template <class T>
     615            inline const T& save(const T& var)
     616                { return var; }
     617
     618            template <class T>
     619            static void (XMLPortVariableHelperClass::*getLoader(const T& var))(const T& value, T* var)
     620                { return &XMLPortVariableHelperClass::load<T>; }
     621
     622            template <class T>
     623            static const T& (XMLPortVariableHelperClass::*getSaver(const T& var))(const T& var)
     624                { return &XMLPortVariableHelperClass::save<T>; }
     625    };
    572626}
    573627
Note: See TracChangeset for help on using the changeset viewer.