Modulo 5.0.0
Loading...
Searching...
No Matches
SubscriptionInterface.hpp
1#pragma once
2
3#include <rclcpp/subscription.hpp>
4
5#include "modulo_core/communication/MessagePair.hpp"
6#include "modulo_core/exceptions.hpp"
7
9
10// forward declaration of derived SubscriptionHandler class
11template<typename MsgT>
12class SubscriptionHandler;
13
19class SubscriptionInterface : public std::enable_shared_from_this<SubscriptionInterface> {
20public:
25 explicit SubscriptionInterface(std::shared_ptr<MessagePairInterface> message_pair = nullptr);
26
30 SubscriptionInterface(const SubscriptionInterface& subscription) = default;
31
35 virtual ~SubscriptionInterface() = default;
36
55 template<typename MsgT>
56 std::shared_ptr<SubscriptionHandler<MsgT>> get_handler(bool validate_pointer = true);
57
61 [[nodiscard]] std::shared_ptr<MessagePairInterface> get_message_pair() const;
62
67 void set_message_pair(const std::shared_ptr<MessagePairInterface>& message_pair);
68
69protected:
70 std::shared_ptr<MessagePairInterface> message_pair_;
71};
72
73template<typename MsgT>
74inline std::shared_ptr<SubscriptionHandler<MsgT>> SubscriptionInterface::get_handler(bool validate_pointer) {
75 std::shared_ptr<SubscriptionHandler<MsgT>> subscription_ptr;
76 try {
77 subscription_ptr = std::dynamic_pointer_cast<SubscriptionHandler<MsgT>>(this->shared_from_this());
78 } catch (const std::exception& ex) {
79 if (validate_pointer) {
80 throw exceptions::InvalidPointerException("Subscription interface is not managed by a valid pointer");
81 }
82 }
83 if (subscription_ptr == nullptr && validate_pointer) {
85 "Unable to cast subscription interface to a subscription pointer of requested type");
86 }
87 return subscription_ptr;
88}
89}// namespace modulo_core::communication
Interface class to enable non-templated subscriptions with ROS subscriptions from derived Subscriptio...
SubscriptionInterface(const SubscriptionInterface &subscription)=default
Copy constructor from another SubscriptionInterface.
std::shared_ptr< SubscriptionHandler< MsgT > > get_handler(bool validate_pointer=true)
Get a pointer to a derived SubscriptionHandler instance from a SubscriptionInterface pointer.
void set_message_pair(const std::shared_ptr< MessagePairInterface > &message_pair)
Set the pointer to the message pair of the SubscriptionInterface.
std::shared_ptr< MessagePairInterface > message_pair_
The pointer to the stored MessagePair instance.
std::shared_ptr< MessagePairInterface > get_message_pair() const
Get the pointer to the message pair of the SubscriptionInterface.
virtual ~SubscriptionInterface()=default
Default virtual destructor.
An exception class to notify if the result of getting an instance of a derived class through dynamic ...
An exception class to notify if an object has no reference count (if the object is not owned by any p...
Modulo Core communication module for handling messages on publication and subscription interfaces.