|
| 1 | +// Copyright 2023 Elroy Air, 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_LIFECYCLE__LIFECYCLE_SUBSCRIPTION_HPP_ |
| 16 | +#define RCLCPP_LIFECYCLE__LIFECYCLE_SUBSCRIPTION_HPP_ |
| 17 | + |
| 18 | +// #include <functional> |
| 19 | +// #include <memory> |
| 20 | + |
| 21 | +#include "rclcpp/subscription.hpp" |
| 22 | + |
| 23 | +namespace rclcpp_lifecycle |
| 24 | +{ |
| 25 | + |
| 26 | +/// @brief Child class of rclcpp::Subscription that adds lifecycle functionality. |
| 27 | +/** |
| 28 | + * This class is a child class of rclcpp::Subscription that adds a lifecycle |
| 29 | + * check to the callback function. If the node is in an inactive state, the |
| 30 | + * callback will not be called. |
| 31 | + */ |
| 32 | +template< |
| 33 | + typename MessageT, |
| 34 | + typename AllocatorT = std::allocator<void>, |
| 35 | + typename SubscribedT = typename rclcpp::TypeAdapter<MessageT>::custom_type, |
| 36 | + typename ROSMessageT = typename rclcpp::TypeAdapter<MessageT>::ros_message_type, |
| 37 | + typename MessageMemoryStrategyT = rclcpp::message_memory_strategy::MessageMemoryStrategy< |
| 38 | + ROSMessageT, |
| 39 | + AllocatorT |
| 40 | + >> |
| 41 | +class LifecycleSubscription : public SimpleManagedEntity, |
| 42 | + public rclcpp::Subscription<MessageT, AllocatorT> |
| 43 | +{ |
| 44 | + |
| 45 | +private: |
| 46 | + using SubscriptionTopicStatisticsSharedPtr = |
| 47 | + std::shared_ptr<rclcpp::topic_statistics::SubscriptionTopicStatistics<ROSMessageT>>; |
| 48 | + |
| 49 | +public: |
| 50 | + LifecycleSubscription( |
| 51 | + rclcpp::node_interfaces::NodeBaseInterface * node_base, |
| 52 | + const rosidl_message_type_support_t & type_support_handle, |
| 53 | + const std::string & topic_name, |
| 54 | + const rclcpp::QoS & qos, |
| 55 | + rclcpp::AnySubscriptionCallback<MessageT, AllocatorT> callback, |
| 56 | + const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & options, |
| 57 | + typename MessageMemoryStrategyT::SharedPtr message_memory_strategy, |
| 58 | + SubscriptionTopicStatisticsSharedPtr subscription_topic_statistics = nullptr) |
| 59 | + : rclcpp::Subscription<MessageT>( |
| 60 | + node_base, |
| 61 | + type_support_handle, |
| 62 | + topic_name, |
| 63 | + qos, |
| 64 | + callback, |
| 65 | + options, |
| 66 | + message_memory_strategy, |
| 67 | + subscription_topic_statistics) |
| 68 | + { |
| 69 | + } |
| 70 | + |
| 71 | + /// Check if we need to handle the message, and execute the callback if we do. |
| 72 | + virtual void handle_message( |
| 73 | + std::shared_ptr<void> & message, const rclcpp::MessageInfo & message_info) override |
| 74 | + { |
| 75 | + if (!this->is_activated()) { |
| 76 | + return; |
| 77 | + } |
| 78 | + rclcpp::Subscription<MessageT>::handle_message(message, message_info); |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +} // namespace rclcpp_lifecycle |
| 83 | + |
| 84 | +#endif // RCLCPP_LIFECYCLE__LIFECYCLE_SUBSCRIPTION_HPP_ |
0 commit comments