Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add return value version of get_parameter_or #1813

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions rclcpp/include/rclcpp/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,24 @@ class Node : public std::enable_shared_from_this<Node>
ParameterT & parameter,
const ParameterT & alternative_value) const;

/// Return the parameter value, or the "alternative_value" if not set.
/**
* If the parameter was not set, then the "alternative_value" argument is returned.
*
* 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<typename ParameterT>
aprotyas marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
11 changes: 11 additions & 0 deletions rclcpp/include/rclcpp/node_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,17 @@ Node::get_parameter_or(
return got_parameter;
}

template<typename ParameterT>
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.
Expand Down
27 changes: 27 additions & 0 deletions rclcpp/test/rclcpp/test_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<rclcpp::Node>(
"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<rclcpp::Node>(
Expand Down