3#include "modulo_core/communication/SubscriptionInterface.hpp"
5#include "modulo_core/concepts.hpp"
14template<
typename MsgT>
15class SubscriptionHandler :
public SubscriptionInterface {
23 std::shared_ptr<MessagePairInterface> message_pair =
nullptr,
24 const rclcpp::Logger& logger = rclcpp::get_logger(
"SubscriptionHandler"));
34 [[nodiscard]] std::shared_ptr<rclcpp::Subscription<MsgT>>
get_subscription()
const;
41 void set_subscription(
const std::shared_ptr<rclcpp::Subscription<MsgT>>& subscription);
52 std::function<void(
const std::shared_ptr<MsgT>)>
get_callback();
59 std::function<void(
const std::shared_ptr<MsgT>)>
get_callback(
const std::function<
void()>& user_callback);
69 std::shared_ptr<SubscriptionInterface>
76 void handle_callback_exceptions();
84 std::function<void(
const std::shared_ptr<MsgT>)> get_translated_callback();
92 std::function<void(
const std::shared_ptr<MsgT>)> get_raw_callback();
94 std::shared_ptr<rclcpp::Subscription<MsgT>> subscription_;
95 rclcpp::Logger logger_;
96 std::shared_ptr<rclcpp::Clock> clock_;
97 std::function<void()> user_callback_ = [] {
101template<
typename MsgT>
103 std::shared_ptr<MessagePairInterface> message_pair,
const rclcpp::Logger& logger)
104 :
SubscriptionInterface(std::move(message_pair)), logger_(logger), clock_(std::make_shared<rclcpp::Clock>()) {}
106template<
typename MsgT>
108 this->subscription_.reset();
111template<
typename MsgT>
113 return this->subscription_;
116template<
typename MsgT>
118 if (subscription ==
nullptr) {
121 this->subscription_ = subscription;
124template<
typename MsgT>
126 if (this->message_pair_ ==
nullptr) {
130 if (this->message_pair_->get_type() == MessageType::CUSTOM_MESSAGE) {
132 return get_raw_callback();
134 __builtin_unreachable();
136 return get_translated_callback();
139 return get_raw_callback();
143template<
typename MsgT>
145 return [
this](
const std::shared_ptr<MsgT> message) {
147 this->get_message_pair()->template read<MsgT, MsgT>(*message);
148 this->user_callback_();
150 this->handle_callback_exceptions();
155template<
typename MsgT>
157 this->user_callback_ = user_callback;
160template<
typename MsgT>
161std::function<void(
const std::shared_ptr<MsgT>)>
163 this->set_user_callback(user_callback);
164 return this->get_callback();
167template<
typename MsgT>
169 const std::shared_ptr<rclcpp::Subscription<MsgT>>& subscription) {
170 this->set_subscription(subscription);
171 return std::shared_ptr<SubscriptionInterface>(this->shared_from_this());
174template<
typename MsgT>
180 RCLCPP_WARN_STREAM_THROTTLE(
181 this->logger_, *this->clock_, 1000,
"Exception in subscription callback: " << ex.what());
182 }
catch (
const std::exception& ex) {
183 RCLCPP_WARN_STREAM_THROTTLE(
184 this->logger_, *this->clock_, 1000,
"Unhandled exception in subscription user callback: " << ex.what());
The SubscriptionHandler handles different types of ROS subscriptions to receive data from those subsc...
std::function< void(const std::shared_ptr< MsgT >)> get_callback()
Get a callback function that will be associated with the ROS subscription to receive and translate me...
SubscriptionHandler(std::shared_ptr< MessagePairInterface > message_pair=nullptr, const rclcpp::Logger &logger=rclcpp::get_logger("SubscriptionHandler"))
Constructor with the message pair.
~SubscriptionHandler() override
Destructor to explicitly reset the subscription pointer.
std::shared_ptr< SubscriptionInterface > create_subscription_interface(const std::shared_ptr< rclcpp::Subscription< MsgT > > &subscription)
Create a SubscriptionInterface pointer through an instance of a SubscriptionHandler by providing a RO...
std::shared_ptr< rclcpp::Subscription< MsgT > > get_subscription() const
Getter of the ROS subscription.
void set_subscription(const std::shared_ptr< rclcpp::Subscription< MsgT > > &subscription)
Setter of the ROS subscription.
void set_user_callback(const std::function< void()> &user_callback)
Setter of a user callback function to be executed after the subscription callback.
Interface class to enable non-templated subscriptions with ROS subscriptions from derived Subscriptio...
A base class for all core exceptions.
An exception class to notify that a certain pointer is null.
Modulo Core communication module for handling messages on publication and subscription interfaces.