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 "common/Console.hh"
28 #include "msgs/msgs.hh"
29 #include "common/Exception.hh"
30 
31 namespace gazebo
32 {
33  namespace transport
34  {
37 
41  {
45  public: CallbackHelper(bool _latching = false);
46 
48  public: virtual ~CallbackHelper();
49 
52  public: virtual std::string GetMsgType() const;
53 
57  public: virtual bool HandleData(const std::string &_newdata) = 0;
58 
62  public: virtual bool IsLocal() const = 0;
63 
66  public: bool GetLatching() const;
67 
70  public: unsigned int GetId() const;
71 
74  protected: bool latching;
75 
77  private: static unsigned int idCounter;
78 
80  private: unsigned int id;
81  };
82 
84  typedef boost::shared_ptr<CallbackHelper> CallbackHelperPtr;
85 
86 
89  template<class M>
91  {
96  public: CallbackHelperT(const boost::function<
97  void (const boost::shared_ptr<M const> &)> &_cb,
98  bool _latching = false)
99  : CallbackHelper(_latching), callback(_cb)
100  {
101  // Just some code to make sure we have a google protobuf.
102  /*M test;
103  google::protobuf::Message *m;
104  if ((m =dynamic_cast<google::protobuf::Message*>(&test))
105  == NULL)
106  gzthrow("Message type must be a google::protobuf type\n");
107  */
108  }
109 
110  // documentation inherited
111  public: std::string GetMsgType() const
112  {
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  return m->GetTypeName();
119  }
120 
121  // documentation inherited
122  public: virtual bool HandleData(const std::string &_newdata)
123  {
124  boost::shared_ptr<M> m(new M);
125  m->ParseFromString(_newdata);
126  this->callback(m);
127  return true;
128  }
129 
130  // documentation inherited
131  public: virtual bool IsLocal() const
132  {
133  return true;
134  }
135 
136  private: boost::function<void (const boost::shared_ptr<M const> &)>
137  callback;
138  };
139 
145  {
151  const boost::function<void (const std::string &)> &_cb,
152  bool _latching = false)
153  : CallbackHelper(_latching), callback(_cb)
154  {
155  }
156 
157  // documentation inherited
158  public: std::string GetMsgType() const
159  {
160  return "raw";
161  }
162 
163  // documentation inherited
164  public: virtual bool HandleData(const std::string &_newdata)
165  {
166  this->callback(_newdata);
167  return true;
168  }
169 
170  // documentation inherited
171  public: virtual bool IsLocal() const
172  {
173  return true;
174  }
175 
176  private: boost::function<void (const std::string &)> callback;
177  };
179  }
180 }
181 #endif