15 #ifndef RCLCPP__ANY_SUBSCRIPTION_CALLBACK_HPP_
16 #define RCLCPP__ANY_SUBSCRIPTION_CALLBACK_HPP_
23 #include <type_traits>
30 #include "tracetools/tracetools.h"
31 #include "tracetools/utils.hpp"
36 template<
typename MessageT,
typename Alloc>
40 using MessageAlloc =
typename MessageAllocTraits::allocator_type;
64 : shared_ptr_callback_(nullptr), shared_ptr_with_info_callback_(nullptr),
65 const_shared_ptr_callback_(nullptr), const_shared_ptr_with_info_callback_(nullptr),
66 unique_ptr_callback_(nullptr), unique_ptr_with_info_callback_(nullptr)
68 message_allocator_ = std::make_shared<MessageAlloc>(*allocator.
get());
83 void set(CallbackT callback)
85 shared_ptr_callback_ = callback;
93 SharedPtrWithInfoCallback
97 void set(CallbackT callback)
99 shared_ptr_with_info_callback_ = callback;
107 ConstSharedPtrCallback
111 void set(CallbackT callback)
113 const_shared_ptr_callback_ = callback;
121 ConstSharedPtrWithInfoCallback
125 void set(CallbackT callback)
127 const_shared_ptr_with_info_callback_ = callback;
139 void set(CallbackT callback)
141 unique_ptr_callback_ = callback;
149 UniquePtrWithInfoCallback
153 void set(CallbackT callback)
155 unique_ptr_with_info_callback_ = callback;
161 TRACEPOINT(callback_start, (
const void *)
this,
false);
162 if (shared_ptr_callback_) {
163 shared_ptr_callback_(message);
164 }
else if (shared_ptr_with_info_callback_) {
165 shared_ptr_with_info_callback_(message, message_info);
166 }
else if (const_shared_ptr_callback_) {
167 const_shared_ptr_callback_(message);
168 }
else if (const_shared_ptr_with_info_callback_) {
169 const_shared_ptr_with_info_callback_(message, message_info);
170 }
else if (unique_ptr_callback_) {
171 auto ptr = MessageAllocTraits::allocate(*message_allocator_.
get(), 1);
172 MessageAllocTraits::construct(*message_allocator_.
get(), ptr, *message);
174 }
else if (unique_ptr_with_info_callback_) {
175 auto ptr = MessageAllocTraits::allocate(*message_allocator_.
get(), 1);
176 MessageAllocTraits::construct(*message_allocator_.
get(), ptr, *message);
177 unique_ptr_with_info_callback_(
MessageUniquePtr(ptr, message_deleter_), message_info);
181 TRACEPOINT(callback_end, (
const void *)
this);
187 TRACEPOINT(callback_start, (
const void *)
this,
true);
188 if (const_shared_ptr_callback_) {
189 const_shared_ptr_callback_(message);
190 }
else if (const_shared_ptr_with_info_callback_) {
191 const_shared_ptr_with_info_callback_(message, message_info);
194 unique_ptr_callback_ || unique_ptr_with_info_callback_ ||
195 shared_ptr_callback_ || shared_ptr_with_info_callback_)
198 "unexpected dispatch_intra_process const shared "
199 "message call with no const shared_ptr callback");
204 TRACEPOINT(callback_end, (
const void *)
this);
210 TRACEPOINT(callback_start, (
const void *)
this,
true);
211 if (shared_ptr_callback_) {
213 shared_ptr_callback_(shared_message);
214 }
else if (shared_ptr_with_info_callback_) {
216 shared_ptr_with_info_callback_(shared_message, message_info);
217 }
else if (unique_ptr_callback_) {
218 unique_ptr_callback_(
std::move(message));
219 }
else if (unique_ptr_with_info_callback_) {
220 unique_ptr_with_info_callback_(
std::move(message), message_info);
221 }
else if (const_shared_ptr_callback_ || const_shared_ptr_with_info_callback_) {
223 "unexpected dispatch_intra_process unique message call"
224 " with const shared_ptr callback");
228 TRACEPOINT(callback_end, (
const void *)
this);
233 return const_shared_ptr_callback_ || const_shared_ptr_with_info_callback_;
238 #ifndef TRACETOOLS_DISABLED
239 if (shared_ptr_callback_) {
241 rclcpp_callback_register,
243 get_symbol(shared_ptr_callback_));
244 }
else if (shared_ptr_with_info_callback_) {
246 rclcpp_callback_register,
248 get_symbol(shared_ptr_with_info_callback_));
249 }
else if (unique_ptr_callback_) {
251 rclcpp_callback_register,
253 get_symbol(unique_ptr_callback_));
254 }
else if (unique_ptr_with_info_callback_) {
256 rclcpp_callback_register,
258 get_symbol(unique_ptr_with_info_callback_));
260 #endif // TRACETOOLS_DISABLED
265 MessageDeleter message_deleter_;
270 #endif // RCLCPP__ANY_SUBSCRIPTION_CALLBACK_HPP_