Changeset 6417 for code/trunk/src/libraries/core/ConfigValueIncludes.h
- Timestamp:
- Dec 25, 2009, 10:23:58 PM (15 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/core/ConfigValueIncludes.h
r6105 r6417 23 23 * Fabian 'x3n' Landau 24 24 * Co-authors: 25 * ...25 * Reto Grieder (functions) 26 26 * 27 27 */ 28 28 29 29 /** 30 @file 31 @brief Definition of macros for config-values. 30 @file 31 @brief 32 Definition of macros and functions for config-values. 32 33 */ 33 34 … … 41 42 #include "ConfigFileManager.h" 42 43 44 namespace 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 } 43 74 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 48 82 */ 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) 83 #define SetConfigValue(varname, defaultValue) \ 84 orxonox::setConfigValueGeneric(this, &varname, ConfigFileType::Settings, this->getIdentifier()->getName(), #varname, defaultValue) 58 85 59 #define SetConfigValue(varname, defvalue) SetConfigValueGeneric(ConfigFileType::Settings, varname, #varname, identifier##varname->getName(), defvalue) 86 /** Sets a runtime configurable value (simplified macro version of setConfigValueGeneric) 87 If the container for the value doesn't yet exist, a new one is created. 88 Also, the @a varname argument will be modified and set to the new value (default or from ini file). 89 @param variable 90 Variable name as C++ identifier. 91 @param entryName 92 Name of the entry in the ini file (e.g. [MySection] myValue) 93 @param defaultValue 94 Value to be used if it cannot be read from the ini file 95 */ 96 #define SetConfigValueAlias(variable, entryName, defaultValue) \ 97 orxonox::setConfigValueGeneric(this, &variable, ConfigFileType::Settings, this->getIdentifier()->getName(), entryName, defaultValue) 60 98 61 99 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 100 namespace orxonox 101 { 102 /** Resets a runtime configurable value to its default. 103 If the container for the value doesn't yet exist, a warning is displayed. 104 Also, the @a variable argument will be modified and set to the default value. 105 @param object 106 Class instance that the config value should belong to (usually just 'this') 107 @param variable 108 Pointer to the variable where the value should be written to 109 @param entryName 110 Name of the entry in the ini file (e.g. [MySection] myValue) 111 */ 112 template <class T, class V> 113 inline void resetConfigValueGeneric(T* object, V* variable, const std::string& entryName) 114 { 115 ConfigValueContainer* container = object->getIdentifier()->getConfigValueContainer(entryName); 116 if (container) 117 { 118 container->reset(); 119 container->getValue(variable, object); 120 } 121 else 122 { 123 COUT(2) << "Warning: Couldn't reset config-value '" << entryName << "' in class '" 124 << object->getIdentifier()->getName() << "', corresponding container doesn't exist." << std::endl; 125 } 126 } 127 } 128 129 /** Resets a runtime configurable value to its default (simplified macro version of modifyConfigValueGeneric) 130 If the container for the value doesn't yet exist, a warning is displayed. 131 Also, the @a varname argument will be modified and set to the default value. 132 @param varname 133 Variable name as C++ identifier. It will be used as entry name and as variable pointer 66 134 */ 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) 135 #define ResetConfigValue(varname) \ 136 orxonox::resetConfigValueGeneric(this, &varname, #varname) 78 137 79 138 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 139 /** Modifies a runtime configurable value by using a modifier and some arguments. 140 If the container for the value doesn't yet exist, a warning is displayed. 141 Also, the @a variable argument will be modified and set to the current value. 142 @param object 143 Class instance that the config value should belong to (usually just 'this') 144 @param variable 145 Pointer to the variable where the value should be written to 146 @param entryName 147 Name of the entry in the ini file (e.g. [MySection] myValue) 148 @param modifier 149 On of these functions: set, tset, add, remove, reset, update 150 @param ... 151 Arguments for the modifier function 83 152 */ 84 #define ResetConfigValue(varname) \ 85 orxonox::ConfigValueContainer* container##varname##reset = this->getIdentifier()->getConfigValueContainer(#varname); \ 86 if (container##varname##reset) \ 153 #define ModifyConfigValueGeneric(object, variable, entryName, modifier, ...) \ 154 if (orxonox::ConfigValueContainer* container = object->getIdentifier()->getConfigValueContainer(entryName)) \ 87 155 { \ 88 container ##varname##reset->reset(); \89 container ##varname##reset->getValue(&varname, this); \156 container->modifier(__VA_ARGS__); \ 157 container->getValue(variable, object); \ 90 158 } \ 91 159 else \ 92 160 { \ 93 COUT(2) << "Warning: Couldn't reset config-value '" << #varname << "', corresponding container doesn't exist." << std::endl; \ 161 COUT(2) << "Warning: Couln't modify config-value '" << entryName << "' in class '" \ 162 << object->getIdentifier()->getName() << "', corresponding container doesn't exist." << std::endl; \ 94 163 } 95 164 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 165 /** Modifies a runtime configurable value by using a modifier and some arguments. 166 If the container for the value doesn't yet exist, a warning is displayed. 167 Also, the @a varname argument will be modified and set to the current value. 168 @param varname 169 Variable name as C++ identifier. It will be used as entry name and as variable pointer 170 @param modifier 171 On of these functions: set, tset, add, remove, reset, update 172 @param ... 173 Arguments for the modifier function 101 174 */ 102 175 #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 } 176 ModifyConfigValueGeneric(this, &varname, #varname, modifier, __VA_ARGS__) 113 177 114 178 #endif /* _ConfigValueIncludes_H__ */
Note: See TracChangeset
for help on using the changeset viewer.