diff options
author | Aaron Rossetto <aaron.rossetto@ni.com> | 2020-03-18 07:36:29 -0500 |
---|---|---|
committer | Aaron Rossetto <aaron.rossetto@ni.com> | 2020-03-18 07:43:20 -0500 |
commit | 3f1554f1a5a71190e2105354b8f6a8faa3347124 (patch) | |
tree | d2f6afa8b0a0b044f642566124e038ab997a6b18 | |
parent | b12c3b203773649e61f61344829924ab7ad43110 (diff) | |
download | uhd-3f1554f1a5a71190e2105354b8f6a8faa3347124.tar.gz uhd-3f1554f1a5a71190e2105354b8f6a8faa3347124.tar.bz2 uhd-3f1554f1a5a71190e2105354b8f6a8faa3347124.zip |
lib: Use from_str<bool> in constrained_device_args_t
This modifies `constrained_device_args_t::bool_arg::parse()` to use
`uhd::cast::from_str<bool>` to interpret strings as Boolean values,
deduplicating the string parsing code and single-sourcing it from
`uhd::cast`.
-rw-r--r-- | host/lib/include/uhdlib/usrp/constrained_device_args.hpp | 24 |
1 files changed, 7 insertions, 17 deletions
diff --git a/host/lib/include/uhdlib/usrp/constrained_device_args.hpp b/host/lib/include/uhdlib/usrp/constrained_device_args.hpp index 7c516f80c..6d836c84d 100644 --- a/host/lib/include/uhdlib/usrp/constrained_device_args.hpp +++ b/host/lib/include/uhdlib/usrp/constrained_device_args.hpp @@ -10,6 +10,7 @@ #include <uhd/exception.hpp> #include <uhd/types/device_addr.hpp> +#include <uhd/utils/cast.hpp> #include <unordered_map> #include <boost/algorithm/string.hpp> #include <boost/assign/list_of.hpp> @@ -220,27 +221,16 @@ public: // Types inline void parse(const std::string& str_rep) { try { - _value = (std::stoi(str_rep) != 0); - } catch (std::exception& ex) { if (str_rep.empty()) { - // If str_rep is empty then the device_addr was set - // without a value which means that the user "set" the flag - _value = true; - } else if (boost::algorithm::to_lower_copy(str_rep) == "true" - || boost::algorithm::to_lower_copy(str_rep) == "yes" - || boost::algorithm::to_lower_copy(str_rep) == "y" - || str_rep == "1") { + // If str_rep is empty, the flag is interpreted as set _value = true; - } else if (boost::algorithm::to_lower_copy(str_rep) == "false" - || boost::algorithm::to_lower_copy(str_rep) == "no" - || boost::algorithm::to_lower_copy(str_rep) == "n" - || str_rep == "0") { - _value = false; } else { - throw uhd::value_error( - str(boost::format("Error parsing boolean parameter %s: %s.") - % key() % ex.what())); + _value = uhd::cast::from_str<bool>(str_rep); } + } catch (std::exception& ex) { + throw uhd::value_error( + str(boost::format("Error parsing boolean parameter %s: %s.") + % key() % ex.what())); } } inline virtual std::string to_string() const |