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