From e250da5003bcc7a7068e54b11ba86e51563255be Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Mon, 26 Aug 2019 10:51:52 -0700 Subject: utils: cast: Add from_str() typecast This is the inverse to std::to_string(), and we can overload it with UHD-internal types. --- host/include/uhd/utils/cast.hpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'host/include') 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 +#include #include #include @@ -18,7 +20,8 @@ namespace uhd { namespace cast { // Example: // uint16_t x = hexstr_cast("0xDEADBEEF"); // Uses stringstream. -template UHD_INLINE T hexstr_cast(const std::string& in) +template +inline T hexstr_cast(const std::string& in) { T x; std::stringstream ss; @@ -27,6 +30,22 @@ template UHD_INLINE T hexstr_cast(const std::string& in) return x; } +//! Generic cast-from-string function +template +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 */ -- cgit v1.2.3