|
| 1 | +// Copyright 2022 Open Source Robotics Foundation, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef RCLCPP__RUNTIME_TYPE_SUBSCRIPTION_HPP_ |
| 16 | +#define RCLCPP__RUNTIME_TYPE_SUBSCRIPTION_HPP_ |
| 17 | + |
| 18 | +#include <functional> |
| 19 | +#include <memory> |
| 20 | +#include <string> |
| 21 | + |
| 22 | +#include "rcpputils/shared_library.hpp" |
| 23 | + |
| 24 | +#include "rclcpp/callback_group.hpp" |
| 25 | +#include "rclcpp/macros.hpp" |
| 26 | +#include "rclcpp/node_interfaces/node_base_interface.hpp" |
| 27 | +#include "rclcpp/node_interfaces/node_topics_interface.hpp" |
| 28 | +#include "rclcpp/qos.hpp" |
| 29 | +#include "rclcpp/serialized_message.hpp" |
| 30 | +#include "rclcpp/subscription_base.hpp" |
| 31 | +#include "rclcpp/typesupport_helpers.hpp" |
| 32 | +#include "rclcpp/visibility_control.hpp" |
| 33 | + |
| 34 | +namespace rclcpp |
| 35 | +{ |
| 36 | + |
| 37 | +/// %Subscription for messages whose type descriptions are obtained at runtime. |
| 38 | +/** |
| 39 | + * Since the type is not known at compile time, this is not a template, and the dynamic library |
| 40 | + * containing type support information has to be identified and loaded based on the type name. |
| 41 | + * |
| 42 | + * NOTE(methylDragon): No considerations for intra-process handling are made. |
| 43 | + */ |
| 44 | +class RuntimeTypeSubscription : public rclcpp::SubscriptionBase |
| 45 | +{ |
| 46 | +public: |
| 47 | + // cppcheck-suppress unknownMacro |
| 48 | + RCLCPP_SMART_PTR_DEFINITIONS(RuntimeTypeSubscription) |
| 49 | + |
| 50 | + template<typename AllocatorT = std::allocator<void>> |
| 51 | + RuntimeTypeSubscription( |
| 52 | + rclcpp::node_interfaces::NodeBaseInterface * node_base, |
| 53 | + rosidl_message_type_support_t & type_support_handle, |
| 54 | + const std::string & topic_name, |
| 55 | + const rclcpp::QoS & qos, |
| 56 | + // TODO(methylDragons): Eventually roll out an rclcpp::DynamicData that encompasses the ser |
| 57 | + // support and DynamicData, and pass that to the callback |
| 58 | + std::function<void( |
| 59 | + std::shared_ptr<serialization_support_t>, std::shared_ptr<ser_dynamic_data_t> |
| 60 | + )> callback, |
| 61 | + const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & options, |
| 62 | + bool use_take_runtime_type_message = false) |
| 63 | + : SubscriptionBase( |
| 64 | + node_base, |
| 65 | + type_support_handle, |
| 66 | + topic_name, |
| 67 | + options.to_rcl_subscription_options(qos), |
| 68 | + options.event_callbacks, |
| 69 | + options.use_default_callbacks, |
| 70 | + false, |
| 71 | + true, |
| 72 | + use_take_runtime_type_message), |
| 73 | + ts_(type_support_handle), |
| 74 | + callback_(callback) |
| 75 | + { |
| 76 | + if(type_support_handle.typesupport_identifier |
| 77 | + != rmw_typesupport_runtime_type_introspection_c__identifier) |
| 78 | + { |
| 79 | + throw std::runtime_error( |
| 80 | + "RuntimeTypeSubscription must use runtime type introspection type support!"); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // TODO(methylDragon): |
| 85 | + /// Deferred type description constructor, only usable if the middleware implementation supports |
| 86 | + /// type discovery |
| 87 | + // template<typename AllocatorT = std::allocator<void>> |
| 88 | + // RuntimeTypeSubscription( |
| 89 | + // rclcpp::node_interfaces::NodeBaseInterface * node_base, |
| 90 | + // const std::string & topic_name, |
| 91 | + // const rclcpp::QoS & qos, |
| 92 | + // // TODO(methylDragons): Eventually roll out an rclcpp::DynamicData that encompasses the ser |
| 93 | + // // support and DynamicData, and pass that to the callback |
| 94 | + // std::function<void( |
| 95 | + // std::shared_ptr<serialization_support_t>, std::shared_ptr<ser_dynamic_data_t> |
| 96 | + // )> callback, |
| 97 | + // const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & options, |
| 98 | + // const char * serialization_lib_name = nullptr) |
| 99 | + // : SubscriptionBase( |
| 100 | + // node_base, |
| 101 | + // // NOTE(methylDragon): Since the typesupport is deferred, it needs to be modified post-hoc |
| 102 | + // // which means it technically isn't const correct... |
| 103 | + // *rmw_get_runtime_type_message_typesupport_handle(serialization_lib_name), |
| 104 | + // topic_name, |
| 105 | + // options.to_rcl_subscription_options(qos), |
| 106 | + // options.event_callbacks, |
| 107 | + // options.use_default_callbacks, |
| 108 | + // false, |
| 109 | + // true), |
| 110 | + // callback_(callback) |
| 111 | + // {} |
| 112 | + |
| 113 | + RCLCPP_PUBLIC |
| 114 | + virtual ~RuntimeTypeSubscription() = default; |
| 115 | + |
| 116 | + // Same as create_serialized_message() as the subscription is to serialized_messages only |
| 117 | + RCLCPP_PUBLIC |
| 118 | + std::shared_ptr<void> create_message() override; |
| 119 | + |
| 120 | + RCLCPP_PUBLIC |
| 121 | + std::shared_ptr<rclcpp::SerializedMessage> create_serialized_message() override; |
| 122 | + |
| 123 | + /// Cast the message to a rclcpp::SerializedMessage and call the callback. |
| 124 | + RCLCPP_PUBLIC |
| 125 | + void handle_message( |
| 126 | + std::shared_ptr<void> & message, const rclcpp::MessageInfo & message_info) override; |
| 127 | + |
| 128 | + /// Handle dispatching rclcpp::SerializedMessage to user callback. |
| 129 | + RCLCPP_PUBLIC |
| 130 | + void |
| 131 | + handle_serialized_message( |
| 132 | + const std::shared_ptr<rclcpp::SerializedMessage> & serialized_message, |
| 133 | + const rclcpp::MessageInfo & message_info) override; |
| 134 | + |
| 135 | + /// This function is currently not implemented. |
| 136 | + RCLCPP_PUBLIC |
| 137 | + void handle_loaned_message( |
| 138 | + void * loaned_message, const rclcpp::MessageInfo & message_info) override; |
| 139 | + |
| 140 | + // Same as return_serialized_message() as the subscription is to serialized_messages only |
| 141 | + RCLCPP_PUBLIC |
| 142 | + void return_message(std::shared_ptr<void> & message) override; |
| 143 | + |
| 144 | + RCLCPP_PUBLIC |
| 145 | + void return_serialized_message(std::shared_ptr<rclcpp::SerializedMessage> & message) override; |
| 146 | + |
| 147 | + |
| 148 | + // RUNTIME TYPE ================================================================================== |
| 149 | + // TODO(methylDragon): Reorder later |
| 150 | + RCLCPP_PUBLIC |
| 151 | + std::shared_ptr<ser_dynamic_type_t> |
| 152 | + get_dynamic_type() override; |
| 153 | + |
| 154 | + RCLCPP_PUBLIC |
| 155 | + std::shared_ptr<ser_dynamic_data_t> |
| 156 | + get_dynamic_data() override; |
| 157 | + |
| 158 | + RCLCPP_PUBLIC |
| 159 | + std::shared_ptr<serialization_support_t> |
| 160 | + get_serialization_support() override; |
| 161 | + |
| 162 | + RCLCPP_PUBLIC |
| 163 | + void handle_runtime_type_message( |
| 164 | + const std::shared_ptr<serialization_support_t> & ser, |
| 165 | + const std::shared_ptr<ser_dynamic_data_t> & dyn_data, |
| 166 | + const rclcpp::MessageInfo & message_info |
| 167 | + ) override; |
| 168 | + |
| 169 | + |
| 170 | +private: |
| 171 | + RCLCPP_DISABLE_COPY(RuntimeTypeSubscription) |
| 172 | + |
| 173 | + rosidl_message_type_support_t & ts_; |
| 174 | + |
| 175 | + std::function<void( |
| 176 | + std::shared_ptr<serialization_support_t>, std::shared_ptr<ser_dynamic_data_t> |
| 177 | + )> callback_; |
| 178 | +}; |
| 179 | + |
| 180 | +} // namespace rclcpp |
| 181 | + |
| 182 | +#endif // RCLCPP__RUNTIME_TYPE_SUBSCRIPTION_HPP_ |
0 commit comments