15 #ifndef RCLCPP__EXPERIMENTAL__SUBSCRIPTION_INTRA_PROCESS_BUFFER_HPP_
16 #define RCLCPP__EXPERIMENTAL__SUBSCRIPTION_INTRA_PROCESS_BUFFER_HPP_
23 #include "rcl/error_handling.h"
27 #include "rclcpp/experimental/buffers/intra_process_buffer.hpp"
28 #include "rclcpp/experimental/create_intra_process_buffer.hpp"
29 #include "rclcpp/experimental/subscription_intra_process_base.hpp"
30 #include "rclcpp/experimental/ros_message_intra_process_buffer.hpp"
31 #include "rclcpp/qos.hpp"
32 #include "rclcpp/type_support_decl.hpp"
36 namespace experimental
40 typename SubscribedType,
41 typename Alloc = std::allocator<SubscribedType>,
42 typename Deleter = std::default_delete<SubscribedType>,
45 typename ROSMessageType = SubscribedType
48 typename allocator::AllocRebind<ROSMessageType, Alloc>::allocator_type,
49 allocator::Deleter<typename allocator::AllocRebind<ROSMessageType, Alloc>::allocator_type,
55 using SubscribedTypeAllocatorTraits = allocator::AllocRebind<SubscribedType, Alloc>;
56 using SubscribedTypeAllocator =
typename SubscribedTypeAllocatorTraits::allocator_type;
57 using SubscribedTypeDeleter = allocator::Deleter<SubscribedTypeAllocator, SubscribedType>;
59 using ROSMessageTypeAllocatorTraits = allocator::AllocRebind<ROSMessageType, Alloc>;
60 using ROSMessageTypeAllocator =
typename ROSMessageTypeAllocatorTraits::allocator_type;
61 using ROSMessageTypeDeleter = allocator::Deleter<ROSMessageTypeAllocator, ROSMessageType>;
63 using ConstMessageSharedPtr = std::shared_ptr<const ROSMessageType>;
64 using MessageUniquePtr = std::unique_ptr<ROSMessageType, ROSMessageTypeDeleter>;
66 using ConstDataSharedPtr = std::shared_ptr<const SubscribedType>;
67 using SubscribedTypeUniquePtr = std::unique_ptr<SubscribedType, SubscribedTypeDeleter>;
76 std::shared_ptr<Alloc> allocator,
77 rclcpp::Context::SharedPtr context,
78 const std::string & topic_name,
82 ROSMessageTypeDeleter>(
83 context, topic_name, qos_profile),
84 subscribed_type_allocator_(*allocator)
86 allocator::set_allocator_for_deleter(&subscribed_type_deleter_, &subscribed_type_allocator_);
89 buffer_ = rclcpp::experimental::create_intra_process_buffer<SubscribedType, Alloc,
90 SubscribedTypeDeleter>(
93 std::make_shared<Alloc>(subscribed_type_allocator_));
100 return buffer_->has_data();
103 SubscribedTypeUniquePtr
104 convert_ros_message_to_subscribed_type_unique_ptr(
const ROSMessageType & msg)
106 if constexpr (!std::is_same<SubscribedType, ROSMessageType>::value) {
107 auto ptr = SubscribedTypeAllocatorTraits::allocate(subscribed_type_allocator_, 1);
108 SubscribedTypeAllocatorTraits::construct(subscribed_type_allocator_, ptr);
110 return SubscribedTypeUniquePtr(ptr, subscribed_type_deleter_);
112 throw std::runtime_error(
113 "convert_ros_message_to_subscribed_type_unique_ptr "
114 "unexpectedly called without TypeAdapter");
119 provide_intra_process_message(ConstMessageSharedPtr message)
override
121 if constexpr (std::is_same<SubscribedType, ROSMessageType>::value) {
122 buffer_->add_shared(std::move(message));
123 trigger_guard_condition();
125 buffer_->add_shared(convert_ros_message_to_subscribed_type_unique_ptr(*message));
126 trigger_guard_condition();
128 this->invoke_on_new_message();
132 provide_intra_process_message(MessageUniquePtr message)
override
134 if constexpr (std::is_same<SubscribedType, ROSMessageType>::value) {
135 buffer_->add_unique(std::move(message));
136 trigger_guard_condition();
138 buffer_->add_unique(convert_ros_message_to_subscribed_type_unique_ptr(*message));
139 trigger_guard_condition();
141 this->invoke_on_new_message();
145 provide_intra_process_data(ConstDataSharedPtr message)
147 buffer_->add_shared(std::move(message));
148 trigger_guard_condition();
149 this->invoke_on_new_message();
153 provide_intra_process_data(SubscribedTypeUniquePtr message)
155 buffer_->add_unique(std::move(message));
156 trigger_guard_condition();
157 this->invoke_on_new_message();
161 use_take_shared_method()
const override
163 return buffer_->use_take_shared_method();
168 trigger_guard_condition()
override
173 BufferUniquePtr buffer_;
174 SubscribedTypeAllocator subscribed_type_allocator_;
175 SubscribedTypeDeleter subscribed_type_deleter_;
RCLCPP_PUBLIC void trigger()
Notify the wait set waiting on this condition, if any, that the condition had been met.
Encapsulation of Quality of Service settings.
bool is_ready(rcl_wait_set_t *wait_set) override
Check if the Waitable is ready.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Container for subscription's, guard condition's, etc to be waited on.
Template structure used to adapt custom, user-defined types to ROS types.