Skip to content

Commit 36a6572

Browse files
committed
WIP dynamic-serialized subscription
Signed-off-by: Emerson Knapp <[email protected]>
1 parent f8c11e9 commit 36a6572

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

rclcpp/include/rclcpp/dynamic_subscription.hpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
namespace rclcpp
3838
{
3939

40+
typedef std::variant<
41+
std::function<void (dynamic_typesupport::DynamicMessage::SharedPtr)>,
42+
std::function<void (dynamic_typesupport::DynamicMessage::SharedPtr, const MessageInfo &)>,
43+
std::function<void (std::shared_ptr<SerializedMessage>)>,
44+
std::function<void (std::shared_ptr<SerializedMessage>, const MessageInfo &)>
45+
> AnyDynamicSubscriptionCallback;
46+
4047
/// %Subscription for messages whose type descriptions are obtained at runtime.
4148
/**
4249
* Since the type is not known at compile time, this is not a template, and the dynamic library
@@ -56,10 +63,7 @@ class DynamicSubscription : public rclcpp::SubscriptionBase
5663
rclcpp::dynamic_typesupport::DynamicMessageTypeSupport::SharedPtr type_support,
5764
const std::string & topic_name,
5865
const rclcpp::QoS & qos,
59-
std::function<void(
60-
rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr,
61-
const rosidl_runtime_c__type_description__TypeDescription &
62-
)> callback,
66+
AnyDynamicSubscriptionCallback callback,
6367
const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & options)
6468
: SubscriptionBase(
6569
node_base,
@@ -69,6 +73,8 @@ class DynamicSubscription : public rclcpp::SubscriptionBase
6973
qos),
7074
options.event_callbacks,
7175
options.use_default_callbacks,
76+
is_callback_serialized(callback) ?
77+
DeliveredMessageKind::SERIALIZED_MESSAGE :
7278
DeliveredMessageKind::DYNAMIC_MESSAGE),
7379
ts_(type_support),
7480
callback_(callback),
@@ -159,11 +165,10 @@ class DynamicSubscription : public rclcpp::SubscriptionBase
159165
private:
160166
RCLCPP_DISABLE_COPY(DynamicSubscription)
161167

168+
static bool is_callback_serialized(const rclcpp::AnyDynamicSubscriptionCallback &);
169+
162170
rclcpp::dynamic_typesupport::DynamicMessageTypeSupport::SharedPtr ts_;
163-
std::function<void(
164-
rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr,
165-
const rosidl_runtime_c__type_description__TypeDescription &
166-
)> callback_;
171+
rclcpp::AnyDynamicSubscriptionCallback callback_;
167172

168173
rclcpp::dynamic_typesupport::DynamicSerializationSupport::SharedPtr serialization_support_;
169174
rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr dynamic_message_;

rclcpp/src/rclcpp/dynamic_subscription.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,17 @@ void DynamicSubscription::handle_message(std::shared_ptr<void> &, const rclcpp::
4141
}
4242

4343
void DynamicSubscription::handle_serialized_message(
44-
const std::shared_ptr<rclcpp::SerializedMessage> &, const rclcpp::MessageInfo &)
44+
const std::shared_ptr<rclcpp::SerializedMessage> & message,
45+
const rclcpp::MessageInfo & message_info)
4546
{
46-
throw rclcpp::exceptions::UnimplementedError(
47-
"handle_serialized_message is not implemented for DynamicSubscription");
47+
using PlainCallback = std::function<void (std::shared_ptr<SerializedMessage>)>;
48+
using InfoCallback = std::function<
49+
void (std::shared_ptr<SerializedMessage>, const MessageInfo &)>;
50+
if (std::holds_alternative<PlainCallback>(callback_)) {
51+
std::get<PlainCallback>(callback_)(message);
52+
} else {
53+
std::get<InfoCallback>(callback_)(message, message_info);
54+
}
4855
}
4956

5057
void DynamicSubscription::handle_loaned_message(void *, const rclcpp::MessageInfo &)
@@ -106,8 +113,25 @@ void DynamicSubscription::handle_dynamic_message(
106113
const rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr & message,
107114
const rclcpp::MessageInfo & message_info)
108115
{
109-
(void) message_info;
110-
callback_(message, ts_->get_rosidl_runtime_c_type_description());
116+
using PlainCallback = std::function<void (dynamic_typesupport::DynamicMessage::SharedPtr)>;
117+
using InfoCallback = std::function<
118+
void (dynamic_typesupport::DynamicMessage::SharedPtr, const MessageInfo &)>;
119+
if (std::holds_alternative<PlainCallback>(callback_)) {
120+
std::get<PlainCallback>(callback_)(message);
121+
} else {
122+
std::get<InfoCallback>(callback_)(message, message_info);
123+
}
124+
}
125+
126+
bool DynamicSubscription::is_callback_serialized(
127+
const rclcpp::AnyDynamicSubscriptionCallback & callback)
128+
{
129+
using PlainCallback = std::function<void (std::shared_ptr<SerializedMessage>)>;
130+
using InfoCallback =
131+
std::function<void (std::shared_ptr<SerializedMessage>, const MessageInfo &)>;
132+
return
133+
std::holds_alternative<PlainCallback>(callback) ||
134+
std::holds_alternative<InfoCallback>(callback);
111135
}
112136

113137
} // namespace rclcpp

0 commit comments

Comments
 (0)