home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / string_cast.h < prev    next >
C/C++ Source or Header  |  2008-01-23  |  2KB  |  105 lines

  1. #ifndef K3DSDK_STRING_CAST_H
  2. #define K3DSDK_STRING_CAST_H
  3.  
  4. #include "path.h"
  5.  
  6. #include <boost/format.hpp>
  7.  
  8. #include <limits>
  9. #include <sstream>
  10. #include <string>
  11.  
  12. namespace k3d
  13. {
  14.  
  15. ///////////////////////////////////////////////////////////////////////////
  16. // string_cast
  17.  
  18. /// Converts any serializable type to a string
  19. template<typename type>
  20. const std::string string_cast(const type& RHS)
  21. {
  22.     std::ostringstream buffer;
  23.     if(std::numeric_limits<type>::is_specialized)
  24.             buffer.precision(std::numeric_limits<type>::digits10 + 1);
  25.  
  26.     buffer << RHS;
  27.     return buffer.str();
  28. }
  29.  
  30. /// Specialization of string_cast for type bool
  31. template<>
  32. inline const std::string string_cast<bool>(const bool& RHS)
  33. {
  34.     return RHS ? "true" : "false";
  35. }
  36.  
  37. /// Specialization of string_cast for strings
  38. template<>
  39. inline const std::string string_cast<std::string>(const std::string& RHS)
  40. {
  41.     return RHS;
  42. }
  43.  
  44. /// Specialization of string_cast() for filesystem::path
  45. /** \todo Is this really the correct behavior?  */
  46. template<>
  47. inline const std::string string_cast<filesystem::path>(const filesystem::path& RHS)
  48. {
  49.     return RHS.native_utf8_string().raw();
  50. }
  51.  
  52. /// Specialization of string_cast() for boost::format
  53. template<>
  54. inline const std::string string_cast<boost::format>(const boost::format& RHS)
  55. {
  56.     return RHS.str();
  57. }
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // from_string
  61.  
  62. /// Converts a string into any serializeable type
  63. template<typename type>
  64. const type from_string(const std::string& Value, const type& Default)
  65. {
  66.     type result = Default;
  67.     std::istringstream stream(Value.c_str());
  68.     stream >> result;
  69.  
  70.     return result;
  71. }
  72.  
  73. /// Specialization of from_string for type bool
  74. template<>
  75. inline const bool from_string(const std::string& Value, const bool& Default)
  76. {
  77.     bool result = Default;
  78.  
  79.     if(Value == "true")
  80.         result = true;
  81.     else if(Value == "false")
  82.         result = false;
  83.  
  84.     return result;
  85. }
  86.  
  87. /// Specialization of from_string for strings
  88. template<>
  89. inline const std::string from_string(const std::string& Value, const std::string& Default)
  90. {
  91.     return Value;
  92. }
  93.  
  94. /// Specialization of from_string for filesystem::path
  95. template<>
  96. inline const filesystem::path from_string(const std::string& Value, const filesystem::path& DefaultValue)
  97. {
  98.     return filesystem::native_path(ustring::from_utf8(Value));
  99. }
  100.  
  101. } // namespace k3d
  102.  
  103. #endif // !K3DSDK_STRING_CAST_H
  104.  
  105.