From 7838a42a9f173e9a0552edf1f949733dc70939ff Mon Sep 17 00:00:00 2001 From: Dan Rose Date: Fri, 11 Oct 2019 21:56:25 -0500 Subject: [PATCH] Templatize so it works both before and after RMW changes Note that the `extern "C"` declarations in this file are quite unnecessary - due to the rmw.h header, the declared interface will have C linkage (though there may be additional overloads that use C++ linkage) --- rmw_cyclonedds_cpp/src/rmw_node.cpp | 60 +++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/rmw_cyclonedds_cpp/src/rmw_node.cpp b/rmw_cyclonedds_cpp/src/rmw_node.cpp index e5d6bb4f..c605c4f4 100755 --- a/rmw_cyclonedds_cpp/src/rmw_node.cpp +++ b/rmw_cyclonedds_cpp/src/rmw_node.cpp @@ -1085,14 +1085,27 @@ extern "C" rmw_ret_t rmw_fini_publisher_allocation(rmw_publisher_allocation_t * return RMW_RET_ERROR; } -extern "C" rmw_publisher_t * rmw_create_publisher( - const rmw_node_t * node, - const rosidl_message_type_support_t * type_supports, const char * topic_name, - const rmw_qos_profile_t * qos_policies, const rmw_publisher_options_t * publisher_options) +template +rmw_publisher_t * rmw_create_publisher( + const rmw_node_t * node, const rosidl_message_type_support_t * type_supports, + const char * topic_name, const rmw_qos_profile_t * qos_policies, + const T_rmw_publisher_options * publisher_options) +{ + RET_NULL_X(publisher_options, return nullptr); + T_rmw_publisher rmw_publisher = + rmw_create_publisher(node, type_supports, topic_name, qos_policies); + if (rmw_publisher != nullptr) { + rmw_publisher.options = publisher_options; + } + return rmw_publisher; +} + +rmw_publisher_t * rmw_create_publisher( + const rmw_node_t * node, const rosidl_message_type_support_t * type_supports, + const char * topic_name, const rmw_qos_profile_t * qos_policies) { CddsPublisher * pub; rmw_publisher_t * rmw_publisher; - RET_NULL_X(publisher_options, return nullptr); if ((pub = create_cdds_publisher(node, type_supports, topic_name, qos_policies)) == nullptr) { goto fail_common_init; } @@ -1103,7 +1116,6 @@ extern "C" rmw_publisher_t * rmw_create_publisher( rmw_publisher->topic_name = reinterpret_cast(rmw_allocate(strlen(topic_name) + 1)); RET_ALLOC_X(rmw_publisher->topic_name, goto fail_topic_name); memcpy(const_cast(rmw_publisher->topic_name), topic_name, strlen(topic_name) + 1); - rmw_publisher->options = *publisher_options; return rmw_publisher; fail_topic_name: rmw_publisher_free(rmw_publisher); @@ -1272,17 +1284,32 @@ extern "C" rmw_ret_t rmw_fini_subscription_allocation(rmw_subscription_allocatio return RMW_RET_ERROR; } -extern "C" rmw_subscription_t * rmw_create_subscription( - const rmw_node_t * node, - const rosidl_message_type_support_t * type_supports, const char * topic_name, - const rmw_qos_profile_t * qos_policies, const rmw_subscription_options_t * subscription_options) +template +rmw_subscription_t * rmw_create_subscription( + const rmw_node_t * node, const rosidl_message_type_support_t * type_supports, + const char * topic_name, const rmw_qos_profile_t * qos_policies, + const T_rmw_subscription_options * subscription_options) { + // since https://github.com/ros2/rmw/pull/187 + RET_NULL_X(subscription_options, return nullptr); + T_rmw_subscription * rmw_subscription = rmw_create_subscription( + node, type_supports, topic_name, qos_policies, subscription_options->ignore_local_publication); + if (rmw_subscription != nullptr) { + rmw_subscription->options = *subscription_options; + } + return rmw_subscription; +} + +rmw_subscription_t * rmw_create_subscription( + const rmw_node_t * node, const rosidl_message_type_support_t * type_supports, + const char * topic_name, const rmw_qos_profile_t * qos_policies, bool ignore_local_publications) +{ + // before https://github.com/ros2/rmw/pull/187 CddsSubscription * sub; rmw_subscription_t * rmw_subscription; - RET_NULL_X(subscription_options, return nullptr); - if ((sub = - create_cdds_subscription(node, type_supports, topic_name, qos_policies, - subscription_options->ignore_local_publications)) == nullptr) + if ( + (sub = create_cdds_subscription( + node, type_supports, topic_name, qos_policies, ignore_local_publications)) == nullptr) { goto fail_common_init; } @@ -1294,14 +1321,13 @@ extern "C" rmw_subscription_t * rmw_create_subscription( reinterpret_cast(rmw_allocate(strlen(topic_name) + 1)); RET_ALLOC_X(rmw_subscription->topic_name, goto fail_topic_name); memcpy(const_cast(rmw_subscription->topic_name), topic_name, strlen(topic_name) + 1); - rmw_subscription->options = *subscription_options; return rmw_subscription; fail_topic_name: rmw_subscription_free(rmw_subscription); fail_subscription: if (dds_delete(sub->rdcondh) < 0) { - RCUTILS_LOG_ERROR_NAMED("rmw_cyclonedds_cpp", - "failed to delete readcondition during error handling"); + RCUTILS_LOG_ERROR_NAMED( + "rmw_cyclonedds_cpp", "failed to delete readcondition during error handling"); } if (dds_delete(sub->subh) < 0) { RCUTILS_LOG_ERROR_NAMED("rmw_cyclonedds_cpp", "failed to delete reader during error handling");