Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands/src/libraries/core/ConfigValueIncludes.h @ 6703

Last change on this file since 6703 was 6243, checked in by rgrieder, 15 years ago

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).

  • Property svn:eol-style set to native
File size: 6.6 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 = 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}
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
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
121*/
122#define ResetConfigValue(varname) \
123    orxonox::resetConfigValueGeneric(this, &varname, #varname)
124
125
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
139*/
140#define ModifyConfigValueGeneric(object, variable, entryName, modifier, ...) \
141    if (orxonox::ConfigValueContainer* container = object->getIdentifier()->getConfigValueContainer(entryName)) \
142    { \
143        container->modifier(__VA_ARGS__); \
144        container->getValue(variable, object); \
145    } \
146    else \
147    { \
148        COUT(2) << "Warning: Couln't modify config-value '" << entryName << "' in class '" \
149                << object->getIdentifier()->getName() << "', corresponding container doesn't exist." << std::endl; \
150    }
151
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
161*/
162#define ModifyConfigValue(varname, modifier, ...) \
163    ModifyConfigValueGeneric(this, &varname, #varname, modifier, __VA_ARGS__)
164
165#endif /* _ConfigValueIncludes_H__ */
Note: See TracBrowser for help on using the repository browser.