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 2012 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 
59  public: virtual bool HandleData(const std::string &_newdata) = 0;
60 
64  public: virtual bool HandleMessage(MessagePtr _newMsg) = 0;
65 
69  public: virtual bool IsLocal() const = 0;
70 
73  public: bool GetLatching() const;
74 
77  public: unsigned int GetId() const;
78 
81  protected: bool latching;
82 
84  private: static unsigned int idCounter;
85 
87  private: unsigned int id;
88  };
89 
91  typedef boost::shared_ptr<CallbackHelper> CallbackHelperPtr;
92 
93 
96  template<class M>
98  {
103  public: CallbackHelperT(const boost::function<
104  void (const boost::shared_ptr<M const> &)> &_cb,
105  bool _latching = false)
106  : CallbackHelper(_latching), callback(_cb)
107  {
108  // Just some code to make sure we have a google protobuf.
109  /*M test;
110  google::protobuf::Message *m;
111  if ((m =dynamic_cast<google::protobuf::Message*>(&test))
112  == NULL)
113  gzthrow("Message type must be a google::protobuf type\n");
114  */
115  }
116 
117  // documentation inherited
118  public: std::string GetMsgType() const
119  {
120  M test;
121  google::protobuf::Message *m;
122  if ((m = dynamic_cast<google::protobuf::Message*>(&test))
123  == NULL)
124  gzthrow("Message type must be a google::protobuf type\n");
125  return m->GetTypeName();
126  }
127 
128  // documentation inherited
129  public: virtual bool HandleData(const std::string &_newdata)
130  {
131  boost::shared_ptr<M> m(new M);
132  m->ParseFromString(_newdata);
133  this->callback(m);
134  return true;
135  }
136 
137  // documentation inherited
138  public: virtual bool HandleMessage(MessagePtr _newMsg)
139  {
140  this->callback(boost::dynamic_pointer_cast<M>(_newMsg));
141  return true;
142  }
143 
144  // documentation inherited
145  public: virtual bool IsLocal() const
146  {
147  return true;
148  }
149 
150  private: boost::function<void (const boost::shared_ptr<M const> &)>
151  callback;
152  };
153 
159  {
165  const boost::function<void (const std::string &)> &_cb,
166  bool _latching = false)
167  : CallbackHelper(_latching), callback(_cb)
168  {
169  }
170 
171  // documentation inherited
172  public: std::string GetMsgType() const
173  {
174  return "raw";
175  }
176 
177  // documentation inherited
178  public: virtual bool HandleData(const std::string &_newdata)
179  {
180  this->callback(_newdata);
181  return true;
182  }
183 
184  // documentation inherited
185  public: virtual bool HandleMessage(MessagePtr _newMsg)
186  {
187  std::string data;
188  _newMsg->SerializeToString(&data);
189  this->callback(data);
190  return true;
191  }
192 
193 
194  // documentation inherited
195  public: virtual bool IsLocal() const
196  {
197  return true;
198  }
199 
200  private: boost::function<void (const std::string &)> callback;
201  };
203  }
204 }
205 #endif