From cd96887a5dad48ffd21e444485a94bbdf7fa747b Mon Sep 17 00:00:00 2001 From: yamacir-kit Date: Mon, 16 Dec 2024 16:09:21 +0900 Subject: [PATCH] Cleanup class template `SubscriberWrapper` Signed-off-by: yamacir-kit --- .../include/concealer/autoware_universe.hpp | 8 +-- .../concealer/field_operator_application.hpp | 2 +- .../include/concealer/subscriber_wrapper.hpp | 51 ++++++++----------- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/external/concealer/include/concealer/autoware_universe.hpp b/external/concealer/include/concealer/autoware_universe.hpp index 20df13aafac..18a4d15c080 100644 --- a/external/concealer/include/concealer/autoware_universe.hpp +++ b/external/concealer/include/concealer/autoware_universe.hpp @@ -56,10 +56,10 @@ class AutowareUniverse : public rclcpp::Node, using TurnIndicatorsReport = autoware_vehicle_msgs::msg::TurnIndicatorsReport; using VelocityReport = autoware_vehicle_msgs::msg::VelocityReport; - SubscriberWrapper getCommand; - SubscriberWrapper getGearCommand; - SubscriberWrapper getTurnIndicatorsCommand; - SubscriberWrapper getPathWithLaneId; + SubscriberWrapper getCommand; + SubscriberWrapper getGearCommand; + SubscriberWrapper getTurnIndicatorsCommand; + SubscriberWrapper getPathWithLaneId; PublisherWrapper setAcceleration; PublisherWrapper setOdometry; diff --git a/external/concealer/include/concealer/field_operator_application.hpp b/external/concealer/include/concealer/field_operator_application.hpp index 686a44e37ac..c0ee1ff7070 100644 --- a/external/concealer/include/concealer/field_operator_application.hpp +++ b/external/concealer/include/concealer/field_operator_application.hpp @@ -131,7 +131,7 @@ struct FieldOperatorApplication : public rclcpp::Node, std::string minimum_risk_maneuver_behavior; // clang-format off - SubscriberWrapper getAutowareState; + SubscriberWrapper getAutowareState; SubscriberWrapper getCommand; SubscriberWrapper getCooperateStatusArray; SubscriberWrapper getEmergencyState; diff --git a/external/concealer/include/concealer/subscriber_wrapper.hpp b/external/concealer/include/concealer/subscriber_wrapper.hpp index 8247a872e3a..eb4fde75af0 100644 --- a/external/concealer/include/concealer/subscriber_wrapper.hpp +++ b/external/concealer/include/concealer/subscriber_wrapper.hpp @@ -20,46 +20,39 @@ namespace concealer { -enum class ThreadSafety : bool { unsafe, safe }; - -template +template class SubscriberWrapper { - typename MessageType::ConstSharedPtr current_value = std::make_shared(); + typename Message::ConstSharedPtr current_value = std::make_shared(); - typename rclcpp::Subscription::SharedPtr subscription; + typename rclcpp::Subscription::SharedPtr subscription; public: - auto operator()() const -> decltype(auto) - { - if constexpr (thread_safety == ThreadSafety::unsafe) { - return *current_value; - } else { - return *std::atomic_load(¤t_value); - } - } + auto operator()() const -> const auto & { return *std::atomic_load(¤t_value); } - template - SubscriberWrapper( - const std::string & topic, const rclcpp::QoS & quality_of_service, - NodeInterface & autoware_interface, - const std::function & callback = {}) - : subscription(autoware_interface.template create_subscription( + template + explicit SubscriberWrapper( + const std::string & topic, const rclcpp::QoS & quality_of_service, Autoware & autoware, + const Callback & callback) + : subscription(autoware.template create_subscription( topic, quality_of_service, - [this, callback](const typename MessageType::ConstSharedPtr message) { - if constexpr (thread_safety == ThreadSafety::safe) { - std::atomic_store(¤t_value, message); - if (current_value and callback) { - callback(*std::atomic_load(¤t_value)); - } - } else { - if (current_value = message; current_value and callback) { - callback(*current_value); - } + [this, callback](const typename Message::ConstSharedPtr & message) { + if (std::atomic_store(¤t_value, message); current_value) { + callback(*std::atomic_load(¤t_value)); } })) { } + + template + explicit SubscriberWrapper( + const std::string & topic, const rclcpp::QoS & quality_of_service, Autoware & autoware) + : subscription(autoware.template create_subscription( + topic, quality_of_service, [this](const typename Message::ConstSharedPtr & message) { + std::atomic_store(¤t_value, message); + })) + { + } }; } // namespace concealer