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 /host/include | |
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.
Diffstat (limited to 'host/include')
-rw-r--r-- | host/include/uhd/utils/cast.hpp | 21 |
1 files changed, 20 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 */ |