Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/ConfigValueIncludes.h @ 6536

Last change on this file since 6536 was 6536, checked in by rgrieder, 14 years ago

Merged revisions 6430-6440 from the gamestate branch to the trunk.
This adds keybindings merging functionality.

(from log of r6437)
When running development builds, the keybinder will merge the local file and the one from the data folder.
Catch: if you want to remove a binding, you'll have to write "NoBinding" (not case sensitive) to override the default command

The keybind command already does that for you though.

  • Property svn:eol-style set to native
File size: 7.3 KB
Line 
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 *      Reto Grieder (functions)
26 *
27 */
28
29/**
30@file
31@brief
32    Definition of macros and functions for config-values.
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
43namespace orxonox
44{
45    /** Sets a runtime configurable value.
46        If the container for the value doesn't yet exist, a new one is created.
47        Also, the @a variable argument will be modified and set to the new value (default or from ini file).
48    @param object
49        Class instance that the config value should belong to (usually just 'this')
50    @param variable
51        Pointer to the variable where the value should be written to
52    @param type
53        Type of the config file, usually ConfigFileType::Settings
54    @param sectionName
55        Name of the section in the ini file (e.g. [MySection])
56    @param entryName
57        Name of the entry in the ini file (e.g. [MySection] myValue)
58    @param defaultValue
59        Value to be used if it cannot be read from the ini file
60    */
61    template <class T, class D, class V>
62    inline ConfigValueContainer& setConfigValueGeneric(T* object, V* variable, ConfigFileType::Value type, const std::string& sectionName, const std::string& entryName, const D& defaultValue)
63    {
64        ConfigValueContainer* container = ClassIdentifier<T>::getIdentifier()->getConfigValueContainer(entryName);
65        if (!container)
66        {
67            container = new ConfigValueContainer(type, ClassIdentifier<T>::getIdentifier(), sectionName, entryName, defaultValue, *variable);
68            ClassIdentifier<T>::getIdentifier()->addConfigValueContainer(entryName, container);
69        }
70        return container->getValue(variable, object);
71    }
72}
73
74/** Sets a runtime configurable value (simplified macro version of setConfigValueGeneric)
75    If the container for the value doesn't yet exist, a new one is created.
76    Also, the @a varname argument will be modified and set to the new value (default or from ini file).
77@param varname
78    Variable name as C++ identifier. It will be used as entry name and as variable pointer
79@param defaultValue
80    Value to be used if it cannot be read from the ini file
81*/
82#define SetConfigValue(varname, defaultValue) \
83    orxonox::setConfigValueGeneric(this, &varname, ConfigFileType::Settings, this->getIdentifier()->getName(), #varname, defaultValue)
84
85/** Sets a runtime configurable value (simplified macro version of setConfigValueGeneric)
86    If the container for the value doesn't yet exist, a new one is created.
87    Also, the @a varname argument will be modified and set to the new value (default or from ini file).
88@param variable
89    Variable name as C++ identifier.
90@param entryName
91    Name of the entry in the ini file (e.g. [MySection] myValue)
92@param defaultValue
93    Value to be used if it cannot be read from the ini file
94*/
95#define SetConfigValueAlias(variable, entryName, defaultValue) \
96    orxonox::setConfigValueGeneric(this, &variable, ConfigFileType::Settings, this->getIdentifier()->getName(), entryName, defaultValue)
97
98
99namespace orxonox
100{
101    /** Resets a runtime configurable value to its default.
102        If the container for the value doesn't yet exist, a warning is displayed.
103        Also, the @a variable argument will be modified and set to the default value.
104    @param object
105        Class instance that the config value should belong to (usually just 'this')
106    @param variable
107        Pointer to the variable where the value should be written to
108    @param entryName
109        Name of the entry in the ini file (e.g. [MySection] myValue)
110    */
111    template <class T, class V>
112    inline void resetConfigValueGeneric(T* object, V* variable, const std::string& entryName)
113    {
114        ConfigValueContainer* container = ClassIdentifier<T>::getIdentifier()->getConfigValueContainer(entryName);
115        if (container)
116        {
117            container->reset();
118            container->getValue(variable, object);
119        }
120        else
121        {
122            COUT(2) << "Warning: Couldn't reset config-value '" << entryName << "' in class '"
123                    << ClassIdentifier<T>::getIdentifier()->getName() << "', corresponding container doesn't exist." << std::endl;
124        }
125    }
126}
127
128/** Resets a runtime configurable value to its default (simplified macro version of modifyConfigValueGeneric)
129    If the container for the value doesn't yet exist, a warning is displayed.
130    Also, the @a varname argument will be modified and set to the default value.
131@param varname
132    Variable name as C++ identifier. It will be used as entry name and as variable pointer
133*/
134#define ResetConfigValue(varname) \
135    orxonox::resetConfigValueGeneric(this, &varname, #varname)
136
137
138/** Modifies a runtime configurable value by using a modifier and some arguments.
139    If the container for the value doesn't yet exist, a warning is displayed.
140    Also, the @a variable argument will be modified and set to the current value.
141@param object
142    Class instance that the config value should belong to (usually just 'this')
143@param variable
144    Pointer to the variable where the value should be written to
145@param entryName
146    Name of the entry in the ini file (e.g. [MySection] myValue)
147@param modifier
148    On of these functions: set, tset, add, remove, reset, update
149@param ...
150    Arguments for the modifier function
151*/
152#define ModifyConfigValueGeneric(object, variable, entryName, modifier, ...) \
153    if (orxonox::ConfigValueContainer* container = ClassByObjectType(object)->getConfigValueContainer(entryName)) \
154    { \
155        container->modifier(__VA_ARGS__); \
156        container->getValue(variable, object); \
157    } \
158    else \
159    { \
160        COUT(2) << "Warning: Couln't modify config-value '" << entryName << "' in class '" \
161                << ClassByObjectType(object)->getName() << "', corresponding container doesn't exist." << std::endl; \
162    }
163
164/** Modifies a runtime configurable value by using a modifier and some arguments.
165    If the container for the value doesn't yet exist, a warning is displayed.
166    Also, the @a varname argument will be modified and set to the current value.
167@param varname
168    Variable name as C++ identifier. It will be used as entry name and as variable pointer
169@param modifier
170    On of these functions: set, tset, add, remove, reset, update
171@param ...
172    Arguments for the modifier function
173*/
174#define ModifyConfigValue(varname, modifier, ...) \
175    ModifyConfigValueGeneric(this, &varname, #varname, modifier, __VA_ARGS__)
176
177#endif /* _ConfigValueIncludes_H__ */
Note: See TracBrowser for help on using the repository browser.