Changeset 1595 for code/branches/hud/src/util
- Timestamp:
- Jun 12, 2008, 10:42:15 PM (16 years ago)
- Location:
- code/branches/hud/src/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/hud/src/util/Convert.h
r1588 r1595 43 43 #include "SubString.h" 44 44 #include "MultiTypeMath.h" 45 #include "String.h" 45 46 46 47 // disable annoying warning about forcing value to boolean … … 320 321 } 321 322 }; 323 324 // convert from string Shortcut 325 template <class ToType> 326 ToType convertFromString(std::string str) 327 { 328 return getConvertedValue<std::string, ToType>(str); 329 } 322 330 323 331 … … 411 419 //////////////////// 412 420 421 // bool to std::string 422 template <> 423 struct ConverterSpecialized<bool, std::string, _Explicit_> 424 { 425 enum { specialized = true }; 426 static bool convert(std::string* output, const bool& input) 427 { 428 if (input) 429 *output = "true"; 430 else 431 *output = "false"; 432 return false; 433 } 434 }; 435 413 436 // Vector2 to std::string 414 437 template <> … … 501 524 //////////////////// 502 525 526 // std::string to bool 527 template <> 528 struct ConverterSpecialized<std::string, bool, _Explicit_> 529 { 530 enum { specialized = true }; 531 static bool convert(bool* output, const std::string& input) 532 { 533 std::string stripped = getLowercase(removeTrailingWhitespaces(input)); 534 if (stripped == "true" || stripped == "on" || stripped == "yes") 535 { 536 *output = true; 537 return true; 538 } 539 else if (stripped == "false" || stripped == "off" || stripped == "no") 540 { 541 *output = false; 542 return true; 543 } 544 545 std::istringstream iss(input); 546 if (iss >> (*output)) 547 return true; 548 else 549 return false; 550 } 551 }; 552 503 553 // std::string to Vector2 504 554 template <> -
code/branches/hud/src/util/String.h
r1505 r1595 70 70 _UtilExport unsigned int getNextCommentPosition(const std::string& str, unsigned int start = 0); 71 71 72 //! The Convert class has some static member functions to convert strings to values and values to strings.73 class _UtilExport Convert74 {75 public:76 /**77 @brief Converts a value of any type to a string.78 @param output The string to write the result in79 @param input The variable to convert80 @return True if the conversion succeded81 82 @example83 float f = 3.14;84 std::string output;85 bool success = Convert::ToString(&output, f);86 */87 template <typename T>88 static bool ToString(std::string* output, T input)89 {90 std::ostringstream oss;91 if (oss << input)92 {93 (*output) = oss.str();94 return true;95 }96 97 return false;98 }99 100 /**101 @brief Converts a value of any type to a string and assigns a defaultvalue if the conversion fails.102 @param output The string to write the result in103 @param input The variable to convert104 @param fallbackString The assigned string if the conversion fails.105 @return True if the conversion succeeded106 107 @example108 float f = 3.14;109 std::string output;110 bool success = Convert::ToString(&output, f, "0.000000");111 */112 template <typename T>113 static bool ToString(std::string* output, T input, const std::string& fallbackString)114 {115 if (Convert::ToString(output, input))116 return true;117 118 (*output) = fallbackString;119 return false;120 }121 122 /**123 @brief Converts a string to a value of any type.124 @param output The variable to assign the result to125 @param input The string to convert126 @return True if the conversion succeeded127 128 @example129 std::string input = "3.14";130 float f;131 bool success = string2Number(&f, input);132 */133 template <typename T>134 static bool FromString(T* output, const std::string& input)135 {136 std::istringstream iss(input);137 if (iss >> (*output))138 return true;139 140 return false;141 }142 143 /**144 @brief Converts a string to a value of any type.145 @param output The variable to assign the result to146 @param input The string to convert147 @param fallbackValue The assigned value if the conversion fails148 @return True if the conversion succeeded149 150 @example151 std::string input = "3.14";152 float f;153 bool success = string2Number(&f, input, 0.000000);154 */155 template <typename T>156 static bool FromString(T* output, const std::string& input, T fallbackValue)157 {158 if (Convert::FromString(output, input))159 return true;160 161 (*output) = fallbackValue;162 return false;163 }164 };165 166 72 #endif /* _Util_String_H__ */
Note: See TracChangeset
for help on using the changeset viewer.