Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/ConfigValueContainer.cc @ 1025

Last change on this file since 1025 was 1023, checked in by landauf, 16 years ago

fixed bug, removed some debug output

File size: 5.8 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28/**
29    @file ConfigValueContainer.cc
30    @brief Implementation of the ConfigValueContainer class.
31*/
32
33#include <fstream>
34
35#include "ConfigValueContainer.h"
36#include "Language.h"
37
38
39namespace orxonox
40{
41    /**
42        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
43        @param value This is only needed to determine the right type.
44        @param classname The name of the class the variable belongs to
45        @param varname The name of the variable
46        @param defvalue The default-value
47    */
48    ConfigValueContainer::ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, MultiTypeMath defvalue)
49    {
50        this->type_ = type;
51        this->identifier_ = identifier;
52        this->sectionname_ = identifier->getName();
53        this->varname_ = varname;
54
55        this->value_ = defvalue;
56        this->bAddedDescription_ = false;
57
58        this->defvalueString_ = defvalue.toString();
59        this->update();
60    }
61
62    /**
63        @brief Assigns a new value to the config-value of all objects and writes the change into the config-file.
64        @param input The new value
65        @return True if the new value was successfully assigned
66    */
67    bool ConfigValueContainer::set(const std::string& input)
68    {
69        bool success = this->tset(input);
70        this->setLineInConfigFile(input);
71        return success;
72    }
73
74    /**
75        @brief Assigns a new value to the config-value of all objects, but doesn't change the config-file (t stands for temporary).
76        @param input The new value
77        @return True if the new value was successfully assigned
78    */
79    bool ConfigValueContainer::tset(const std::string& input)
80    {
81        bool success = this->parse(input);
82        if (this->identifier_)
83            this->identifier_->updateConfigValues();
84        return success;
85    }
86
87    /**
88        @brief Sets the value of the variable back to the default value and resets the config-file entry.
89    */
90    bool ConfigValueContainer::reset()
91    {
92        return this->set(this->defvalueString_);
93    }
94
95    /**
96        @brief Retrieves the configured value from the currently loaded config-file.
97    */
98    void ConfigValueContainer::update()
99    {
100        this->value_.fromString(ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, this->defvalueString_));
101    }
102
103    /**
104        @brief Parses a given std::string into a value of the type of the associated variable and assigns it.
105        @param input The string to convert
106        @return True if the string was successfully parsed
107    */
108    bool ConfigValueContainer::parse(const std::string& input)
109    {
110        MultiTypeMath temp = this->value_;
111        if (temp.fromString(input))
112        {
113            this->value_ = temp;
114            return true;
115        }
116        return false;
117    }
118
119    /**
120        @brief Parses a given std::string into a value of the type of the associated variable and assigns it.
121        @param input The string to convert
122        @param defvalue The default value to assign if the parsing fails
123        @return True if the string was successfully parsed
124    */
125    bool ConfigValueContainer::parse(const std::string& input, const MultiTypeMath& defvalue)
126    {
127        MultiTypeMath temp = defvalue;
128        if (temp.fromString(input))
129        {
130            this->value_ = temp;
131            return true;
132        }
133        else
134        {
135            this->value_ = defvalue;
136            return false;
137        }
138    }
139
140    /**
141        @brief Sets the corresponding entry in the config-file to a given value.
142    */
143    void ConfigValueContainer::setLineInConfigFile(const std::string& input)
144    {
145        ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, input);
146    }
147
148    /**
149        @brief Sets the corresponding entry in the config-file back to the default value.
150    */
151    void ConfigValueContainer::resetLineInConfigFile()
152    {
153        this->setLineInConfigFile(this->value_.toString());
154    }
155
156    /**
157        @brief Adds a description to the config-value.
158        @param description The description
159    */
160    void ConfigValueContainer::description(const std::string& description)
161    {
162        if (!this->bAddedDescription_)
163        {
164            this->description_ = std::string("ConfigValueDescription::" + this->identifier_->getName() + "::" + this->varname_);
165            AddLanguageEntry(this->description_, description);
166            this->bAddedDescription_ = true;
167        }
168    }
169
170    /**
171        @brief Returns the description of the config-value.
172        @return The description
173    */
174    const std::string& ConfigValueContainer::getDescription() const
175    {
176        return GetLocalisation(this->description_);
177    }
178}
Note: See TracBrowser for help on using the repository browser.