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 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file ConfigValueContainer.h |
---|
31 | @brief Definition of the ConfigValueContainer class. |
---|
32 | |
---|
33 | The ConfigValueContainer class contains all needed informations about a configurable variable: |
---|
34 | - the name of the variable |
---|
35 | - the name of the class the variable belongs to |
---|
36 | - the default value |
---|
37 | - the user-specified value |
---|
38 | - a pointer to the entry in the config-file |
---|
39 | |
---|
40 | This is needed to assign the configured values to all newly created objects. |
---|
41 | */ |
---|
42 | |
---|
43 | #ifndef _ConfigValueContainer_H__ |
---|
44 | #define _ConfigValueContainer_H__ |
---|
45 | |
---|
46 | #include "CorePrereqs.h" |
---|
47 | |
---|
48 | #include <string> |
---|
49 | #include <vector> |
---|
50 | |
---|
51 | #include "util/Math.h" |
---|
52 | #include "util/MultiTypeMath.h" |
---|
53 | #include "ConfigFileManager.h" |
---|
54 | |
---|
55 | namespace orxonox |
---|
56 | { |
---|
57 | //! The ConfigValuecontainer contains all needed informations about a configurable variable. |
---|
58 | /** |
---|
59 | The ConfigValueContainer class contains all needed informations about a configurable variable: |
---|
60 | - the name of the variable |
---|
61 | - the name of the class the variable belongs to |
---|
62 | - the default value |
---|
63 | - the user-specified value |
---|
64 | - a pointer to the entry in the config-file |
---|
65 | |
---|
66 | This is needed to assign the configured values to all newly created objects. |
---|
67 | |
---|
68 | The container searches for the entry in the config file. |
---|
69 | If there is an entry, it parses the specified value and assigns it to the variable of the right type. |
---|
70 | If there is no entry, it adds the entry with the default-value to the section of the variables class. |
---|
71 | If there is no section, the section and the entry are added to the end of the config-file. |
---|
72 | */ |
---|
73 | class _CoreExport ConfigValueContainer |
---|
74 | { |
---|
75 | public: |
---|
76 | ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const MultiTypeMath& defvalue); |
---|
77 | ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const std::vector<MultiTypeMath>& defvalue); |
---|
78 | |
---|
79 | /** @brief Returns the configured value. @param value This is only needed to determine the right type. @return The value */ |
---|
80 | template <typename T> |
---|
81 | inline ConfigValueContainer& getValue(T* value) |
---|
82 | { this->value_.getValue(value); return *this; } |
---|
83 | template <typename T> |
---|
84 | inline ConfigValueContainer& getValue(std::vector<T>* value) |
---|
85 | { |
---|
86 | value->clear(); |
---|
87 | for (unsigned int i = 0; i < this->valueVector_.size(); i++) |
---|
88 | value->push_back(this->valueVector_[i]); |
---|
89 | return *this; |
---|
90 | } |
---|
91 | |
---|
92 | inline const std::string& getName() const |
---|
93 | { return this->varname_; } |
---|
94 | inline bool isVector() const |
---|
95 | { return this->bIsVector_; } |
---|
96 | inline unsigned int getVectorSize() const |
---|
97 | { return this->valueVector_.size(); } |
---|
98 | |
---|
99 | void description(const std::string& description); |
---|
100 | const std::string& getDescription() const; |
---|
101 | |
---|
102 | bool add(const std::string& input); |
---|
103 | bool remove(unsigned int index); |
---|
104 | bool set(const std::string& input); |
---|
105 | bool tset(const std::string& input); |
---|
106 | bool reset(); |
---|
107 | void update(); |
---|
108 | |
---|
109 | /** @brief Converts the config-value to a string. @return The string */ |
---|
110 | inline std::string toString() const |
---|
111 | { return this->value_.toString(); } |
---|
112 | /** @brief Returns the typename of the assigned config-value. @return The typename */ |
---|
113 | inline std::string getTypename() const |
---|
114 | { return this->value_.getTypename(); } |
---|
115 | |
---|
116 | private: |
---|
117 | bool parse(const std::string& input); |
---|
118 | bool parse(const std::string& input, const MultiTypeMath& defvalue); |
---|
119 | |
---|
120 | bool set(unsigned int index, const std::string& input); |
---|
121 | bool tset(unsigned int index, const std::string& input); |
---|
122 | bool parse(unsigned int index, const std::string& input); |
---|
123 | bool parse(unsigned int index, const std::string& input, const MultiTypeMath& defvalue); |
---|
124 | |
---|
125 | bool bIsVector_; //!< True if the container contains a std::vector |
---|
126 | |
---|
127 | ConfigFileType type_; //!< The type of the corresponding config-file |
---|
128 | Identifier* identifier_; //!< The identifier of the class |
---|
129 | std::string sectionname_; //!< The name of the class the variable belongs to |
---|
130 | std::string varname_; //!< The name of the variable |
---|
131 | std::string defvalueString_; //!< The string of the default-value |
---|
132 | std::vector<std::string> defvalueStringVector_; //!< A vector, containg the strings of the default-values in case we're storing a vector |
---|
133 | |
---|
134 | MultiTypeMath value_; //!< The value |
---|
135 | std::vector<MultiTypeMath> valueVector_; //!< A vector, containg the values in case we're storing a vector |
---|
136 | |
---|
137 | bool bAddedDescription_; //!< True if a description was added |
---|
138 | LanguageEntryLabel description_; //!< The description |
---|
139 | }; |
---|
140 | } |
---|
141 | |
---|
142 | #endif /* _ConfigValueContainer_H__ */ |
---|