Skip to content

Commit

Permalink
Cleanup class template SubscriberWrapper
Browse files Browse the repository at this point in the history
Signed-off-by: yamacir-kit <[email protected]>
  • Loading branch information
yamacir-kit committed Dec 16, 2024
1 parent 3897b9f commit cd96887
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
8 changes: 4 additions & 4 deletions external/concealer/include/concealer/autoware_universe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ class AutowareUniverse : public rclcpp::Node,
using TurnIndicatorsReport = autoware_vehicle_msgs::msg::TurnIndicatorsReport;
using VelocityReport = autoware_vehicle_msgs::msg::VelocityReport;

SubscriberWrapper<Control, ThreadSafety::safe> getCommand;
SubscriberWrapper<GearCommand, ThreadSafety::safe> getGearCommand;
SubscriberWrapper<TurnIndicatorsCommand, ThreadSafety::safe> getTurnIndicatorsCommand;
SubscriberWrapper<PathWithLaneId, ThreadSafety::safe> getPathWithLaneId;
SubscriberWrapper<Control> getCommand;
SubscriberWrapper<GearCommand> getGearCommand;
SubscriberWrapper<TurnIndicatorsCommand> getTurnIndicatorsCommand;
SubscriberWrapper<PathWithLaneId> getPathWithLaneId;

PublisherWrapper<AccelWithCovarianceStamped> setAcceleration;
PublisherWrapper<nav_msgs::msg::Odometry> setOdometry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ struct FieldOperatorApplication : public rclcpp::Node,
std::string minimum_risk_maneuver_behavior;

// clang-format off
SubscriberWrapper<autoware_system_msgs::msg::AutowareState, ThreadSafety::safe> getAutowareState;
SubscriberWrapper<autoware_system_msgs::msg::AutowareState> getAutowareState;
SubscriberWrapper<autoware_control_msgs::msg::Control> getCommand;
SubscriberWrapper<tier4_rtc_msgs::msg::CooperateStatusArray> getCooperateStatusArray;
SubscriberWrapper<tier4_external_api_msgs::msg::Emergency> getEmergencyState;
Expand Down
51 changes: 22 additions & 29 deletions external/concealer/include/concealer/subscriber_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,39 @@

namespace concealer
{
enum class ThreadSafety : bool { unsafe, safe };

template <typename MessageType, ThreadSafety thread_safety = ThreadSafety::unsafe>
template <typename Message>
class SubscriberWrapper
{
typename MessageType::ConstSharedPtr current_value = std::make_shared<const MessageType>();
typename Message::ConstSharedPtr current_value = std::make_shared<const Message>();

typename rclcpp::Subscription<MessageType>::SharedPtr subscription;
typename rclcpp::Subscription<Message>::SharedPtr subscription;

public:
auto operator()() const -> decltype(auto)
{
if constexpr (thread_safety == ThreadSafety::unsafe) {
return *current_value;
} else {
return *std::atomic_load(&current_value);
}
}
auto operator()() const -> const auto & { return *std::atomic_load(&current_value); }

template <typename NodeInterface>
SubscriberWrapper(
const std::string & topic, const rclcpp::QoS & quality_of_service,
NodeInterface & autoware_interface,
const std::function<void(const MessageType &)> & callback = {})
: subscription(autoware_interface.template create_subscription<MessageType>(
template <typename Autoware, typename Callback>
explicit SubscriberWrapper(
const std::string & topic, const rclcpp::QoS & quality_of_service, Autoware & autoware,
const Callback & callback)
: subscription(autoware.template create_subscription<Message>(
topic, quality_of_service,
[this, callback](const typename MessageType::ConstSharedPtr message) {
if constexpr (thread_safety == ThreadSafety::safe) {
std::atomic_store(&current_value, message);
if (current_value and callback) {
callback(*std::atomic_load(&current_value));
}
} else {
if (current_value = message; current_value and callback) {
callback(*current_value);
}
[this, callback](const typename Message::ConstSharedPtr & message) {
if (std::atomic_store(&current_value, message); current_value) {
callback(*std::atomic_load(&current_value));
}
}))
{
}

template <typename Autoware>
explicit SubscriberWrapper(
const std::string & topic, const rclcpp::QoS & quality_of_service, Autoware & autoware)
: subscription(autoware.template create_subscription<Message>(
topic, quality_of_service, [this](const typename Message::ConstSharedPtr & message) {
std::atomic_store(&current_value, message);
}))
{
}
};
} // namespace concealer

Expand Down

0 comments on commit cd96887

Please sign in to comment.