Modulo 4.2.2
Loading...
Searching...
No Matches
PublisherHandler.hpp
1#pragma once
2
3#include <rclcpp/logging.hpp>
4
5#include "modulo_core/communication/PublisherInterface.hpp"
6
7#include <rclcpp_lifecycle/lifecycle_publisher.hpp>
8
10
18template<typename PubT, typename MsgT>
20public:
26 PublisherHandler(PublisherType type, std::shared_ptr<PubT> publisher);
27
31 ~PublisherHandler() override;
32
36 virtual void activate() override;
37
41 virtual void deactivate() override;
42
46 void publish() override;
47
53 void publish(const MsgT& message) const;
54
59 std::shared_ptr<PublisherInterface>
60 create_publisher_interface(const std::shared_ptr<MessagePairInterface>& message_pair);
61
62private:
63 std::shared_ptr<PubT> publisher_;
64
66};
67
68template<typename PubT, typename MsgT>
70 : PublisherInterface(type), publisher_(std::move(publisher)) {}
71
72template<typename PubT, typename MsgT>
74 this->publisher_.reset();
75}
76
77template<typename PubT, typename MsgT>
79 if constexpr (std::derived_from<PubT, rclcpp_lifecycle::LifecyclePublisher<MsgT>>) {
80 if (this->publisher_ == nullptr) {
81 throw exceptions::NullPointerException("Publisher not set");
82 }
83 try {
84 this->publisher_->on_activate();
85 } catch (const std::exception& ex) {
86 throw exceptions::CoreException(ex.what());
87 }
88 }
89}
90
91template<typename PubT, typename MsgT>
93 if constexpr (std::derived_from<PubT, rclcpp_lifecycle::LifecyclePublisher<MsgT>>) {
94 if (this->publisher_ == nullptr) {
95 throw exceptions::NullPointerException("Publisher not set");
96 }
97 try {
98 this->publisher_->on_deactivate();
99 } catch (const std::exception& ex) {
100 throw exceptions::CoreException(ex.what());
101 }
102 }
103}
104
105template<typename PubT, typename MsgT>
107 try {
109 if (this->message_pair_ == nullptr) {
110 throw exceptions::NullPointerException("Message pair is not set, nothing to publish");
111 }
112 publish(this->message_pair_->write<MsgT, MsgT>());
113 } else {
115 }
116 } catch (const exceptions::CoreException& ex) {
117 throw;
118 }
119}
120
121template<typename PubT, typename MsgT>
122void PublisherHandler<PubT, MsgT>::publish(const MsgT& message) const {
123 if (this->publisher_ == nullptr) {
124 throw exceptions::NullPointerException("Publisher not set");
125 }
126 try {
127 this->publisher_->publish(message);
128 } catch (const std::exception& ex) {
129 throw exceptions::CoreException(ex.what());
130 }
131}
132
133template<typename PubT, typename MsgT>
134inline std::shared_ptr<PublisherInterface>
135PublisherHandler<PubT, MsgT>::create_publisher_interface(const std::shared_ptr<MessagePairInterface>& message_pair) {
136 std::shared_ptr<PublisherInterface> publisher_interface;
137 try {
138 publisher_interface = std::shared_ptr<PublisherInterface>(this->shared_from_this());
139 } catch (const std::exception& ex) {
140 throw exceptions::CoreException(ex.what());
141 }
142 publisher_interface->set_message_pair(message_pair);
143 return publisher_interface;
144}
145}// namespace modulo_core::communication
The PublisherHandler handles different types of ROS publishers to activate, deactivate and publish da...
virtual void activate() override
Activate ROS publisher of a derived PublisherHandler instance through the PublisherInterface pointer.
void publish() override
Publish the data stored in the message pair through the ROS publisher of a derived PublisherHandler i...
std::shared_ptr< PublisherInterface > create_publisher_interface(const std::shared_ptr< MessagePairInterface > &message_pair)
Create a PublisherInterface instance from the current PublisherHandler.
~PublisherHandler() override
Destructor to explicitly reset the publisher pointer.
PublisherHandler(PublisherType type, std::shared_ptr< PubT > publisher)
Constructor with the publisher type and the pointer to the ROS publisher.
virtual void deactivate() override
Deactivate ROS publisher of a derived PublisherHandler instance through the PublisherInterface pointe...
Interface class to enable non-templated activating/deactivating/publishing of ROS publishers from der...
virtual void publish()
Publish the data stored in the message pair through the ROS publisher of a derived PublisherHandler i...
std::shared_ptr< MessagePairInterface > message_pair_
The pointer to the stored MessagePair instance.
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.
PublisherType
Enum of supported ROS publisher types for the PublisherInterface.