str_cast.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Boost.Geometry
  2. // Copyright (c) 2018, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_GEOMETRY_SRS_PROJECTIONS_STR_CAST_HPP
  8. #define BOOST_GEOMETRY_SRS_PROJECTIONS_STR_CAST_HPP
  9. #include <boost/config.hpp>
  10. #include <boost/geometry/core/exception.hpp>
  11. #include <boost/throw_exception.hpp>
  12. #include <boost/type_traits/is_integral.hpp>
  13. #include <boost/type_traits/is_signed.hpp>
  14. #include <cstdlib>
  15. #include <string>
  16. namespace boost { namespace geometry
  17. {
  18. class bad_str_cast : public geometry::exception
  19. {
  20. virtual char const* what() const throw()
  21. {
  22. return "Unable to convert from string.";
  23. }
  24. };
  25. #ifndef DOXYGEN_NO_DETAIL
  26. namespace detail
  27. {
  28. template
  29. <
  30. typename T,
  31. bool IsIntegral = boost::is_integral<T>::value,
  32. bool IsSigned = boost::is_signed<T>::value
  33. >
  34. struct str_cast_traits_strtox
  35. {
  36. static inline T apply(const char *str, char **str_end)
  37. {
  38. return strtod(str, str_end);
  39. }
  40. };
  41. template <typename T>
  42. struct str_cast_traits_strtox<T, true, true>
  43. {
  44. static inline T apply(const char *str, char **str_end)
  45. {
  46. return strtol(str, str_end, 0);
  47. }
  48. };
  49. template <typename T>
  50. struct str_cast_traits_strtox<T, true, false>
  51. {
  52. static inline T apply(const char *str, char **str_end)
  53. {
  54. return strtoul(str, str_end, 0);
  55. }
  56. };
  57. template <typename T>
  58. struct str_cast_traits_strtox<T, false, false>
  59. {
  60. static inline T apply(const char *str, char **str_end)
  61. {
  62. return strtod(str, str_end);
  63. }
  64. };
  65. // Assuming a compiler supporting r-value references
  66. // supports long long and strtoll, strtoull, strtof, strtold
  67. // If it's MSVC enable in MSVC++ 12.0 aka Visual Studio 2013
  68. // TODO: in MSVC-11.0 _strtoi64() intrinsic could be used
  69. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && (!defined(_MSC_VER) || (_MSC_VER >= 1800))
  70. template <>
  71. struct str_cast_traits_strtox<long long, true, true>
  72. {
  73. static inline long long apply(const char *str, char **str_end)
  74. {
  75. return strtoll(str, str_end, 0);
  76. }
  77. };
  78. template <>
  79. struct str_cast_traits_strtox<unsigned long long, true, false>
  80. {
  81. static inline unsigned long long apply(const char *str, char **str_end)
  82. {
  83. return strtoull(str, str_end, 0);
  84. }
  85. };
  86. template <>
  87. struct str_cast_traits_strtox<float, false, false>
  88. {
  89. static inline float apply(const char *str, char **str_end)
  90. {
  91. return strtof(str, str_end);
  92. }
  93. };
  94. template <>
  95. struct str_cast_traits_strtox<long double, false, false>
  96. {
  97. static inline long double apply(const char *str, char **str_end)
  98. {
  99. return strtold(str, str_end);
  100. }
  101. };
  102. #endif // C++11 strtox supported
  103. template <typename T>
  104. struct str_cast_traits_generic
  105. {
  106. static inline T apply(const char *str)
  107. {
  108. char * str_end = (char*)(void*)str;
  109. T res = str_cast_traits_strtox
  110. <
  111. typename boost::remove_cv<T>::type
  112. >::apply(str, &str_end);
  113. if (str_end == str)
  114. {
  115. BOOST_THROW_EXCEPTION( bad_str_cast() );
  116. }
  117. return res;
  118. }
  119. };
  120. } // namespace detail
  121. #endif // DOXYGEN_NO_DETAIL
  122. template <typename T>
  123. struct str_cast_traits
  124. {
  125. template <typename String>
  126. static inline T apply(String const& str)
  127. {
  128. return detail::str_cast_traits_generic<T>::apply(str.c_str());
  129. }
  130. };
  131. template <typename T, typename String>
  132. inline T str_cast(String const& str)
  133. {
  134. return str_cast_traits<T>::apply(str);
  135. }
  136. }} // namespace boost::geometry
  137. #endif // BOOST_GEOMETRY_SRS_PROJECTIONS_STR_CAST_HPP