From 743f9d6aa8b3c6eaa94e38d71a8a5e1b376fa73e Mon Sep 17 00:00:00 2001 From: gavanderhoorn Date: Wed, 6 Feb 2019 15:56:36 +0100 Subject: [PATCH 1/2] Use templating with DebugArrayString(..). Avoids having to create multiple variants for vectors with different value_types. --- .../rosparam_shortcuts/rosparam_shortcuts.h | 17 ++++++++++++++-- src/rosparam_shortcuts.cpp | 20 ------------------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/include/rosparam_shortcuts/rosparam_shortcuts.h b/include/rosparam_shortcuts/rosparam_shortcuts.h index 5e28f13..6cfef34 100644 --- a/include/rosparam_shortcuts/rosparam_shortcuts.h +++ b/include/rosparam_shortcuts/rosparam_shortcuts.h @@ -96,11 +96,24 @@ bool get(const std::string &parent_name, const ros::NodeHandle &nh, const std::s /** * \brief Output a string of values from an array for debugging * \param array of values + * \tparam ValueType Type of the elements in @values * \return string of numbers separated by commas */ -std::string getDebugArrayString(std::vector values); +template +std::string getDebugArrayString(std::vector values) +{ + std::string debug_values{}; + for (const auto &value : values) debug_values += std::to_string(value) + ","; + return debug_values; +} -std::string getDebugArrayString(std::vector values); +template <> +std::string getDebugArrayString(std::vector values) +{ + std::string debug_values{}; + for (const auto &value : values) debug_values += value + ","; + return debug_values; +} /** * \brief Convert from 6 doubles of [x,y,z] [r,p,y] or 7 doubles of [x, y, z, qw, qx, qy, qz] to a transform diff --git a/src/rosparam_shortcuts.cpp b/src/rosparam_shortcuts.cpp index 81e3e1b..0785512 100644 --- a/src/rosparam_shortcuts.cpp +++ b/src/rosparam_shortcuts.cpp @@ -233,26 +233,6 @@ bool get(const std::string &parent_name, const ros::NodeHandle &nh, const std::s return true; } -std::string getDebugArrayString(std::vector values) -{ - std::stringstream debug_values; - for (std::size_t i = 0; i < values.size(); ++i) - { - debug_values << values[i] << ","; - } - return debug_values.str(); -} - -std::string getDebugArrayString(std::vector values) -{ - std::stringstream debug_values; - for (std::size_t i = 0; i < values.size(); ++i) - { - debug_values << values[i] << ","; - } - return debug_values.str(); -} - bool convertDoublesToEigen(const std::string &parent_name, std::vector values, Eigen::Affine3d &transform) { if (values.size() == 6) From f7fdbdfa6df040a33cbc2ce3c98411d574c3025a Mon Sep 17 00:00:00 2001 From: gavanderhoorn Date: Wed, 6 Feb 2019 15:57:55 +0100 Subject: [PATCH 2/2] Add get(..) overload for vector of bool. --- .../rosparam_shortcuts/rosparam_shortcuts.h | 3 +++ src/rosparam_shortcuts.cpp | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/rosparam_shortcuts/rosparam_shortcuts.h b/include/rosparam_shortcuts/rosparam_shortcuts.h index 6cfef34..e5d2f44 100644 --- a/include/rosparam_shortcuts/rosparam_shortcuts.h +++ b/include/rosparam_shortcuts/rosparam_shortcuts.h @@ -78,6 +78,9 @@ bool get(const std::string &parent_name, const ros::NodeHandle &nh, const std::s bool get(const std::string &parent_name, const ros::NodeHandle &nh, const std::string ¶m_name, std::vector &values); +bool get(const std::string &parent_name, const ros::NodeHandle &nh, const std::string ¶m_name, + std::vector &values); + bool get(const std::string &parent_name, const ros::NodeHandle &nh, const std::string ¶m_name, int &value); bool get(const std::string &parent_name, const ros::NodeHandle &nh, const std::string ¶m_name, std::size_t &value); diff --git a/src/rosparam_shortcuts.cpp b/src/rosparam_shortcuts.cpp index 0785512..72213a1 100644 --- a/src/rosparam_shortcuts.cpp +++ b/src/rosparam_shortcuts.cpp @@ -120,6 +120,27 @@ bool get(const std::string &parent_name, const ros::NodeHandle &nh, const std::s return true; } +bool get(const std::string &parent_name, const ros::NodeHandle &nh, const std::string ¶m_name, + std::vector &values) +{ + // Load a param + if (!nh.hasParam(param_name)) + { + ROS_ERROR_STREAM_NAMED(parent_name, "Missing parameter '" << nh.getNamespace() << "/" << param_name << "'."); + return false; + } + nh.getParam(param_name, values); + + if (values.empty()) + ROS_WARN_STREAM_NAMED(parent_name, "Empty vector for parameter '" << nh.getNamespace() << "/" << param_name << "'" + "."); + + ROS_DEBUG_STREAM_NAMED(parent_name, "Loaded parameter '" << nh.getNamespace() << "/" << param_name + << "' with values [" << getDebugArrayString(values) << "]"); + + return true; +} + bool get(const std::string &parent_name, const ros::NodeHandle &nh, const std::string ¶m_name, int &value) { // Load a param