[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: |
---|
[6417] | 25 | * Reto Grieder (functions) |
---|
[1505] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
[6417] | 30 | @file |
---|
| 31 | @brief |
---|
| 32 | Definition of macros and functions for config-values. |
---|
[1505] | 33 | */ |
---|
| 34 | |
---|
| 35 | #ifndef _ConfigValueIncludes_H__ |
---|
| 36 | #define _ConfigValueIncludes_H__ |
---|
| 37 | |
---|
| 38 | #include "CorePrereqs.h" |
---|
| 39 | |
---|
| 40 | #include "Identifier.h" |
---|
| 41 | #include "ConfigValueContainer.h" |
---|
| 42 | #include "ConfigFileManager.h" |
---|
| 43 | |
---|
[6417] | 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 | { |
---|
[6423] | 65 | ConfigValueContainer* container = ClassIdentifier<T>::getIdentifier()->getConfigValueContainer(entryName); |
---|
[6417] | 66 | if (!container) |
---|
| 67 | { |
---|
[6423] | 68 | container = new ConfigValueContainer(type, ClassIdentifier<T>::getIdentifier(), sectionName, entryName, defaultValue, *variable); |
---|
| 69 | ClassIdentifier<T>::getIdentifier()->addConfigValueContainer(entryName, container); |
---|
[6417] | 70 | } |
---|
| 71 | return container->getValue(variable, object); |
---|
| 72 | } |
---|
| 73 | } |
---|
[1505] | 74 | |
---|
[6417] | 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 |
---|
[1505] | 82 | */ |
---|
[6417] | 83 | #define SetConfigValue(varname, defaultValue) \ |
---|
| 84 | orxonox::setConfigValueGeneric(this, &varname, ConfigFileType::Settings, this->getIdentifier()->getName(), #varname, defaultValue) |
---|
[1505] | 85 | |
---|
[6417] | 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 |
---|
[1505] | 95 | */ |
---|
[6417] | 96 | #define SetConfigValueAlias(variable, entryName, defaultValue) \ |
---|
| 97 | orxonox::setConfigValueGeneric(this, &variable, ConfigFileType::Settings, this->getIdentifier()->getName(), entryName, defaultValue) |
---|
[1505] | 98 | |
---|
[1747] | 99 | |
---|
[6417] | 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 | { |
---|
[6423] | 115 | ConfigValueContainer* container = ClassIdentifier<T>::getIdentifier()->getConfigValueContainer(entryName); |
---|
[6417] | 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 '" |
---|
[6423] | 124 | << ClassIdentifier<T>::getIdentifier()->getName() << "', corresponding container doesn't exist." << std::endl; |
---|
[6417] | 125 | } |
---|
| 126 | } |
---|
| 127 | } |
---|
[1747] | 128 | |
---|
[6417] | 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 |
---|
[1505] | 134 | */ |
---|
| 135 | #define ResetConfigValue(varname) \ |
---|
[6417] | 136 | orxonox::resetConfigValueGeneric(this, &varname, #varname) |
---|
[1505] | 137 | |
---|
[1747] | 138 | |
---|
[6417] | 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 |
---|
[1505] | 152 | */ |
---|
[6417] | 153 | #define ModifyConfigValueGeneric(object, variable, entryName, modifier, ...) \ |
---|
[6423] | 154 | if (orxonox::ConfigValueContainer* container = ClassByObjectType(object)->getConfigValueContainer(entryName)) \ |
---|
[1505] | 155 | { \ |
---|
[6417] | 156 | container->modifier(__VA_ARGS__); \ |
---|
| 157 | container->getValue(variable, object); \ |
---|
[1505] | 158 | } \ |
---|
| 159 | else \ |
---|
| 160 | { \ |
---|
[6417] | 161 | COUT(2) << "Warning: Couln't modify config-value '" << entryName << "' in class '" \ |
---|
[6423] | 162 | << ClassByObjectType(object)->getName() << "', corresponding container doesn't exist." << std::endl; \ |
---|
[1505] | 163 | } |
---|
| 164 | |
---|
[6417] | 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 |
---|
| 174 | */ |
---|
| 175 | #define ModifyConfigValue(varname, modifier, ...) \ |
---|
| 176 | ModifyConfigValueGeneric(this, &varname, #varname, modifier, __VA_ARGS__) |
---|
| 177 | |
---|
[1505] | 178 | #endif /* _ConfigValueIncludes_H__ */ |
---|