Changeset 726 for code/branches/FICN/src/misc/Convert.h
- Timestamp:
- Dec 30, 2007, 1:50:02 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/misc/Convert.h
r724 r726 20 20 * 21 21 * Author: 22 * Benjamin Grauer 23 * Co-authors: 22 24 * Fabian 'x3n' Landau 23 * Co-authors:24 * ...25 *26 25 */ 27 26 … … 37 36 #include <sstream> 38 37 39 //! The Convert class has some static member functions to convert strings to values and values to strings. 40 class Convert 38 39 // DEFAULT CLASS 40 template <typename FromType, typename ToType> 41 class Converter 41 42 { 42 public: 43 /** 44 @brief Converts a value of any type to a string. 45 @param output The string to write the result in 46 @param input The variable to convert 47 @return True if the conversion succeded 48 49 @example 50 float f = 3.14; 51 std::string output; 52 bool success = Convert::ToString(output, f); 53 */ 54 template <typename T> 55 static bool ToString(std::string* output, T input) 56 { 57 std::ostringstream oss; 58 if (oss << input) 59 { 60 (*output) = oss.str(); 61 return true; 62 } 63 64 return false; 65 } 66 67 /** 68 @brief Converts a value of any type to a string and assigns a defaultvalue if the conversion fails. 69 @param output The string to write the result in 70 @param input The variable to convert 71 @param fallbackString The assigned string if the conversion fails. 72 @return True if the conversion succeeded 73 74 @example 75 float f = 3.14; 76 std::string output; 77 bool success = Convert::ToString(output, f, "0.000000"); 78 */ 79 template <typename T> 80 static bool ToString(std::string* output, T input, const std::string& fallbackString) 81 { 82 if (Convert::ToString(output, input)) 83 return true; 84 85 (*output) = fallbackString; 86 return false; 87 } 88 89 /** 90 @brief Converts a string to a value of any type. 91 @param output The variable to assign the result to 92 @param input The string to convert 93 @return True if the conversion succeeded 94 95 @example 96 std::string input = "3.14"; 97 float f; 98 bool success = string2Number(f, input); 99 */ 100 template <typename T> 101 static bool FromString(T* output, const std::string& input) 102 { 103 std::istringstream iss(input); 104 if (iss >> (*output)) 105 return true; 106 107 return false; 108 } 109 110 /** 111 @brief Converts a string to a value of any type. 112 @param output The variable to assign the result to 113 @param input The string to convert 114 @param fallbackValue The assigned value if the conversion fails 115 @return True if the conversion succeeded 116 117 @example 118 std::string input = "3.14"; 119 float f; 120 bool success = string2Number(f, input, 0.000000); 121 */ 122 template <typename T> 123 static bool FromString(T* output, const std::string& input, T fallbackValue) 124 { 125 if (Convert::FromString(output, input)) 126 return true; 127 128 (*output) = fallbackValue; 129 return false; 130 } 43 public: 44 bool operator()(ToType* output, const FromType& input) const 45 { 46 return false; 47 } 131 48 }; 132 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 133 103 #endif /* _Convert_H__ */
Note: See TracChangeset
for help on using the changeset viewer.