From 0f8c595ec50f5a8e7320515d1a5ab48077378e3c Mon Sep 17 00:00:00 2001 From: Kenji Miyake Date: Fri, 29 Oct 2021 21:26:08 +0900 Subject: [PATCH 1/3] Add test case Signed-off-by: Kenji Miyake --- rclcpp/test/rclcpp/test_node.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/rclcpp/test/rclcpp/test_node.cpp b/rclcpp/test/rclcpp/test_node.cpp index f3fb13119f..7b4bade94b 100644 --- a/rclcpp/test/rclcpp/test_node.cpp +++ b/rclcpp/test/rclcpp/test_node.cpp @@ -2075,6 +2075,33 @@ TEST_F(TestNode, get_parameter_or_undeclared_parameters_allowed) { } } +// test get_parameter_or with return value +TEST_F(TestNode, get_parameter_or_with_return_value) { + auto node = std::make_shared( + "test_get_parameter_or_node"_unq); + { + // normal use (declare first) still works + auto name = "parameter"_unq; + + node->declare_parameter(name, 42); + EXPECT_TRUE(node->has_parameter(name)); + + { + const int value = node->get_parameter_or(name, 43); + EXPECT_EQ(value, 42); + } + } + { + // normal use, no declare first + auto name = "parameter"_unq; + + { + const int value = node->get_parameter_or(name, 43); + EXPECT_EQ(value, 43); + } + } +} + // test get_parameters with undeclared not allowed TEST_F(TestNode, get_parameters_undeclared_parameters_not_allowed) { auto node = std::make_shared( From a9c1a1844670f35e937f1ae10d73cd330c256729 Mon Sep 17 00:00:00 2001 From: Kenji Miyake Date: Tue, 2 Nov 2021 01:56:41 +0900 Subject: [PATCH 2/3] Add return value version of get_parameter_or Signed-off-by: Kenji Miyake --- rclcpp/include/rclcpp/node.hpp | 19 +++++++++++++++++++ rclcpp/include/rclcpp/node_impl.hpp | 11 +++++++++++ 2 files changed, 30 insertions(+) diff --git a/rclcpp/include/rclcpp/node.hpp b/rclcpp/include/rclcpp/node.hpp index 7e5f402f8a..a4f3939452 100644 --- a/rclcpp/include/rclcpp/node.hpp +++ b/rclcpp/include/rclcpp/node.hpp @@ -720,6 +720,25 @@ class Node : public std::enable_shared_from_this ParameterT & parameter, const ParameterT & alternative_value) const; + /// Get the parameter value, or the "alternative_value" if not set, and assign it to "parameter". + /** + * If the parameter was not set, then the "parameter" argument is assigned + * the "alternative_value". + * + * This method will not throw the rclcpp::exceptions::ParameterNotDeclaredException exception. + * + * In all cases, the parameter is never set or declared within the node. + * + * \param[in] name The name of the parameter to get. + * \param[in] alternative_value Value to be stored in output if the parameter was not set. + * \returns The value of the parameter. + */ + template + ParameterT + get_parameter_or( + const std::string & name, + const ParameterT & alternative_value) const; + /// Return the parameters by the given parameter names. /** * Like get_parameters(), this method may throw the diff --git a/rclcpp/include/rclcpp/node_impl.hpp b/rclcpp/include/rclcpp/node_impl.hpp index 46f54aa054..086c2bb17c 100644 --- a/rclcpp/include/rclcpp/node_impl.hpp +++ b/rclcpp/include/rclcpp/node_impl.hpp @@ -313,6 +313,17 @@ Node::get_parameter_or( return got_parameter; } +template +ParameterT +Node::get_parameter_or( + const std::string & name, + const ParameterT & alternative_value) const +{ + ParameterT parameter; + get_parameter_or(name, parameter, alternative_value); + return parameter; +} + // this is a partially-specialized version of get_parameter above, // where our concrete type for ParameterT is std::map, but the to-be-determined // type is the value in the map. From 2b3b53db3ea1737955aa37d26d29391b937a66c7 Mon Sep 17 00:00:00 2001 From: Kenji Miyake <31987104+kenji-miyake@users.noreply.github.com> Date: Tue, 2 Nov 2021 10:49:17 +0900 Subject: [PATCH 3/3] Update rclcpp/include/rclcpp/node.hpp Co-authored-by: Abrar Rahman Protyasha Update rclcpp/include/rclcpp/node.hpp Co-authored-by: Abrar Rahman Protyasha Signed-off-by: Kenji Miyake Signed-off-by: Kenji Miyake <31987104+kenji-miyake@users.noreply.github.com> --- rclcpp/include/rclcpp/node.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/rclcpp/include/rclcpp/node.hpp b/rclcpp/include/rclcpp/node.hpp index a4f3939452..2150f60e1c 100644 --- a/rclcpp/include/rclcpp/node.hpp +++ b/rclcpp/include/rclcpp/node.hpp @@ -720,10 +720,9 @@ class Node : public std::enable_shared_from_this ParameterT & parameter, const ParameterT & alternative_value) const; - /// Get the parameter value, or the "alternative_value" if not set, and assign it to "parameter". + /// Return the parameter value, or the "alternative_value" if not set. /** - * If the parameter was not set, then the "parameter" argument is assigned - * the "alternative_value". + * If the parameter was not set, then the "alternative_value" argument is returned. * * This method will not throw the rclcpp::exceptions::ParameterNotDeclaredException exception. *