Changeset 1625 for code/trunk/src/util
- Timestamp:
- Jun 26, 2008, 2:13:17 PM (16 years ago)
- Location:
- code/trunk/src/util
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/util/Convert.h
r1505 r1625 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 … … 299 300 }; 300 301 302 // convert to string Shortcut 303 template <class FromType> 304 std::string convertToString(FromType value) 305 { 306 return getConvertedValue<FromType, std::string>(value); 307 } 308 301 309 // convert from string 302 310 template <class ToType> … … 313 321 } 314 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 } 315 330 316 331 … … 404 419 //////////////////// 405 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 406 436 // Vector2 to std::string 407 437 template <> … … 494 524 //////////////////// 495 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 496 553 // std::string to Vector2 497 554 template <> … … 611 668 612 669 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 613 if (tokens.size() >= 4)670 if (tokens.size() >= 3) 614 671 { 615 672 if (!ConvertValue(&(output->r), tokens[0])) … … 619 676 if (!ConvertValue(&(output->b), tokens[2])) 620 677 return false; 621 if (!ConvertValue(&(output->a), tokens[3])) 622 return false; 678 if (tokens.size() >= 4) 679 { 680 if (!ConvertValue(&(output->a), tokens[3])) 681 return false; 682 } 683 else 684 output->a = 1.0; 623 685 624 686 return true; -
code/trunk/src/util/Math.cc
r1608 r1625 30 30 31 31 #include "Math.h" 32 #include "Convert.h" 32 33 33 34 /** … … 136 137 return (targetposition + targetvelocity * time); 137 138 } 139 140 unsigned long getUniqueNumber() 141 { 142 static unsigned long aNumber = 135; 143 return aNumber++; 144 } 145 146 std::string getUniqueNumberStr() 147 { 148 return convertToString(getUniqueNumber()); 149 } -
code/trunk/src/util/Math.h
r1566 r1625 33 33 34 34 #include <ostream> 35 #include <string> 35 36 36 37 #include <OgreMath.h> … … 39 40 #include <OgreVector4.h> 40 41 #include <OgreMatrix3.h> 42 #include <OgreMatrix4.h> 41 43 #include <OgreQuaternion.h> 42 44 #include <OgreColourValue.h> … … 50 52 typedef Ogre::Vector4 Vector4; 51 53 typedef Ogre::Matrix3 Matrix3; 54 typedef Ogre::Matrix4 Matrix4; 52 55 typedef Ogre::Quaternion Quaternion; 53 56 typedef Ogre::ColourValue ColourValue; … … 161 164 } 162 165 166 _UtilExport unsigned long getUniqueNumber(); 167 _UtilExport std::string getUniqueNumberStr(); 168 163 169 class _UtilExport IntVector2 164 170 { -
code/trunk/src/util/String.cc
r1505 r1625 31 31 #include <cctype> 32 32 #include <iostream> 33 34 /** 35 @brief Blank string as variable so you can use const std::string& even 36 if you have to return "". 37 */ 38 std::string blankString = ""; 33 39 34 40 /** -
code/trunk/src/util/String.h
r1505 r1625 35 35 #include <sstream> 36 36 37 extern _UtilExport std::string blankString; 38 37 39 _UtilExport void strip(std::string* str); 38 40 _UtilExport std::string getStripped(const std::string& str); … … 70 72 _UtilExport unsigned int getNextCommentPosition(const std::string& str, unsigned int start = 0); 71 73 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 74 #endif /* _Util_String_H__ */ -
code/trunk/src/util/UtilPrereqs.h
r1505 r1625 61 61 //----------------------------------------------------------------------- 62 62 class ArgReader; 63 class Convert;64 63 class MultiTypePrimitive; 65 64 class MultiTypeString;
Note: See TracChangeset
for help on using the changeset viewer.