Modulo 4.2.2
Loading...
Searching...
No Matches
MessagePairInterface.hpp
1#pragma once
2
3#include <memory>
4
5#include "modulo_core/communication/MessageType.hpp"
6#include "modulo_core/exceptions.hpp"
7
9
10// forward declaration of derived MessagePair class
11template<typename MsgT, typename DataT>
12class MessagePair;
13
19class MessagePairInterface : public std::enable_shared_from_this<MessagePairInterface> {
20public:
25 explicit MessagePairInterface(MessageType type);
26
30 virtual ~MessagePairInterface() = default;
31
35 MessagePairInterface(const MessagePairInterface& message_pair) = default;
36
55 template<typename MsgT, typename DataT>
56 [[nodiscard]] std::shared_ptr<MessagePair<MsgT, DataT>> get_message_pair(bool validate_pointer = true);
57
67 template<typename MsgT, typename DataT>
68 [[nodiscard]] MsgT write();
69
79 template<typename MsgT, typename DataT>
80 void read(const MsgT& message);
81
86 MessageType get_type() const;
87
88private:
89 MessageType type_;
90};
91
92template<typename MsgT, typename DataT>
93inline std::shared_ptr<MessagePair<MsgT, DataT>> MessagePairInterface::get_message_pair(bool validate_pointer) {
94 std::shared_ptr<MessagePair<MsgT, DataT>> message_pair_ptr;
95 try {
96 message_pair_ptr = std::dynamic_pointer_cast<MessagePair<MsgT, DataT>>(this->shared_from_this());
97 } catch (const std::exception& ex) {
98 if (validate_pointer) {
99 throw exceptions::InvalidPointerException("Message pair interface is not managed by a valid pointer");
100 }
101 }
102 if (message_pair_ptr == nullptr && validate_pointer) {
104 "Unable to cast message pair interface to a message pair pointer of requested type");
105 }
106 return message_pair_ptr;
107}
108
109template<typename MsgT, typename DataT>
111 return this->template get_message_pair<MsgT, DataT>()->write_message();
112}
113
114template<typename MsgT, typename DataT>
115inline void MessagePairInterface::read(const MsgT& message) {
116 this->template get_message_pair<MsgT, DataT>()->read_message(message);
117}
118}// namespace modulo_core::communication
Interface class to enable non-templated writing and reading ROS messages from derived MessagePair ins...
virtual ~MessagePairInterface()=default
Default virtual destructor.
std::shared_ptr< MessagePair< MsgT, DataT > > get_message_pair(bool validate_pointer=true)
Get a pointer to a derived MessagePair instance from a MessagePairInterface pointer.
MessageType get_type() const
Get the MessageType of the MessagePairInterface.
MsgT write()
Get the ROS message of a derived MessagePair instance through the MessagePairInterface pointer.
void read(const MsgT &message)
Read a ROS message and set the data of the derived MessagePair instance through the MessagePairInterf...
MessagePairInterface(const MessagePairInterface &message_pair)=default
Copy constructor from another MessagePairInterface.
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.
MessageType
Enum of all supported ROS message types for the MessagePairInterface.