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

Let integer values be assigned to double parameters #1024

Closed
wants to merge 4 commits into from
Closed
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
22 changes: 13 additions & 9 deletions rclcpp/include/rclcpp/parameter_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class ParameterValue

template<ParameterType type>
constexpr
typename std::enable_if<type == ParameterType::PARAMETER_BOOL, const bool &>::type
typename std::enable_if<type == ParameterType::PARAMETER_BOOL, bool>::type
get() const
{
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_BOOL) {
Expand All @@ -155,7 +155,7 @@ class ParameterValue

template<ParameterType type>
constexpr
typename std::enable_if<type == ParameterType::PARAMETER_INTEGER, const int64_t &>::type
typename std::enable_if<type == ParameterType::PARAMETER_INTEGER, int64_t>::type
get() const
{
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER) {
Expand All @@ -166,13 +166,17 @@ class ParameterValue

template<ParameterType type>
constexpr
typename std::enable_if<type == ParameterType::PARAMETER_DOUBLE, const double &>::type
typename std::enable_if<type == ParameterType::PARAMETER_DOUBLE, double>::type
get() const
{
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE) {
throw ParameterTypeException(ParameterType::PARAMETER_DOUBLE, get_type());
// Allow integer values to be cast to double
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really document this somewhere, maybe in the high level get/declare_parameter() APIs, or maybe just right here.

if (value_.type == rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER) {
return static_cast<double>(value_.integer_value);
}
return value_.double_value;
if (value_.type == rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE) {
return value_.double_value;
}
throw ParameterTypeException(ParameterType::PARAMETER_DOUBLE, get_type());
}

template<ParameterType type>
Expand Down Expand Up @@ -250,7 +254,7 @@ class ParameterValue

template<typename type>
constexpr
typename std::enable_if<std::is_same<type, bool>::value, const bool &>::type
typename std::enable_if<std::is_same<type, bool>::value, bool>::type
get() const
{
return get<ParameterType::PARAMETER_BOOL>();
Expand All @@ -259,15 +263,15 @@ class ParameterValue
template<typename type>
constexpr
typename std::enable_if<
std::is_integral<type>::value && !std::is_same<type, bool>::value, const int64_t &>::type
std::is_integral<type>::value && !std::is_same<type, bool>::value, int64_t>::type
get() const
{
return get<ParameterType::PARAMETER_INTEGER>();
}

template<typename type>
constexpr
typename std::enable_if<std::is_floating_point<type>::value, const double &>::type
typename std::enable_if<std::is_floating_point<type>::value, double>::type
get() const
{
return get<ParameterType::PARAMETER_DOUBLE>();
Expand Down
10 changes: 8 additions & 2 deletions rclcpp/test/test_parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ TEST(TestParameter, bool_variant) {

TEST(TestParameter, integer_variant) {
const int TEST_VALUE {42};
const double TEST_VALUE_DOUBLE {static_cast<double>(TEST_VALUE)};

// Direct instantiation
rclcpp::Parameter integer_variant("integer_param", TEST_VALUE);
Expand All @@ -133,8 +134,10 @@ TEST(TestParameter, integer_variant) {
integer_variant.get_value_message().type);
EXPECT_EQ(TEST_VALUE, integer_variant.as_int());

// Implicit conversion to double
EXPECT_EQ(TEST_VALUE_DOUBLE, integer_variant.as_double());

EXPECT_THROW(integer_variant.as_bool(), rclcpp::ParameterTypeException);
EXPECT_THROW(integer_variant.as_double(), rclcpp::ParameterTypeException);
EXPECT_THROW(integer_variant.as_string(), rclcpp::ParameterTypeException);
EXPECT_THROW(integer_variant.as_byte_array(), rclcpp::ParameterTypeException);
EXPECT_THROW(integer_variant.as_bool_array(), rclcpp::ParameterTypeException);
Expand Down Expand Up @@ -166,6 +169,7 @@ TEST(TestParameter, integer_variant) {

TEST(TestParameter, long_integer_variant) {
const int64_t TEST_VALUE {std::numeric_limits<int64_t>::max()};
const double TEST_VALUE_DOUBLE {static_cast<double>(TEST_VALUE)};

// Direct instantiation
rclcpp::Parameter long_variant("long_integer_param", TEST_VALUE);
Expand All @@ -181,8 +185,10 @@ TEST(TestParameter, long_integer_variant) {
long_variant.get_value_message().type);
EXPECT_EQ(TEST_VALUE, long_variant.as_int());

// Implicit conversion to double
EXPECT_EQ(TEST_VALUE_DOUBLE, long_variant.as_double());

EXPECT_THROW(long_variant.as_bool(), rclcpp::ParameterTypeException);
EXPECT_THROW(long_variant.as_double(), rclcpp::ParameterTypeException);
EXPECT_THROW(long_variant.as_string(), rclcpp::ParameterTypeException);
EXPECT_THROW(long_variant.as_byte_array(), rclcpp::ParameterTypeException);
EXPECT_THROW(long_variant.as_bool_array(), rclcpp::ParameterTypeException);
Expand Down