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


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Sun Feb 3 2019 03:29:54