Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ois_update/src/libraries/core/ConfigValueIncludes.h @ 7669

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

merged doc branch back to trunk

  • Property svn:eol-style set to native
File size: 9.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    @defgroup ConfigFile Config file
31    @ingroup Config
32*/
33
34/**
35    @file
36    @ingroup Config ConfigFile
37    @brief Definition of macros and functions for config-values.
38
39    An example of how to use SetConfigValue():
40
41    Definition of a class in the header-file:
42    @code
43    class MyClass : public BaseObject
44    {
45        public:
46            MyClass();              // Constructor
47            void setConfigValues(); // Inherited function
48
49            const std::string& getName()
50                { return this->name_; }
51
52            float getVersion()
53                { return this->version_; }
54
55        private:
56            std::string name_;
57            float version_;
58    };
59    @endcode
60
61    Implementation of the class source-file:
62    @code
63    MyClass::MyClass()
64    {
65        // Macro-call to create an Identifier
66        RegisterObject(MyClass);
67
68        // Function-call to assign the config-values to the new object
69        this->setConfigValues();
70    }
71
72    void MyClass::setConfigValues()
73    {
74        SetConfigValue(name_, "Orxonox").description("The name of the game");
75        SetConfigValue(version_, "1.0").description("The version-number");
76    }
77    @endcode
78
79    Extract of orxonox.ini:
80    @code
81    [MyClass]
82    name_ = "Orxonox"
83    version_ = 1.1 // We have changed this value from 1.0 to 1.1
84    @endcode
85
86    Some other code:
87    @code
88    MyObject orxonoxobject;
89    std::cout << "Name:    " << orxonoxobject.getName() << std::endl;
90    std::cout << "Version: " << orxonoxobject.getVersion() << std::endl;
91    @endcode
92
93    Output:
94    @code
95    Name:    Orxonox
96    Version: 1.1
97    @endcode
98*/
99
100#ifndef _ConfigValueIncludes_H__
101#define _ConfigValueIncludes_H__
102
103#include "CorePrereqs.h"
104
105#include "Identifier.h"
106#include "ConfigValueContainer.h"
107
108namespace orxonox
109{
110    /** Sets a runtime configurable value.
111        If the container for the value doesn't yet exist, a new one is created.
112        Also, the @a variable argument will be modified and set to the new value (default or from ini file).
113    @param object
114        Class instance that the config value should belong to (usually just 'this')
115    @param variable
116        Pointer to the variable where the value should be written to
117    @param type
118        Type of the config file, usually ConfigFileType::Settings
119    @param sectionName
120        Name of the section in the ini file (e.g. [MySection])
121    @param entryName
122        Name of the entry in the ini file (e.g. [MySection] myValue)
123    @param defaultValue
124        Value to be used if it cannot be read from the ini file
125    */
126    template <class T, class D, class V>
127    inline ConfigValueContainer& setConfigValueGeneric(T* object, V* variable, ConfigFileType::Value type, const std::string& sectionName, const std::string& entryName, const D& defaultValue)
128    {
129        ConfigValueContainer* container = ClassIdentifier<T>::getIdentifier()->getConfigValueContainer(entryName);
130        if (!container)
131        {
132            container = new ConfigValueContainer(type, ClassIdentifier<T>::getIdentifier(), sectionName, entryName, defaultValue, *variable);
133            ClassIdentifier<T>::getIdentifier()->addConfigValueContainer(entryName, container);
134        }
135        return container->getValue(variable, object);
136    }
137}
138
139/** Sets a runtime configurable value (simplified macro version of setConfigValueGeneric)
140    If the container for the value doesn't yet exist, a new one is created.
141    Also, the @a varname argument will be modified and set to the new value (default or from ini file).
142@param varname
143    Variable name as C++ identifier. It will be used as entry name and as variable pointer
144@param defaultValue
145    Value to be used if it cannot be read from the ini file
146*/
147#define SetConfigValue(varname, defaultValue) \
148    orxonox::setConfigValueGeneric(this, &varname, ConfigFileType::Settings, this->getIdentifier()->getName(), #varname, defaultValue)
149
150/** Sets a runtime configurable value (simplified macro version of setConfigValueGeneric)
151    If the container for the value doesn't yet exist, a new one is created.
152    Also, the @a varname argument will be modified and set to the new value (default or from ini file).
153@param variable
154    Variable name as C++ identifier.
155@param entryName
156    Name of the entry in the ini file (e.g. [MySection] myValue)
157@param defaultValue
158    Value to be used if it cannot be read from the ini file
159*/
160#define SetConfigValueAlias(variable, entryName, defaultValue) \
161    orxonox::setConfigValueGeneric(this, &variable, ConfigFileType::Settings, this->getIdentifier()->getName(), entryName, defaultValue)
162
163/** Sets a runtime configurable value (simplified macro version of setConfigValueGeneric)
164    If the container for the value doesn't yet exist, a new one is created.
165    Also, the @a varname argument will be modified and set to the new value (default or from ini file).
166@param variable
167    Variable name as C++ identifier.
168@param sectionName
169    Name of the section in the ini file (e.g. [MySection])
170@param entryName
171    Name of the entry in the ini file (e.g. [MySection] myValue)
172@param defaultValue
173    Value to be used if it cannot be read from the ini file
174*/
175#define SetConfigValueExternal(variable, sectionName, entryName, defaultValue) \
176    orxonox::setConfigValueGeneric(this, &variable, ConfigFileType::Settings, sectionName, entryName, defaultValue)
177
178
179namespace orxonox
180{
181    /** Resets a runtime configurable value to its default.
182        If the container for the value doesn't yet exist, a warning is displayed.
183        Also, the @a variable argument will be modified and set to the default value.
184    @param object
185        Class instance that the config value should belong to (usually just 'this')
186    @param variable
187        Pointer to the variable where the value should be written to
188    @param entryName
189        Name of the entry in the ini file (e.g. [MySection] myValue)
190    */
191    template <class T, class V>
192    inline void resetConfigValueGeneric(T* object, V* variable, const std::string& entryName)
193    {
194        ConfigValueContainer* container = ClassIdentifier<T>::getIdentifier()->getConfigValueContainer(entryName);
195        if (container)
196        {
197            container->reset();
198            container->getValue(variable, object);
199        }
200        else
201        {
202            COUT(2) << "Warning: Couldn't reset config-value '" << entryName << "' in class '"
203                    << ClassIdentifier<T>::getIdentifier()->getName() << "', corresponding container doesn't exist." << std::endl;
204        }
205    }
206}
207
208/** Resets a runtime configurable value to its default (simplified macro version of modifyConfigValueGeneric)
209    If the container for the value doesn't yet exist, a warning is displayed.
210    Also, the @a varname argument will be modified and set to the default value.
211@param varname
212    Variable name as C++ identifier. It will be used as entry name and as variable pointer
213*/
214#define ResetConfigValue(varname) \
215    orxonox::resetConfigValueGeneric(this, &varname, #varname)
216
217
218/** Modifies a runtime configurable value by using a modifier and some arguments.
219    If the container for the value doesn't yet exist, a warning is displayed.
220    Also, the @a variable argument will be modified and set to the current value.
221@param object
222    Class instance that the config value should belong to (usually just 'this')
223@param variable
224    Pointer to the variable where the value should be written to
225@param entryName
226    Name of the entry in the ini file (e.g. [MySection] myValue)
227@param modifier
228    On of these functions: set, tset, add, remove, reset, update
229@param ...
230    Arguments for the modifier function
231*/
232#define ModifyConfigValueGeneric(object, variable, entryName, modifier, ...) \
233    if (orxonox::ConfigValueContainer* container = ClassByObjectType(object)->getConfigValueContainer(entryName)) \
234    { \
235        container->modifier(__VA_ARGS__); \
236        container->getValue(variable, object); \
237    } \
238    else \
239    { \
240        COUT(2) << "Warning: Couln't modify config-value '" << entryName << "' in class '" \
241                << ClassByObjectType(object)->getName() << "', corresponding container doesn't exist." << std::endl; \
242    }
243
244/** Modifies a runtime configurable value by using a modifier and some arguments.
245    If the container for the value doesn't yet exist, a warning is displayed.
246    Also, the @a varname argument will be modified and set to the current value.
247@param varname
248    Variable name as C++ identifier. It will be used as entry name and as variable pointer
249@param modifier
250    On of these functions: set, tset, add, remove, reset, update
251@param ...
252    Arguments for the modifier function
253*/
254#define ModifyConfigValue(varname, modifier, ...) \
255    ModifyConfigValueGeneric(this, &varname, #varname, modifier, __VA_ARGS__)
256
257#endif /* _ConfigValueIncludes_H__ */
Note: See TracBrowser for help on using the repository browser.