All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CallbackHelper.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 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 
27 #include "gazebo/common/Console.hh"
28 #include "gazebo/msgs/msgs.hh"
30 
32 #include "gazebo/util/system.hh"
33 
34 namespace gazebo
35 {
36  namespace transport
37  {
40 
44  {
48  public: CallbackHelper(bool _latching = false);
49 
51  public: virtual ~CallbackHelper();
52 
55  public: virtual std::string GetMsgType() const;
56 
63  public: virtual bool HandleData(const std::string &_newdata,
64  boost::function<void(uint32_t)> _cb, uint32_t _id) = 0;
65 
69  public: virtual bool HandleMessage(MessagePtr _newMsg) = 0;
70 
74  public: virtual bool IsLocal() const = 0;
75 
78  public: bool GetLatching() const;
79 
83  public: void SetLatching(bool _latch);
84 
87  public: unsigned int GetId() const;
88 
91  protected: bool latching;
92 
94  private: static unsigned int idCounter;
95 
97  private: unsigned int id;
98  };
99 
101  typedef boost::shared_ptr<CallbackHelper> CallbackHelperPtr;
102 
103 
106  template<class M>
108  {
113  public: CallbackHelperT(const boost::function<
114  void (const boost::shared_ptr<M const> &)> &_cb,
115  bool _latching = false)
116  : CallbackHelper(_latching), callback(_cb)
117  {
118  // Just some code to make sure we have a google protobuf.
119  /*M test;
120  google::protobuf::Message *m;
121  if ((m =dynamic_cast<google::protobuf::Message*>(&test))
122  == NULL)
123  gzthrow("Message type must be a google::protobuf type\n");
124  */
125  }
126 
127  // documentation inherited
128  public: std::string GetMsgType() const
129  {
130  M test;
131  google::protobuf::Message *m;
132  if ((m = dynamic_cast<google::protobuf::Message*>(&test))
133  == NULL)
134  gzthrow("Message type must be a google::protobuf type\n");
135  return m->GetTypeName();
136  }
137 
138  // documentation inherited
139  public: virtual bool HandleData(const std::string &_newdata,
140  boost::function<void(uint32_t)> _cb, uint32_t _id)
141  {
142  boost::shared_ptr<M> m(new M);
143  m->ParseFromString(_newdata);
144  this->callback(m);
145  if (!_cb.empty())
146  _cb(_id);
147  return true;
148  }
149 
150  // documentation inherited
151  public: virtual bool HandleMessage(MessagePtr _newMsg)
152  {
153  this->callback(boost::dynamic_pointer_cast<M>(_newMsg));
154  return true;
155  }
156 
157  // documentation inherited
158  public: virtual bool IsLocal() const
159  {
160  return true;
161  }
162 
163  private: boost::function<void (const boost::shared_ptr<M const> &)>
164  callback;
165  };
166 
172  {
178  const boost::function<void (const std::string &)> &_cb,
179  bool _latching = false)
180  : CallbackHelper(_latching), callback(_cb)
181  {
182  }
183 
184  // documentation inherited
185  public: std::string GetMsgType() const
186  {
187  return "raw";
188  }
189 
190  // documentation inherited
191  public: virtual bool HandleData(const std::string &_newdata,
192  boost::function<void(uint32_t)> _cb, uint32_t _id)
193  {
194  this->callback(_newdata);
195  if (!_cb.empty())
196  _cb(_id);
197  return true;
198  }
199 
200  // documentation inherited
201  public: virtual bool HandleMessage(MessagePtr _newMsg)
202  {
203  std::string data;
204  _newMsg->SerializeToString(&data);
205  this->callback(data);
206  return true;
207  }
208 
209 
210  // documentation inherited
211  public: virtual bool IsLocal() const
212  {
213  return true;
214  }
215 
216  private: boost::function<void (const std::string &)> callback;
217  };
219  }
220 }
221 #endif
std::string GetMsgType() const
Get the typename of the message that is handled.
Definition: CallbackHelper.hh:185
virtual bool HandleData(const std::string &_newdata, boost::function< void(uint32_t)> _cb, uint32_t _id)
Process new incoming data.
Definition: CallbackHelper.hh:191
virtual bool IsLocal() const
Is the callback local?
Definition: CallbackHelper.hh:158
virtual bool HandleMessage(MessagePtr _newMsg)
Process new incoming message.
Definition: CallbackHelper.hh:151
A helper class to handle callbacks when messages arrive.
Definition: CallbackHelper.hh:43
#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:107
virtual bool IsLocal() const
Is the callback local?
Definition: CallbackHelper.hh:211
std::string GetMsgType() const
Get the typename of the message that is handled.
Definition: CallbackHelper.hh:128
CallbackHelperT(const boost::function< void(const boost::shared_ptr< M const > &)> &_cb, bool _latching=false)
Constructor.
Definition: CallbackHelper.hh:113
boost::shared_ptr< google::protobuf::Message > MessagePtr
Definition: TransportTypes.hh:40
#define NULL
Definition: CommonTypes.hh:30
RawCallbackHelper(const boost::function< void(const std::string &)> &_cb, bool _latching=false)
Constructor.
Definition: CallbackHelper.hh:177
virtual bool HandleMessage(MessagePtr _newMsg)
Process new incoming message.
Definition: CallbackHelper.hh:201
virtual bool HandleData(const std::string &_newdata, boost::function< void(uint32_t)> _cb, uint32_t _id)
Process new incoming data.
Definition: CallbackHelper.hh:139
Used to connect publishers to subscribers, where the subscriber wants the raw data from the publisher...
Definition: CallbackHelper.hh:171
boost::shared_ptr< CallbackHelper > CallbackHelperPtr
boost shared pointer to transport::CallbackHelper
Definition: CallbackHelper.hh:101
#define GAZEBO_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system.hh:48
bool latching
True means that the callback helper will get the last published message on the topic.
Definition: CallbackHelper.hh:91