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

Use const& signature for read-only sub callbacks #337

Merged
merged 1 commit into from
Feb 16, 2022
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
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