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 
42 #include "boost/function.hpp"
43 
44 
45 namespace rs2rosinternal
46 {
47 
48 template<typename M>
50 {
51  std::shared_ptr<M> operator()()
52  {
53  return std::make_shared<M>();
54  }
55 };
56 
57 template<typename M>
58 ROS_DEPRECATED inline std::shared_ptr<M> defaultMessageCreateFunction()
59 {
60  return DefaultMessageCreator<M>()();
61 }
62 
68 template<typename M>
70 {
71 public:
74  typedef std::shared_ptr<Message> MessagePtr;
75  typedef std::shared_ptr<ConstMessage> ConstMessagePtr;
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 
104  MessageEvent(const MessageEvent<void const>& rhs, const CreateFunction& create)
105  {
106  init(std::const_pointer_cast<Message>(std::static_pointer_cast<ConstMessage>(rhs.getMessage())), rhs.getConnectionHeaderPtr(), rhs.getReceiptTime(), rhs.nonConstWillCopy(), create);
107  }
108 
112  MessageEvent(const ConstMessagePtr& message)
113  {
114  init(message, std::shared_ptr<M_string>(), rs2rosinternal::Time::now(), true, rs2rosinternal::DefaultMessageCreator<Message>());
115  }
116 
117  MessageEvent(const ConstMessagePtr& message, const std::shared_ptr<M_string>& connection_header, rs2rosinternal::Time receipt_time)
118  {
119  init(message, connection_header, receipt_time, true, rs2rosinternal::DefaultMessageCreator<Message>());
120  }
121 
122  MessageEvent(const ConstMessagePtr& message, rs2rosinternal::Time receipt_time)
123  {
124  init(message, std::shared_ptr<M_string>(), receipt_time, true, rs2rosinternal::DefaultMessageCreator<Message>());
125  }
126 
127  MessageEvent(const ConstMessagePtr& message, const std::shared_ptr<M_string>& connection_header, rs2rosinternal::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 std::shared_ptr<M_string>& connection_header, rs2rosinternal::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(std::static_pointer_cast<Message>(rhs.getMessage()), rhs.getConnectionHeaderPtr(), rhs.getReceiptTime(), rhs.nonConstWillCopy(), rhs.getMessageFactory());
144  message_copy_.reset();
145  }
146 
148  {
149  init(std::const_pointer_cast<Message>(std::static_pointer_cast<ConstMessage>(rhs.getMessage())), rhs.getConnectionHeaderPtr(), rhs.getReceiptTime(), rhs.nonConstWillCopy(), rhs.getMessageFactory());
150  message_copy_.reset();
151  }
152 
158  std::shared_ptr<M> getMessage() const
159  {
160  return copyMessageIfNecessary<M>();
161  }
162 
166  const std::shared_ptr<ConstMessage>& getConstMessage() const { return message_; }
170  M_string& getConnectionHeader() const { return *connection_header_; }
171  const std::shared_ptr<M_string>& getConnectionHeaderPtr() const { return connection_header_; }
172 
176  const std::string& getPublisherName() const { return connection_header_ ? (*connection_header_)["callerid"] : s_unknown_publisher_string_; }
177 
181  rs2rosinternal::Time getReceiptTime() const { return receipt_time_; }
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 
198  return nonconst_need_copy_ < rhs.nonconst_need_copy_;
199  }
200 
201  bool operator==(const MessageEvent<M>& rhs)
202  {
203  return message_ = rhs.message_ && receipt_time_ == rhs.receipt_time_ && nonconst_need_copy_ == rhs.nonconst_need_copy_;
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>
216  {
217  if (boost::is_const<M>::value || !nonconst_need_copy_)
218  {
219  return std::const_pointer_cast<Message>(message_);
220  }
221 
222  if (message_copy_)
223  {
224  return message_copy_;
225  }
226 
227  assert(create_);
228  message_copy_ = create_();
229  *message_copy_ = *message_;
230 
231  return message_copy_;
232  }
233 
234  template<typename M2>
236  {
237  return std::const_pointer_cast<Message>(message_);
238  }
239 
240  ConstMessagePtr message_;
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
242  mutable MessagePtr message_copy_;
243  std::shared_ptr<M_string> connection_header_;
246  CreateFunction create_;
247 
249 };
250 
251 template<typename M> const std::string MessageEvent<M>::s_unknown_publisher_string_("unknown_publisher");
252 
253 
254 }
255 
256 #endif // ROSCPP_MESSAGE_EVENT_H
MessageEvent(const MessageEvent< Message > &rhs, bool nonconst_need_copy)
Definition: message_event.h:92
GLenum GLuint GLenum GLsizei const GLchar * message
MessageEvent(const ConstMessagePtr &message, rs2rosinternal::Time receipt_time)
MessageEvent(const MessageEvent< void const > &rhs, const CreateFunction &create)
rs2rosinternal::Time getReceiptTime() const
Returns the time at which this message was received.
std::remove_const< M >::type Message
Definition: message_event.h:73
#define ROS_DEPRECATED
boost::add_const< M >::type ConstMessage
Definition: message_event.h:72
GLsizei const GLchar *const * string
MessageEvent(const ConstMessagePtr &message)
Event type for subscriptions, const rs2rosinternal::MessageEvent<M const>& can be used in your callba...
Definition: message_event.h:69
ROS_DEPRECATED std::shared_ptr< M > defaultMessageCreateFunction()
Definition: message_event.h:58
void operator=(const MessageEvent< ConstMessage > &rhs)
boost::function< MessagePtr()> CreateFunction
Definition: message_event.h:76
boost::enable_if< boost::is_void< M2 >, std::shared_ptr< M > >::type copyMessageIfNecessary() const
std::shared_ptr< Message > MessagePtr
Definition: message_event.h:74
const std::string & getPublisherName() const
Returns the name of the node which published this message.
MessageEvent(const MessageEvent< Message > &rhs)
Definition: message_event.h:82
M_string & getConnectionHeader() const
Retrieve the connection header.
MessageEvent(const MessageEvent< ConstMessage > &rhs)
Definition: message_event.h:87
std::map< std::string, std::string > M_string
Definition: datatypes.h:45
Time representation. May either represent wall clock time or ROS clock time.
Definition: time.h:177
void init(void)
Definition: boing.c:180
const std::shared_ptr< ConstMessage > & getConstMessage() const
Retrieve a const version of the message.
std::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...
std::shared_ptr< ConstMessage > ConstMessagePtr
Definition: message_event.h:75
MessageEvent(const ConstMessagePtr &message, const std::shared_ptr< M_string > &connection_header, rs2rosinternal::Time receipt_time, bool nonconst_need_copy, const CreateFunction &create)
bool operator==(const MessageEvent< M > &rhs)
void init(const ConstMessagePtr &message, const std::shared_ptr< M_string > &connection_header, rs2rosinternal::Time receipt_time, bool nonconst_need_copy, const CreateFunction &create)
const CreateFunction & getMessageFactory() const
MessageEvent(const MessageEvent< ConstMessage > &rhs, bool nonconst_need_copy)
Definition: message_event.h:98
static const std::string s_unknown_publisher_string_
GLenum type
boost::disable_if< boost::is_void< M2 >, std::shared_ptr< M > >::type copyMessageIfNecessary() const
void operator=(const MessageEvent< Message > &rhs)
rs2rosinternal::Time receipt_time_
bool operator!=(const MessageEvent< M > &rhs)
static Time now()
Retrieve the current time. If ROS clock time is in use, this returns the time according to the ROS cl...
Definition: time.cpp:244
MessageEvent(const ConstMessagePtr &message, const std::shared_ptr< M_string > &connection_header, rs2rosinternal::Time receipt_time)
const std::shared_ptr< M_string > & getConnectionHeaderPtr() const
std::shared_ptr< M_string > connection_header_
std::shared_ptr< M > operator()()
Definition: message_event.h:51


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:21