CallbackHelper.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2016 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef _CALLBACKHELPER_HH_
18 #define _CALLBACKHELPER_HH_
19 
20 #include <google/protobuf/message.h>
21 #include <boost/function.hpp>
22 #include <boost/shared_ptr.hpp>
23 
24 #include <vector>
25 #include <string>
26 #include <mutex>
27 
28 #include "gazebo/common/Console.hh"
29 #include "gazebo/msgs/msgs.hh"
31 
33 #include "gazebo/util/system.hh"
34 
35 namespace gazebo
36 {
37  namespace transport
38  {
41 
45  {
49  public: CallbackHelper(bool _latching = false);
50 
52  public: virtual ~CallbackHelper();
53 
56  public: virtual std::string GetMsgType() const;
57 
64  public: virtual bool HandleData(const std::string &_newdata,
65  boost::function<void(uint32_t)> _cb, uint32_t _id) = 0;
66 
70  public: virtual bool HandleMessage(MessagePtr _newMsg) = 0;
71 
75  public: virtual bool IsLocal() const = 0;
76 
79  public: bool GetLatching() const;
80 
84  public: void SetLatching(bool _latch);
85 
88  public: unsigned int GetId() const;
89 
92  protected: bool latching;
93 
95  protected: mutable std::mutex latchingMutex;
96 
98  private: static unsigned int idCounter;
99 
101  private: unsigned int id;
102  };
103 
105  typedef boost::shared_ptr<CallbackHelper> CallbackHelperPtr;
106 
107 
110  template<class M>
112  {
117  public: CallbackHelperT(const boost::function<
118  void (const boost::shared_ptr<M const> &)> &_cb,
119  bool _latching = false)
120  : CallbackHelper(_latching), callback(_cb)
121  {
122  // Just some code to make sure we have a google protobuf.
123  /*M test;
124  google::protobuf::Message *m;
125  if ((m =dynamic_cast<google::protobuf::Message*>(&test))
126  == NULL)
127  gzthrow("Message type must be a google::protobuf type\n");
128  */
129  }
130 
131  // documentation inherited
132  public: std::string GetMsgType() const
133  {
134  M test;
135  google::protobuf::Message *m;
136  if ((m = dynamic_cast<google::protobuf::Message*>(&test))
137  == NULL)
138  gzthrow("Message type must be a google::protobuf type\n");
139  return m->GetTypeName();
140  }
141 
142  // documentation inherited
143  public: virtual bool HandleData(const std::string &_newdata,
144  boost::function<void(uint32_t)> _cb, uint32_t _id)
145  {
146  this->SetLatching(false);
147  boost::shared_ptr<M> m(new M);
148  m->ParseFromString(_newdata);
149  this->callback(m);
150  if (!_cb.empty())
151  _cb(_id);
152  return true;
153  }
154 
155  // documentation inherited
156  public: virtual bool HandleMessage(MessagePtr _newMsg)
157  {
158  this->SetLatching(false);
159  this->callback(boost::dynamic_pointer_cast<M>(_newMsg));
160  return true;
161  }
162 
163  // documentation inherited
164  public: virtual bool IsLocal() const
165  {
166  return true;
167  }
168 
169  private: boost::function<void (const boost::shared_ptr<M const> &)>
170  callback;
171  };
172 
178  {
184  const boost::function<void (const std::string &)> &_cb,
185  bool _latching = false)
186  : CallbackHelper(_latching), callback(_cb)
187  {
188  }
189 
190  // documentation inherited
191  public: std::string GetMsgType() const
192  {
193  return "raw";
194  }
195 
196  // documentation inherited
197  public: virtual bool HandleData(const std::string &_newdata,
198  boost::function<void(uint32_t)> _cb, uint32_t _id)
199  {
200  this->SetLatching(false);
201  this->callback(_newdata);
202  if (!_cb.empty())
203  _cb(_id);
204  return true;
205  }
206 
207  // documentation inherited
208  public: virtual bool HandleMessage(MessagePtr _newMsg)
209  {
210  this->SetLatching(false);
211  std::string data;
212  _newMsg->SerializeToString(&data);
213  this->callback(data);
214  return true;
215  }
216 
217 
218  // documentation inherited
219  public: virtual bool IsLocal() const
220  {
221  return true;
222  }
223 
224  private: boost::function<void (const std::string &)> callback;
225  };
227  }
228 }
229 #endif
std::string GetMsgType() const
Get the typename of the message that is handled.
Definition: CallbackHelper.hh:191
virtual bool HandleData(const std::string &_newdata, boost::function< void(uint32_t)> _cb, uint32_t _id)
Process new incoming data.
Definition: CallbackHelper.hh:197
void SetLatching(bool _latch)
Set whether this callback is latching.
virtual bool IsLocal() const
Is the callback local?
Definition: CallbackHelper.hh:164
virtual bool HandleMessage(MessagePtr _newMsg)
Process new incoming message.
Definition: CallbackHelper.hh:156
A helper class to handle callbacks when messages arrive.
Definition: CallbackHelper.hh:44
#define gzthrow(msg)
This macro logs an error to the throw stream and throws an exception that contains the file name and ...
Definition: Exception.hh:39
Forward declarations for transport.
Callback helper Template.
Definition: CallbackHelper.hh:111
virtual bool IsLocal() const
Is the callback local?
Definition: CallbackHelper.hh:219
std::string GetMsgType() const
Get the typename of the message that is handled.
Definition: CallbackHelper.hh:132
std::mutex latchingMutex
Mutex to protect the latching variable.
Definition: CallbackHelper.hh:95
CallbackHelperT(const boost::function< void(const boost::shared_ptr< M const > &)> &_cb, bool _latching=false)
Constructor.
Definition: CallbackHelper.hh:117
boost::shared_ptr< google::protobuf::Message > MessagePtr
Definition: TransportTypes.hh:45
#define NULL
Definition: CommonTypes.hh:31
RawCallbackHelper(const boost::function< void(const std::string &)> &_cb, bool _latching=false)
Constructor.
Definition: CallbackHelper.hh:183
virtual bool HandleMessage(MessagePtr _newMsg)
Process new incoming message.
Definition: CallbackHelper.hh:208
#define GZ_TRANSPORT_VISIBLE
Definition: system.hh:159
virtual bool HandleData(const std::string &_newdata, boost::function< void(uint32_t)> _cb, uint32_t _id)
Process new incoming data.
Definition: CallbackHelper.hh:143
Used to connect publishers to subscribers, where the subscriber wants the raw data from the publisher...
Definition: CallbackHelper.hh:177
boost::shared_ptr< CallbackHelper > CallbackHelperPtr
boost shared pointer to transport::CallbackHelper
Definition: CallbackHelper.hh:105
bool latching
True means that the callback helper will get the last published message on the topic.
Definition: CallbackHelper.hh:92