message_event.h
Go to the documentation of this file.
1 
2 /*
3  * Copyright (C) 2010, Willow Garage, Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef ROSCPP_MESSAGE_EVENT_H
30 #define ROSCPP_MESSAGE_EVENT_H
31 
32 #include "ros/time.h"
33 #include <ros/datatypes.h>
34 #include <ros/message_traits.h>
35 
36 #include <boost/type_traits/is_void.hpp>
37 #include <boost/type_traits/is_base_of.hpp>
38 #include <boost/type_traits/is_const.hpp>
39 #include <boost/type_traits/add_const.hpp>
40 #include <boost/type_traits/remove_const.hpp>
41 #include <boost/utility/enable_if.hpp>
42 #include <boost/function.hpp>
43 #include <boost/make_shared.hpp>
44 
45 namespace ros
46 {
47 
48 template<typename M>
50 {
52  {
53  return boost::make_shared<M>();
54  }
55 };
56 
57 template<typename M>
59 {
60  return DefaultMessageCreator<M>()();
61 }
62 
68 template<typename M>
70 {
71 public:
72  typedef typename boost::add_const<M>::type ConstMessage;
73  typedef typename boost::remove_const<M>::type Message;
76  typedef boost::function<MessagePtr()> CreateFunction;
77 
79  : nonconst_need_copy_(true)
80  {}
81 
83  {
84  *this = rhs;
85  }
86 
88  {
89  *this = rhs;
90  }
91 
92  MessageEvent(const MessageEvent<Message>& rhs, bool nonconst_need_copy)
93  {
94  *this = rhs;
95  nonconst_need_copy_ = nonconst_need_copy;
96  }
97 
98  MessageEvent(const MessageEvent<ConstMessage>& rhs, bool nonconst_need_copy)
99  {
100  *this = rhs;
101  nonconst_need_copy_ = nonconst_need_copy;
102  }
103 
105  {
106  init(boost::const_pointer_cast<Message>(boost::static_pointer_cast<ConstMessage>(rhs.getMessage())), rhs.getConnectionHeaderPtr(), rhs.getReceiptTime(), rhs.nonConstWillCopy(), create);
107  }
108 
113  {
115  }
116 
117  MessageEvent(const ConstMessagePtr& message, const boost::shared_ptr<M_string>& connection_header, ros::Time receipt_time)
118  {
119  init(message, connection_header, receipt_time, true, ros::DefaultMessageCreator<Message>());
120  }
121 
122  MessageEvent(const ConstMessagePtr& message, ros::Time receipt_time)
123  {
124  init(message, boost::shared_ptr<M_string>(), receipt_time, true, ros::DefaultMessageCreator<Message>());
125  }
126 
127  MessageEvent(const ConstMessagePtr& message, const boost::shared_ptr<M_string>& connection_header, ros::Time receipt_time, bool nonconst_need_copy, const CreateFunction& create)
128  {
129  init(message, connection_header, receipt_time, nonconst_need_copy, create);
130  }
131 
132  void init(const ConstMessagePtr& message, const boost::shared_ptr<M_string>& connection_header, ros::Time receipt_time, bool nonconst_need_copy, const CreateFunction& create)
133  {
134  message_ = message;
135  connection_header_ = connection_header;
136  receipt_time_ = receipt_time;
137  nonconst_need_copy_ = nonconst_need_copy;
138  create_ = create;
139  }
140 
142  {
143  init(boost::static_pointer_cast<Message>(rhs.getMessage()), rhs.getConnectionHeaderPtr(), rhs.getReceiptTime(), rhs.nonConstWillCopy(), rhs.getMessageFactory());
144  message_copy_.reset();
145  }
146 
148  {
149  init(boost::const_pointer_cast<Message>(boost::static_pointer_cast<ConstMessage>(rhs.getMessage())), rhs.getConnectionHeaderPtr(), rhs.getReceiptTime(), rhs.nonConstWillCopy(), rhs.getMessageFactory());
150  message_copy_.reset();
151  }
152 
159  {
160  return copyMessageIfNecessary<M>();
161  }
162 
172 
176  const std::string& getPublisherName() const { return connection_header_ ? (*connection_header_)["callerid"] : s_unknown_publisher_string_; }
177 
182 
183  bool nonConstWillCopy() const { return nonconst_need_copy_; }
184  bool getMessageWillCopy() const { return !boost::is_const<M>::value && nonconst_need_copy_; }
185 
186  bool operator<(const MessageEvent<M>& rhs)
187  {
188  if (message_ != rhs.message_)
189  {
190  return message_ < rhs.message_;
191  }
192 
193  if (receipt_time_ != rhs.receipt_time_)
194  {
195  return receipt_time_ < rhs.receipt_time_;
196  }
197 
199  }
200 
201  bool operator==(const MessageEvent<M>& rhs)
202  {
204  }
205 
206  bool operator!=(const MessageEvent<M>& rhs)
207  {
208  return !(*this == rhs);
209  }
210 
211  const CreateFunction& getMessageFactory() const { return create_; }
212 
213 private:
214  template<typename M2>
215  typename boost::disable_if<boost::is_void<M2>, boost::shared_ptr<M> >::type copyMessageIfNecessary() const
216  {
217  if (boost::is_const<M>::value || !nonconst_need_copy_)
218  {
219  return boost::const_pointer_cast<Message>(message_);
220  }
221 
222  if (message_copy_)
223  {
224  return message_copy_;
225  }
226 
227  assert(create_);
230 
231  return message_copy_;
232  }
233 
234  template<typename M2>
235  typename boost::enable_if<boost::is_void<M2>, boost::shared_ptr<M> >::type copyMessageIfNecessary() const
236  {
237  return boost::const_pointer_cast<Message>(message_);
238  }
239 
241  // Kind of ugly to make this mutable, but it means we can pass a const MessageEvent to a callback and not worry about other things being modified
247 
248  static const std::string s_unknown_publisher_string_;
249 };
250 
251 template<typename M> const std::string MessageEvent<M>::s_unknown_publisher_string_("unknown_publisher");
252 
253 }
254 
255 #endif // ROSCPP_MESSAGE_EVENT_H
ros::MessageEvent::MessageEvent
MessageEvent(const MessageEvent< Message > &rhs)
Definition: message_event.h:82
datatypes.h
ros::MessageEvent::getReceiptTime
ros::Time getReceiptTime() const
Returns the time at which this message was received.
Definition: message_event.h:181
boost::shared_ptr
Definition: message_forward.h:48
ros::MessageEvent::nonconst_need_copy_
bool nonconst_need_copy_
Definition: message_event.h:245
ros
time.h
ros::MessageEvent::MessageEvent
MessageEvent(const ConstMessagePtr &message, ros::Time receipt_time)
Definition: message_event.h:122
ros::MessageEvent::connection_header_
boost::shared_ptr< M_string > connection_header_
Definition: message_event.h:243
ros::MessageEvent::getPublisherName
const std::string & getPublisherName() const
Returns the name of the node which published this message.
Definition: message_event.h:176
ros::MessageEvent::s_unknown_publisher_string_
static const std::string s_unknown_publisher_string_
Definition: message_event.h:248
ros::MessageEvent::ConstMessagePtr
boost::shared_ptr< ConstMessage > ConstMessagePtr
Definition: message_event.h:75
ros::MessageEvent::CreateFunction
boost::function< MessagePtr()> CreateFunction
Definition: message_event.h:76
ros::MessageEvent::getConstMessage
const boost::shared_ptr< ConstMessage > & getConstMessage() const
Retrieve a const version of the message.
Definition: message_event.h:166
ROS_DEPRECATED
#define ROS_DEPRECATED
ros::MessageEvent::message_
ConstMessagePtr message_
Definition: message_event.h:240
ros::MessageEvent::MessageEvent
MessageEvent(const MessageEvent< ConstMessage > &rhs)
Definition: message_event.h:87
ros::MessageEvent::getMessageWillCopy
bool getMessageWillCopy() const
Definition: message_event.h:184
ros::MessageEvent::MessageEvent
MessageEvent(const ConstMessagePtr &message, const boost::shared_ptr< M_string > &connection_header, ros::Time receipt_time, bool nonconst_need_copy, const CreateFunction &create)
Definition: message_event.h:127
ros::DefaultMessageCreator
Definition: message_event.h:49
ros::MessageEvent::MessagePtr
boost::shared_ptr< Message > MessagePtr
Definition: message_event.h:74
ros::MessageEvent::MessageEvent
MessageEvent()
Definition: message_event.h:78
ros::MessageEvent::MessageEvent
MessageEvent(const ConstMessagePtr &message)
Definition: message_event.h:112
ros::DefaultMessageCreator::operator()
boost::shared_ptr< M > operator()()
Definition: message_event.h:51
message_traits.h
ros::MessageEvent::create_
CreateFunction create_
Definition: message_event.h:246
ros::MessageEvent::Message
boost::remove_const< M >::type Message
Definition: message_event.h:73
ros::MessageEvent::operator<
bool operator<(const MessageEvent< M > &rhs)
Definition: message_event.h:186
ros::MessageEvent::operator=
void operator=(const MessageEvent< ConstMessage > &rhs)
Definition: message_event.h:147
ros::MessageEvent::message_copy_
MessagePtr message_copy_
Definition: message_event.h:242
ros::MessageEvent::receipt_time_
ros::Time receipt_time_
Definition: message_event.h:244
ros::MessageEvent::copyMessageIfNecessary
boost::disable_if< boost::is_void< M2 >, boost::shared_ptr< M > >::type copyMessageIfNecessary() const
Definition: message_event.h:215
ros::MessageEvent::operator==
bool operator==(const MessageEvent< M > &rhs)
Definition: message_event.h:201
ros::MessageEvent::ConstMessage
boost::add_const< M >::type ConstMessage
Definition: message_event.h:72
ros::Time
ros::MessageEvent::MessageEvent
MessageEvent(const MessageEvent< void const > &rhs, const CreateFunction &create)
Definition: message_event.h:104
ros::MessageEvent::MessageEvent
MessageEvent(const MessageEvent< Message > &rhs, bool nonconst_need_copy)
Definition: message_event.h:92
ros::MessageEvent::copyMessageIfNecessary
boost::enable_if< boost::is_void< M2 >, boost::shared_ptr< M > >::type copyMessageIfNecessary() const
Definition: message_event.h:235
ros::MessageEvent::getConnectionHeaderPtr
const boost::shared_ptr< M_string > & getConnectionHeaderPtr() const
Definition: message_event.h:171
ros::MessageEvent::getMessageFactory
const CreateFunction & getMessageFactory() const
Definition: message_event.h:211
ros::MessageEvent::getMessage
boost::shared_ptr< M > getMessage() const
Retrieve the message. If M is const, this returns a reference to it. If M is non const and this event...
Definition: message_event.h:158
ros::MessageEvent::getConnectionHeader
M_string & getConnectionHeader() const
Retrieve the connection header.
Definition: message_event.h:170
ros::MessageEvent::MessageEvent
MessageEvent(const ConstMessagePtr &message, const boost::shared_ptr< M_string > &connection_header, ros::Time receipt_time)
Definition: message_event.h:117
ros::defaultMessageCreateFunction
ROS_DEPRECATED boost::shared_ptr< M > defaultMessageCreateFunction()
Definition: message_event.h:58
ros::MessageEvent::init
void init(const ConstMessagePtr &message, const boost::shared_ptr< M_string > &connection_header, ros::Time receipt_time, bool nonconst_need_copy, const CreateFunction &create)
Definition: message_event.h:132
ros::MessageEvent::operator!=
bool operator!=(const MessageEvent< M > &rhs)
Definition: message_event.h:206
ros::MessageEvent
Event type for subscriptions, const ros::MessageEvent<M const>& can be used in your callback instead ...
Definition: message_event.h:69
ros::MessageEvent::nonConstWillCopy
bool nonConstWillCopy() const
Definition: message_event.h:183
ros::M_string
std::map< std::string, std::string > M_string
ros::Time::now
static Time now()
ros::MessageEvent::operator=
void operator=(const MessageEvent< Message > &rhs)
Definition: message_event.h:141
ros::MessageEvent::MessageEvent
MessageEvent(const MessageEvent< ConstMessage > &rhs, bool nonconst_need_copy)
Definition: message_event.h:98


roscpp_traits
Author(s): Josh Faust, Dirk Thomas
autogenerated on Sat Jun 17 2023 02:32:39