Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/core/ConfigValueIncludes.h @ 7363

Last change on this file since 7363 was 7363, checked in by landauf, 14 years ago

assigned a group to each header file in the core library

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