Skip to content

Commit

Permalink
Cleanup class 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 5, 2024
1 parent a81e664 commit 7ccebb2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 35 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 @@ -54,12 +54,12 @@ class AutowareUniverse : public rclcpp::Node,
using AckermannControlCommand = autoware_auto_control_msgs::msg::AckermannControlCommand;
using AccelWithCovarianceStamped = geometry_msgs::msg::AccelWithCovarianceStamped;

SubscriberWrapper<AckermannControlCommand, ThreadSafety::safe> getAckermannControlCommand;
SubscriberWrapper<AckermannControlCommand> getAckermannControlCommand;
public:
SubscriberWrapper<GearCommand, ThreadSafety::safe> getGearCommand;
SubscriberWrapper<GearCommand> getGearCommand;
private:
SubscriberWrapper<TurnIndicatorsCommand, ThreadSafety::safe> getTurnIndicatorsCommand;
SubscriberWrapper<PathWithLaneId, ThreadSafety::safe> getPathWithLaneId;
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 @@ -64,10 +64,10 @@ class FieldOperatorApplicationFor<AutowareUniverse>
// clang-format off
SubscriberWrapper<autoware_auto_control_msgs::msg::AckermannControlCommand> getAckermannControlCommand;
#if __has_include(<autoware_system_msgs/msg/autoware_state.hpp>)
SubscriberWrapper<autoware_system_msgs::msg::AutowareState, ThreadSafety::safe> getAutowareState;
SubscriberWrapper<autoware_system_msgs::msg::AutowareState> getAutowareState;
#endif
#if __has_include(<autoware_auto_system_msgs/msg/autoware_state.hpp>)
SubscriberWrapper<autoware_auto_system_msgs::msg::AutowareState, ThreadSafety::safe> getAutowareAutoState;
SubscriberWrapper<autoware_auto_system_msgs::msg::AutowareState> getAutowareAutoState;
#endif
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 7ccebb2

Please sign in to comment.