diff options
author | Martin Braun <martin.braun@ettus.com> | 2019-08-26 10:51:52 -0700 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2019-11-26 11:49:10 -0800 |
commit | e250da5003bcc7a7068e54b11ba86e51563255be (patch) | |
tree | a02f3a67a428f3fcafee8c6a5e1d9fcfb03f750a | |
parent | e9fa920ad86ee9d270789a564726a08591408312 (diff) | |
download | uhd-e250da5003bcc7a7068e54b11ba86e51563255be.tar.gz uhd-e250da5003bcc7a7068e54b11ba86e51563255be.tar.bz2 uhd-e250da5003bcc7a7068e54b11ba86e51563255be.zip |
utils: cast: Add from_str() typecast
This is the inverse to std::to_string(), and we can overload it with
UHD-internal types.
-rw-r--r-- | host/include/uhd/utils/cast.hpp | 21 | ||||
-rw-r--r-- | host/lib/utils/CMakeLists.txt | 1 | ||||
-rw-r--r-- | host/lib/utils/cast.cpp | 39 | ||||
-rw-r--r-- | host/tests/cast_test.cpp | 8 |
4 files changed, 68 insertions, 1 deletions
diff --git a/host/include/uhd/utils/cast.hpp b/host/include/uhd/utils/cast.hpp index d6d86d119..fd591dae7 100644 --- a/host/include/uhd/utils/cast.hpp +++ b/host/include/uhd/utils/cast.hpp @@ -1,6 +1,7 @@ // // Copyright 2014-2015 Ettus Research LLC // Copyright 2018 Ettus Research, a National Instruments Company +// Copyright 2019 Ettus Research, a National Instruments Brand // // SPDX-License-Identifier: GPL-3.0-or-later // @@ -9,6 +10,7 @@ #define INCLUDED_UHD_UTILS_CAST_HPP #include <uhd/config.hpp> +#include <uhd/exception.hpp> #include <sstream> #include <string> @@ -18,7 +20,8 @@ namespace uhd { namespace cast { // Example: // uint16_t x = hexstr_cast<uint16_t>("0xDEADBEEF"); // Uses stringstream. -template <typename T> UHD_INLINE T hexstr_cast(const std::string& in) +template <typename T> +inline T hexstr_cast(const std::string& in) { T x; std::stringstream ss; @@ -27,6 +30,22 @@ template <typename T> UHD_INLINE T hexstr_cast(const std::string& in) return x; } +//! Generic cast-from-string function +template <typename data_t> +data_t from_str(const std::string&) +{ + throw uhd::runtime_error("Cannot convert from string!"); +} + +template <> +UHD_API double from_str(const std::string& val); + +template <> +UHD_API int from_str(const std::string& val); + +template <> +UHD_API std::string from_str(const std::string& val); + }} // namespace uhd::cast #endif /* INCLUDED_UHD_UTILS_CAST_HPP */ diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt index b4dd72c94..74b292fbb 100644 --- a/host/lib/utils/CMakeLists.txt +++ b/host/lib/utils/CMakeLists.txt @@ -219,6 +219,7 @@ set_source_files_properties( # Append sources ######################################################################## LIBUHD_APPEND_SOURCES( + ${CMAKE_CURRENT_SOURCE_DIR}/cast.cpp ${CMAKE_CURRENT_SOURCE_DIR}/csv.cpp ${CMAKE_CURRENT_SOURCE_DIR}/config_parser.cpp ${CMAKE_CURRENT_SOURCE_DIR}/compat_check.cpp diff --git a/host/lib/utils/cast.cpp b/host/lib/utils/cast.cpp new file mode 100644 index 000000000..635d844ee --- /dev/null +++ b/host/lib/utils/cast.cpp @@ -0,0 +1,39 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#include <uhd/utils/cast.hpp> + +using namespace uhd; + +template <> +double cast::from_str(const std::string& val) +{ + try { + return std::stod(val); + } catch (std::invalid_argument&) { + throw uhd::runtime_error(std::string("Cannot convert `") + val + "' to double!"); + } catch (std::out_of_range&) { + throw uhd::runtime_error(std::string("Cannot convert `") + val + "' to double!"); + } +} + +template <> +int cast::from_str(const std::string& val) +{ + try { + return std::stoi(val); + } catch (std::invalid_argument&) { + throw uhd::runtime_error(std::string("Cannot convert `") + val + "' to int!"); + } catch (std::out_of_range&) { + throw uhd::runtime_error(std::string("Cannot convert `") + val + "' to int!"); + } +} + +template <> +std::string cast::from_str(const std::string& val) +{ + return val; +} diff --git a/host/tests/cast_test.cpp b/host/tests/cast_test.cpp index 296574b48..9f7941a1d 100644 --- a/host/tests/cast_test.cpp +++ b/host/tests/cast_test.cpp @@ -20,3 +20,11 @@ BOOST_AUTO_TEST_CASE(test_mac_addr) << "?" << std::endl; BOOST_CHECK_EQUAL(x, correct_result); } + +BOOST_AUTO_TEST_CASE(test_from_str) +{ + using namespace uhd::cast; + BOOST_CHECK_EQUAL(5.0, from_str<double>("5.0")); + BOOST_CHECK_EQUAL(23, from_str<int>("23")); + BOOST_CHECK_EQUAL("foo", from_str<std::string>("foo")); +} |