Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 4, 2009, 3:12:10 PM (15 years ago)
Author:
rgrieder
Message:

Modified config value macros so you can use them as one-liner.
And the macro code also gone: it can now be easily debugged in an inline function.
(Changes do not apply to ModifyConfigValue because it was impossible to do).

Location:
code/branches/presentation2/src/libraries/core
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation2/src/libraries/core/CMakeLists.txt

    r6214 r6243  
    8686  TOLUA_FILES
    8787    CommandExecutor.h
    88       ConfigFileManager.h
     88    ConfigFileManager.h
    8989    Game.h
    9090    Loader.h
  • code/branches/presentation2/src/libraries/core/ConfigValueContainer.h

    r6219 r6243  
    121121                @param defvalue The default-value
    122122            */
    123             template <class V>
    124             ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& sectionname, const std::string& varname, const std::vector<V>& defvalue)
     123            template <class D, class V>
     124            ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& sectionname, const std::string& varname, const std::vector<D>& defvalue, const std::vector<V>& value)
    125125            {
    126126                this->init(type, identifier, sectionname, varname);
  • code/branches/presentation2/src/libraries/core/ConfigValueIncludes.h

    r6105 r6243  
    2323 *      Fabian 'x3n' Landau
    2424 *   Co-authors:
    25  *      ...
     25 *      Reto Grieder (functions)
    2626 *
    2727 */
    2828
    2929/**
    30     @file
    31     @brief Definition of macros for config-values.
     30@file
     31@brief
     32    Definition of macros and functions for config-values.
    3233*/
    3334
     
    4142#include "ConfigFileManager.h"
    4243
     44namespace orxonox
     45{
     46    /** Sets a runtime configurable value.
     47        If the container for the value doesn't yet exist, a new one is created.
     48        Also, the @a variable argument will be modified and set to the new value (default or from ini file).
     49    @param object
     50        Class instance that the config value should belong to (usually just 'this')
     51    @param variable
     52        Pointer to the variable where the value should be written to
     53    @param type
     54        Type of the config file, usually ConfigFileType::Settings
     55    @param sectionName
     56        Name of the section in the ini file (e.g. [MySection])
     57    @param entryName
     58        Name of the entry in the ini file (e.g. [MySection] myValue)
     59    @param defaultValue
     60        Value to be used if it cannot be read from the ini file
     61    */
     62    template <class T, class D, class V>
     63    inline ConfigValueContainer& setConfigValueGeneric(T* object, V* variable, ConfigFileType type, const std::string& sectionName, const std::string& entryName, const D& defaultValue)
     64    {
     65        ConfigValueContainer* container = object->getIdentifier()->getConfigValueContainer(entryName);
     66        if (!container)
     67        {
     68            container = new ConfigValueContainer(type, object->getIdentifier(), sectionName, entryName, defaultValue, *variable);
     69            object->getIdentifier()->addConfigValueContainer(entryName, container);
     70        }
     71        return container->getValue(variable, object);
     72    }
     73}
    4374
    44 /**
    45     @brief Assigns the value, defined in the config-file, to the variable (or the default-value, if there is no entry in the file).
    46     @param varname The name of the variable
    47     @param defvalue The default-value of the variable
     75/** Sets a runtime configurable value (simplified macro version of setConfigValueGeneric)
     76    If the container for the value doesn't yet exist, a new one is created.
     77    Also, the @a varname argument will be modified and set to the new value (default or from ini file).
     78@param varname
     79    Variable name as C++ identifier. It will be used as entry name and as variable pointer
     80@param defaultValue
     81    Value to be used if it cannot be read from the ini file
    4882*/
    49 #define SetConfigValueGeneric(type, varname, entryname, sectionname, defvalue) \
    50     static orxonox::Identifier* identifier##varname = this->getIdentifier(); \
    51     orxonox::ConfigValueContainer* container##varname = identifier##varname->getConfigValueContainer(entryname); \
    52     if (!container##varname) \
    53     { \
    54         container##varname = new orxonox::ConfigValueContainer(type, identifier##varname, sectionname, entryname, defvalue, varname); \
    55         identifier##varname->addConfigValueContainer(entryname, container##varname); \
    56     } \
    57     container##varname->getValue(&varname, this)
    58 
    59 #define SetConfigValue(varname, defvalue) SetConfigValueGeneric(ConfigFileType::Settings, varname, #varname, identifier##varname->getName(), defvalue)
     83#define SetConfigValue(varname, defaultValue) \
     84    orxonox::setConfigValueGeneric(this, &varname, ConfigFileType::Settings, this->getIdentifier()->getName(), #varname, defaultValue)
    6085
    6186
    62 /**
    63     @brief Assigns the vector-values, defined in the config-file, to the vector (or the default-value, if there are no entries in the file).
    64     @param varname The name of the std::vector
    65     @param defvalue The default-value
     87namespace orxonox
     88{
     89    /** Resets a runtime configurable value to its default.
     90        If the container for the value doesn't yet exist, a warning is displayed.
     91        Also, the @a variable argument will be modified and set to the default value.
     92    @param object
     93        Class instance that the config value should belong to (usually just 'this')
     94    @param variable
     95        Pointer to the variable where the value should be written to
     96    @param entryName
     97        Name of the entry in the ini file (e.g. [MySection] myValue)
     98    */
     99    template <class T, class V>
     100    inline void resetConfigValueGeneric(T* object, V* variable, const std::string& entryName)
     101    {
     102        ConfigValueContainer* container = object->getIdentifier()->getConfigValueContainer(entryName);
     103        if (container)
     104        {
     105            container->reset();
     106            container->getValue(variable, object);
     107        }
     108        else
     109        {
     110            COUT(2) << "Warning: Couldn't reset config-value '" << entryName << "' in class '"
     111                    << object->getIdentifier()->getName() << "', corresponding container doesn't exist." << std::endl;
     112        }
     113    }
     114}
     115
     116/** Resets a runtime configurable value to its default (simplified macro version of modifyConfigValueGeneric)
     117    If the container for the value doesn't yet exist, a warning is displayed.
     118    Also, the @a varname argument will be modified and set to the default value.
     119@param varname
     120    Variable name as C++ identifier. It will be used as entry name and as variable pointer
    66121*/
    67 #define SetConfigValueVectorGeneric(type, varname, defvalue) \
    68     static orxonox::Identifier* identifier##varname = this->getIdentifier(); \
    69     orxonox::ConfigValueContainer* container##varname = identifier##varname->getConfigValueContainer(#varname); \
    70     if (!container##varname) \
    71     { \
    72         container##varname = new orxonox::ConfigValueContainer(type, identifier##varname, identifier##varname->getName(), #varname, defvalue); \
    73         identifier##varname->addConfigValueContainer(#varname, container##varname); \
    74     } \
    75     container##varname->getValue(&varname, this)
    76 
    77 #define SetConfigValueVector(varname, defvalue) SetConfigValueVectorGeneric(ConfigFileType::Settings, varname, defvalue)
     122#define ResetConfigValue(varname) \
     123    orxonox::resetConfigValueGeneric(this, &varname, #varname)
    78124
    79125
    80 /**
    81     @brief Sets the variable and the config-file entry back to the previously defined default-value.
    82     @param varname The name of the variable
     126/** Modifies a runtime configurable value by using a modifier and some arguments.
     127    If the container for the value doesn't yet exist, a warning is displayed.
     128    Also, the @a variable argument will be modified and set to the current value.
     129@param object
     130    Class instance that the config value should belong to (usually just 'this')
     131@param variable
     132    Pointer to the variable where the value should be written to
     133@param entryName
     134    Name of the entry in the ini file (e.g. [MySection] myValue)
     135@param modifier
     136    On of these functions: set, tset, add, remove, reset, update
     137@param ...
     138    Arguments for the modifier function
    83139*/
    84 #define ResetConfigValue(varname) \
    85     orxonox::ConfigValueContainer* container##varname##reset = this->getIdentifier()->getConfigValueContainer(#varname); \
    86     if (container##varname##reset) \
     140#define ModifyConfigValueGeneric(object, variable, entryName, modifier, ...) \
     141    if (orxonox::ConfigValueContainer* container = object->getIdentifier()->getConfigValueContainer(entryName)) \
    87142    { \
    88         container##varname##reset->reset(); \
    89         container##varname##reset->getValue(&varname, this); \
     143        container->modifier(__VA_ARGS__); \
     144        container->getValue(variable, object); \
    90145    } \
    91146    else \
    92147    { \
    93         COUT(2) << "Warning: Couldn't reset config-value '" << #varname << "', corresponding container doesn't exist." << std::endl; \
     148        COUT(2) << "Warning: Couln't modify config-value '" << entryName << "' in class '" \
     149                << object->getIdentifier()->getName() << "', corresponding container doesn't exist." << std::endl; \
    94150    }
    95151
    96 
    97 /**
    98     @brief Modifies a config-value by using a modifier and some arguments.
    99     @param varname The name of the config-value
    100     @param modifier The name of the modifier: set, tset, add, remove, reset, update
     152/** Modifies a runtime configurable value by using a modifier and some arguments.
     153    If the container for the value doesn't yet exist, a warning is displayed.
     154    Also, the @a varname argument will be modified and set to the current value.
     155@param varname
     156    Variable name as C++ identifier. It will be used as entry name and as variable pointer
     157@param modifier
     158    On of these functions: set, tset, add, remove, reset, update
     159@param ...
     160    Arguments for the modifier function
    101161*/
    102162#define ModifyConfigValue(varname, modifier, ...) \
    103     orxonox::ConfigValueContainer* container##varname##modify##modifier = this->getIdentifier()->getConfigValueContainer(#varname); \
    104     if (container##varname##modify##modifier) \
    105     { \
    106         container##varname##modify##modifier->modifier(__VA_ARGS__); \
    107         container##varname##modify##modifier->getValue(&varname, this); \
    108     } \
    109     else \
    110     { \
    111         COUT(2) << "Warning: Couln't modify config-value '" << #varname << "', corresponding container doesn't exist." << std::endl; \
    112     }
     163    ModifyConfigValueGeneric(this, &varname, #varname, modifier, __VA_ARGS__)
    113164
    114165#endif /* _ConfigValueIncludes_H__ */
  • code/branches/presentation2/src/libraries/core/Core.cc

    r6183 r6243  
    183183        const unsigned int defaultLevelLogFile = 4;
    184184#endif
    185         SetConfigValueGeneric(ConfigFileType::Settings, softDebugLevelLogFile_, "softDebugLevelLogFile", "OutputHandler", defaultLevelLogFile)
     185        setConfigValueGeneric(this, &this->softDebugLevelLogFile_, ConfigFileType::Settings, "OutputHandler", "softDebugLevelLogFile", defaultLevelLogFile)
    186186            .description("The maximum level of debug output shown in the log file");
    187187        OutputHandler::getInstance().setSoftDebugLevel(OutputHandler::logFileOutputListenerName_s, this->softDebugLevelLogFile_);
  • code/branches/presentation2/src/libraries/core/Shell.cc

    r6190 r6243  
    9898        SetConfigValue(historyOffset_, 0)
    9999            .callback(this, &Shell::commandHistoryOffsetChanged);
    100         SetConfigValueVectorGeneric(commandHistoryConfigFileType_, commandHistory_, std::vector<std::string>());
     100        setConfigValueGeneric(this, &commandHistory_, commandHistoryConfigFileType_, "Shell", "commandHistory_", std::vector<std::string>());
    101101
    102102#ifdef ORXONOX_RELEASE
     
    105105        const unsigned int defaultLevel = 3;
    106106#endif
    107         SetConfigValueGeneric(ConfigFileType::Settings, softDebugLevel_, "softDebugLevel" + this->consoleName_, "OutputHandler", defaultLevel)
     107        setConfigValueGeneric(this, &softDebugLevel_, ConfigFileType::Settings, "OutputHandler", "softDebugLevel" + this->consoleName_, defaultLevel)
    108108            .description("The maximal level of debug output shown in the Shell");
    109109        this->setSoftDebugLevel(this->softDebugLevel_);
Note: See TracChangeset for help on using the changeset viewer.