All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 
33 namespace gazebo
34 {
35  namespace transport
36  {
39 
43  {
47  public: CallbackHelper(bool _latching = false);
48 
50  public: virtual ~CallbackHelper();
51 
54  public: virtual std::string GetMsgType() const;
55 
62  public: virtual bool HandleData(const std::string &_newdata,
63  boost::function<void(uint32_t)> _cb, uint32_t _id) = 0;
64 
68  public: virtual bool HandleMessage(MessagePtr _newMsg) = 0;
69 
73  public: virtual bool IsLocal() const = 0;
74 
77  public: bool GetLatching() const;
78 
81  public: unsigned int GetId() const;
82 
85  protected: bool latching;
86 
88  private: static unsigned int idCounter;
89 
91  private: unsigned int id;
92  };
93 
95  typedef boost::shared_ptr<CallbackHelper> CallbackHelperPtr;
96 
97 
100  template<class M>
102  {
107  public: CallbackHelperT(const boost::function<
108  void (const boost::shared_ptr<M const> &)> &_cb,
109  bool _latching = false)
110  : CallbackHelper(_latching), callback(_cb)
111  {
112  // Just some code to make sure we have a google protobuf.
113  /*M test;
114  google::protobuf::Message *m;
115  if ((m =dynamic_cast<google::protobuf::Message*>(&test))
116  == NULL)
117  gzthrow("Message type must be a google::protobuf type\n");
118  */
119  }
120 
121  // documentation inherited
122  public: std::string GetMsgType() const
123  {
124  M test;
125  google::protobuf::Message *m;
126  if ((m = dynamic_cast<google::protobuf::Message*>(&test))
127  == NULL)
128  gzthrow("Message type must be a google::protobuf type\n");
129  return m->GetTypeName();
130  }
131 
132  // documentation inherited
133  public: virtual bool HandleData(const std::string &_newdata,
134  boost::function<void(uint32_t)> _cb, uint32_t _id)
135  {
136  boost::shared_ptr<M> m(new M);
137  m->ParseFromString(_newdata);
138  this->callback(m);
139  if (!_cb.empty())
140  _cb(_id);
141  return true;
142  }
143 
144  // documentation inherited
145  public: virtual bool HandleMessage(MessagePtr _newMsg)
146  {
147  this->callback(boost::dynamic_pointer_cast<M>(_newMsg));
148  return true;
149  }
150 
151  // documentation inherited
152  public: virtual bool IsLocal() const
153  {
154  return true;
155  }
156 
157  private: boost::function<void (const boost::shared_ptr<M const> &)>
158  callback;
159  };
160 
166  {
172  const boost::function<void (const std::string &)> &_cb,
173  bool _latching = false)
174  : CallbackHelper(_latching), callback(_cb)
175  {
176  }
177 
178  // documentation inherited
179  public: std::string GetMsgType() const
180  {
181  return "raw";
182  }
183 
184  // documentation inherited
185  public: virtual bool HandleData(const std::string &_newdata,
186  boost::function<void(uint32_t)> _cb, uint32_t _id)
187  {
188  this->callback(_newdata);
189  if (!_cb.empty())
190  _cb(_id);
191  return true;
192  }
193 
194  // documentation inherited
195  public: virtual bool HandleMessage(MessagePtr _newMsg)
196  {
197  std::string data;
198  _newMsg->SerializeToString(&data);
199  this->callback(data);
200  return true;
201  }
202 
203 
204  // documentation inherited
205  public: virtual bool IsLocal() const
206  {
207  return true;
208  }
209 
210  private: boost::function<void (const std::string &)> callback;
211  };
213  }
214 }
215 #endif