| 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 |  *      Benjamin Grauer | 
|---|
| 23 |  *   Co-authors: | 
|---|
| 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 |  | 
|---|
| 38 |  | 
|---|
| 39 | // DEFAULT CLASS | 
|---|
| 40 | template <typename FromType, typename ToType> | 
|---|
| 41 | class Converter | 
|---|
| 42 | { | 
|---|
| 43 |  public: | 
|---|
| 44 |   bool operator()(ToType* output, const FromType& input) const | 
|---|
| 45 |   { | 
|---|
| 46 |     return false; | 
|---|
| 47 |   } | 
|---|
| 48 | }; | 
|---|
| 49 |  | 
|---|
| 50 | // PARTIAL SPECIALIZATION TO CONVERT TO STRINGS | 
|---|
| 51 | template<typename FromType> | 
|---|
| 52 | class Converter<FromType, std::string> | 
|---|
| 53 | { | 
|---|
| 54 |  public: | 
|---|
| 55 |   bool operator()(std::string* output, const FromType& input) const | 
|---|
| 56 |   { | 
|---|
| 57 |     std::ostringstream oss; | 
|---|
| 58 |     if (oss << input) | 
|---|
| 59 |     { | 
|---|
| 60 |       (*output) = oss.str(); | 
|---|
| 61 |       return true; | 
|---|
| 62 |     } | 
|---|
| 63 |     else | 
|---|
| 64 |       return false; | 
|---|
| 65 |   } | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 | // PARTIAL SPECIALIZATION TO CONVERT FROM STRING | 
|---|
| 69 | template<typename ToType> | 
|---|
| 70 | class Converter<std::string, ToType> | 
|---|
| 71 | { | 
|---|
| 72 |  public: | 
|---|
| 73 |   bool operator()(ToType* output, const std::string& input) const | 
|---|
| 74 |   { | 
|---|
| 75 |     std::istringstream iss(input); | 
|---|
| 76 |     if (iss >> (*output)) | 
|---|
| 77 |       return true; | 
|---|
| 78 |     else | 
|---|
| 79 |       return false; | 
|---|
| 80 |   } | 
|---|
| 81 | }; | 
|---|
| 82 |  | 
|---|
| 83 | // FUNCTION SO WE DO NOT HAVE TO TELL THE COMPILER ABOUT THE TYPE | 
|---|
| 84 | template<typename FromType, typename ToType> | 
|---|
| 85 | static bool ConvertValue(ToType* output, const FromType& input) | 
|---|
| 86 | { | 
|---|
| 87 |   Converter<FromType, ToType> converter; | 
|---|
| 88 |   return converter(output, input); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | // THE SAME, BUT WITH DEFAULT VALUE | 
|---|
| 92 | template<typename FromType, typename ToType> | 
|---|
| 93 | static bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback) | 
|---|
| 94 | { | 
|---|
| 95 |   Converter<FromType, ToType> converter; | 
|---|
| 96 |   if (converter(output, input)) | 
|---|
| 97 |     return true; | 
|---|
| 98 |  | 
|---|
| 99 |   (*output) = fallback; | 
|---|
| 100 |   return false; | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | #endif /* _Convert_H__ */ | 
|---|