Modulo 4.2.2
Loading...
Searching...
No Matches
PublisherInterface.hpp
1#pragma once
2
3#include <memory>
4
5#include "modulo_core/communication/MessagePair.hpp"
6#include "modulo_core/communication/PublisherType.hpp"
7#include "modulo_core/exceptions.hpp"
8
10
11// forward declaration of derived Publisher class
12template<typename PubT, typename MsgT>
13class PublisherHandler;
14
20class PublisherInterface : public std::enable_shared_from_this<PublisherInterface> {
21public:
27 explicit PublisherInterface(PublisherType type, std::shared_ptr<MessagePairInterface> message_pair = nullptr);
28
32 PublisherInterface(const PublisherInterface& publisher) = default;
33
37 virtual ~PublisherInterface() = default;
38
57 template<typename PubT, typename MsgT>
58 std::shared_ptr<PublisherHandler<PubT, MsgT>> get_handler(bool validate_pointer = true);
59
68 virtual void activate();
69
78 virtual void deactivate();
79
90 virtual void publish();
91
95 [[nodiscard]] std::shared_ptr<MessagePairInterface> get_message_pair() const;
96
101 void set_message_pair(const std::shared_ptr<MessagePairInterface>& message_pair);
102
107 PublisherType get_type() const;
108
109protected:
110 std::shared_ptr<MessagePairInterface> message_pair_;
111
112private:
123 template<typename MsgT>
124 void publish(const MsgT& message);
125
126 PublisherType type_;
127};
128
129template<typename PubT, typename MsgT>
130inline std::shared_ptr<PublisherHandler<PubT, MsgT>> PublisherInterface::get_handler(bool validate_pointer) {
131 std::shared_ptr<PublisherHandler<PubT, MsgT>> publisher_ptr;
132 try {
133 publisher_ptr = std::dynamic_pointer_cast<PublisherHandler<PubT, MsgT>>(this->shared_from_this());
134 } catch (const std::exception& ex) {
135 if (validate_pointer) {
136 throw exceptions::InvalidPointerException("Publisher interface is not managed by a valid pointer");
137 }
138 }
139 if (publisher_ptr == nullptr && validate_pointer) {
141 "Unable to cast publisher interface to a publisher pointer of requested type");
142 }
143 return publisher_ptr;
144}
145}// namespace modulo_core::communication
Interface class to enable non-templated activating/deactivating/publishing of ROS publishers from der...
virtual ~PublisherInterface()=default
Default virtual destructor.
virtual void deactivate()
Deactivate ROS publisher of a derived PublisherHandler instance through the PublisherInterface pointe...
PublisherInterface(const PublisherInterface &publisher)=default
Copy constructor from another PublisherInterface.
PublisherType get_type() const
Get the type of the publisher interface.
virtual void publish()
Publish the data stored in the message pair through the ROS publisher of a derived PublisherHandler i...
std::shared_ptr< MessagePairInterface > get_message_pair() const
Get the pointer to the message pair of the PublisherInterface.
std::shared_ptr< MessagePairInterface > message_pair_
The pointer to the stored MessagePair instance.
std::shared_ptr< PublisherHandler< PubT, MsgT > > get_handler(bool validate_pointer=true)
Get a pointer to a derived PublisherHandler instance from a PublisherInterface pointer.
void set_message_pair(const std::shared_ptr< MessagePairInterface > &message_pair)
Set the pointer to the message pair of the PublisherInterface.
virtual void activate()
Activate ROS publisher of a derived PublisherHandler instance through the PublisherInterface pointer.
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.
PublisherType
Enum of supported ROS publisher types for the PublisherInterface.