USRP Hardware Driver and Device Manual  Version: 4.11.0.0-0-g0d7ed3b1
UHD and USRP Manual
uhd::cast Namespace Reference

Namespaces

 detail
 Template implementations for uhd::cast::to_str() for common types.
 

Functions

template<typename T >
T hexstr_cast (const std::string &in)
 Convert a hexadecimal string into a value. More...
 
template<typename T >
T fromstr_cast (const std::string &in)
 Convert hexadecimal, decimal, octal or other strings that support the >> operator into a value depending on the prefix. More...
 
template<typename data_t >
data_t from_str (const std::string &val)
 Generic cast-from-string function. More...
 
template<>
UHD_API bool from_str (const std::string &val)
 Specialization of uhd::cast::from_str() for Boolean values. More...
 
UHD_API std::string to_ordinal_string (int val)
 Create an ordinal string from a number. More...
 
template<typename T >
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. More...
 
template<typename T >
std::string to_str (const std::complex< T > &val)
 SFINAE-based template for std::complex<T> types. More...
 
std::string to_str (const std::string &val)
 Overload for std::string and C-style strings. More...
 
std::string to_str (const char *val)
 

Function Documentation

◆ from_str() [1/2]

template<typename data_t >
data_t uhd::cast::from_str ( const std::string &  val)

Generic cast-from-string function.

Handles size_t aliases (unsigned long, unsigned long long) via if constexpr when they are not the same type as uint32_t or uint64_t. Explicit specializations below override this for bool, double, int, uint32_t, uint64_t, uint16_t, uint8_t, and std::string.

Note that we need this generic template to handle arbitrary types at runtime. However, this means we can't use SFINAE to create template specializations, which means any type that requires template logic needs to go into here and must be explicitly handled with if constexpr.

◆ from_str() [2/2]

template<>
UHD_API std::string uhd::cast::from_str ( const std::string &  val)
inline

Specialization of uhd::cast::from_str() for Boolean values.

Specialization of uhd::cast::from_str() for strings.

Specialization of uhd::cast::from_str() for uint64_t values.

Specialization of uhd::cast::from_str() for uint32_t values.

Specialization of uhd::cast::from_str() for uint16_t values.

Specialization of uhd::cast::from_str() for uint8_t values.

Specialization of uhd::cast::from_str() for integer values.

Specialization of uhd::cast::from_str() for double-precision values.

Examples evaluating to true: 'True', 'Yes', 'y', '1', empty string Examples evaluating to false: 'false', 'NO', 'n', '0' Throws uhd::runtime_error if the string can't be converted to bool

◆ fromstr_cast()

template<typename T >
T uhd::cast::fromstr_cast ( const std::string &  in)
inline

Convert hexadecimal, decimal, octal or other strings that support the >> operator into a value depending on the prefix.

Example: 10, 0x10, 010 get parsed to decimal 10, 16, 8. uint32_t x = fromstr_cast<uint32_t>("0xaffe"); Uses istringstream.

◆ hexstr_cast()

template<typename T >
T uhd::cast::hexstr_cast ( const std::string &  in)
inline

Convert a hexadecimal string into a value.

Example: uint16_t x = hexstr_cast<uint16_t>("0xDEADBEEF"); Uses stringstream.

◆ to_ordinal_string()

UHD_API std::string uhd::cast::to_ordinal_string ( int  val)

Create an ordinal string from a number.

◆ to_str() [1/4]

std::string uhd::cast::to_str ( const char *  val)
inline

◆ to_str() [2/4]

template<typename T >
std::string uhd::cast::to_str ( const std::complex< T > &  val)

SFINAE-based template for std::complex<T> types.

This template will work for any std::complex<T> where T has a to_str() function Creates a string of the form "a+jb" or "a-jb"

◆ to_str() [3/4]

std::string uhd::cast::to_str ( const std::string &  val)
inline

Overload for std::string and C-style strings.

◆ to_str() [4/4]

template<typename T >
auto uhd::cast::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.

SFINAE-based template for any type that has a to_string() method.

Template specialization for uint8_t.

Template specialization for int8_t.

Template for floating-point types with round-trip safe precision.

SFINAE-based template for enum types.

This template will automatically work for integer types that std::to_string supports (int, unsigned int, long, unsigned long, long long, unsigned long long, size_t, etc.), excluding int8_t and uint8_t which need special handling, and floating-point types which need precision handling, and excluding types that have their own to_string() method.

Convert enum types to their underlying integral type, then to string

Uses ostringstream with max_digits10 precision and classic locale to ensure round-trip safety for floating-point values.

This template will automatically work for any type T that has a to_string() method that returns a type convertible to std::string.