[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 | |
---|
[497] | 28 | /*! |
---|
| 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 | |
---|
| 45 | #include <string> |
---|
| 46 | #include <list> |
---|
| 47 | |
---|
[682] | 48 | #include "CorePrereqs.h" |
---|
| 49 | |
---|
[497] | 50 | #include "OgreVector2.h" |
---|
| 51 | #include "OgreVector3.h" |
---|
| 52 | #include "OgreColourValue.h" |
---|
| 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: |
---|
[703] | 75 | enum VariableType |
---|
| 76 | { |
---|
| 77 | Int, |
---|
| 78 | uInt, |
---|
| 79 | Char, |
---|
| 80 | uChar, |
---|
| 81 | Float, |
---|
| 82 | Double, |
---|
| 83 | LongDouble, |
---|
| 84 | Bool, |
---|
| 85 | ConstChar, |
---|
| 86 | String, |
---|
| 87 | Vector2, |
---|
| 88 | Vector3, |
---|
| 89 | ColourValue |
---|
| 90 | }; |
---|
| 91 | |
---|
[497] | 92 | ConfigValueContainer(const std::string& classname, const std::string& varname, int defvalue); |
---|
[667] | 93 | ConfigValueContainer(const std::string& classname, const std::string& varname, unsigned int defvalue); |
---|
| 94 | ConfigValueContainer(const std::string& classname, const std::string& varname, char defvalue); |
---|
| 95 | ConfigValueContainer(const std::string& classname, const std::string& varname, unsigned char defvalue); |
---|
| 96 | ConfigValueContainer(const std::string& classname, const std::string& varname, float defvalue); |
---|
[497] | 97 | ConfigValueContainer(const std::string& classname, const std::string& varname, double defvalue); |
---|
[703] | 98 | ConfigValueContainer(const std::string& classname, const std::string& varname, long double defvalue); |
---|
[497] | 99 | ConfigValueContainer(const std::string& classname, const std::string& varname, bool defvalue); |
---|
[667] | 100 | ConfigValueContainer(const std::string& classname, const std::string& varname, const std::string& defvalue); |
---|
[497] | 101 | ConfigValueContainer(const std::string& classname, const std::string& varname, const char* defvalue); |
---|
| 102 | ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::Vector2 defvalue); |
---|
| 103 | ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::Vector3 defvalue); |
---|
| 104 | ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::ColourValue defvalue); |
---|
| 105 | |
---|
[667] | 106 | /** @returns the value. @param value This is only needed to determine the right type. */ |
---|
[497] | 107 | inline int getValue(int value) { return this->value_.value_int_; } |
---|
[667] | 108 | /** @returns the value. @param value This is only needed to determine the right type. */ |
---|
| 109 | inline unsigned int getValue(unsigned int value) { return this->value_.value_uint_; } |
---|
| 110 | /** @returns the value. @param value This is only needed to determine the right type. */ |
---|
| 111 | inline char getValue(char value) { return this->value_.value_char_; } |
---|
| 112 | /** @returns the value. @param value This is only needed to determine the right type. */ |
---|
| 113 | inline unsigned char getValue(unsigned char value) { return this->value_.value_uchar_; } |
---|
| 114 | /** @returns the value. @param value This is only needed to determine the right type. */ |
---|
| 115 | inline float getValue(float value) { return this->value_.value_float_; } |
---|
| 116 | /** @returns the value. @param value This is only needed to determine the right type. */ |
---|
[497] | 117 | inline double getValue(double value) { return this->value_.value_double_; } |
---|
[667] | 118 | /** @returns the value. @param value This is only needed to determine the right type. */ |
---|
[703] | 119 | inline double getValue(long double value) { return this->value_.value_long_double_; } |
---|
| 120 | /** @returns the value. @param value This is only needed to determine the right type. */ |
---|
[497] | 121 | inline bool getValue(bool value) { return this->value_.value_bool_; } |
---|
[667] | 122 | /** @returns the value. @param value This is only needed to determine the right type. */ |
---|
| 123 | inline const std::string& getValue(const std::string& value) { return this->value_string_; } |
---|
| 124 | /** @returns the value. @param value This is only needed to determine the right type. */ |
---|
| 125 | inline const char* getValue(const char* value) { return this->value_string_.c_str(); } |
---|
| 126 | /** @returns the value. @param value This is only needed to determine the right type. */ |
---|
[497] | 127 | inline Ogre::Vector2 getValue(const Ogre::Vector2& value) { return this->value_vector2_; } |
---|
[667] | 128 | /** @returns the value. @param value This is only needed to determine the right type. */ |
---|
[497] | 129 | inline Ogre::Vector3 getValue(const Ogre::Vector3& value) { return this->value_vector3_; } |
---|
[667] | 130 | /** @returns the value. @param value This is only needed to determine the right type. */ |
---|
[497] | 131 | inline Ogre::ColourValue getValue(const Ogre::ColourValue& value) { return this->value_colourvalue_; } |
---|
| 132 | |
---|
[703] | 133 | bool parseSting(const std::string& input); |
---|
| 134 | void resetConfigFileEntry(); |
---|
| 135 | void resetConfigValue(); |
---|
| 136 | |
---|
[497] | 137 | private: |
---|
[703] | 138 | bool parseSting(const std::string& input, int defvalue); |
---|
| 139 | bool parseSting(const std::string& input, unsigned int defvalue); |
---|
| 140 | bool parseSting(const std::string& input, char defvalue); |
---|
| 141 | bool parseSting(const std::string& input, unsigned char defvalue); |
---|
| 142 | bool parseSting(const std::string& input, float defvalue); |
---|
| 143 | bool parseSting(const std::string& input, double defvalue); |
---|
| 144 | bool parseSting(const std::string& input, long double defvalue); |
---|
| 145 | bool parseSting(const std::string& input, bool defvalue); |
---|
| 146 | bool parseSting(const std::string& input, const std::string& defvalue); |
---|
| 147 | bool parseSting(const std::string& input, const char* defvalue); |
---|
| 148 | bool parseSting(const std::string& input, const Ogre::Vector2& defvalue); |
---|
| 149 | bool parseSting(const std::string& input, const Ogre::Vector3& defvalue); |
---|
| 150 | bool parseSting(const std::string& input, const Ogre::ColourValue& defvalue); |
---|
| 151 | |
---|
| 152 | static std::list<std::string>& getConfigFileLines(); |
---|
| 153 | static bool finishedReadingConfigFile(bool finished = false); |
---|
| 154 | void searchConfigFileLine(); |
---|
| 155 | std::string parseValueString(bool bStripped = true); |
---|
| 156 | |
---|
| 157 | static std::string getStrippedLine(const std::string& line); |
---|
| 158 | static bool isEmpty(const std::string& line); |
---|
| 159 | static bool isComment(const std::string& line); |
---|
| 160 | static void readConfigFile(const std::string& filename); |
---|
| 161 | static void writeConfigFile(const std::string& filename); |
---|
| 162 | |
---|
[497] | 163 | std::string classname_; //!< The name of the class the variable belongs to |
---|
| 164 | std::string varname_; //!< The name of the variable |
---|
[667] | 165 | std::string defvalueString_; //!< The string of the default-variable |
---|
[497] | 166 | |
---|
| 167 | union MultiType |
---|
| 168 | { |
---|
| 169 | int value_int_; //!< The value, if the variable is of the type int |
---|
[667] | 170 | unsigned int value_uint_; //!< The value, if the variable is of the type unsigned int |
---|
| 171 | char value_char_; //!< The value, if the variable is of the type char |
---|
| 172 | unsigned char value_uchar_; //!< The value, if the variable is of the type unsigned char |
---|
| 173 | float value_float_; //!< The value, if the variable is of the type float |
---|
[497] | 174 | double value_double_; //!< The value, if the variable is of the type double |
---|
[703] | 175 | long double value_long_double_; //!< The value, if the variable is of the type long double |
---|
[497] | 176 | bool value_bool_; //!< The value, if the variable is of the type bool |
---|
[667] | 177 | } value_; //!< The value of the variable |
---|
[497] | 178 | |
---|
| 179 | std::string value_string_; //!< The value, if the variable is of the type string |
---|
| 180 | Ogre::Vector2 value_vector2_; //!< The value, if the variable is of the type Vector2 |
---|
| 181 | Ogre::Vector3 value_vector3_; //!< The value, if the variable is of the type Vector3 |
---|
| 182 | Ogre::ColourValue value_colourvalue_; //!< The value, if the variable is of the type ColourValue |
---|
| 183 | |
---|
| 184 | std::list<std::string>::iterator configFileLine_; //!< An iterator, pointing to the entry of the variable in the config-file |
---|
[667] | 185 | |
---|
[703] | 186 | VariableType type_; //!< The type of the variable |
---|
[497] | 187 | }; |
---|
| 188 | } |
---|
| 189 | |
---|
[673] | 190 | #endif /* _ConfigValueContainer_H__ */ |
---|