From 21605b4e8b58053f4fa91a0a5136261da33e3bfb Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Fri, 29 Sep 2017 13:51:43 -0700 Subject: types: sensor_value_t can now be created from map - Adds unit tests for sensor_value_t also --- host/lib/types/sensors.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'host/lib') diff --git a/host/lib/types/sensors.cpp b/host/lib/types/sensors.cpp index 4bffc1bd9..d015cf666 100644 --- a/host/lib/types/sensors.cpp +++ b/host/lib/types/sensors.cpp @@ -57,6 +57,57 @@ sensor_value_t::sensor_value_t( /* NOP */ } +static sensor_value_t::data_type_t _string_to_type( + const std::string &type_str +) { + if (type_str == "STRING") { + return sensor_value_t::STRING; + } else if (type_str == "REALNUM") { + return sensor_value_t::REALNUM; + } else if (type_str == "INTEGER") { + return sensor_value_t::INTEGER; + } else if (type_str == "BOOLEAN") { + return sensor_value_t::BOOLEAN; + } else { + throw uhd::value_error( + std::string("Invalid sensor value type: ") + type_str + ); + } +} + +sensor_value_t::sensor_value_t( + const std::map &sensor_dict +): + name(sensor_dict.at("name")), + value(sensor_dict.at("value")), + unit(sensor_dict.at("unit")), + type(_string_to_type(sensor_dict.at("type"))) +{ + UHD_ASSERT_THROW(not name.empty()); + UHD_ASSERT_THROW(not value.empty()); + try { + if (type == INTEGER) { + to_int(); + } else if (type == REALNUM) { + to_real(); + } + } + catch (const std::invalid_argument&) { + throw uhd::value_error(str( + boost::format("Could not convert sensor value `%s' to type `%s'") + % value + % sensor_dict.at("type") + )); + } + catch (const std::out_of_range&) { + throw uhd::value_error(str( + boost::format("Could not convert sensor value `%s' to type `%s'") + % value + % sensor_dict.at("type") + )); + } +} + sensor_value_t::sensor_value_t(const sensor_value_t& source) { *this = source; -- cgit v1.2.3