Skip to content

Commit

Permalink
Use const& signature for read-only sub callbacks
Browse files Browse the repository at this point in the history
The constant reference message signatures should be preferred over the
shared pointer to constant message signatures for read-only subscriber
callbacks, as discussed in ros2/rclcpp#1598.

As such, it makes sense to migrate to said signatures in the examples.

Signed-off-by: Abrar Rahman Protyasha <[email protected]>
  • Loading branch information
aprotyas committed Feb 9, 2022
1 parent 200a16c commit 3c8098b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions rclcpp/topics/minimal_subscriber/member_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class MinimalSubscriber : public rclcpp::Node
}

private:
void topic_callback(const std_msgs::msg::String::ConstSharedPtr msg) const
void topic_callback(const std_msgs::msg::String & msg) const
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str());
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MinimalSubscriberWithTopicStatistics : public rclcpp::Node
// configure the topic name (default '/statistics')
// options.topic_stats_options.publish_topic = "/topic_statistics"

auto callback = [this](std_msgs::msg::String::ConstSharedPtr msg) {
auto callback = [this](const std_msgs::msg::String & msg) {
this->topic_callback(msg);
};

Expand All @@ -45,9 +45,9 @@ class MinimalSubscriberWithTopicStatistics : public rclcpp::Node
}

private:
void topic_callback(const std_msgs::msg::String::ConstSharedPtr msg) const
void topic_callback(const std_msgs::msg::String & msg) const
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str());
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ class MinimalSubscriberWithUniqueNetworkFlowEndpoints : public rclcpp::Node
}

private:
void topic_1_callback(const std_msgs::msg::String::ConstSharedPtr msg) const
void topic_1_callback(const std_msgs::msg::String & msg) const
{
RCLCPP_INFO(this->get_logger(), "Topic 1 news: '%s'", msg->data.c_str());
RCLCPP_INFO(this->get_logger(), "Topic 1 news: '%s'", msg.data.c_str());
}
void topic_2_callback(const std_msgs::msg::String::ConstSharedPtr msg) const
void topic_2_callback(const std_msgs::msg::String & msg) const
{
RCLCPP_INFO(this->get_logger(), "Topic 2 news: '%s'", msg->data.c_str());
RCLCPP_INFO(this->get_logger(), "Topic 2 news: '%s'", msg.data.c_str());
}
/// Print network flow endpoints in JSON-like format
void print_network_flow_endpoints(
Expand Down
4 changes: 2 additions & 2 deletions rclcpp/topics/minimal_subscriber/not_composable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ rclcpp::Node::SharedPtr g_node = nullptr;
* examples for the "new" recommended styles. This example is only included
* for completeness because it is similar to "classic" standalone ROS nodes. */

void topic_callback(const std_msgs::msg::String::ConstSharedPtr msg)
void topic_callback(const std_msgs::msg::String & msg)
{
RCLCPP_INFO(g_node->get_logger(), "I heard: '%s'", msg->data.c_str());
RCLCPP_INFO(g_node->get_logger(), "I heard: '%s'", msg.data.c_str());
}

int main(int argc, char * argv[])
Expand Down

0 comments on commit 3c8098b

Please sign in to comment.