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

Make loaned messages const on the subscription side #992

Open
wants to merge 2 commits into
base: rolling
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions rcl/include/rcl/subscription.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ RCL_WARN_UNUSED
rcl_ret_t
rcl_take_loaned_message(
const rcl_subscription_t * subscription,
void ** loaned_message,
const void ** loaned_message,
rmw_message_info_t * message_info,
rmw_subscription_allocation_t * allocation);

Expand Down Expand Up @@ -662,7 +662,7 @@ RCL_WARN_UNUSED
rcl_ret_t
rcl_return_loaned_message_from_subscription(
const rcl_subscription_t * subscription,
void * loaned_message);
const void * loaned_message);

/// Get the topic name for the subscription.
/**
Expand Down
9 changes: 5 additions & 4 deletions rcl/src/rcl/subscription.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ rcl_take_serialized_message(
rcl_ret_t
rcl_take_loaned_message(
const rcl_subscription_t * subscription,
void ** loaned_message,
const void ** loaned_message,
rmw_message_info_t * message_info,
rmw_subscription_allocation_t * allocation)
{
Expand All @@ -630,7 +630,8 @@ rcl_take_loaned_message(
// Call rmw_take_with_info.
bool taken = false;
rmw_ret_t ret = rmw_take_loaned_message_with_info(
subscription->impl->rmw_handle, loaned_message, &taken, message_info_local, allocation);
subscription->impl->rmw_handle, (void **) loaned_message, &taken, message_info_local,
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems to me that rather than doing this cast here, we should first change rmw_take_loaned_message_with_info to take a const void ** loaned_message. At that point, everything will be const and no casts will be needed.

Same goes below for rmw_return_loaned_message_from_subscription.

allocation);
if (ret != RMW_RET_OK) {
RCL_SET_ERROR_MSG(rmw_get_error_string().str);
return rcl_convert_rmw_ret_to_rcl_ret(ret);
Expand All @@ -646,7 +647,7 @@ rcl_take_loaned_message(
rcl_ret_t
rcl_return_loaned_message_from_subscription(
const rcl_subscription_t * subscription,
void * loaned_message)
const void * loaned_message)
{
RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME, "Subscription releasing loaned message");
if (!rcl_subscription_is_valid(subscription)) {
Expand All @@ -655,7 +656,7 @@ rcl_return_loaned_message_from_subscription(
RCL_CHECK_ARGUMENT_FOR_NULL(loaned_message, RCL_RET_INVALID_ARGUMENT);
return rcl_convert_rmw_ret_to_rcl_ret(
rmw_return_loaned_message_from_subscription(
subscription->impl->rmw_handle, loaned_message));
subscription->impl->rmw_handle, (void *) loaned_message));
}

const char *
Expand Down
9 changes: 5 additions & 4 deletions rcl/test/rcl/test_subscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,9 +703,9 @@ TEST_F(CLASSNAME(TestSubscriptionFixture, RMW_IMPLEMENTATION), test_subscription
});
}

test_msgs__msg__Strings * msg_loaned = nullptr;
const test_msgs__msg__Strings * msg_loaned = nullptr;
ret = rcl_take_loaned_message(
&subscription, reinterpret_cast<void **>(&msg_loaned), nullptr, nullptr);
&subscription, reinterpret_cast<const void **>(&msg_loaned), nullptr, nullptr);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
EXPECT_EQ(
std::string(test_string),
Expand Down Expand Up @@ -764,8 +764,9 @@ TEST_F(CLASSNAME(TestSubscriptionFixture, RMW_IMPLEMENTATION), test_bad_take_loa
&subscription_options);
ASSERT_EQ(RMW_RET_OK, ret) << rcl_get_error_string().str;

test_msgs__msg__Strings * loaned_message = nullptr;
void ** type_erased_loaned_message_pointer = reinterpret_cast<void **>(&loaned_message);
const test_msgs__msg__Strings * loaned_message = nullptr;
const void ** type_erased_loaned_message_pointer =
reinterpret_cast<const void **>(&loaned_message);
rmw_message_info_t * message_info = nullptr; // is a valid argument
rmw_subscription_allocation_t * allocation = nullptr; // is a valid argument
EXPECT_EQ(
Expand Down