str_cast.hpp 3.6 KB

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