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/bind.hpp>
36 #include <boost/thread/mutex.hpp>
37 
38 // For backward compatibility, pull placeholders into global namespace
39 using namespace boost::placeholders;
40 
41 namespace ros
42 {
51  class ROSCPP_DECL Publisher
52  {
53  public:
54  Publisher() {}
55  Publisher(const Publisher& rhs);
56  ~Publisher();
57  Publisher& operator=(const Publisher& other) = default;
58 
67  template <typename M>
68  void publish(const boost::shared_ptr<M>& message) const
69  {
70  using namespace serialization;
71 
72  if (!impl_)
73  {
74  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher");
75  return;
76  }
77 
78  if (!impl_->isValid())
79  {
80  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher (topic [%s])", impl_->topic_.c_str());
81  return;
82  }
83  if (impl_->md5sum_ == "*" ||
84  std::string(mt::md5sum<M>(*message)) == "*" ||
85  impl_->md5sum_ == mt::md5sum<M>(*message)) {
86  ROS_DEBUG_ONCE("Trying to publish message of type [%s/%s] on a "
87  "publisher with type [%s/%s]",
88  mt::datatype<M>(*message), mt::md5sum<M>(*message),
89  impl_->datatype_.c_str(), impl_->md5sum_.c_str());
90  }
91 
93  m.type_info = &typeid(M);
94  m.message = message;
95 
96  publish(boost::bind(serializeMessage<M>, boost::ref(*message)), m);
97  }
98 
102  template <typename M>
103  void publish(const M& message) const
104  {
105  using namespace serialization;
106  namespace mt = ros::message_traits;
107 
108  if (!impl_)
109  {
110  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher");
111  return;
112  }
113 
114  if (!impl_->isValid())
115  {
116  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher (topic [%s])", impl_->topic_.c_str());
117  return;
118  }
119  if (impl_->md5sum_ == "*" ||
120  std::string(mt::md5sum<M>(message)) == "*" ||
121  impl_->md5sum_ == mt::md5sum<M>(message)) {
122  ROS_DEBUG_ONCE("Trying to publish message of type [%s/%s] on a "
123  "publisher with type [%s/%s]",
124  mt::datatype<M>(message), mt::md5sum<M>(message),
125  impl_->datatype_.c_str(), impl_->md5sum_.c_str());
126  }
127 
129  publish(boost::bind(serializeMessage<M>, boost::ref(message)), m);
130  }
131 
142  void shutdown();
143 
147  std::string getTopic() const;
148 
152  uint32_t getNumSubscribers() const;
153 
157  bool isLatched() const;
158 
159  operator void*() const { return (impl_ && impl_->isValid()) ? (void*)1 : (void*)0; }
160 
161  bool operator<(const Publisher& rhs) const
162  {
163  return impl_ < rhs.impl_;
164  }
165 
166  bool operator==(const Publisher& rhs) const
167  {
168  return impl_ == rhs.impl_;
169  }
170 
171  bool operator!=(const Publisher& rhs) const
172  {
173  return impl_ != rhs.impl_;
174  }
175 
176  boost::function<void(const SubscriberLinkPtr &)> getLastMessageCallback() {
177  return boost::bind(&Impl::pushLastMessage, impl_.get(), boost::placeholders::_1);
178  }
179 
180  private:
181 
182  Publisher(const std::string& topic, const std::string& md5sum,
183  const std::string& datatype, bool latch, const NodeHandle& node_handle,
184  const SubscriberCallbacksPtr& callbacks);
185 
186  void publish(const boost::function<SerializedMessage(void)>& serfunc, SerializedMessage& m) const;
187  void incrementSequence() const;
188 
189  class ROSCPP_DECL Impl
190  {
191  public:
192  Impl();
193  ~Impl();
194 
195  void unadvertise();
196  bool isValid() const;
197  void pushLastMessage(const SubscriberLinkPtr &sub_link);
198 
199  std::string topic_;
200  std::string md5sum_;
201  std::string datatype_;
205  bool latch_;
207  boost::mutex last_message_mutex_;
208  };
210  typedef boost::weak_ptr<Impl> ImplWPtr;
211 
213 
214  friend class NodeHandle;
216  };
217 
218  typedef std::vector<Publisher> V_Publisher;
219 }
220 
221 #endif // ROSCPP_PUBLISHER_HANDLE_H
ros::SerializedMessage
ros::Publisher::ImplPtr
boost::shared_ptr< Impl > ImplPtr
Definition: publisher.h:209
ros::Publisher::Impl
Definition: publisher.h:189
boost::shared_ptr< M >
forwards.h
ros
ros::Publisher::Impl::callbacks_
SubscriberCallbacksPtr callbacks_
Definition: publisher.h:203
ros::V_Publisher
std::vector< Publisher > V_Publisher
Definition: publisher.h:218
ros::Publisher::Impl::latch_
bool latch_
Definition: publisher.h:205
ros::Publisher::Impl::node_handle_
NodeHandlePtr node_handle_
Definition: publisher.h:202
ros::SerializedMessage::message
boost::shared_ptr< void const > message
ros::Publisher::ImplWPtr
boost::weak_ptr< Impl > ImplWPtr
Definition: publisher.h:210
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:608
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:68
ros::message_traits
ros::Publisher::impl_
ImplPtr impl_
Definition: publisher.h:212
ros::Publisher::Impl::last_message_
SerializedMessage last_message_
Definition: publisher.h:206
message.h
serialization.h
ROS_ASSERT_MSG
#define ROS_ASSERT_MSG(cond,...)
ros::Publisher::operator!=
bool operator!=(const Publisher &rhs) const
Definition: publisher.h:171
ros::NodeHandle
roscpp's interface for creating subscribers, publishers, etc.
Definition: node_handle.h:85
ros::Publisher
Manages an advertisement on a specific topic.
Definition: publisher.h:51
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:201
ros::Publisher::Publisher
Publisher()
Definition: publisher.h:54
ros::Publisher::Impl::unadvertised_
bool unadvertised_
Definition: publisher.h:204
ros::Publisher::Impl::last_message_mutex_
boost::mutex last_message_mutex_
Definition: publisher.h:207
ros::Publisher::getLastMessageCallback
boost::function< void(const SubscriberLinkPtr &)> getLastMessageCallback()
Definition: publisher.h:176
ros::Publisher::operator==
bool operator==(const Publisher &rhs) const
Definition: publisher.h:166
ros::Publisher::publish
void publish(const M &message) const
Publish a message on the topic associated with this Publisher.
Definition: publisher.h:103
ros::Publisher::operator<
bool operator<(const Publisher &rhs) const
Definition: publisher.h:161
ros::Publisher::Impl::topic_
std::string topic_
Definition: publisher.h:199
ros::Publisher::Impl::md5sum_
std::string md5sum_
Definition: publisher.h:200


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:35