File indexing completed on 2025-01-18 09:36:41
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_GEOMETRY_SRS_PROJECTIONS_STR_CAST_HPP
0012 #define BOOST_GEOMETRY_SRS_PROJECTIONS_STR_CAST_HPP
0013
0014 #include <boost/config.hpp>
0015 #include <boost/geometry/core/exception.hpp>
0016 #include <boost/throw_exception.hpp>
0017 #include <boost/type_traits/remove_cv.hpp>
0018
0019 namespace boost { namespace geometry
0020 {
0021
0022 class bad_str_cast : public geometry::exception
0023 {
0024 char const* what() const noexcept override
0025 {
0026 return "Unable to convert from string.";
0027 }
0028 };
0029
0030 #ifndef DOXYGEN_NO_DETAIL
0031 namespace detail
0032 {
0033
0034 template
0035 <
0036 typename T,
0037 bool IsIntegral = std::is_integral<T>::value,
0038 bool IsSigned = std::is_signed<T>::value
0039 >
0040 struct str_cast_traits_strtox
0041 {
0042 static inline T apply(const char *str, char **str_end)
0043 {
0044 return strtod(str, str_end);
0045 }
0046 };
0047
0048 template <typename T>
0049 struct str_cast_traits_strtox<T, true, true>
0050 {
0051 static inline T apply(const char *str, char **str_end)
0052 {
0053 return strtol(str, str_end, 0);
0054 }
0055 };
0056
0057 template <typename T>
0058 struct str_cast_traits_strtox<T, true, false>
0059 {
0060 static inline T apply(const char *str, char **str_end)
0061 {
0062 return strtoul(str, str_end, 0);
0063 }
0064 };
0065
0066 template <typename T>
0067 struct str_cast_traits_strtox<T, false, false>
0068 {
0069 static inline T apply(const char *str, char **str_end)
0070 {
0071 return strtod(str, str_end);
0072 }
0073 };
0074
0075 template <>
0076 struct str_cast_traits_strtox<long long, true, true>
0077 {
0078 static inline long long apply(const char *str, char **str_end)
0079 {
0080 return strtoll(str, str_end, 0);
0081 }
0082 };
0083
0084 template <>
0085 struct str_cast_traits_strtox<unsigned long long, true, false>
0086 {
0087 static inline unsigned long long apply(const char *str, char **str_end)
0088 {
0089 return strtoull(str, str_end, 0);
0090 }
0091 };
0092
0093 template <>
0094 struct str_cast_traits_strtox<float, false, false>
0095 {
0096 static inline float apply(const char *str, char **str_end)
0097 {
0098 return strtof(str, str_end);
0099 }
0100 };
0101
0102 template <>
0103 struct str_cast_traits_strtox<long double, false, false>
0104 {
0105 static inline long double apply(const char *str, char **str_end)
0106 {
0107 return strtold(str, str_end);
0108 }
0109 };
0110
0111 template <typename T>
0112 struct str_cast_traits_generic
0113 {
0114 static inline T apply(const char *str)
0115 {
0116 char * str_end = (char*)(void*)str;
0117 T res = str_cast_traits_strtox
0118 <
0119 typename boost::remove_cv<T>::type
0120 >::apply(str, &str_end);
0121 if (str_end == str)
0122 {
0123 BOOST_THROW_EXCEPTION( bad_str_cast() );
0124 }
0125 return res;
0126 }
0127 };
0128
0129 }
0130 #endif
0131
0132 template <typename T>
0133 struct str_cast_traits
0134 {
0135 template <typename String>
0136 static inline T apply(String const& str)
0137 {
0138 return detail::str_cast_traits_generic<T>::apply(str.c_str());
0139 }
0140 };
0141
0142 template <typename T, typename String>
0143 inline T str_cast(String const& str)
0144 {
0145 return str_cast_traits<T>::apply(str);
0146 }
0147
0148
0149 }}
0150
0151 #endif