str_cast.hpp 3.4 KB

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