15 #ifndef RCLCPP__ANY_SUBSCRIPTION_CALLBACK_HPP_ 16 #define RCLCPP__ANY_SUBSCRIPTION_CALLBACK_HPP_ 23 #include <type_traits> 33 namespace any_subscription_callback
36 template<
typename MessageT,
typename Alloc>
40 using MessageAlloc =
typename MessageAllocTraits::allocator_type;
42 using MessageUniquePtr = std::unique_ptr<MessageT, MessageDeleter>;
44 using SharedPtrCallback = std::function<void(const std::shared_ptr<MessageT>)>;
45 using SharedPtrWithInfoCallback =
47 using ConstSharedPtrCallback = std::function<void(const std::shared_ptr<const MessageT>)>;
48 using ConstSharedPtrWithInfoCallback =
49 std::function<void(const std::shared_ptr<const MessageT>,
const rmw_message_info_t &)>;
50 using UniquePtrCallback = std::function<void(MessageUniquePtr)>;
51 using UniquePtrWithInfoCallback =
52 std::function<void(MessageUniquePtr, const rmw_message_info_t &)>;
54 SharedPtrCallback shared_ptr_callback_;
55 SharedPtrWithInfoCallback shared_ptr_with_info_callback_;
56 ConstSharedPtrCallback const_shared_ptr_callback_;
57 ConstSharedPtrWithInfoCallback const_shared_ptr_with_info_callback_;
58 UniquePtrCallback unique_ptr_callback_;
59 UniquePtrWithInfoCallback unique_ptr_with_info_callback_;
63 : shared_ptr_callback_(nullptr), shared_ptr_with_info_callback_(nullptr),
64 const_shared_ptr_callback_(nullptr), const_shared_ptr_with_info_callback_(nullptr),
65 unique_ptr_callback_(nullptr), unique_ptr_with_info_callback_(nullptr)
67 message_allocator_ = std::make_shared<MessageAlloc>(*allocator.get());
75 typename std::enable_if<
82 void set(CallbackT callback)
84 shared_ptr_callback_ = callback;
89 typename std::enable_if<
92 SharedPtrWithInfoCallback
96 void set(CallbackT callback)
98 shared_ptr_with_info_callback_ = callback;
103 typename std::enable_if<
106 ConstSharedPtrCallback
110 void set(CallbackT callback)
112 const_shared_ptr_callback_ = callback;
117 typename std::enable_if<
120 ConstSharedPtrWithInfoCallback
124 void set(CallbackT callback)
126 const_shared_ptr_with_info_callback_ = callback;
131 typename std::enable_if<
138 void set(CallbackT callback)
140 unique_ptr_callback_ = callback;
145 typename std::enable_if<
148 UniquePtrWithInfoCallback
152 void set(CallbackT callback)
154 unique_ptr_with_info_callback_ = callback;
161 if (shared_ptr_callback_) {
162 shared_ptr_callback_(message);
163 }
else if (shared_ptr_with_info_callback_) {
164 shared_ptr_with_info_callback_(message, message_info);
165 }
else if (const_shared_ptr_callback_) {
166 const_shared_ptr_callback_(message);
167 }
else if (const_shared_ptr_with_info_callback_) {
168 const_shared_ptr_with_info_callback_(message, message_info);
169 }
else if (unique_ptr_callback_) {
170 auto ptr = MessageAllocTraits::allocate(*message_allocator_.get(), 1);
171 MessageAllocTraits::construct(*message_allocator_.get(), ptr, *message);
172 unique_ptr_callback_(MessageUniquePtr(ptr, message_deleter_));
173 }
else if (unique_ptr_with_info_callback_) {
174 auto ptr = MessageAllocTraits::allocate(*message_allocator_.get(), 1);
175 MessageAllocTraits::construct(*message_allocator_.get(), ptr, *message);
176 unique_ptr_with_info_callback_(MessageUniquePtr(ptr, message_deleter_), message_info);
178 throw std::runtime_error(
"unexpected message without any callback set");
186 if (shared_ptr_callback_) {
187 typename std::shared_ptr<MessageT> shared_message = std::move(message);
188 shared_ptr_callback_(shared_message);
189 }
else if (shared_ptr_with_info_callback_) {
190 typename std::shared_ptr<MessageT> shared_message = std::move(message);
191 shared_ptr_with_info_callback_(shared_message, message_info);
192 }
else if (const_shared_ptr_callback_) {
193 typename std::shared_ptr<MessageT const> const_shared_message = std::move(message);
194 const_shared_ptr_callback_(const_shared_message);
195 }
else if (const_shared_ptr_with_info_callback_) {
196 typename std::shared_ptr<MessageT const> const_shared_message = std::move(message);
197 const_shared_ptr_with_info_callback_(const_shared_message, message_info);
198 }
else if (unique_ptr_callback_) {
199 unique_ptr_callback_(std::move(message));
200 }
else if (unique_ptr_with_info_callback_) {
201 unique_ptr_with_info_callback_(std::move(message), message_info);
203 throw std::runtime_error(
"unexpected message without any callback set");
208 std::shared_ptr<MessageAlloc> message_allocator_;
209 MessageDeleter message_deleter_;
215 #endif // RCLCPP__ANY_SUBSCRIPTION_CALLBACK_HPP_
Definition: any_subscription_callback.hpp:37
Definition: allocator_common.hpp:24
void set_allocator_for_deleter(D *deleter, Alloc *alloc)
Definition: allocator_deleter.hpp:72
typename std::allocator_traits< Alloc >::template rebind_traits< T > AllocRebind
Definition: allocator_common.hpp:30
AnySubscriptionCallback(std::shared_ptr< Alloc > allocator)
Definition: any_subscription_callback.hpp:62
Definition: function_traits.hpp:141
void dispatch_intra_process(MessageUniquePtr &message, const rmw_message_info_t &message_info)
Definition: any_subscription_callback.hpp:182
void dispatch(std::shared_ptr< MessageT > message, const rmw_message_info_t &message_info)
Definition: any_subscription_callback.hpp:157
typename std::conditional< std::is_same< typename std::allocator_traits< Alloc >::template rebind_alloc< T >, typename std::allocator< void >::template rebind< T >::other >::value, std::default_delete< T >, AllocatorDeleter< Alloc > >::type Deleter
Definition: allocator_deleter.hpp:101