From 8bf189c40679749d54979a7275566e958f1cafb0 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Fri, 27 Nov 2026 15:37:11 -0800 Subject: [PATCH] Fix non-const shared_ptr callback deprecation warnings with GCC Subscription callbacks taking a `std::shared_ptr` to a non-const message were deprecated in https://github.com/ros2/rclcpp/pull/2975 (Lyrical), but the `[[deprecated]]` function, `AnySubscriptionCallback::set_deprecated()`, is only used inside rclcpp headers. Downstream packages include these as system headers, and GCC does not show warnings located in system headers, even if user code triggered the template instantiation, so only Clang >= 18 shows the warning. Instead, add `[[deprecated]]` overloads of `create_subscription()` and `create_generic_subscription()` (free functions and `Node` and `LifecycleNode` member functions), constrained to the deprecated callback signatures, so that the warning is located at the user's call. Move the `create_generic_subscription()` implementation to `rclcpp::detail` so that both overloads can use it, like `create_subscription()`. Then remove `set_deprecated()` and the `RCLCPP_AVOID_DEPRECATIONS_FOR_UNIT_TESTS` macro. Since GCC users will only see the warning starting with Makoa, add TODOs to remove these overloads and the support for these callback signatures after Makoa. Also update the `rclcpp_action` tests that used a deprecated callback. Part of https://github.com/ros2/rclcpp/issues/3176 Signed-off-by: Christophe Bedard --- .../rclcpp/any_subscription_callback.hpp | 72 ++------------- .../rclcpp/create_generic_subscription.hpp | 88 ++++++++++++++----- rclcpp/include/rclcpp/create_subscription.hpp | 74 ++++++++++++++++ .../subscription_callback_type_helper.hpp | 67 ++++++++++++++ rclcpp/include/rclcpp/node.hpp | 55 ++++++++++++ rclcpp/include/rclcpp/node_impl.hpp | 46 ++++++++++ .../rclcpp/test_any_subscription_callback.cpp | 61 ++++++++++++- .../test/rclcpp/test_create_subscription.cpp | 51 +++++++++++ rclcpp_action/test/test_client.cpp | 2 +- rclcpp_action/test/test_generic_client.cpp | 2 +- .../rclcpp_lifecycle/lifecycle_node.hpp | 54 ++++++++++++ .../rclcpp_lifecycle/lifecycle_node_impl.hpp | 48 ++++++++++ rclcpp_lifecycle/test/test_lifecycle_node.cpp | 29 ++++++ 13 files changed, 559 insertions(+), 90 deletions(-) diff --git a/rclcpp/include/rclcpp/any_subscription_callback.hpp b/rclcpp/include/rclcpp/any_subscription_callback.hpp index d66d7b7cab..dc1910584d 100644 --- a/rclcpp/include/rclcpp/any_subscription_callback.hpp +++ b/rclcpp/include/rclcpp/any_subscription_callback.hpp @@ -138,6 +138,7 @@ struct AnySubscriptionCallbackPossibleTypes const std::shared_ptr &, const rclcpp::MessageInfo &)>; + // TODO(christophebedard): remove after Makoa // Deprecated signatures: using SharedPtrCallback = std::function)>; @@ -378,9 +379,9 @@ class AnySubscriptionCallback /// Generic function for setting the callback. /** - * There are specializations that overload this in order to deprecate some - * callback signatures, and also to fix ambiguity between shared_ptr and - * unique_ptr callback signatures when using them with lambda functions. + * There are specializations that overload this in order to fix ambiguity + * between shared_ptr and unique_ptr callback signatures when using them with + * lambda functions. */ template AnySubscriptionCallback @@ -392,77 +393,14 @@ class AnySubscriptionCallback // converted to one another, e.g. shared_ptr and unique_ptr. using scbth = detail::SubscriptionCallbackTypeHelper; - // Determine if the given CallbackT is a deprecated signature or not. - constexpr auto is_deprecated = - rclcpp::function_traits::same_arguments< - typename scbth::callback_type, - std::function)> - >::value || - rclcpp::function_traits::same_arguments< - typename scbth::callback_type, - std::function, const rclcpp::MessageInfo &)> - >::value || - rclcpp::function_traits::same_arguments< - typename scbth::callback_type, - std::function)> - >::value || - rclcpp::function_traits::same_arguments< - typename scbth::callback_type, - std::function, const rclcpp::MessageInfo &)> - >::value || - rclcpp::function_traits::same_arguments< - typename scbth::callback_type, - std::function)> - >::value || - rclcpp::function_traits::same_arguments< - typename scbth::callback_type, - std::function, const rclcpp::MessageInfo &)> - >::value; - // Use the discovered type to force the type of callback when assigning // into the variant. - if constexpr (is_deprecated) { - // If deprecated, call sub-routine that is deprecated. - set_deprecated(static_cast(callback)); - } else { - // Otherwise just assign it. - callback_variant_ = static_cast(callback); - } + callback_variant_ = static_cast(callback); // Return copy of self for easier testing, normally will be compiled out. return *this; } - /// Function for shared_ptr to non-const MessageT, which is deprecated. - template - // *INDENT-OFF* - #if !defined(RCLCPP_AVOID_DEPRECATIONS_FOR_UNIT_TESTS) - // suppress deprecation warnings in `test_any_subscription_callback.cpp` - [[deprecated("use 'void(std::shared_ptr)' instead")]] - #endif - // *INDENT-ON* - void - set_deprecated(std::function)> callback) - { - callback_variant_ = callback; - } - - /// Function for shared_ptr to non-const MessageT with MessageInfo, which is deprecated. - template - // *INDENT-OFF* - #if !defined(RCLCPP_AVOID_DEPRECATIONS_FOR_UNIT_TESTS) - // suppress deprecation warnings in `test_any_subscription_callback.cpp` - [[deprecated( - "use 'void(std::shared_ptr, const rclcpp::MessageInfo &)' instead" - )]] - #endif - // *INDENT-ON* - void - set_deprecated(std::function, const rclcpp::MessageInfo &)> callback) - { - callback_variant_ = callback; - } - /// Disable the callback from being called during dispatch. void disable() { diff --git a/rclcpp/include/rclcpp/create_generic_subscription.hpp b/rclcpp/include/rclcpp/create_generic_subscription.hpp index 381da45994..90483c4be1 100644 --- a/rclcpp/include/rclcpp/create_generic_subscription.hpp +++ b/rclcpp/include/rclcpp/create_generic_subscription.hpp @@ -22,6 +22,7 @@ #include "rcl/subscription.h" #include "rclcpp/any_subscription_callback.hpp" +#include "rclcpp/detail/subscription_callback_type_helper.hpp" #include "rclcpp/generic_subscription.hpp" #include "rclcpp/node_interfaces/node_topics_interface.hpp" #include "rclcpp/qos.hpp" @@ -32,33 +33,18 @@ namespace rclcpp { -/// Create and return a GenericSubscription. -/** - * The returned pointer will never be empty, but this function can throw various exceptions, for - * instance when the message's package can not be found on the AMENT_PREFIX_PATH. - * - * \param topics_interface NodeTopicsInterface pointer used in parts of the setup. - * \param topic_name Topic name - * \param topic_type Topic type - * \param qos %QoS settings - * \param callback Callback for new messages of serialized form - * \param options %Publisher options. - * Not all publisher options are currently respected, the only relevant options for this - * publisher are `event_callbacks`, `use_default_callbacks`, and `%callback_group`. - */ +namespace detail +{ template< typename CallbackT, - typename AllocatorT = std::allocator> + typename AllocatorT> std::shared_ptr create_generic_subscription( - rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface, + const rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr & topics_interface, const std::string & topic_name, const std::string & topic_type, const rclcpp::QoS & qos, CallbackT && callback, - const rclcpp::SubscriptionOptionsWithAllocator & options = ( - rclcpp::SubscriptionOptionsWithAllocator() - ) -) + const rclcpp::SubscriptionOptionsWithAllocator & options) { auto ts_lib = rclcpp::get_typesupport_library( topic_type, "rosidl_typesupport_cpp"); @@ -83,6 +69,68 @@ std::shared_ptr create_generic_subscription( return subscription; } +} // namespace detail + +/// Create and return a GenericSubscription. +/** + * The returned pointer will never be empty, but this function can throw various exceptions, for + * instance when the message's package can not be found on the AMENT_PREFIX_PATH. + * + * \param topics_interface NodeTopicsInterface pointer used in parts of the setup. + * \param topic_name Topic name + * \param topic_type Topic type + * \param qos %QoS settings + * \param callback Callback for new messages of serialized form + * \param options %Publisher options. + * Not all publisher options are currently respected, the only relevant options for this + * publisher are `event_callbacks`, `use_default_callbacks`, and `%callback_group`. + */ +template< + typename CallbackT, + typename AllocatorT = std::allocator> +std::shared_ptr create_generic_subscription( + rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface, + const std::string & topic_name, + const std::string & topic_type, + const rclcpp::QoS & qos, + CallbackT && callback, + const rclcpp::SubscriptionOptionsWithAllocator & options = ( + rclcpp::SubscriptionOptionsWithAllocator() + ) +) +{ + return rclcpp::detail::create_generic_subscription( + topics_interface, topic_name, topic_type, qos, std::forward(callback), options); +} + +// TODO(christophebedard): remove after Makoa +/// Create and return a GenericSubscription. +/** + * \deprecated Subscription callbacks taking a std::shared_ptr to a non-const + * rclcpp::SerializedMessage are deprecated, use + * std::shared_ptr instead. + */ +template< + typename CallbackT, + typename AllocatorT = std::allocator> +requires rclcpp::detail::deprecated_subscription_callback +[[deprecated( + "subscription callbacks taking a std::shared_ptr to a non-const message are deprecated, " + "use std::shared_ptr instead")]] +std::shared_ptr create_generic_subscription( + rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface, + const std::string & topic_name, + const std::string & topic_type, + const rclcpp::QoS & qos, + CallbackT && callback, + const rclcpp::SubscriptionOptionsWithAllocator & options = ( + rclcpp::SubscriptionOptionsWithAllocator() + ) +) +{ + return rclcpp::detail::create_generic_subscription( + topics_interface, topic_name, topic_type, qos, std::forward(callback), options); +} } // namespace rclcpp diff --git a/rclcpp/include/rclcpp/create_subscription.hpp b/rclcpp/include/rclcpp/create_subscription.hpp index 84b6be5cb3..2e1741907b 100644 --- a/rclcpp/include/rclcpp/create_subscription.hpp +++ b/rclcpp/include/rclcpp/create_subscription.hpp @@ -23,6 +23,7 @@ #include #include "rclcpp/detail/qos_parameters.hpp" +#include "rclcpp/detail/subscription_callback_type_helper.hpp" #include "rclcpp/node_interfaces/get_node_topics_interface.hpp" #include "rclcpp/node_interfaces/node_parameters_interface.hpp" #include "rclcpp/node_interfaces/node_topics_interface.hpp" @@ -135,6 +136,42 @@ create_subscription( node, node, topic_name, qos, std::forward(callback), options, msg_mem_strat); } +// TODO(christophebedard): remove after Makoa +/// Create and return a subscription of the given MessageT type. +/** + * \deprecated Subscription callbacks taking a std::shared_ptr to a non-const message are + * deprecated, use std::shared_ptr instead. + */ +template< + typename MessageT, + typename CallbackT, + typename AllocatorT = std::allocator, + typename SubscriptionT = rclcpp::Subscription, + typename MessageMemoryStrategyT = typename SubscriptionT::MessageMemoryStrategyType, + typename NodeT> +requires rclcpp::detail::deprecated_subscription_callback +[[deprecated( + "subscription callbacks taking a std::shared_ptr to a non-const message are deprecated, " + "use std::shared_ptr instead")]] +typename std::shared_ptr +create_subscription( + NodeT & node, + const std::string & topic_name, + const rclcpp::QoS & qos, + CallbackT && callback, + const rclcpp::SubscriptionOptionsWithAllocator & options = ( + rclcpp::SubscriptionOptionsWithAllocator() + ), + typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = ( + MessageMemoryStrategyT::create_default() + ) +) +{ + return rclcpp::detail::create_subscription< + MessageT, CallbackT, AllocatorT, SubscriptionT, MessageMemoryStrategyT>( + node, node, topic_name, qos, std::forward(callback), options, msg_mem_strat); +} + /// Create and return a subscription of the given MessageT type. /** * See \ref create_subscription(). @@ -166,6 +203,43 @@ create_subscription( std::forward(callback), options, msg_mem_strat); } +// TODO(christophebedard): remove after Makoa +/// Create and return a subscription of the given MessageT type. +/** + * \deprecated Subscription callbacks taking a std::shared_ptr to a non-const message are + * deprecated, use std::shared_ptr instead. + */ +template< + typename MessageT, + typename CallbackT, + typename AllocatorT = std::allocator, + typename SubscriptionT = rclcpp::Subscription, + typename MessageMemoryStrategyT = typename SubscriptionT::MessageMemoryStrategyType> +requires rclcpp::detail::deprecated_subscription_callback +[[deprecated( + "subscription callbacks taking a std::shared_ptr to a non-const message are deprecated, " + "use std::shared_ptr instead")]] +typename std::shared_ptr +create_subscription( + rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & node_parameters, + rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr & node_topics, + const std::string & topic_name, + const rclcpp::QoS & qos, + CallbackT && callback, + const rclcpp::SubscriptionOptionsWithAllocator & options = ( + rclcpp::SubscriptionOptionsWithAllocator() + ), + typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = ( + MessageMemoryStrategyT::create_default() + ) +) +{ + return rclcpp::detail::create_subscription< + MessageT, CallbackT, AllocatorT, SubscriptionT, MessageMemoryStrategyT>( + node_parameters, node_topics, topic_name, qos, + std::forward(callback), options, msg_mem_strat); +} + } // namespace rclcpp #endif // RCLCPP__CREATE_SUBSCRIPTION_HPP_ diff --git a/rclcpp/include/rclcpp/detail/subscription_callback_type_helper.hpp b/rclcpp/include/rclcpp/detail/subscription_callback_type_helper.hpp index 0ebe6efd66..db7ba4b6d8 100644 --- a/rclcpp/include/rclcpp/detail/subscription_callback_type_helper.hpp +++ b/rclcpp/include/rclcpp/detail/subscription_callback_type_helper.hpp @@ -15,11 +15,14 @@ #ifndef RCLCPP__DETAIL__SUBSCRIPTION_CALLBACK_TYPE_HELPER_HPP_ #define RCLCPP__DETAIL__SUBSCRIPTION_CALLBACK_TYPE_HELPER_HPP_ +#include #include #include #include "rclcpp/function_traits.hpp" #include "rclcpp/message_info.hpp" +#include "rclcpp/serialized_message.hpp" +#include "rclcpp/type_adapter.hpp" namespace rclcpp { @@ -129,6 +132,7 @@ struct SubscriptionCallbackTypeHelper< std::function &, const rclcpp::MessageInfo &)>; }; +// TODO(christophebedard): remove everything below after Makoa template struct SubscriptionCallbackTypeHelper< MessageT, @@ -160,6 +164,69 @@ struct SubscriptionCallbackTypeHelper< std::function, const rclcpp::MessageInfo &)>; }; +/// Whether CallbackT is a deprecated subscription callback signature for MessageT. +/** + * Subscription callbacks taking a std::shared_ptr to a non-const message (MessageT, the ROS + * message type of a type adapter, or rclcpp::SerializedMessage), with or without + * rclcpp::MessageInfo, are deprecated in favor of the std::shared_ptr signatures. + * + * CallbackT must be supported by rclcpp::function_traits, see deprecated_subscription_callback. + */ +template +struct is_deprecated_subscription_callback +{ +private: + using callback_type = + typename SubscriptionCallbackTypeHelper>::callback_type; + using SubscribedType = typename rclcpp::TypeAdapter::custom_type; + using ROSMessageType = typename rclcpp::TypeAdapter::ros_message_type; + +public: + static constexpr bool value = + rclcpp::function_traits::same_arguments< + callback_type, + std::function)> + >::value || + rclcpp::function_traits::same_arguments< + callback_type, + std::function, const rclcpp::MessageInfo &)> + >::value || + rclcpp::function_traits::same_arguments< + callback_type, + std::function)> + >::value || + rclcpp::function_traits::same_arguments< + callback_type, + std::function, const rclcpp::MessageInfo &)> + >::value || + rclcpp::function_traits::same_arguments< + callback_type, + std::function)> + >::value || + rclcpp::function_traits::same_arguments< + callback_type, + std::function, const rclcpp::MessageInfo &)> + >::value; +}; + +/// Whether rclcpp::function_traits supports CallbackT. +template +concept function_traits_compatible = + std::is_function>::value || + std::is_bind_expression::value || + requires {&CallbackT::operator();}; + +/// Whether CallbackT is a deprecated subscription callback signature for MessageT. +/** + * This can be used to constrain overloads, since it does not instantiate + * is_deprecated_subscription_callback for types that are not supported by + * rclcpp::function_traits, which would be a hard error. + */ +template +concept deprecated_subscription_callback = + (function_traits_compatible>) && + is_deprecated_subscription_callback::value; + } // namespace detail } // namespace rclcpp diff --git a/rclcpp/include/rclcpp/node.hpp b/rclcpp/include/rclcpp/node.hpp index e8a1273c63..8a84b1a8c3 100644 --- a/rclcpp/include/rclcpp/node.hpp +++ b/rclcpp/include/rclcpp/node.hpp @@ -39,6 +39,7 @@ #include "rclcpp/client.hpp" #include "rclcpp/clock.hpp" #include "rclcpp/context.hpp" +#include "rclcpp/detail/subscription_callback_type_helper.hpp" #include "rclcpp/event.hpp" #include "rclcpp/generic_client.hpp" #include "rclcpp/generic_publisher.hpp" @@ -63,6 +64,7 @@ #include "rclcpp/publisher.hpp" #include "rclcpp/publisher_options.hpp" #include "rclcpp/qos.hpp" +#include "rclcpp/serialized_message.hpp" #include "rclcpp/service.hpp" #include "rclcpp/subscription.hpp" #include "rclcpp/subscription_options.hpp" @@ -227,6 +229,35 @@ class Node : public std::enable_shared_from_this ) ); + // TODO(christophebedard): remove after Makoa + /// Create and return a Subscription. + /** + * \deprecated Subscription callbacks taking a std::shared_ptr to a non-const message are + * deprecated, use std::shared_ptr instead. + */ + template< + typename MessageT, + typename CallbackT, + typename AllocatorT = std::allocator, + typename SubscriptionT = rclcpp::Subscription, + typename MessageMemoryStrategyT = typename SubscriptionT::MessageMemoryStrategyType + > + requires rclcpp::detail::deprecated_subscription_callback + [[deprecated( + "subscription callbacks taking a std::shared_ptr to a non-const message are deprecated, " + "use std::shared_ptr instead")]] + std::shared_ptr + create_subscription( + const std::string & topic_name, + const rclcpp::QoS & qos, + CallbackT && callback, + const SubscriptionOptionsWithAllocator & options = + SubscriptionOptionsWithAllocator(), + typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = ( + MessageMemoryStrategyT::create_default() + ) + ); + /// Create a wall timer that uses the wall clock to drive the callback. /** * \param[in] period Time interval between triggers of the callback. @@ -370,6 +401,30 @@ class Node : public std::enable_shared_from_this ) ); + // TODO(christophebedard): remove after Makoa + /// Create and return a GenericSubscription. + /** + * \deprecated Subscription callbacks taking a std::shared_ptr to a non-const + * rclcpp::SerializedMessage are deprecated, use + * std::shared_ptr instead. + */ + template< + typename CallbackT, + typename AllocatorT = std::allocator> + requires rclcpp::detail::deprecated_subscription_callback + [[deprecated( + "subscription callbacks taking a std::shared_ptr to a non-const message are deprecated, " + "use std::shared_ptr instead")]] + std::shared_ptr create_generic_subscription( + const std::string & topic_name, + const std::string & topic_type, + const rclcpp::QoS & qos, + CallbackT && callback, + const rclcpp::SubscriptionOptionsWithAllocator & options = ( + rclcpp::SubscriptionOptionsWithAllocator() + ) + ); + /// Declare and initialize a parameter, return the effective value. /** * This method is used to declare that a parameter exists on this node. diff --git a/rclcpp/include/rclcpp/node_impl.hpp b/rclcpp/include/rclcpp/node_impl.hpp index c699e7519e..674adb2efd 100644 --- a/rclcpp/include/rclcpp/node_impl.hpp +++ b/rclcpp/include/rclcpp/node_impl.hpp @@ -98,6 +98,32 @@ Node::create_subscription( msg_mem_strat); } +template< + typename MessageT, + typename CallbackT, + typename AllocatorT, + typename SubscriptionT, + typename MessageMemoryStrategyT> +requires rclcpp::detail::deprecated_subscription_callback +std::shared_ptr +Node::create_subscription( + const std::string & topic_name, + const rclcpp::QoS & qos, + CallbackT && callback, + const SubscriptionOptionsWithAllocator & options, + typename MessageMemoryStrategyT::SharedPtr msg_mem_strat) +{ + return rclcpp::detail::create_subscription< + MessageT, CallbackT, AllocatorT, SubscriptionT, MessageMemoryStrategyT>( + *this, + *this, + extend_name_with_sub_namespace(topic_name, this->get_sub_namespace()), + qos, + std::forward(callback), + options, + msg_mem_strat); +} + template typename rclcpp::WallTimer::SharedPtr Node::create_wall_timer( @@ -219,6 +245,26 @@ Node::create_generic_subscription( ); } +template +requires rclcpp::detail::deprecated_subscription_callback +std::shared_ptr +Node::create_generic_subscription( + const std::string & topic_name, + const std::string & topic_type, + const rclcpp::QoS & qos, + CallbackT && callback, + const rclcpp::SubscriptionOptionsWithAllocator & options) +{ + return rclcpp::detail::create_generic_subscription( + node_topics_, + extend_name_with_sub_namespace(topic_name, this->get_sub_namespace()), + topic_type, + qos, + std::forward(callback), + options + ); +} + template auto diff --git a/rclcpp/test/rclcpp/test_any_subscription_callback.cpp b/rclcpp/test/rclcpp/test_any_subscription_callback.cpp index d83580fc7b..31f1cd3ee9 100644 --- a/rclcpp/test/rclcpp/test_any_subscription_callback.cpp +++ b/rclcpp/test/rclcpp/test_any_subscription_callback.cpp @@ -20,8 +20,9 @@ #include #include -#define RCLCPP_AVOID_DEPRECATIONS_FOR_UNIT_TESTS 1 #include "rclcpp/any_subscription_callback.hpp" +#include "rclcpp/detail/subscription_callback_type_helper.hpp" +#include "rclcpp/subscription_options.hpp" #include "test_msgs/msg/empty.hpp" // Type adapter to be used in tests. @@ -83,6 +84,64 @@ class TestAnySubscriptionCallbackTA : public ::testing::Test rclcpp::MessageInfo message_info_; }; +struct DeprecatedSubscriptionCallbackHelper +{ + void callback(const test_msgs::msg::Empty::SharedPtr) {} + void const_callback(const test_msgs::msg::Empty::ConstSharedPtr) {} +}; + +TEST(TestDeprecatedSubscriptionCallback, deprecated_subscription_callback) { + using rclcpp::detail::deprecated_subscription_callback; + using test_msgs::msg::Empty; + + // Deprecated: std::shared_ptr to a non-const message, with or without rclcpp::MessageInfo + auto shared_ptr = [](std::shared_ptr) {}; + auto shared_ptr_info = [](std::shared_ptr, const rclcpp::MessageInfo &) {}; + auto serialized = [](std::shared_ptr) {}; + auto serialized_info = + [](std::shared_ptr, const rclcpp::MessageInfo &) {}; + auto custom = [](std::shared_ptr) {}; + DeprecatedSubscriptionCallbackHelper helper; + auto bind = std::bind( + &DeprecatedSubscriptionCallbackHelper::callback, &helper, std::placeholders::_1); + static_assert(deprecated_subscription_callback); + static_assert(deprecated_subscription_callback); + static_assert(deprecated_subscription_callback); + static_assert(deprecated_subscription_callback); + static_assert(deprecated_subscription_callback); + static_assert(deprecated_subscription_callback); + static_assert(deprecated_subscription_callback), Empty>); + static_assert( + deprecated_subscription_callback)>, Empty>); + static_assert(deprecated_subscription_callback); + static_assert(deprecated_subscription_callback); + static_assert(deprecated_subscription_callback); + + // Not deprecated + auto const_shared_ptr = [](std::shared_ptr) {}; + auto const_shared_ptr_info = [](std::shared_ptr, const rclcpp::MessageInfo &) {}; + auto const_ref = [](const Empty &) {}; + auto unique_ptr = [](std::unique_ptr) {}; + auto const_ref_shared_const_ptr = [](const std::shared_ptr &) {}; + auto const_serialized = [](std::shared_ptr) {}; + auto const_custom = [](std::shared_ptr) {}; + auto const_bind = std::bind( + &DeprecatedSubscriptionCallbackHelper::const_callback, &helper, std::placeholders::_1); + static_assert(!deprecated_subscription_callback); + static_assert(!deprecated_subscription_callback); + static_assert(!deprecated_subscription_callback); + static_assert(!deprecated_subscription_callback); + static_assert(!deprecated_subscription_callback); + static_assert(!deprecated_subscription_callback); + static_assert(!deprecated_subscription_callback); + static_assert(!deprecated_subscription_callback); + static_assert(!deprecated_subscription_callback); + + // Not callable, e.g., when checking an overload that does not match the call + static_assert(!deprecated_subscription_callback); + static_assert(!deprecated_subscription_callback); +} + TEST_F(TestAnySubscriptionCallback, construct_destruct) { // Default constructor. rclcpp::AnySubscriptionCallback asc1; diff --git a/rclcpp/test/rclcpp/test_create_subscription.cpp b/rclcpp/test/rclcpp/test_create_subscription.cpp index d3f14a4ea3..35ecce9808 100644 --- a/rclcpp/test/rclcpp/test_create_subscription.cpp +++ b/rclcpp/test/rclcpp/test_create_subscription.cpp @@ -16,8 +16,10 @@ #include +#include "rclcpp/create_generic_subscription.hpp" #include "rclcpp/create_subscription.hpp" #include "rclcpp/node.hpp" +#include "rclcpp/serialized_message.hpp" #include "test_msgs/msg/empty.hpp" class TestCreateSubscription : public ::testing::Test @@ -89,3 +91,52 @@ TEST_F(TestCreateSubscription, create_with_intra_process_com) { ASSERT_NE(nullptr, subscription); EXPECT_STREQ("/ns/topic_name", subscription->get_topic_name()); } + +TEST_F(TestCreateSubscription, create_with_deprecated_callback) { + auto node = std::make_shared("my_node", "/ns"); + auto sub_node = node->create_sub_node("sub"); + const rclcpp::QoS qos(10); + auto options = rclcpp::SubscriptionOptions(); + auto callback = [](test_msgs::msg::Empty::SharedPtr) {}; + auto serialized_callback = [](std::shared_ptr) {}; + auto node_parameters = node->get_node_parameters_interface(); + auto node_topics = node->get_node_topics_interface(); + +#if !defined(_WIN32) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#else // !defined(_WIN32) +# pragma warning(push) +# pragma warning(disable: 4996) +#endif + auto node_subscription = + node->create_subscription("topic_name", qos, callback); + auto sub_node_subscription = + sub_node->create_subscription("topic_name", qos, callback); + auto subscription = + rclcpp::create_subscription(node, "topic_name", qos, callback, options); + auto separated_subscription = rclcpp::create_subscription( + node_parameters, node_topics, "topic_name", qos, callback, options); + auto node_generic_subscription = node->create_generic_subscription( + "generic_topic_name", "test_msgs/msg/Empty", qos, serialized_callback); + auto generic_subscription = rclcpp::create_generic_subscription( + node_topics, "generic_topic_name", "test_msgs/msg/Empty", qos, serialized_callback); +#if !defined(_WIN32) +# pragma GCC diagnostic pop +#else // !defined(_WIN32) +# pragma warning(pop) +#endif + + ASSERT_NE(nullptr, node_subscription); + EXPECT_STREQ("/ns/topic_name", node_subscription->get_topic_name()); + ASSERT_NE(nullptr, sub_node_subscription); + EXPECT_STREQ("/ns/sub/topic_name", sub_node_subscription->get_topic_name()); + ASSERT_NE(nullptr, subscription); + EXPECT_STREQ("/ns/topic_name", subscription->get_topic_name()); + ASSERT_NE(nullptr, separated_subscription); + EXPECT_STREQ("/ns/topic_name", separated_subscription->get_topic_name()); + ASSERT_NE(nullptr, node_generic_subscription); + EXPECT_STREQ("/ns/generic_topic_name", node_generic_subscription->get_topic_name()); + ASSERT_NE(nullptr, generic_subscription); + EXPECT_STREQ("/ns/generic_topic_name", generic_subscription->get_topic_name()); +} diff --git a/rclcpp_action/test/test_client.cpp b/rclcpp_action/test/test_client.cpp index c461d1eeed..88e0cf38f4 100644 --- a/rclcpp_action/test/test_client.cpp +++ b/rclcpp_action/test/test_client.cpp @@ -242,7 +242,7 @@ class TestClient : public ::testing::Test // to be executed. { auto subscription = client_node->create_subscription( - "test_topic", 10, [](const test_msgs::msg::Empty::SharedPtr) {}); + "test_topic", 10, [](const test_msgs::msg::Empty::ConstSharedPtr) {}); subscription_content_filter_supported_ = subscription->is_cft_supported(); } } diff --git a/rclcpp_action/test/test_generic_client.cpp b/rclcpp_action/test/test_generic_client.cpp index 0bca9acd0b..df3ccb2fa4 100644 --- a/rclcpp_action/test/test_generic_client.cpp +++ b/rclcpp_action/test/test_generic_client.cpp @@ -233,7 +233,7 @@ class TestGenericClient : public ::testing::Test // to be executed. { auto subscription = client_node->create_subscription( - "test_topic", 10, [](const test_msgs::msg::Empty::SharedPtr) {}); + "test_topic", 10, [](const test_msgs::msg::Empty::ConstSharedPtr) {}); subscription_content_filter_supported_ = subscription->is_cft_supported(); } } diff --git a/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node.hpp b/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node.hpp index 43ff0e3fa4..31036e89e3 100644 --- a/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node.hpp +++ b/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node.hpp @@ -56,6 +56,7 @@ #include "rclcpp/callback_group.hpp" #include "rclcpp/client.hpp" #include "rclcpp/clock.hpp" +#include "rclcpp/detail/subscription_callback_type_helper.hpp" #include "rclcpp/event.hpp" #include "rclcpp/generic_publisher.hpp" #include "rclcpp/generic_subscription.hpp" @@ -78,6 +79,7 @@ #include "rclcpp/publisher.hpp" #include "rclcpp/publisher_options.hpp" #include "rclcpp/qos.hpp" +#include "rclcpp/serialized_message.hpp" #include "rclcpp/service.hpp" #include "rclcpp/subscription.hpp" #include "rclcpp/subscription_options.hpp" @@ -254,6 +256,34 @@ class LifecycleNode : public node_interfaces::LifecycleNodeInterface, ) ); + // TODO(christophebedard): remove after Makoa + /// Create and return a Subscription. + /** + * \deprecated Subscription callbacks taking a std::shared_ptr to a non-const message are + * deprecated, use std::shared_ptr instead. + */ + template< + typename MessageT, + typename CallbackT, + typename AllocatorT = std::allocator, + typename SubscriptionT = rclcpp::Subscription, + typename MessageMemoryStrategyT = typename SubscriptionT::MessageMemoryStrategyType> + requires rclcpp::detail::deprecated_subscription_callback + [[deprecated( + "subscription callbacks taking a std::shared_ptr to a non-const message are deprecated, " + "use std::shared_ptr instead")]] + std::shared_ptr + create_subscription( + const std::string & topic_name, + const rclcpp::QoS & qos, + CallbackT && callback, + const SubscriptionOptionsWithAllocator & options = + create_default_subscription_options(), + typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = ( + MessageMemoryStrategyT::create_default() + ) + ); + /// Create a timer that uses the wall clock to drive the callback. /** * \param[in] period Time interval between triggers of the callback. @@ -337,6 +367,30 @@ class LifecycleNode : public node_interfaces::LifecycleNodeInterface, ) ); + // TODO(christophebedard): remove after Makoa + /// Create and return a GenericSubscription. + /** + * \deprecated Subscription callbacks taking a std::shared_ptr to a non-const + * rclcpp::SerializedMessage are deprecated, use + * std::shared_ptr instead. + */ + template< + typename CallbackT, + typename AllocatorT = std::allocator> + requires rclcpp::detail::deprecated_subscription_callback + [[deprecated( + "subscription callbacks taking a std::shared_ptr to a non-const message are deprecated, " + "use std::shared_ptr instead")]] + std::shared_ptr create_generic_subscription( + const std::string & topic_name, + const std::string & topic_type, + const rclcpp::QoS & qos, + CallbackT && callback, + const rclcpp::SubscriptionOptionsWithAllocator & options = ( + rclcpp::SubscriptionOptionsWithAllocator() + ) + ); + /// Declare and initialize a parameter, return the effective value. /** * \sa rclcpp::Node::declare_parameter diff --git a/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node_impl.hpp b/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node_impl.hpp index c2de16e3df..d489ec3a36 100644 --- a/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node_impl.hpp +++ b/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node_impl.hpp @@ -90,6 +90,32 @@ LifecycleNode::create_subscription( msg_mem_strat); } +template< + typename MessageT, + typename CallbackT, + typename AllocatorT, + typename SubscriptionT, + typename MessageMemoryStrategyT> +requires rclcpp::detail::deprecated_subscription_callback +std::shared_ptr +LifecycleNode::create_subscription( + const std::string & topic_name, + const rclcpp::QoS & qos, + CallbackT && callback, + const rclcpp::SubscriptionOptionsWithAllocator & options, + typename MessageMemoryStrategyT::SharedPtr msg_mem_strat) +{ + return rclcpp::detail::create_subscription< + MessageT, CallbackT, AllocatorT, SubscriptionT, MessageMemoryStrategyT>( + *this, + *this, + topic_name, + qos, + std::forward(callback), + options, + msg_mem_strat); +} + template typename rclcpp::WallTimer::SharedPtr LifecycleNode::create_wall_timer( @@ -186,6 +212,28 @@ LifecycleNode::create_generic_subscription( ); } +template +requires rclcpp::detail::deprecated_subscription_callback +std::shared_ptr +LifecycleNode::create_generic_subscription( + const std::string & topic_name, + const std::string & topic_type, + const rclcpp::QoS & qos, + CallbackT && callback, + const rclcpp::SubscriptionOptionsWithAllocator & options) +{ + return rclcpp::detail::create_generic_subscription( + node_topics_, + // TODO(karsten1987): LifecycleNode is currently not supporting subnamespaces + // see https://github.com/ros2/rclcpp/issues/1614 + topic_name, + topic_type, + qos, + std::forward(callback), + options + ); +} + template auto LifecycleNode::declare_parameter( diff --git a/rclcpp_lifecycle/test/test_lifecycle_node.cpp b/rclcpp_lifecycle/test/test_lifecycle_node.cpp index c69219d965..331979bbc9 100644 --- a/rclcpp_lifecycle/test/test_lifecycle_node.cpp +++ b/rclcpp_lifecycle/test/test_lifecycle_node.cpp @@ -39,6 +39,7 @@ #include "rclcpp/node_options.hpp" #include "rclcpp/parameter.hpp" #include "rclcpp/parameter_value.hpp" +#include "rclcpp/serialized_message.hpp" #include "rclcpp/utilities.hpp" #include "rclcpp_lifecycle/lifecycle_node.hpp" @@ -652,6 +653,34 @@ TEST_F(TestDefaultStateMachine, lifecycle_subscriber) { SUCCEED(); } +TEST_F(TestDefaultStateMachine, lifecycle_subscriber_deprecated_callback) { + auto test_node = std::make_shared>("testnode"); + + auto cb = [](std::shared_ptr msg) {(void) msg;}; + auto generic_cb = [](std::shared_ptr msg) {(void) msg;}; +#if !defined(_WIN32) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#else // !defined(_WIN32) +# pragma warning(push) +# pragma warning(disable: 4996) +#endif + auto lifecycle_sub = + test_node->create_subscription("~/empty", 10, cb); + auto lifecycle_generic_sub = test_node->create_generic_subscription( + "~/empty_generic", "lifecycle_msgs/msg/State", 10, generic_cb); +#if !defined(_WIN32) +# pragma GCC diagnostic pop +#else // !defined(_WIN32) +# pragma warning(pop) +#endif + + ASSERT_NE(nullptr, lifecycle_sub); + EXPECT_STREQ("/testnode/empty", lifecycle_sub->get_topic_name()); + ASSERT_NE(nullptr, lifecycle_generic_sub); + EXPECT_STREQ("/testnode/empty_generic", lifecycle_generic_sub->get_topic_name()); +} + // Parameters are tested more thoroughly in rclcpp's test_node.cpp // These are provided for coverage of lifecycle node's API TEST_F(TestDefaultStateMachine, declare_parameters) {