aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--host/include/uhd/utils/cast.hpp15
-rw-r--r--host/lib/utils/cast.cpp25
-rw-r--r--host/tests/cast_test.cpp20
3 files changed, 60 insertions, 0 deletions
diff --git a/host/include/uhd/utils/cast.hpp b/host/include/uhd/utils/cast.hpp
index fd591dae7..7bfb05a4c 100644
--- a/host/include/uhd/utils/cast.hpp
+++ b/host/include/uhd/utils/cast.hpp
@@ -37,12 +37,27 @@ data_t from_str(const std::string&)
throw uhd::runtime_error("Cannot convert from string!");
}
+// Specializations of `uhd::cast::from_str()` for supported data types
+
+//! Specialization of `uhd::cast::from_str()` for Boolean 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`
+template <>
+UHD_API bool from_str(const std::string& val);
+
+//! Specialization of `uhd::cast::from_str()` for double-precision values
template <>
UHD_API double from_str(const std::string& val);
+//! Specialization of `uhd::cast::from_str()` for integer values
template <>
UHD_API int from_str(const std::string& val);
+//! Specialization of `uhd::cast::from_str()` for strings
+//
+// This function simply returns the incoming string
template <>
UHD_API std::string from_str(const std::string& val);
diff --git a/host/lib/utils/cast.cpp b/host/lib/utils/cast.cpp
index 635d844ee..6b008ff45 100644
--- a/host/lib/utils/cast.cpp
+++ b/host/lib/utils/cast.cpp
@@ -5,10 +5,35 @@
//
#include <uhd/utils/cast.hpp>
+#include <uhd/exception.hpp>
+#include <boost/algorithm/string.hpp>
using namespace uhd;
template <>
+bool cast::from_str(const std::string& val)
+{
+ try {
+ return (std::stoi(val) != 0);
+ } catch (std::exception& ex) {
+ if (boost::algorithm::to_lower_copy(val) == "true"
+ || boost::algorithm::to_lower_copy(val) == "yes"
+ || boost::algorithm::to_lower_copy(val) == "y"
+ || val == "1") {
+ return true;
+ } else if (boost::algorithm::to_lower_copy(val) == "false"
+ || boost::algorithm::to_lower_copy(val) == "no"
+ || boost::algorithm::to_lower_copy(val) == "n"
+ || val == "0") {
+ return false;
+ } else {
+ throw uhd::runtime_error("Cannot convert `" + val + "' to boolean!");
+ return false;
+ }
+ }
+}
+
+template <>
double cast::from_str(const std::string& val)
{
try {
diff --git a/host/tests/cast_test.cpp b/host/tests/cast_test.cpp
index 9f7941a1d..556cb94a0 100644
--- a/host/tests/cast_test.cpp
+++ b/host/tests/cast_test.cpp
@@ -27,4 +27,24 @@ BOOST_AUTO_TEST_CASE(test_from_str)
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"));
+
+ BOOST_CHECK_EQUAL(true, from_str<bool>("true"));
+ BOOST_CHECK_EQUAL(true, from_str<bool>("True"));
+ BOOST_CHECK_EQUAL(true, from_str<bool>("Y"));
+ BOOST_CHECK_EQUAL(true, from_str<bool>("y"));
+ BOOST_CHECK_EQUAL(true, from_str<bool>("YES"));
+ BOOST_CHECK_EQUAL(true, from_str<bool>("yEs"));
+ BOOST_CHECK_EQUAL(true, from_str<bool>("1"));
+
+ BOOST_CHECK_EQUAL(false, from_str<bool>("false"));
+ BOOST_CHECK_EQUAL(false, from_str<bool>("False"));
+ BOOST_CHECK_EQUAL(false, from_str<bool>("n"));
+ BOOST_CHECK_EQUAL(false, from_str<bool>("N"));
+ BOOST_CHECK_EQUAL(false, from_str<bool>("No"));
+ BOOST_CHECK_EQUAL(false, from_str<bool>("nO"));
+ BOOST_CHECK_EQUAL(false, from_str<bool>("0"));
+
+ BOOST_CHECK_THROW(from_str<bool>(""), uhd::runtime_error);
+ BOOST_CHECK_THROW(from_str<bool>("abc"), uhd::runtime_error);
+ BOOST_CHECK_THROW(from_str<bool>("deadbeef"), uhd::runtime_error);
}