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 1 commit
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
10 changes: 8 additions & 2 deletions rclcpp/include/rclcpp/parameter_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ class ParameterValue
typename std::enable_if<type == ParameterType::PARAMETER_DOUBLE, const double &>::type
get() const
{
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE) {
// Allow integers to be cast to doubles
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE &&
value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER)
{
throw ParameterTypeException(ParameterType::PARAMETER_DOUBLE, get_type());
}
return value_.double_value;
Expand Down Expand Up @@ -228,7 +231,10 @@ class ParameterValue
type == ParameterType::PARAMETER_DOUBLE_ARRAY, const std::vector<double> &>::type
get() const
{
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE_ARRAY) {
// Allow integer arrays to be cast to double arrays
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE_ARRAY &&
value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER_ARRAY)
{
throw ParameterTypeException(ParameterType::PARAMETER_DOUBLE_ARRAY, get_type());
}
return value_.double_array_value;
Expand Down
8 changes: 8 additions & 0 deletions rclcpp/src/rclcpp/parameter_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,16 @@ ParameterValue::ParameterValue(const bool bool_value)
ParameterValue::ParameterValue(const int int_value)
{
value_.integer_value = int_value;
// Allow integers to be cast to doubles
value_.double_value = static_cast<double>(int_value);
value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER;
}

ParameterValue::ParameterValue(const int64_t int_value)
{
value_.integer_value = int_value;
// Allow integers to be cast to doubles
value_.double_value = static_cast<double>(int_value);
value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER;
}

Expand Down Expand Up @@ -189,12 +193,16 @@ ParameterValue::ParameterValue(const std::vector<bool> & bool_array_value)
ParameterValue::ParameterValue(const std::vector<int> & int_array_value)
{
value_.integer_array_value.assign(int_array_value.cbegin(), int_array_value.cend());
// Allow integer arrays to be cast to double arrays
value_.double_array_value.assign(int_array_value.cbegin(), int_array_value.cend());
Copy link
Member

Choose a reason for hiding this comment

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

I'm not a fan of this. For primitive types it is not as bad (though I still don't like the idea that the value has two values stored), but for the array I think there's a chance that could be lots of overhead.

value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER_ARRAY;
}

ParameterValue::ParameterValue(const std::vector<int64_t> & int_array_value)
{
value_.integer_array_value = int_array_value;
// Allow integer arrays to be cast to double arrays
value_.double_array_value.assign(int_array_value.cbegin(), int_array_value.cend());
value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER_ARRAY;
}

Expand Down
20 changes: 16 additions & 4 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 Expand Up @@ -453,6 +459,7 @@ TEST(TestParameter, bool_array_variant) {
TEST(TestParameter, integer_array_variant) {
const std::vector<int> TEST_VALUE
{42, -99, std::numeric_limits<int>::max(), std::numeric_limits<int>::lowest(), 0};
const std::vector<double> TEST_VALUE_DOUBLE(TEST_VALUE.cbegin(), TEST_VALUE.cend());

// Direct instantiation
rclcpp::Parameter integer_array_variant("integer_array_param", TEST_VALUE);
Expand Down Expand Up @@ -483,13 +490,15 @@ TEST(TestParameter, integer_array_variant) {
EXPECT_EQ(TEST_VALUE.end(), mismatches.first);
EXPECT_EQ(param_value.end(), mismatches.second);

// Implicit conversion to double array
EXPECT_EQ(TEST_VALUE_DOUBLE, integer_array_variant.as_double_array());

EXPECT_THROW(integer_array_variant.as_bool(), rclcpp::ParameterTypeException);
EXPECT_THROW(integer_array_variant.as_int(), rclcpp::ParameterTypeException);
EXPECT_THROW(integer_array_variant.as_double(), rclcpp::ParameterTypeException);
EXPECT_THROW(integer_array_variant.as_string(), rclcpp::ParameterTypeException);
EXPECT_THROW(integer_array_variant.as_byte_array(), rclcpp::ParameterTypeException);
EXPECT_THROW(integer_array_variant.as_bool_array(), rclcpp::ParameterTypeException);
EXPECT_THROW(integer_array_variant.as_double_array(), rclcpp::ParameterTypeException);
EXPECT_THROW(integer_array_variant.as_string_array(), rclcpp::ParameterTypeException);

EXPECT_EQ(
Expand Down Expand Up @@ -532,6 +541,7 @@ TEST(TestParameter, integer_array_variant) {
TEST(TestParameter, long_integer_array_variant) {
const std::vector<int64_t> TEST_VALUE
{42, -99, std::numeric_limits<int64_t>::max(), std::numeric_limits<int64_t>::lowest(), 0};
const std::vector<double> TEST_VALUE_DOUBLE(TEST_VALUE.cbegin(), TEST_VALUE.cend());

rclcpp::Parameter long_array_variant("long_integer_array_param", TEST_VALUE);
EXPECT_EQ("long_integer_array_param", long_array_variant.get_name());
Expand All @@ -548,13 +558,15 @@ TEST(TestParameter, long_integer_array_variant) {
EXPECT_EQ(TEST_VALUE, long_array_variant.get_value_message().integer_array_value);
EXPECT_EQ(TEST_VALUE, long_array_variant.as_integer_array());

// Implicit conversion to double array
EXPECT_EQ(TEST_VALUE_DOUBLE, long_array_variant.as_double_array());

EXPECT_THROW(long_array_variant.as_bool(), rclcpp::ParameterTypeException);
EXPECT_THROW(long_array_variant.as_int(), rclcpp::ParameterTypeException);
EXPECT_THROW(long_array_variant.as_double(), rclcpp::ParameterTypeException);
EXPECT_THROW(long_array_variant.as_string(), rclcpp::ParameterTypeException);
EXPECT_THROW(long_array_variant.as_byte_array(), rclcpp::ParameterTypeException);
EXPECT_THROW(long_array_variant.as_bool_array(), rclcpp::ParameterTypeException);
EXPECT_THROW(long_array_variant.as_double_array(), rclcpp::ParameterTypeException);
EXPECT_THROW(long_array_variant.as_string_array(), rclcpp::ParameterTypeException);

EXPECT_EQ(
Expand Down