15 #ifndef RCLCPP__ANY_SUBSCRIPTION_CALLBACK_HPP_
16 #define RCLCPP__ANY_SUBSCRIPTION_CALLBACK_HPP_
21 #include <type_traits>
25 #include "tracetools/tracetools.h"
26 #include "tracetools/utils.hpp"
42 template<
typename MessageT,
typename AllocatorT>
50 template<
typename MessageT,
typename AllocatorT>
112 using ConstRefCallback =
typename HelperT::ConstRefCallback;
113 using ConstRefWithInfoCallback =
typename HelperT::ConstRefWithInfoCallback;
115 using UniquePtrCallback =
typename HelperT::UniquePtrCallback;
116 using UniquePtrWithInfoCallback =
typename HelperT::UniquePtrWithInfoCallback;
118 using SharedConstPtrCallback =
typename HelperT::SharedConstPtrCallback;
119 using SharedConstPtrWithInfoCallback =
typename HelperT::SharedConstPtrWithInfoCallback;
121 using ConstRefSharedConstPtrCallback =
122 typename HelperT::ConstRefSharedConstPtrCallback;
123 using ConstRefSharedConstPtrWithInfoCallback =
124 typename HelperT::ConstRefSharedConstPtrWithInfoCallback;
126 using SharedPtrCallback =
typename HelperT::SharedPtrCallback;
127 using SharedPtrWithInfoCallback =
typename HelperT::SharedPtrWithInfoCallback;
133 message_allocator_ = allocator;
137 [[deprecated(
"use AnySubscriptionCallback(const AllocatorT & allocator) instead")]]
141 if (allocator ==
nullptr) {
144 message_allocator_ = *allocator;
156 template<
typename CallbackT>
167 constexpr
auto is_deprecated =
169 typename scbth::callback_type,
174 typename scbth::callback_type,
180 if constexpr (is_deprecated) {
182 set_deprecated(
static_cast<typename scbth::callback_type
>(callback));
185 callback_variant_ =
static_cast<typename scbth::callback_type
>(callback);
201 callback_variant_ = callback;
213 callback_variant_ = callback;
219 auto ptr = MessageAllocTraits::allocate(message_allocator_, 1);
220 MessageAllocTraits::construct(message_allocator_, ptr, *message);
229 TRACEPOINT(callback_start,
static_cast<const void *
>(
this),
false);
231 if (callback_variant_.index() == 0) {
232 if (std::get<0>(callback_variant_) ==
nullptr) {
239 [&message, &message_info,
this](
auto && callback) {
240 using T = std::decay_t<decltype(callback)>;
242 if constexpr (std::is_same_v<T, ConstRefCallback>) {
244 }
else if constexpr (std::is_same_v<T, ConstRefWithInfoCallback>) {
245 callback(*message, message_info);
246 }
else if constexpr (std::is_same_v<T, UniquePtrCallback>) {
248 }
else if constexpr (std::is_same_v<T, UniquePtrWithInfoCallback>) {
250 }
else if constexpr (
251 std::is_same_v<T, SharedConstPtrCallback>||
252 std::is_same_v<T, ConstRefSharedConstPtrCallback>||
253 std::is_same_v<T, SharedPtrCallback>)
256 }
else if constexpr (
257 std::is_same_v<T, SharedConstPtrWithInfoCallback>||
258 std::is_same_v<T, ConstRefSharedConstPtrWithInfoCallback>||
259 std::is_same_v<T, SharedPtrWithInfoCallback>)
261 callback(message, message_info);
263 static_assert(always_false_v<T>,
"unhandled callback type");
265 }, callback_variant_);
266 TRACEPOINT(callback_end,
static_cast<const void *
>(
this));
274 TRACEPOINT(callback_start,
static_cast<const void *
>(
this),
true);
276 if (callback_variant_.index() == 0) {
277 if (std::get<0>(callback_variant_) ==
nullptr) {
284 [&message, &message_info,
this](
auto && callback) {
285 using T = std::decay_t<decltype(callback)>;
287 if constexpr (std::is_same_v<T, ConstRefCallback>) {
289 }
else if constexpr (std::is_same_v<T, ConstRefWithInfoCallback>) {
290 callback(*message, message_info);
291 }
else if constexpr (
292 std::is_same_v<T, UniquePtrCallback>||
293 std::is_same_v<T, SharedPtrCallback>)
296 }
else if constexpr (
297 std::is_same_v<T, UniquePtrWithInfoCallback>||
298 std::is_same_v<T, SharedPtrWithInfoCallback>)
301 }
else if constexpr (
302 std::is_same_v<T, SharedConstPtrCallback>||
303 std::is_same_v<T, ConstRefSharedConstPtrCallback>)
306 }
else if constexpr (
307 std::is_same_v<T, SharedConstPtrWithInfoCallback>||
308 std::is_same_v<T, ConstRefSharedConstPtrWithInfoCallback>)
310 callback(message, message_info);
312 static_assert(always_false_v<T>,
"unhandled callback type");
314 }, callback_variant_);
315 TRACEPOINT(callback_end,
static_cast<const void *
>(
this));
323 TRACEPOINT(callback_start,
static_cast<const void *
>(
this),
true);
325 if (callback_variant_.index() == 0) {
326 if (std::get<0>(callback_variant_) ==
nullptr) {
333 [&message, &message_info](
auto && callback) {
334 using T = std::decay_t<decltype(callback)>;
336 if constexpr (std::is_same_v<T, ConstRefCallback>) {
338 }
else if constexpr (std::is_same_v<T, ConstRefWithInfoCallback>) {
339 callback(*message, message_info);
340 }
else if constexpr (
341 std::is_same_v<T, UniquePtrCallback>||
342 std::is_same_v<T, SharedConstPtrCallback>||
343 std::is_same_v<T, ConstRefSharedConstPtrCallback>||
344 std::is_same_v<T, SharedPtrCallback>)
347 }
else if constexpr (
348 std::is_same_v<T, UniquePtrWithInfoCallback>||
349 std::is_same_v<T, SharedConstPtrWithInfoCallback>||
350 std::is_same_v<T, ConstRefSharedConstPtrWithInfoCallback>||
351 std::is_same_v<T, SharedPtrWithInfoCallback>)
353 callback(
std::move(message), message_info);
355 static_assert(always_false_v<T>,
"unhandled callback type");
357 }, callback_variant_);
358 TRACEPOINT(callback_end,
static_cast<const void *
>(
this));
366 std::holds_alternative<SharedConstPtrCallback>(callback_variant_) ||
367 std::holds_alternative<SharedConstPtrWithInfoCallback>(callback_variant_) ||
368 std::holds_alternative<ConstRefSharedConstPtrCallback>(callback_variant_) ||
369 std::holds_alternative<ConstRefSharedConstPtrWithInfoCallback>(callback_variant_);
375 #ifndef TRACETOOLS_DISABLED
377 [
this](
auto && callback) {
379 rclcpp_callback_register,
380 static_cast<const void *
>(
this),
381 tracetools::get_symbol(callback));
382 }, callback_variant_);
383 #endif // TRACETOOLS_DISABLED
386 typename HelperT::variant_type &
389 return callback_variant_;
392 const typename HelperT::variant_type &
395 return callback_variant_;
403 typename HelperT::variant_type callback_variant_;
405 MessageAlloc message_allocator_;
406 MessageDeleter message_deleter_;
411 #endif // RCLCPP__ANY_SUBSCRIPTION_CALLBACK_HPP_