50 std::istringstream is(in);
51 is >> std::setbase(0) >> x;
66template <
typename data_t>
70 (std::is_same_v<data_t,
71 size_t> || std::is_same_v<data_t, unsigned long> || std::is_same_v<data_t, unsigned long long>)&&!std::
72 is_same_v<data_t, uint32_t> && !std::is_same_v<data_t, uint64_t>) {
74 if (!val.empty() && val[0] ==
'-') {
75 throw std::out_of_range(
"negative value for unsigned type");
78 unsigned long long tmp = std::stoull(val, &pos);
79 if (pos != val.length()) {
80 throw std::invalid_argument(
"trailing characters");
82 if (tmp > std::numeric_limits<data_t>::max()) {
83 throw std::out_of_range(
"value out of range");
85 return static_cast<data_t
>(tmp);
86 }
catch (std::invalid_argument&) {
88 std::string(
"Cannot convert `") + val +
"' to numeric type!");
89 }
catch (std::out_of_range&) {
91 std::string(
"Cannot convert `") + val +
"' to numeric type!");
152 ->
decltype(std::declval<T>().to_string(), std::true_type{});
167auto to_str(
const T& val) -> std::enable_if_t<
168 std::is_arithmetic_v<
170 decltype(std::to_string(val))>
172 return std::to_string(val);
179auto to_str(
const T& val) -> std::enable_if_t<std::is_enum_v<T>, std::string>
181 return std::to_string(
static_cast<std::underlying_type_t<T>
>(val));
189auto to_str(
const T& val) -> std::enable_if_t<
190 std::is_floating_point_v<T> && !detail::has_to_string_method<T>::value,
193 std::ostringstream oss;
194 oss.imbue(std::locale::classic());
195 oss.precision(std::numeric_limits<T>::max_digits10);
205 -> std::enable_if_t<std::is_same_v<T, int8_t> || std::is_same_v<T, signed char>,
208 return std::to_string(
static_cast<int>(val));
214 -> std::enable_if_t<std::is_same_v<T, uint8_t> || std::is_same_v<T, unsigned char>,
217 return std::to_string(
static_cast<unsigned int>(val));
225std::string
to_str(
const std::complex<T>& val)
227 std::string real_str =
to_str(val.real());
228 T imag_val = val.imag();
231 return real_str +
"+j" +
to_str(imag_val);
233 return real_str +
"-j" +
to_str(-imag_val);
243 -> std::enable_if_t<std::is_convertible_v<
decltype(val.to_string()), std::string>,
246 return val.to_string();
250inline std::string
to_str(
const std::string& val)
255inline std::string
to_str(
const char* val)
257 return std::string(val);
#define UHD_API
Definition config.h:87
Template implementations for uhd::cast::to_str() for common types.
Definition cast.hpp:149
auto has_to_string_method_impl(int) -> decltype(std::declval< T >().to_string(), std::true_type{})
decltype(has_to_string_method_impl< T >(0)) has_to_string_method
Definition cast.hpp:156
T fromstr_cast(const std::string &in)
Definition cast.hpp:47
T hexstr_cast(const std::string &in)
Convert a hexadecimal string into a value.
Definition cast.hpp:30
auto to_str(const T &val) -> std::enable_if_t< std::is_arithmetic_v< T > &&!std::is_floating_point_v< T > &&!std::is_same_v< T, int8_t > &&!std::is_same_v< T, uint8_t > &&!std::is_enum_v< T > &&!detail::has_to_string_method< T >::value, decltype(std::to_string(val))>
SFINAE-based template for integer types that std::to_string can handle.
Definition cast.hpp:167
data_t from_str(const std::string &val)
Generic cast-from-string function.
Definition cast.hpp:67
UHD_API std::string to_ordinal_string(int val)
Create an ordinal string from a number.
Definition build_info.hpp:12
Definition exception.hpp:132