[721] | 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: |
---|
[726] | 22 | * Benjamin Grauer |
---|
| 23 | * Co-authors: |
---|
[721] | 24 | * Fabian 'x3n' Landau |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | /*! |
---|
| 28 | @file Convert.h |
---|
| 29 | @brief Definition and Implementation of the Convert class. |
---|
| 30 | */ |
---|
| 31 | |
---|
| 32 | #ifndef _Convert_H__ |
---|
| 33 | #define _Convert_H__ |
---|
| 34 | |
---|
| 35 | #include <string> |
---|
| 36 | #include <sstream> |
---|
| 37 | |
---|
[792] | 38 | #include "UtilPrereqs.h" |
---|
[726] | 39 | |
---|
| 40 | // DEFAULT CLASS |
---|
| 41 | template <typename FromType, typename ToType> |
---|
[792] | 42 | class _UtilExport Converter |
---|
[721] | 43 | { |
---|
[726] | 44 | public: |
---|
| 45 | bool operator()(ToType* output, const FromType& input) const |
---|
| 46 | { |
---|
| 47 | return false; |
---|
| 48 | } |
---|
| 49 | }; |
---|
[721] | 50 | |
---|
[726] | 51 | // PARTIAL SPECIALIZATION TO CONVERT TO STRINGS |
---|
| 52 | template<typename FromType> |
---|
[792] | 53 | class _UtilExport Converter<FromType, std::string> |
---|
[726] | 54 | { |
---|
| 55 | public: |
---|
| 56 | bool operator()(std::string* output, const FromType& input) const |
---|
| 57 | { |
---|
| 58 | std::ostringstream oss; |
---|
| 59 | if (oss << input) |
---|
[721] | 60 | { |
---|
[726] | 61 | (*output) = oss.str(); |
---|
| 62 | return true; |
---|
[721] | 63 | } |
---|
[726] | 64 | else |
---|
| 65 | return false; |
---|
| 66 | } |
---|
| 67 | }; |
---|
[721] | 68 | |
---|
[726] | 69 | // PARTIAL SPECIALIZATION TO CONVERT FROM STRING |
---|
| 70 | template<typename ToType> |
---|
[792] | 71 | class _UtilExport Converter<std::string, ToType> |
---|
[726] | 72 | { |
---|
| 73 | public: |
---|
| 74 | bool operator()(ToType* output, const std::string& input) const |
---|
| 75 | { |
---|
| 76 | std::istringstream iss(input); |
---|
| 77 | if (iss >> (*output)) |
---|
| 78 | return true; |
---|
| 79 | else |
---|
| 80 | return false; |
---|
| 81 | } |
---|
| 82 | }; |
---|
[721] | 83 | |
---|
[726] | 84 | // FUNCTION SO WE DO NOT HAVE TO TELL THE COMPILER ABOUT THE TYPE |
---|
| 85 | template<typename FromType, typename ToType> |
---|
[792] | 86 | static _UtilExport bool ConvertValue(ToType* output, const FromType& input) |
---|
[726] | 87 | { |
---|
| 88 | Converter<FromType, ToType> converter; |
---|
| 89 | return converter(output, input); |
---|
| 90 | } |
---|
[721] | 91 | |
---|
[726] | 92 | // THE SAME, BUT WITH DEFAULT VALUE |
---|
| 93 | template<typename FromType, typename ToType> |
---|
[792] | 94 | static _UtilExport bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback) |
---|
[726] | 95 | { |
---|
| 96 | Converter<FromType, ToType> converter; |
---|
| 97 | if (converter(output, input)) |
---|
| 98 | return true; |
---|
[721] | 99 | |
---|
[726] | 100 | (*output) = fallback; |
---|
| 101 | return false; |
---|
| 102 | } |
---|
[721] | 103 | |
---|
| 104 | #endif /* _Convert_H__ */ |
---|