Changeset 667 for code/branches/FICN/src/misc/String2Number.h
- Timestamp:
- Dec 22, 2007, 2:13:18 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/misc/String2Number.h
r659 r667 49 49 }; 50 50 51 52 /** 53 @brief Converts a number of any type to a string. 54 @param variable The variable, containing the number. 55 56 @example 57 float f = 3.14; 58 std::string output = number2String(f); 59 */ 60 template <typename T> 61 const std::string& number2String(T variable) 62 { 63 std::ostringstream oss; 64 if (oss << variable) 65 return *(new std::string(oss.str())); 66 67 return *(new std::string("")); 68 } 69 70 /** 71 @brief Converts a number of any type to a string and assigns a defaultvalue if the conversion fails. 72 @param variable The variable, containing the number. 73 @param fallbackString The assigned string if the conversion fails. 74 75 @example 76 float f = 3.14; 77 std::string output = number2String(f, "0.000000"); 78 */ 79 template <typename T> 80 const std::string& number2String(T variable, const std::string& fallbackString) 81 { 82 std::cout << "N2Sa: " << variable << std::endl; 83 84 std::ostringstream oss; 85 if (oss << variable) 86 { 87 std::cout << "N2Sb: " << oss.str() << std::endl; 88 return *(new std::string(oss.str())); 89 } 90 91 return fallbackString; 92 } 93 94 /** 95 @brief Converts a string to a number of any type. 96 @param variable The variable wherein the value gets written 97 @return True if the conversion was successful 98 99 @example 100 float f; 101 bool success = string2Number(f, "3.14"); 102 */ 103 template <typename T> 104 bool string2Number(T& variable, const std::string& valueString) 105 { 106 std::istringstream iss(valueString); 107 if (iss >> variable) 108 return true; 109 110 return false; 111 } 112 113 /** 114 @brief Converts a string to a number of any type. 115 @param variable The variable wherein the value gets written 116 @param fallbackValue The assigned value if the conversion fails 117 @return True if the conversion was successful 118 119 @example 120 float f; 121 bool success = string2Number(f, "3.14", 0.000000); 122 */ 123 template <typename T> 124 bool string2Number(T& variable, const std::string& valueString, T fallbackValue) 125 { 126 std::cout << "S2Na: " << valueString << std::endl; 127 std::istringstream iss(valueString); 128 if (iss >> variable) 129 { 130 std::cout << "S2Nb: " << variable << std::endl; 131 return true; 132 } 133 134 variable = fallbackValue; 135 return false; 136 } 137 51 138 #endif
Note: See TracChangeset
for help on using the changeset viewer.