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  if (impl_->md5sum_ == "*" ||
80  std::string(mt::md5sum<M>(*message)) == "*" ||
81  impl_->md5sum_ == mt::md5sum<M>(*message)) {
82  ROS_DEBUG_ONCE("Trying to publish message of type [%s/%s] on a "
83  "publisher with type [%s/%s]",
84  mt::datatype<M>(*message), mt::md5sum<M>(*message),
85  impl_->datatype_.c_str(), impl_->md5sum_.c_str());
86  }
87 
89  m.type_info = &typeid(M);
90  m.message = message;
91 
92  publish(boost::bind(serializeMessage<M>, boost::ref(*message)), m);
93  }
94 
98  template <typename M>
99  void publish(const M& message) const
100  {
101  using namespace serialization;
102  namespace mt = ros::message_traits;
103 
104  if (!impl_)
105  {
106  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher");
107  return;
108  }
109 
110  if (!impl_->isValid())
111  {
112  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher (topic [%s])", impl_->topic_.c_str());
113  return;
114  }
115  if (impl_->md5sum_ == "*" ||
116  std::string(mt::md5sum<M>(message)) == "*" ||
117  impl_->md5sum_ == mt::md5sum<M>(message)) {
118  ROS_DEBUG_ONCE("Trying to publish message of type [%s/%s] on a "
119  "publisher with type [%s/%s]",
120  mt::datatype<M>(message), mt::md5sum<M>(message),
121  impl_->datatype_.c_str(), impl_->md5sum_.c_str());
122  }
123 
125  publish(boost::bind(serializeMessage<M>, boost::ref(message)), m);
126  }
127 
138  void shutdown();
139 
143  std::string getTopic() const;
144 
148  uint32_t getNumSubscribers() const;
149 
153  bool isLatched() const;
154 
155  operator void*() const { return (impl_ && impl_->isValid()) ? (void*)1 : (void*)0; }
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  bool operator!=(const Publisher& rhs) const
168  {
169  return impl_ != rhs.impl_;
170  }
171 
172  private:
173 
174  Publisher(const std::string& topic, const std::string& md5sum,
175  const std::string& datatype, const NodeHandle& node_handle,
176  const SubscriberCallbacksPtr& callbacks);
177 
178  void publish(const boost::function<SerializedMessage(void)>& serfunc, SerializedMessage& m) const;
179  void incrementSequence() const;
180 
181  class ROSCPP_DECL Impl
182  {
183  public:
184  Impl();
185  ~Impl();
186 
187  void unadvertise();
188  bool isValid() const;
189 
190  std::string topic_;
191  std::string md5sum_;
192  std::string datatype_;
196  };
198  typedef boost::weak_ptr<Impl> ImplWPtr;
199 
200  ImplPtr impl_;
201 
202  friend class NodeHandle;
204  };
205 
206  typedef std::vector<Publisher> V_Publisher;
207 }
208 
209 #endif // ROSCPP_PUBLISHER_HANDLE_H
210 
bool operator<(const Publisher &rhs) const
Definition: publisher.h:157
boost::weak_ptr< Impl > ImplWPtr
Definition: publisher.h:198
NodeHandlePtr node_handle_
Definition: publisher.h:193
std::string topic_
Definition: publisher.h:190
void publish(const M &message) const
Publish a message on the topic associated with this Publisher.
Definition: publisher.h:99
#define ROS_DEBUG_ONCE(...)
bool operator!=(const Publisher &rhs) const
Definition: publisher.h:167
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:200
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:192
std::string md5sum_
Definition: publisher.h:191
Manages an advertisement on a specific topic.
Definition: publisher.h:47
bool operator==(const Publisher &rhs) const
Definition: publisher.h:162
SubscriberCallbacksPtr callbacks_
Definition: publisher.h:194
boost::shared_ptr< Impl > ImplPtr
Definition: publisher.h:197
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:206


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas
autogenerated on Mon Feb 28 2022 23:33:27