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 
37 namespace ros
38 {
47  class ROSCPP_DECL Publisher
48  {
49  public:
50  Publisher() {}
51  Publisher(const Publisher& rhs);
52  ~Publisher();
53  Publisher& operator=(const Publisher& other) = default;
54 
63  template <typename M>
64  void publish(const boost::shared_ptr<M>& message) const
65  {
66  using namespace serialization;
67 
68  if (!impl_)
69  {
70  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher");
71  return;
72  }
73 
74  if (!impl_->isValid())
75  {
76  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher (topic [%s])", impl_->topic_.c_str());
77  return;
78  }
79 
80  ROS_ASSERT_MSG(impl_->md5sum_ == "*" || std::string(mt::md5sum<M>(*message)) == "*" || impl_->md5sum_ == mt::md5sum<M>(*message),
81  "Trying to publish message of type [%s/%s] on a publisher with type [%s/%s]",
82  mt::datatype<M>(*message), mt::md5sum<M>(*message),
83  impl_->datatype_.c_str(), impl_->md5sum_.c_str());
84 
86  m.type_info = &typeid(M);
87  m.message = message;
88 
89  publish(boost::bind(serializeMessage<M>, boost::ref(*message)), m);
90  }
91 
95  template <typename M>
96  void publish(const M& message) const
97  {
98  using namespace serialization;
99  namespace mt = ros::message_traits;
100 
101  if (!impl_)
102  {
103  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher");
104  return;
105  }
106 
107  if (!impl_->isValid())
108  {
109  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher (topic [%s])", impl_->topic_.c_str());
110  return;
111  }
112 
113  ROS_ASSERT_MSG(impl_->md5sum_ == "*" || std::string(mt::md5sum<M>(message)) == "*" || impl_->md5sum_ == mt::md5sum<M>(message),
114  "Trying to publish message of type [%s/%s] on a publisher with type [%s/%s]",
115  mt::datatype<M>(message), mt::md5sum<M>(message),
116  impl_->datatype_.c_str(), impl_->md5sum_.c_str());
117 
119  publish(boost::bind(serializeMessage<M>, boost::ref(message)), m);
120  }
121 
132  void shutdown();
133 
137  std::string getTopic() const;
138 
142  uint32_t getNumSubscribers() const;
143 
147  bool isLatched() const;
148 
149  operator void*() const { return (impl_ && impl_->isValid()) ? (void*)1 : (void*)0; }
150 
151  bool operator<(const Publisher& rhs) const
152  {
153  return impl_ < rhs.impl_;
154  }
155 
156  bool operator==(const Publisher& rhs) const
157  {
158  return impl_ == rhs.impl_;
159  }
160 
161  bool operator!=(const Publisher& rhs) const
162  {
163  return impl_ != rhs.impl_;
164  }
165 
166  private:
167 
168  Publisher(const std::string& topic, const std::string& md5sum,
169  const std::string& datatype, const NodeHandle& node_handle,
170  const SubscriberCallbacksPtr& callbacks);
171 
172  void publish(const boost::function<SerializedMessage(void)>& serfunc, SerializedMessage& m) const;
173  void incrementSequence() const;
174 
175  class ROSCPP_DECL Impl
176  {
177  public:
178  Impl();
179  ~Impl();
180 
181  void unadvertise();
182  bool isValid() const;
183 
184  std::string topic_;
185  std::string md5sum_;
186  std::string datatype_;
190  };
192  typedef boost::weak_ptr<Impl> ImplWPtr;
193 
194  ImplPtr impl_;
195 
196  friend class NodeHandle;
198  };
199 
200  typedef std::vector<Publisher> V_Publisher;
201 }
202 
203 #endif // ROSCPP_PUBLISHER_HANDLE_H
204 
bool operator<(const Publisher &rhs) const
Definition: publisher.h:151
boost::weak_ptr< Impl > ImplWPtr
Definition: publisher.h:192
NodeHandlePtr node_handle_
Definition: publisher.h:187
std::string topic_
Definition: publisher.h:184
void publish(const M &message) const
Publish a message on the topic associated with this Publisher.
Definition: publisher.h:96
bool operator!=(const Publisher &rhs) const
Definition: publisher.h:161
void publish(const boost::shared_ptr< M > &message) const
Publish a message on the topic associated with this Publisher.
Definition: publisher.h:64
const char * datatype()
ImplPtr impl_
Definition: publisher.h:194
const std::type_info * type_info
#define ROS_ASSERT_MSG(cond,...)
roscpp&#39;s interface for creating subscribers, publishers, etc.
Definition: node_handle.h:87
std::string datatype_
Definition: publisher.h:186
std::string md5sum_
Definition: publisher.h:185
Manages an advertisement on a specific topic.
Definition: publisher.h:47
bool operator==(const Publisher &rhs) const
Definition: publisher.h:156
SubscriberCallbacksPtr callbacks_
Definition: publisher.h:188
boost::shared_ptr< Impl > ImplPtr
Definition: publisher.h:191
ROSCPP_DECL void shutdown()
Disconnects everything and unregisters from the master. It is generally not necessary to call this fu...
Definition: init.cpp:594
const char * md5sum()
std::vector< Publisher > V_Publisher
Definition: publisher.h:200


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Sun Mar 22 2020 03:20:34