publisher.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, Willow Garage, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef ROSCPP_PUBLISHER_HANDLE_H
29 #define ROSCPP_PUBLISHER_HANDLE_H
30 
31 #include "ros/forwards.h"
32 #include "ros/common.h"
33 #include "ros/message.h"
34 #include "ros/serialization.h"
35 #include <boost/bind.hpp>
36 #include <boost/thread/mutex.hpp>
37 
38 namespace ros
39 {
48  class ROSCPP_DECL Publisher
49  {
50  public:
51  Publisher() {}
52  Publisher(const Publisher& rhs);
53  ~Publisher();
54  Publisher& operator=(const Publisher& other) = default;
55 
64  template <typename M>
65  void publish(const boost::shared_ptr<M>& message) const
66  {
67  using namespace serialization;
68 
69  if (!impl_)
70  {
71  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher");
72  return;
73  }
74 
75  if (!impl_->isValid())
76  {
77  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher (topic [%s])", impl_->topic_.c_str());
78  return;
79  }
80  if (impl_->md5sum_ == "*" ||
81  std::string(mt::md5sum<M>(*message)) == "*" ||
82  impl_->md5sum_ == mt::md5sum<M>(*message)) {
83  ROS_DEBUG_ONCE("Trying to publish message of type [%s/%s] on a "
84  "publisher with type [%s/%s]",
85  mt::datatype<M>(*message), mt::md5sum<M>(*message),
86  impl_->datatype_.c_str(), impl_->md5sum_.c_str());
87  }
88 
90  m.type_info = &typeid(M);
91  m.message = message;
92 
93  publish(boost::bind(serializeMessage<M>, boost::ref(*message)), m);
94  }
95 
99  template <typename M>
100  void publish(const M& message) const
101  {
102  using namespace serialization;
103  namespace mt = ros::message_traits;
104 
105  if (!impl_)
106  {
107  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher");
108  return;
109  }
110 
111  if (!impl_->isValid())
112  {
113  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher (topic [%s])", impl_->topic_.c_str());
114  return;
115  }
116  if (impl_->md5sum_ == "*" ||
117  std::string(mt::md5sum<M>(message)) == "*" ||
118  impl_->md5sum_ == mt::md5sum<M>(message)) {
119  ROS_DEBUG_ONCE("Trying to publish message of type [%s/%s] on a "
120  "publisher with type [%s/%s]",
121  mt::datatype<M>(message), mt::md5sum<M>(message),
122  impl_->datatype_.c_str(), impl_->md5sum_.c_str());
123  }
124 
126  publish(boost::bind(serializeMessage<M>, boost::ref(message)), m);
127  }
128 
139  void shutdown();
140 
144  std::string getTopic() const;
145 
149  uint32_t getNumSubscribers() const;
150 
154  bool isLatched() const;
155 
156  operator void*() const { return (impl_ && impl_->isValid()) ? (void*)1 : (void*)0; }
157 
158  bool operator<(const Publisher& rhs) const
159  {
160  return impl_ < rhs.impl_;
161  }
162 
163  bool operator==(const Publisher& rhs) const
164  {
165  return impl_ == rhs.impl_;
166  }
167 
168  bool operator!=(const Publisher& rhs) const
169  {
170  return impl_ != rhs.impl_;
171  }
172 
173  boost::function<void(const SubscriberLinkPtr &)> getLastMessageCallback() {
174  return boost::bind(&Impl::pushLastMessage, impl_.get(), boost::placeholders::_1);
175  }
176 
177  private:
178 
179  Publisher(const std::string& topic, const std::string& md5sum,
180  const std::string& datatype, bool latch, const NodeHandle& node_handle,
181  const SubscriberCallbacksPtr& callbacks);
182 
183  void publish(const boost::function<SerializedMessage(void)>& serfunc, SerializedMessage& m) const;
184  void incrementSequence() const;
185 
186  class ROSCPP_DECL Impl
187  {
188  public:
189  Impl();
190  ~Impl();
191 
192  void unadvertise();
193  bool isValid() const;
194  void pushLastMessage(const SubscriberLinkPtr &sub_link);
195 
196  std::string topic_;
197  std::string md5sum_;
198  std::string datatype_;
202  bool latch_;
204  boost::mutex last_message_mutex_;
205  };
207  typedef boost::weak_ptr<Impl> ImplWPtr;
208 
210 
211  friend class NodeHandle;
213  };
214 
215  typedef std::vector<Publisher> V_Publisher;
216 }
217 
218 #endif // ROSCPP_PUBLISHER_HANDLE_H
219 
ros::SerializedMessage
ros::Publisher::ImplPtr
boost::shared_ptr< Impl > ImplPtr
Definition: publisher.h:206
ros::Publisher::Impl
Definition: publisher.h:186
boost::shared_ptr< M >
forwards.h
ros
ros::Publisher::Impl::callbacks_
SubscriberCallbacksPtr callbacks_
Definition: publisher.h:200
ros::V_Publisher
std::vector< Publisher > V_Publisher
Definition: publisher.h:215
ros::Publisher::Impl::latch_
bool latch_
Definition: publisher.h:202
ros::Publisher::Impl::node_handle_
NodeHandlePtr node_handle_
Definition: publisher.h:199
ros::SerializedMessage::message
boost::shared_ptr< void const > message
ros::Publisher::ImplWPtr
boost::weak_ptr< Impl > ImplWPtr
Definition: publisher.h:207
ros::shutdown
ROSCPP_DECL void shutdown()
Disconnects everything and unregisters from the master. It is generally not necessary to call this fu...
Definition: init.cpp:594
ros::NodeHandleBackingCollection
Definition: node_handle.cpp:58
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
Publish a message on the topic associated with this Publisher.
Definition: publisher.h:65
ros::message_traits
ros::Publisher::impl_
ImplPtr impl_
Definition: publisher.h:209
ros::Publisher::Impl::last_message_
SerializedMessage last_message_
Definition: publisher.h:203
message.h
serialization.h
ROS_ASSERT_MSG
#define ROS_ASSERT_MSG(cond,...)
ros::Publisher::operator!=
bool operator!=(const Publisher &rhs) const
Definition: publisher.h:168
ros::NodeHandle
roscpp's interface for creating subscribers, publishers, etc.
Definition: node_handle.h:87
ros::Publisher
Manages an advertisement on a specific topic.
Definition: publisher.h:48
ros::SerializedMessage::type_info
const std::type_info * type_info
ROS_DEBUG_ONCE
#define ROS_DEBUG_ONCE(...)
ros::Publisher::Impl::datatype_
std::string datatype_
Definition: publisher.h:198
ros::Publisher::Publisher
Publisher()
Definition: publisher.h:51
ros::Publisher::Impl::unadvertised_
bool unadvertised_
Definition: publisher.h:201
ros::Publisher::Impl::last_message_mutex_
boost::mutex last_message_mutex_
Definition: publisher.h:204
ros::Publisher::getLastMessageCallback
boost::function< void(const SubscriberLinkPtr &)> getLastMessageCallback()
Definition: publisher.h:173
ros::Publisher::operator==
bool operator==(const Publisher &rhs) const
Definition: publisher.h:163
ros::Publisher::publish
void publish(const M &message) const
Publish a message on the topic associated with this Publisher.
Definition: publisher.h:100
ros::Publisher::operator<
bool operator<(const Publisher &rhs) const
Definition: publisher.h:158
ros::Publisher::Impl::topic_
std::string topic_
Definition: publisher.h:196
ros::Publisher::Impl::md5sum_
std::string md5sum_
Definition: publisher.h:197


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas
autogenerated on Thu Feb 11 2021 03:58:34