diff options
author | Martin Braun <martin.braun@ettus.com> | 2019-08-23 11:45:41 -0700 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2019-11-26 11:49:37 -0800 |
commit | acc4dab894db1bb6285c3181548af16c1d1091f6 (patch) | |
tree | e1bc8848a5097d78e220a5b3b3b8c822147d3e93 /host | |
parent | e6da166a179ceda0c74c4acb7eb75ea7dc4201c6 (diff) | |
download | uhd-acc4dab894db1bb6285c3181548af16c1d1091f6.tar.gz uhd-acc4dab894db1bb6285c3181548af16c1d1091f6.tar.bz2 uhd-acc4dab894db1bb6285c3181548af16c1d1091f6.zip |
rfnoc: property: Add option to set properties from strings
Diffstat (limited to 'host')
-rw-r--r-- | host/include/uhd/rfnoc/dirtifier.hpp | 5 | ||||
-rw-r--r-- | host/include/uhd/rfnoc/property.hpp | 21 | ||||
-rw-r--r-- | host/tests/rfnoc_property_test.cpp | 27 |
3 files changed, 53 insertions, 0 deletions
diff --git a/host/include/uhd/rfnoc/dirtifier.hpp b/host/include/uhd/rfnoc/dirtifier.hpp index e31b26595..7999df021 100644 --- a/host/include/uhd/rfnoc/dirtifier.hpp +++ b/host/include/uhd/rfnoc/dirtifier.hpp @@ -46,6 +46,11 @@ public: //! Always dirty, so this can be called as often as we like void force_dirty() {} + void set_from_str(const std::string&) + { + throw uhd::runtime_error("Dirtifier property can never be set!"); + } + private: //! This property cannot be marked clean, but nothing happens if you try void mark_clean() {} diff --git a/host/include/uhd/rfnoc/property.hpp b/host/include/uhd/rfnoc/property.hpp index d01613c58..efe4deb1d 100644 --- a/host/include/uhd/rfnoc/property.hpp +++ b/host/include/uhd/rfnoc/property.hpp @@ -9,6 +9,7 @@ #include <uhd/exception.hpp> #include <uhd/rfnoc/res_source_info.hpp> +#include <uhd/utils/cast.hpp> #include <uhd/utils/dirty_tracked.hpp> #include <memory> #include <string> @@ -98,6 +99,16 @@ public: virtual void force_dirty() = 0; + /*! Set this property's value using a string + * + * This requires the underlying property type to be convertible from a + * string. + * + * \throws uhd::runtime_error if the underlying type has no conversion from + * a string + */ + virtual void set_from_str(const std::string& new_val_str) = 0; + private: friend class prop_accessor_t; @@ -175,6 +186,16 @@ public: new property_t<data_t>(get_id(), get(), new_src_info)); } + void set_from_str(const std::string& new_val_str) + { + try { + set(uhd::cast::from_str<data_t>(new_val_str)); + } catch (uhd::runtime_error& ex) { + throw uhd::runtime_error( + std::string("Property ") + get_id() + ":" + ex.what()); + } + } + //! Returns the source info for the property // const res_source_info& get_src_info() const = 0; diff --git a/host/tests/rfnoc_property_test.cpp b/host/tests/rfnoc_property_test.cpp index 13ba1d4eb..7e5af20a2 100644 --- a/host/tests/rfnoc_property_test.cpp +++ b/host/tests/rfnoc_property_test.cpp @@ -150,3 +150,30 @@ BOOST_AUTO_TEST_CASE(test_dirtifier) BOOST_CHECK(!prop_accessor.are_compatible(&prop_i, &dirtifier)); BOOST_CHECK(!prop_accessor.are_compatible(&dirtifier, &prop_i)); } + +BOOST_AUTO_TEST_CASE(test_from_str) +{ + prop_accessor_t prop_accessor{}; + property_t<double> prop_d{"double_prop", 0.0, {res_source_info::USER}}; + property_t<int> prop_i{"int_prop", 0, {res_source_info::USER}}; + property_t<std::string> prop_s{"str_prop", "0", {res_source_info::USER}}; + prop_accessor.set_access(prop_d, property_base_t::RW); + prop_accessor.set_access(prop_i, property_base_t::RW); + prop_accessor.set_access(prop_s, property_base_t::RW); + + property_base_t* prop_base_ptr_d = static_cast<property_base_t*>(&prop_d); + property_base_t* prop_base_ptr_i = static_cast<property_base_t*>(&prop_i); + property_base_t* prop_base_ptr_s = static_cast<property_base_t*>(&prop_s); + + prop_base_ptr_d->set_from_str(".25"); + BOOST_CHECK_EQUAL(prop_d.get(), 0.25); + + prop_base_ptr_i->set_from_str("23"); + BOOST_CHECK_EQUAL(prop_i.get(), 23); + + prop_base_ptr_s->set_from_str("foo"); + BOOST_CHECK_EQUAL(prop_s.get(), "foo"); + + BOOST_REQUIRE_THROW(prop_base_ptr_d->set_from_str("banana"), uhd::runtime_error); + BOOST_REQUIRE_THROW(prop_base_ptr_i->set_from_str("potato"), uhd::runtime_error); +} |