Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands2/src/libraries/core/ConfigValueIncludes.h @ 7760

Last change on this file since 7760 was 6423, checked in by rgrieder, 15 years ago

Fixed bug that caused derived classes to list the base class's config values as well.
Introduced a new "ClassByObjecType" function that accepts a 'this' pointer and returns the correct identifier (not this→getIdentifier()!).

  • Property svn:eol-style set to native
File size: 7.4 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#include "ConfigFileManager.h"
43
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 = ClassIdentifier<T>::getIdentifier()->getConfigValueContainer(entryName);
66        if (!container)
67        {
68            container = new ConfigValueContainer(type, ClassIdentifier<T>::getIdentifier(), sectionName, entryName, defaultValue, *variable);
69            ClassIdentifier<T>::getIdentifier()->addConfigValueContainer(entryName, container);
70        }
71        return container->getValue(variable, object);
72    }
73}
74
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
82*/
83#define SetConfigValue(varname, defaultValue) \
84    orxonox::setConfigValueGeneric(this, &varname, ConfigFileType::Settings, this->getIdentifier()->getName(), #varname, defaultValue)
85
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)
98
99
100namespace 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 = ClassIdentifier<T>::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                    << ClassIdentifier<T>::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
134*/
135#define ResetConfigValue(varname) \
136    orxonox::resetConfigValueGeneric(this, &varname, #varname)
137
138
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
152*/
153#define ModifyConfigValueGeneric(object, variable, entryName, modifier, ...) \
154    if (orxonox::ConfigValueContainer* container = ClassByObjectType(object)->getConfigValueContainer(entryName)) \
155    { \
156        container->modifier(__VA_ARGS__); \
157        container->getValue(variable, object); \
158    } \
159    else \
160    { \
161        COUT(2) << "Warning: Couln't modify config-value '" << entryName << "' in class '" \
162                << ClassByObjectType(object)->getName() << "', corresponding container doesn't exist." << std::endl; \
163    }
164
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
178#endif /* _ConfigValueIncludes_H__ */
Note: See TracBrowser for help on using the repository browser.