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 
81  ROS_ASSERT_MSG(impl_->md5sum_ == "*" || std::string(mt::md5sum<M>(*message)) == "*" || impl_->md5sum_ == mt::md5sum<M>(*message),
82  "Trying to publish message of type [%s/%s] on a publisher with type [%s/%s]",
83  mt::datatype<M>(*message), mt::md5sum<M>(*message),
84  impl_->datatype_.c_str(), impl_->md5sum_.c_str());
85 
87  m.type_info = &typeid(M);
88  m.message = message;
89 
90  publish(boost::bind(serializeMessage<M>, boost::ref(*message)), m);
91  }
92 
96  template <typename M>
97  void publish(const M& message) const
98  {
99  using namespace serialization;
100  namespace mt = ros::message_traits;
101 
102  if (!impl_)
103  {
104  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher");
105  return;
106  }
107 
108  if (!impl_->isValid())
109  {
110  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher (topic [%s])", impl_->topic_.c_str());
111  return;
112  }
113 
114  ROS_ASSERT_MSG(impl_->md5sum_ == "*" || std::string(mt::md5sum<M>(message)) == "*" || impl_->md5sum_ == mt::md5sum<M>(message),
115  "Trying to publish message of type [%s/%s] on a publisher with type [%s/%s]",
116  mt::datatype<M>(message), mt::md5sum<M>(message),
117  impl_->datatype_.c_str(), impl_->md5sum_.c_str());
118 
120  publish(boost::bind(serializeMessage<M>, boost::ref(message)), m);
121  }
122 
133  void shutdown();
134 
138  std::string getTopic() const;
139 
143  uint32_t getNumSubscribers() const;
144 
148  bool isLatched() const;
149 
150  operator void*() const { return (impl_ && impl_->isValid()) ? (void*)1 : (void*)0; }
151 
152  bool operator<(const Publisher& rhs) const
153  {
154  return impl_ < rhs.impl_;
155  }
156 
157  bool operator==(const Publisher& rhs) const
158  {
159  return impl_ == rhs.impl_;
160  }
161 
162  bool operator!=(const Publisher& rhs) const
163  {
164  return impl_ != rhs.impl_;
165  }
166 
167  boost::function<void(const SubscriberLinkPtr &)> getLastMessageCallback() {
168  return boost::bind(&Impl::pushLastMessage, impl_.get(), boost::placeholders::_1);
169  }
170 
171  private:
172 
173  Publisher(const std::string& topic, const std::string& md5sum,
174  const std::string& datatype, bool latch, const NodeHandle& node_handle,
175  const SubscriberCallbacksPtr& callbacks);
176 
177  void publish(const boost::function<SerializedMessage(void)>& serfunc, SerializedMessage& m) const;
178  void incrementSequence() const;
179 
180  class ROSCPP_DECL Impl
181  {
182  public:
183  Impl();
184  ~Impl();
185 
186  void unadvertise();
187  bool isValid() const;
188  void pushLastMessage(const SubscriberLinkPtr &sub_link);
189 
190  std::string topic_;
191  std::string md5sum_;
192  std::string datatype_;
196  bool latch_;
198  boost::mutex last_message_mutex_;
199  };
201  typedef boost::weak_ptr<Impl> ImplWPtr;
202 
204 
205  friend class NodeHandle;
207  };
208 
209  typedef std::vector<Publisher> V_Publisher;
210 }
211 
212 #endif // ROSCPP_PUBLISHER_HANDLE_H
213 
ros::SerializedMessage
ros::Publisher::ImplPtr
boost::shared_ptr< Impl > ImplPtr
Definition: publisher.h:200
ros::Publisher::Impl
Definition: publisher.h:180
boost::shared_ptr< M >
forwards.h
ros
ros::Publisher::Impl::callbacks_
SubscriberCallbacksPtr callbacks_
Definition: publisher.h:194
ros::V_Publisher
std::vector< Publisher > V_Publisher
Definition: publisher.h:209
ros::Publisher::Impl::latch_
bool latch_
Definition: publisher.h:196
ros::Publisher::Impl::node_handle_
NodeHandlePtr node_handle_
Definition: publisher.h:193
ros::SerializedMessage::message
boost::shared_ptr< void const > message
ros::Publisher::ImplWPtr
boost::weak_ptr< Impl > ImplWPtr
Definition: publisher.h:201
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:203
ros::Publisher::Impl::last_message_
SerializedMessage last_message_
Definition: publisher.h:197
message.h
serialization.h
ROS_ASSERT_MSG
#define ROS_ASSERT_MSG(cond,...)
ros::Publisher::operator!=
bool operator!=(const Publisher &rhs) const
Definition: publisher.h:162
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::Publisher::Impl::datatype_
std::string datatype_
Definition: publisher.h:192
ros::Publisher::Publisher
Publisher()
Definition: publisher.h:51
ros::Publisher::Impl::unadvertised_
bool unadvertised_
Definition: publisher.h:195
ros::Publisher::Impl::last_message_mutex_
boost::mutex last_message_mutex_
Definition: publisher.h:198
ros::Publisher::getLastMessageCallback
boost::function< void(const SubscriberLinkPtr &)> getLastMessageCallback()
Definition: publisher.h:167
ros::Publisher::operator==
bool operator==(const Publisher &rhs) const
Definition: publisher.h:157
ros::Publisher::publish
void publish(const M &message) const
Publish a message on the topic associated with this Publisher.
Definition: publisher.h:97
ros::Publisher::operator<
bool operator<(const Publisher &rhs) const
Definition: publisher.h:152
ros::Publisher::Impl::topic_
std::string topic_
Definition: publisher.h:190
ros::Publisher::Impl::md5sum_
std::string md5sum_
Definition: publisher.h:191


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas
autogenerated on Sat Oct 17 2020 19:28:47