.. _program_listing_file_include_rclcpp_lifecycle_lifecycle_publisher.hpp: Program Listing for File lifecycle_publisher.hpp ================================================ |exhale_lsh| :ref:`Return to documentation for file ` (``include/rclcpp_lifecycle/lifecycle_publisher.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // Copyright 2016 Open Source Robotics Foundation, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef RCLCPP_LIFECYCLE__LIFECYCLE_PUBLISHER_HPP_ #define RCLCPP_LIFECYCLE__LIFECYCLE_PUBLISHER_HPP_ #include #include #include #include "rclcpp/logging.hpp" #include "rclcpp/publisher.hpp" #include "rclcpp_lifecycle/managed_entity.hpp" namespace rclcpp_lifecycle { template> class LifecyclePublisher : public SimpleManagedEntity, public rclcpp::Publisher { public: RCLCPP_SMART_PTR_DEFINITIONS(LifecyclePublisher) using MessageAllocTraits = rclcpp::allocator::AllocRebind; using MessageAlloc = typename MessageAllocTraits::allocator_type; using MessageDeleter = rclcpp::allocator::Deleter; using MessageUniquePtr = std::unique_ptr; LifecyclePublisher( rclcpp::node_interfaces::NodeBaseInterface * node_base, const std::string & topic, const rclcpp::QoS & qos, const rclcpp::PublisherOptionsWithAllocator & options) : rclcpp::Publisher(node_base, topic, qos, options), should_log_(true), logger_(rclcpp::get_logger("LifecyclePublisher")) { } ~LifecyclePublisher() {} virtual void publish(std::unique_ptr msg) { if (!this->is_activated()) { log_publisher_not_enabled(); return; } rclcpp::Publisher::publish(std::move(msg)); } virtual void publish(const MessageT & msg) { if (!this->is_activated()) { log_publisher_not_enabled(); return; } rclcpp::Publisher::publish(msg); } void on_activate() override { SimpleManagedEntity::on_activate(); should_log_ = true; } private: void log_publisher_not_enabled() { // Nothing to do if we are not meant to log if (!should_log_) { return; } // Log the message RCLCPP_WARN( logger_, "Trying to publish message on the topic '%s', but the publisher is not activated", this->get_topic_name()); // We stop logging until the flag gets enabled again should_log_ = false; } bool should_log_ = true; rclcpp::Logger logger_; }; } // namespace rclcpp_lifecycle #endif // RCLCPP_LIFECYCLE__LIFECYCLE_PUBLISHER_HPP_