rclcpp_lifecycle  master
C++ ROS Lifecycle Library API
lifecycle_publisher.hpp
Go to the documentation of this file.
1 // Copyright 2016 Open Source Robotics Foundation, 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_PUBLISHER_HPP_
16 #define RCLCPP_LIFECYCLE__LIFECYCLE_PUBLISHER_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <utility>
21 
22 #include "rclcpp/publisher.hpp"
23 
24 #include "rclcpp/logging.hpp"
25 
26 namespace rclcpp_lifecycle
27 {
29 
37 {
38 public:
40  virtual void on_activate() = 0;
41  virtual void on_deactivate() = 0;
42  virtual bool is_activated() = 0;
43 };
44 
46 
49 template<typename MessageT, typename Alloc = std::allocator<void>>
51  public rclcpp::Publisher<MessageT, Alloc>
52 {
53 public:
55 
56  using MessageAllocTraits = rclcpp::allocator::AllocRebind<MessageT, Alloc>;
57  using MessageAlloc = typename MessageAllocTraits::allocator_type;
58  using MessageDeleter = rclcpp::allocator::Deleter<MessageAlloc, MessageT>;
59  using MessageUniquePtr = std::unique_ptr<MessageT, MessageDeleter>;
60 
62  rclcpp::node_interfaces::NodeBaseInterface * node_base,
63  const std::string & topic,
64  const rclcpp::QoS & qos,
65  const rclcpp::PublisherOptionsWithAllocator<Alloc> & options)
66  : rclcpp::Publisher<MessageT, Alloc>(node_base, topic, qos, options),
67  enabled_(false),
68  logger_(rclcpp::get_logger("LifecyclePublisher"))
69  {
70  }
71 
73 
75 
80  virtual void
82  {
83  if (!enabled_) {
85  logger_,
86  "Trying to publish message on the topic '%s', but the publisher is not activated",
87  this->get_topic_name());
88 
89  return;
90  }
92  }
93 
95 
100  virtual void
101  publish(const MessageT & msg)
102  {
103  if (!enabled_) {
104  RCLCPP_WARN(
105  logger_,
106  "Trying to publish message on the topic '%s', but the publisher is not activated",
107  this->get_topic_name());
108 
109  return;
110  }
112  }
113 
114  virtual void
116  {
117  enabled_ = true;
118  }
119 
120  virtual void
122  {
123  enabled_ = false;
124  }
125 
126  virtual bool
128  {
129  return enabled_;
130  }
131 
132 private:
133  bool enabled_ = false;
134  rclcpp::Logger logger_;
135 };
136 
137 } // namespace rclcpp_lifecycle
138 
139 #endif // RCLCPP_LIFECYCLE__LIFECYCLE_PUBLISHER_HPP_
rclcpp_lifecycle::LifecyclePublisher::on_activate
virtual void on_activate()
Definition: lifecycle_publisher.hpp:115
RCLCPP_WARN
#define RCLCPP_WARN(logger,...)
rclcpp::PublisherBase::get_topic_name
const char * get_topic_name() const
rclcpp::Publisher
std::move
T move(T... args)
rclcpp::Publisher< MessageT, std::allocator< void > >::Publisher
Publisher(rclcpp::node_interfaces::NodeBaseInterface *node_base, const std::string &topic, const rclcpp::QoS &qos, const rclcpp::PublisherOptionsWithAllocator< AllocatorT > &options)
rclcpp_lifecycle::LifecyclePublisher::is_activated
virtual bool is_activated()
Definition: lifecycle_publisher.hpp:127
rclcpp_lifecycle::LifecyclePublisherInterface
base class with only
Definition: lifecycle_publisher.hpp:36
rclcpp_lifecycle::LifecyclePublisherInterface::~LifecyclePublisherInterface
virtual ~LifecyclePublisherInterface()
Definition: lifecycle_publisher.hpp:39
rclcpp
RCLCPP_SMART_PTR_DEFINITIONS
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
rclcpp_lifecycle::LifecyclePublisher::MessageDeleter
rclcpp::allocator::Deleter< MessageAlloc, MessageT > MessageDeleter
Definition: lifecycle_publisher.hpp:58
rclcpp_lifecycle
Definition: lifecycle_node.hpp:92
rclcpp_lifecycle::LifecyclePublisher::on_deactivate
virtual void on_deactivate()
Definition: lifecycle_publisher.hpp:121
rclcpp::Logger
AllocRebind
typename std::allocator_traits< Alloc >::template rebind_traits< T > AllocRebind
rclcpp_lifecycle::LifecyclePublisher::MessageAlloc
typename MessageAllocTraits::allocator_type MessageAlloc
Definition: lifecycle_publisher.hpp:57
rclcpp_lifecycle::LifecyclePublisher::publish
virtual void publish(const MessageT &msg)
LifecyclePublisher publish function.
Definition: lifecycle_publisher.hpp:101
rclcpp_lifecycle::LifecyclePublisher::publish
virtual void publish(std::unique_ptr< MessageT, MessageDeleter > msg)
LifecyclePublisher publish function.
Definition: lifecycle_publisher.hpp:81
publisher.hpp
rclcpp::PublisherOptionsWithAllocator< AllocatorT >
Deleter
typename std::conditional< std::is_same< typename std::allocator_traits< Alloc >::template rebind_alloc< T >, typename std::allocator< void >::template rebind< T >::other >::value, std::default_delete< T >, AllocatorDeleter< Alloc > >::type Deleter
rclcpp_lifecycle::LifecyclePublisher::~LifecyclePublisher
~LifecyclePublisher()
Definition: lifecycle_publisher.hpp:72
std
rclcpp_lifecycle::LifecyclePublisherInterface::on_activate
virtual void on_activate()=0
rclcpp_lifecycle::LifecyclePublisherInterface::is_activated
virtual bool is_activated()=0
rclcpp_lifecycle::LifecyclePublisherInterface::on_deactivate
virtual void on_deactivate()=0
std::unique_ptr
rclcpp_lifecycle::LifecyclePublisher::MessageAllocTraits
rclcpp::allocator::AllocRebind< MessageT, Alloc > MessageAllocTraits
Definition: lifecycle_publisher.hpp:56
rclcpp::Publisher::publish
virtual void publish(std::unique_ptr< MessageT, MessageDeleter > msg)
logging.hpp
rclcpp_lifecycle::LifecyclePublisher
brief child class of rclcpp Publisher class.
Definition: lifecycle_publisher.hpp:50