@@ -176,8 +176,10 @@ class IntraProcessManager
176
176
*/
177
177
template <
178
178
typename MessageT,
179
+ typename ROSMessageType,
179
180
typename Alloc = std::allocator<void >,
180
- typename Deleter = std::default_delete<MessageT>>
181
+ typename Deleter = std::default_delete<MessageT>
182
+ >
181
183
void
182
184
do_intra_process_publish (
183
185
uint64_t intra_process_publisher_id,
@@ -203,7 +205,7 @@ class IntraProcessManager
203
205
// None of the buffers require ownership, so we promote the pointer
204
206
std::shared_ptr<MessageT> msg = std::move (message);
205
207
206
- this ->template add_shared_msg_to_buffers <MessageT, Alloc, Deleter>(
208
+ this ->template add_shared_msg_to_buffers <MessageT, Alloc, Deleter, ROSMessageType >(
207
209
msg, sub_ids.take_shared_subscriptions );
208
210
} else if (!sub_ids.take_ownership_subscriptions .empty () && // NOLINT
209
211
sub_ids.take_shared_subscriptions .size () <= 1 )
@@ -214,8 +216,7 @@ class IntraProcessManager
214
216
concatenated_vector.end (),
215
217
sub_ids.take_ownership_subscriptions .begin (),
216
218
sub_ids.take_ownership_subscriptions .end ());
217
-
218
- this ->template add_owned_msg_to_buffers <MessageT, Alloc, Deleter>(
219
+ this ->template add_owned_msg_to_buffers <MessageT, Alloc, Deleter, ROSMessageType>(
219
220
std::move (message),
220
221
concatenated_vector,
221
222
allocator);
@@ -226,17 +227,19 @@ class IntraProcessManager
226
227
// for the buffers that do not require ownership
227
228
auto shared_msg = std::allocate_shared<MessageT, MessageAllocatorT>(allocator, *message);
228
229
229
- this ->template add_shared_msg_to_buffers <MessageT, Alloc, Deleter>(
230
+ this ->template add_shared_msg_to_buffers <MessageT, Alloc, Deleter, ROSMessageType >(
230
231
shared_msg, sub_ids.take_shared_subscriptions );
231
- this ->template add_owned_msg_to_buffers <MessageT, Alloc, Deleter>(
232
+ this ->template add_owned_msg_to_buffers <MessageT, Alloc, Deleter, ROSMessageType >(
232
233
std::move (message), sub_ids.take_ownership_subscriptions , allocator);
233
234
}
234
235
}
235
236
236
237
template <
237
238
typename MessageT,
239
+ typename ROSMessageType,
238
240
typename Alloc = std::allocator<void >,
239
- typename Deleter = std::default_delete<MessageT>>
241
+ typename Deleter = std::default_delete<MessageT>
242
+ >
240
243
std::shared_ptr<const MessageT>
241
244
do_intra_process_publish_and_return_shared (
242
245
uint64_t intra_process_publisher_id,
@@ -262,7 +265,7 @@ class IntraProcessManager
262
265
// If there are no owning, just convert to shared.
263
266
std::shared_ptr<MessageT> shared_msg = std::move (message);
264
267
if (!sub_ids.take_shared_subscriptions .empty ()) {
265
- this ->template add_shared_msg_to_buffers <MessageT, Alloc, Deleter>(
268
+ this ->template add_shared_msg_to_buffers <MessageT, Alloc, Deleter, ROSMessageType >(
266
269
shared_msg, sub_ids.take_shared_subscriptions );
267
270
}
268
271
return shared_msg;
@@ -272,12 +275,12 @@ class IntraProcessManager
272
275
auto shared_msg = std::allocate_shared<MessageT, MessageAllocatorT>(allocator, *message);
273
276
274
277
if (!sub_ids.take_shared_subscriptions .empty ()) {
275
- this ->template add_shared_msg_to_buffers <MessageT, Alloc, Deleter>(
278
+ this ->template add_shared_msg_to_buffers <MessageT, Alloc, Deleter, ROSMessageType >(
276
279
shared_msg,
277
280
sub_ids.take_shared_subscriptions );
278
281
}
279
282
if (!sub_ids.take_ownership_subscriptions .empty ()) {
280
- this ->template add_owned_msg_to_buffers <MessageT, Alloc, Deleter>(
283
+ this ->template add_owned_msg_to_buffers <MessageT, Alloc, Deleter, ROSMessageType >(
281
284
std::move (message),
282
285
sub_ids.take_ownership_subscriptions ,
283
286
allocator);
@@ -334,14 +337,22 @@ class IntraProcessManager
334
337
template <
335
338
typename MessageT,
336
339
typename Alloc,
337
- typename Deleter>
340
+ typename Deleter,
341
+ typename ROSMessageType>
338
342
void
339
343
add_shared_msg_to_buffers (
340
344
std::shared_ptr<const MessageT> message,
341
345
std::vector<uint64_t > subscription_ids)
342
346
{
343
347
using PublishedType = typename rclcpp::TypeAdapter<MessageT>::custom_type;
344
- using ROSMessageType = typename rclcpp::TypeAdapter<MessageT>::ros_message_type;
348
+ using ROSMessageTypeAllocatorTraits = allocator::AllocRebind<ROSMessageType, Alloc>;
349
+ using ROSMessageTypeAllocator = typename ROSMessageTypeAllocatorTraits::allocator_type;
350
+ using ROSMessageTypeDeleter = allocator::Deleter<ROSMessageTypeAllocator, ROSMessageType>;
351
+
352
+ using PublishedTypeAllocatorTraits = allocator::AllocRebind<PublishedType, Alloc>;
353
+ using PublishedTypeAllocator = typename PublishedTypeAllocatorTraits::allocator_type;
354
+ using PublishedTypeDeleter = allocator::Deleter<PublishedTypeAllocator, PublishedType>;
355
+
345
356
346
357
for (auto id : subscription_ids) {
347
358
auto subscription_it = subscriptions_.find (id);
@@ -351,11 +362,13 @@ class IntraProcessManager
351
362
auto subscription_base = subscription_it->second .lock ();
352
363
if (subscription_base) {
353
364
auto subscription = std::dynamic_pointer_cast<
354
- rclcpp::experimental::SubscriptionIntraProcessBuffer<PublishedType, Alloc, Deleter>
365
+ rclcpp::experimental::SubscriptionIntraProcessBuffer<PublishedType,
366
+ PublishedTypeAllocator, PublishedTypeDeleter, ROSMessageType>
355
367
>(subscription_base);
356
368
if (nullptr == subscription) {
357
369
auto ros_message_subscription = std::dynamic_pointer_cast<
358
- rclcpp::experimental::ROSMessageIntraProcessBuffer<ROSMessageType, Alloc, Deleter>
370
+ rclcpp::experimental::ROSMessageIntraProcessBuffer<ROSMessageType,
371
+ ROSMessageTypeAllocator, ROSMessageTypeDeleter>
359
372
>(subscription_base);
360
373
361
374
if (nullptr == ros_message_subscription) {
@@ -368,9 +381,22 @@ class IntraProcessManager
368
381
if constexpr (rclcpp::TypeAdapter<MessageT>::is_specialized::value) {
369
382
ROSMessageType ros_msg;
370
383
rclcpp::TypeAdapter<MessageT>::convert_to_ros_message (*message, ros_msg);
371
- ros_message_subscription->provide_intra_process_message (ros_msg);
384
+ ros_message_subscription->provide_intra_process_message (
385
+ std::make_shared<ROSMessageType>(ros_msg));
372
386
} else {
373
- ros_message_subscription->provide_intra_process_message (message);
387
+ if constexpr (std::is_same<MessageT, ROSMessageType>::value) {
388
+ ros_message_subscription->provide_intra_process_message (message);
389
+ } else {
390
+ if constexpr (std::is_same<typename rclcpp::TypeAdapter<MessageT,
391
+ ROSMessageType>::ros_message_type, ROSMessageType>::value)
392
+ {
393
+ ROSMessageType ros_msg;
394
+ rclcpp::TypeAdapter<MessageT, ROSMessageType>::convert_to_ros_message (
395
+ *message, ros_msg);
396
+ ros_message_subscription->provide_intra_process_message (
397
+ std::make_shared<ROSMessageType>(ros_msg));
398
+ }
399
+ }
374
400
}
375
401
}
376
402
} else {
@@ -385,7 +411,8 @@ class IntraProcessManager
385
411
template <
386
412
typename MessageT,
387
413
typename Alloc = std::allocator<void >,
388
- typename Deleter = std::default_delete<MessageT>>
414
+ typename Deleter = std::default_delete<MessageT>,
415
+ typename ROSMessageType = MessageT>
389
416
void
390
417
add_owned_msg_to_buffers (
391
418
std::unique_ptr<MessageT, Deleter> message,
@@ -395,7 +422,14 @@ class IntraProcessManager
395
422
using MessageAllocTraits = allocator::AllocRebind<MessageT, Alloc>;
396
423
using MessageUniquePtr = std::unique_ptr<MessageT, Deleter>;
397
424
using PublishedType = typename rclcpp::TypeAdapter<MessageT>::custom_type;
398
- using ROSMessageType = typename rclcpp::TypeAdapter<MessageT>::ros_message_type;
425
+
426
+ using ROSMessageTypeAllocatorTraits = allocator::AllocRebind<ROSMessageType, Alloc>;
427
+ using ROSMessageTypeAllocator = typename ROSMessageTypeAllocatorTraits::allocator_type;
428
+ using ROSMessageTypeDeleter = allocator::Deleter<ROSMessageTypeAllocator, ROSMessageType>;
429
+
430
+ using PublishedTypeAllocatorTraits = allocator::AllocRebind<PublishedType, Alloc>;
431
+ using PublishedTypeAllocator = typename PublishedTypeAllocatorTraits::allocator_type;
432
+ using PublishedTypeDeleter = allocator::Deleter<PublishedTypeAllocator, PublishedType>;
399
433
400
434
for (auto it = subscription_ids.begin (); it != subscription_ids.end (); it++) {
401
435
auto subscription_it = subscriptions_.find (*it);
@@ -405,41 +439,47 @@ class IntraProcessManager
405
439
auto subscription_base = subscription_it->second .lock ();
406
440
if (subscription_base) {
407
441
auto subscription = std::dynamic_pointer_cast<
408
- rclcpp::experimental::SubscriptionIntraProcessBuffer<PublishedType, Alloc, Deleter>
442
+ rclcpp::experimental::SubscriptionIntraProcessBuffer<PublishedType,
443
+ PublishedTypeAllocator, PublishedTypeDeleter, ROSMessageType>
409
444
>(subscription_base);
410
445
if (nullptr == subscription) {
411
446
auto ros_message_subscription = std::dynamic_pointer_cast<
412
- rclcpp::experimental::ROSMessageIntraProcessBuffer<ROSMessageType, Alloc, Deleter>
447
+ rclcpp::experimental::ROSMessageIntraProcessBuffer<ROSMessageType,
448
+ ROSMessageTypeAllocator, ROSMessageTypeDeleter>
413
449
>(subscription_base);
414
450
415
451
if (nullptr == ros_message_subscription) {
416
452
throw std::runtime_error (
417
- " failed to dynamic cast SubscriptionIntraProcessBase to "
453
+ " -- failed to dynamic cast SubscriptionIntraProcessBase to "
418
454
" SubscriptionIntraProcessBuffer<MessageT, Alloc, Deleter>, which "
419
455
" can happen when the publisher and subscription use different "
420
456
" allocator types, which is not supported" );
421
457
} else {
422
458
if constexpr (rclcpp::TypeAdapter<MessageT>::is_specialized::value) {
423
- using ROSMessageTypeAllocatorTraits = allocator::AllocRebind<ROSMessageType, Alloc>;
424
- auto ptr = ROSMessageTypeAllocatorTraits::allocate (allocator, 1 );
425
- ROSMessageTypeAllocatorTraits::construct (allocator, ptr);
426
- Deleter deleter = message.get_deleter ();
427
- auto ros_msg = std::unique_ptr<ROSMessageType, Deleter>(ptr, deleter);
428
- rclcpp::TypeAdapter<MessageT>::convert_to_ros_message (message, ros_msg);
459
+ ROSMessageTypeAllocator ros_message_alloc (allocator);
460
+ auto ptr = ros_message_alloc.allocate (1 );
461
+ ros_message_alloc.construct (ptr);
462
+ ROSMessageTypeDeleter deleter;
463
+ allocator::set_allocator_for_deleter (&deleter, &allocator);
464
+ rclcpp::TypeAdapter<MessageT>::convert_to_ros_message (*message, *ptr);
465
+ auto ros_msg = std::unique_ptr<ROSMessageType, ROSMessageTypeDeleter>(ptr, deleter);
429
466
ros_message_subscription->provide_intra_process_message (std::move (ros_msg));
430
467
} else {
431
- if (std::next (it) == subscription_ids.end ()) {
432
- // If this is the last subscription, give up ownership
433
- ros_message_subscription->provide_intra_process_message (std::move (message));
434
- } else {
435
- // Copy the message since we have additional subscriptions to serve
436
- MessageUniquePtr copy_message;
437
- Deleter deleter = message.get_deleter ();
438
- auto ptr = MessageAllocTraits::allocate (allocator, 1 );
439
- MessageAllocTraits::construct (allocator, ptr, *message);
440
- copy_message = MessageUniquePtr (ptr, deleter);
441
-
442
- ros_message_subscription->provide_intra_process_message (std::move (copy_message));
468
+ if constexpr (std::is_same<MessageT, ROSMessageType>::value) {
469
+ if (std::next (it) == subscription_ids.end ()) {
470
+ // If this is the last subscription, give up ownership
471
+ ros_message_subscription->provide_intra_process_message (std::move (message));
472
+ } else {
473
+ // Copy the message since we have additional subscriptions to serve
474
+ MessageUniquePtr copy_message;
475
+ Deleter deleter = message.get_deleter ();
476
+ allocator::set_allocator_for_deleter (&deleter, &allocator);
477
+ auto ptr = MessageAllocTraits::allocate (allocator, 1 );
478
+ MessageAllocTraits::construct (allocator, ptr, *message);
479
+ copy_message = MessageUniquePtr (ptr, deleter);
480
+
481
+ ros_message_subscription->provide_intra_process_message (std::move (copy_message));
482
+ }
443
483
}
444
484
}
445
485
}
0 commit comments