diff --git a/README.md b/README.md index 2de0c42..408a957 100644 --- a/README.md +++ b/README.md @@ -12,20 +12,20 @@ This project provides the ability to publish and subscribe with Protobuf Datatyp ``` #### Publisher Example ```cpp -publisher_ = this->create_publisher("topic", 10); +publisher_ = this->create_publisher("topic", 10); ``` -To publish a message, it is only required to specify the adapter type to send the topic. +To publish a message, it is only required to specify the protobuf message type to send the topic. #### Subscriber Example ```cpp -subscription2_ = this->create_subscription( +subscription2_ = this->create_subscription( "topic", 10, std::bind(&MinimalSubscriber::topic_callback2, this, _1)); void topic_callback2(const std_msgs::msg::pb::String & msg) const { RCLCPP_INFO(this->get_logger(), "I heard Proto: '%s'", msg.data().c_str()); } ``` -To subscribe to the topic the user needs to specify the adapter type, and for the callback specify the protobuf message to hear the message received. +To subscribe to the topic the user needs to specify the protobuf message type, and for the callback specify the protobuf message to hear the message received. Another path to hear the message is using the ROS types messages: @@ -60,6 +60,9 @@ To adapt the prototype message to a ROS type, protobuf type support generates a * static void convert_to_ros_message(const custom_type &, ros_message_type &), * static void convert_to_custom(const ros_message_type &, custom_type &) +The adapted type is also registered as the default type adapter for the ros type: + +* RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE( /* proto_type */, /* ros_type */ )); The convert function must convert from one type to the other, see [Type Adaptation Feature](https://ros.org/reps/rep-2007.html) for more information. @@ -94,6 +97,8 @@ template<> } }; + +RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE(::std_msgs::msg::pb::String, ::std_msgs::msg::String)); ``` #### Mapping from IDL -> Protobuf Type: diff --git a/rosidl_typeadapter_protobuf_test/test/subscribe_array_types.cpp b/rosidl_typeadapter_protobuf_test/test/subscribe_array_types.cpp index a600d4b..0ddaa88 100644 --- a/rosidl_typeadapter_protobuf_test/test/subscribe_array_types.cpp +++ b/rosidl_typeadapter_protobuf_test/test/subscribe_array_types.cpp @@ -37,7 +37,7 @@ rclcpp::SubscriptionBase::SharedPtr subscribe_arrays( const std::vector> & expected_messages, std::vector & received_messages) { - return subscribe( + return subscribe( node, message_type, expected_messages, received_messages); } @@ -47,7 +47,7 @@ rclcpp::SubscriptionBase::SharedPtr subscribe_unbounded_sequences( const std::vector> & expected_messages, std::vector & received_messages) { - return subscribe( + return subscribe( node, message_type, expected_messages, received_messages); } @@ -57,7 +57,7 @@ rclcpp::SubscriptionBase::SharedPtr subscribe_bounded_plain_sequences( const std::vector> & expected_messages, std::vector & received_messages) { - return subscribe( + return subscribe( node, message_type, expected_messages, received_messages); } @@ -67,7 +67,7 @@ rclcpp::SubscriptionBase::SharedPtr subscribe_bounded_sequences( const std::vector> & expected_messages, std::vector & received_messages) { - return subscribe( + return subscribe( node, message_type, expected_messages, received_messages); } @@ -77,7 +77,7 @@ rclcpp::SubscriptionBase::SharedPtr subscribe_multi_nested( const std::vector> & expected_messages, std::vector & received_messages) { - return subscribe( + return subscribe( node, message_type, expected_messages, received_messages); } @@ -87,6 +87,6 @@ rclcpp::SubscriptionBase::SharedPtr subscribe_nested( const std::vector> & expected_messages, std::vector & received_messages) { - return subscribe( + return subscribe( node, message_type, expected_messages, received_messages); } diff --git a/rosidl_typeadapter_protobuf_test/test/subscribe_basic_types.cpp b/rosidl_typeadapter_protobuf_test/test/subscribe_basic_types.cpp index cb11280..423c82f 100644 --- a/rosidl_typeadapter_protobuf_test/test/subscribe_basic_types.cpp +++ b/rosidl_typeadapter_protobuf_test/test/subscribe_basic_types.cpp @@ -35,7 +35,7 @@ rclcpp::SubscriptionBase::SharedPtr subscribe_empty( const std::vector> & messages_expected, std::vector & received_messages) { - return subscribe( + return subscribe( node, message_type, messages_expected, received_messages); } @@ -45,7 +45,7 @@ rclcpp::SubscriptionBase::SharedPtr subscribe_basic_types( const std::vector> & messages_expected, std::vector & received_messages) { - return subscribe( + return subscribe( node, message_type, messages_expected, received_messages); } @@ -55,7 +55,7 @@ rclcpp::SubscriptionBase::SharedPtr subscribe_builtins( const std::vector> & messages_expected, std::vector & received_messages) { - return subscribe( + return subscribe( node, message_type, messages_expected, received_messages); } @@ -65,7 +65,7 @@ rclcpp::SubscriptionBase::SharedPtr subscribe_constants( const std::vector> & messages_expected, std::vector & received_messages) { - return subscribe( + return subscribe( node, message_type, messages_expected, received_messages); } @@ -75,6 +75,6 @@ rclcpp::SubscriptionBase::SharedPtr subscribe_defaults( const std::vector> & messages_expected, std::vector & received_messages) { - return subscribe( + return subscribe( node, message_type, messages_expected, received_messages); } diff --git a/rosidl_typeadapter_protobuf_test/test/subscribe_string_types.cpp b/rosidl_typeadapter_protobuf_test/test/subscribe_string_types.cpp index 3af82b9..abe842c 100644 --- a/rosidl_typeadapter_protobuf_test/test/subscribe_string_types.cpp +++ b/rosidl_typeadapter_protobuf_test/test/subscribe_string_types.cpp @@ -33,7 +33,7 @@ rclcpp::SubscriptionBase::SharedPtr subscribe_strings( const std::vector> & expected_messages, std::vector & received_messages) { - return subscribe( + return subscribe( node, message_type, expected_messages, received_messages); } @@ -43,6 +43,6 @@ rclcpp::SubscriptionBase::SharedPtr subscribe_wstrings( const std::vector> & expected_messages, std::vector & received_messages) { - return subscribe( + return subscribe( node, message_type, expected_messages, received_messages); } diff --git a/rosidl_typeadapter_protobuf_test/test/test_proto_typeadapt.cpp b/rosidl_typeadapter_protobuf_test/test/test_proto_typeadapt.cpp index ddb3c7c..d7387b5 100644 --- a/rosidl_typeadapter_protobuf_test/test/test_proto_typeadapt.cpp +++ b/rosidl_typeadapter_protobuf_test/test/test_proto_typeadapt.cpp @@ -108,58 +108,58 @@ int main(int argc, char ** argv) if (message == "Empty") { subscriber = subscribe_empty(node, message, messages_empty, received_messages); - publish( + publish( node, message, messages_empty); } else if (message == "BasicTypes") { subscriber = subscribe_basic_types(node, message, messages_basic_types, received_messages); - publish( + publish( node, message, messages_basic_types); } else if (message == "Arrays") { subscriber = subscribe_arrays(node, message, messages_arrays, received_messages); - publish( + publish( node, message, messages_arrays); } else if (message == "UnboundedSequences") { subscriber = subscribe_unbounded_sequences( node, message, messages_unbounded_sequences, received_messages); - publish( + publish( node, message, messages_unbounded_sequences); } else if (message == "BoundedPlainSequences") { subscriber = subscribe_bounded_plain_sequences( node, message, messages_bounded_plain_sequences, received_messages); - publish( + publish( node, message, messages_bounded_plain_sequences); } else if (message == "BoundedSequences") { subscriber = subscribe_bounded_sequences( node, message, messages_bounded_sequences, received_messages); - publish( + publish( node, message, messages_bounded_sequences); } else if (message == "MultiNested") { subscriber = subscribe_multi_nested(node, message, messages_multi_nested, received_messages); - publish( + publish( node, message, messages_multi_nested); } else if (message == "Nested") { subscriber = subscribe_nested(node, message, messages_nested, received_messages); - publish( + publish( node, message, messages_nested); } else if (message == "Builtins") { subscriber = subscribe_builtins(node, message, messages_builtins, received_messages); - publish( + publish( node, message, messages_builtins); } else if (message == "Constants") { subscriber = subscribe_constants(node, message, messages_constants, received_messages); - publish( + publish( node, message, messages_constants); } else if (message == "Defaults") { subscriber = subscribe_defaults(node, message, messages_defaults, received_messages); - publish( + publish( node, message, messages_defaults); } else if (message == "Strings") { subscriber = subscribe_strings(node, message, messages_strings, received_messages); - publish( + publish( node, message, messages_strings); } else if (message == "WStrings") { subscriber = subscribe_wstrings(node, message, messages_wstrings, received_messages); - publish( + publish( node, message, messages_wstrings); } else { fprintf(stderr, "Unknown message argument '%s'\n", message.c_str()); diff --git a/rosidl_typesupport_protobuf_cpp/resource/idl__typeadapter_protobuf_cpp.hpp.em b/rosidl_typesupport_protobuf_cpp/resource/idl__typeadapter_protobuf_cpp.hpp.em index 7978247..6140761 100644 --- a/rosidl_typesupport_protobuf_cpp/resource/idl__typeadapter_protobuf_cpp.hpp.em +++ b/rosidl_typesupport_protobuf_cpp/resource/idl__typeadapter_protobuf_cpp.hpp.em @@ -52,8 +52,6 @@ TEMPLATE( ) }@ - - @[for message in content.get_elements_of_type(Message)]@ @{ @@ -63,49 +61,32 @@ ros_type = ros_type(package_name, interface_path, message) proto_type = protobuf_type(package_name, interface_path, message) }@ -namespace rclcpp +template<> +struct rclcpp::TypeAdapter<@(proto_type), @(ros_type)> { - - template<> - struct TypeAdapter<@(proto_type), @(ros_type)> + using is_specialized = std::true_type; + using custom_type = @(proto_type); + using ros_message_type = @(ros_type); + + static + void + convert_to_ros_message( + const custom_type & source, + ros_message_type & destination) { - using is_specialized = std::true_type; - using custom_type = @(proto_type); - using ros_message_type = @(ros_type); - - static - void - convert_to_ros_message( - const custom_type & source, - ros_message_type & destination) - { - @("::".join(message.structure.namespaced_type.namespaces))::typesupport_protobuf_cpp::convert_to_ros(source, destination); - } - - static - void - convert_to_custom( - const ros_message_type & source, - custom_type & destination) - { - @("::".join(message.structure.namespaced_type.namespaces))::typesupport_protobuf_cpp::convert_to_proto(source, destination); - } - }; - -} - -@[for ns in message.structure.namespaced_type.namespaces]@ -namespace @(ns) -{ -@[end for]@ -namespace typesupport_protobuf_cpp -{ - -using @(message.structure.namespaced_type.name)TypeAdapter = rclcpp::TypeAdapter<@(proto_type), @(ros_type)>; + @("::".join(message.structure.namespaced_type.namespaces))::typesupport_protobuf_cpp::convert_to_ros(source, destination); + } + + static + void + convert_to_custom( + const ros_message_type & source, + custom_type & destination) + { + @("::".join(message.structure.namespaced_type.namespaces))::typesupport_protobuf_cpp::convert_to_proto(source, destination); + } +}; -} // namespace typesupport_protobuf_cpp -@[ for ns in reversed(message.structure.namespaced_type.namespaces)]@ -} // namespace @(ns) -@[ end for]@ +RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE(@(proto_type), @(ros_type)); @[end for]@