From 902d558e64118fcb3b1a497fe6502bba2f95db19 Mon Sep 17 00:00:00 2001 From: Rohan Agrawal Date: Mon, 1 Aug 2016 19:49:21 -0700 Subject: [PATCH] Get single parameter (#245) * added function to get parameter by exact name * added ros1 style get_parameter for parameter variant * added ros1 style get_param for non-variant types * Make the get_parameter functions call a private base function * Parameter Variant requires name in constructor * Cleaned up to no longer need private function * Made exception message more clear --- rclcpp/include/rclcpp/node.hpp | 12 ++++++++++++ rclcpp/include/rclcpp/node_impl.hpp | 10 ++++++++++ rclcpp/src/rclcpp/node.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/rclcpp/include/rclcpp/node.hpp b/rclcpp/include/rclcpp/node.hpp index 5448cef..a9d9aa1 100644 --- a/rclcpp/include/rclcpp/node.hpp +++ b/rclcpp/include/rclcpp/node.hpp @@ -253,6 +253,18 @@ public: std::vector get_parameters(const std::vector & names) const; + RCLCPP_PUBLIC + const rclcpp::parameter::ParameterVariant + get_parameter(const std::string & name) const; + + RCLCPP_PUBLIC + bool get_parameter( + const std::string & name, + rclcpp::parameter::ParameterVariant & parameter) const; + + template + bool get_parameter(const std::string & name, ParameterT & parameter) const; + RCLCPP_PUBLIC std::vector describe_parameters(const std::vector & names) const; diff --git a/rclcpp/include/rclcpp/node_impl.hpp b/rclcpp/include/rclcpp/node_impl.hpp index 7c018ad..654359e 100644 --- a/rclcpp/include/rclcpp/node_impl.hpp +++ b/rclcpp/include/rclcpp/node_impl.hpp @@ -368,6 +368,16 @@ void Node::register_param_change_callback(CallbackT && callback) this->parameters_callback_ = callback; } +template +bool Node::get_parameter(const std::string & name, ParameterT & parameter) const +{ + rclcpp::parameter::ParameterVariant parameter_variant(name, parameter); + bool result = get_parameter(name, parameter_variant); + parameter = parameter_variant.get_value(); + + return result; +} + } // namespace node } // namespace rclcpp diff --git a/rclcpp/src/rclcpp/node.cpp b/rclcpp/src/rclcpp/node.cpp index 84fbf3e..bfba5ed 100644 --- a/rclcpp/src/rclcpp/node.cpp +++ b/rclcpp/src/rclcpp/node.cpp @@ -242,6 +242,31 @@ Node::get_parameters( return results; } +const rclcpp::parameter::ParameterVariant +Node::get_parameter(const std::string & name) const +{ + rclcpp::parameter::ParameterVariant parameter; + + if (get_parameter(name, parameter)) { + return parameter; + } else { + throw std::out_of_range("Parameter '" + name + "' not set"); + } +} + +bool Node::get_parameter(const std::string & name, + rclcpp::parameter::ParameterVariant & parameter) const +{ + std::lock_guard lock(mutex_); + + if (parameters_.count(name)) { + parameter = parameters_.at(name); + return true; + } else { + return false; + } +} + std::vector Node::describe_parameters( const std::vector & names) const