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 Nate Koenig
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  {
43  public: CallbackHelper() : latching(false) {}
45  public: virtual ~CallbackHelper() {}
46 
49  public: virtual std::string GetMsgType() const
50  {
51  return std::string();
52  }
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  {return this->latching;}
68  protected: bool latching;
69  };
70 
72  typedef boost::shared_ptr<CallbackHelper> CallbackHelperPtr;
73 
74 
77  template<class M>
79  {
82  public: CallbackHelperT(const boost::function<
83  void (const boost::shared_ptr<M const> &)> &_cb) : callback(_cb)
84  {
85  // Just some code to make sure we have a google protobuf.
86  /*M test;
87  google::protobuf::Message *m;
88  if ((m =dynamic_cast<google::protobuf::Message*>(&test))
89  == NULL)
90  gzthrow("Message type must be a google::protobuf type\n");
91  */
92  }
93 
94  // documentation inherited
95  public: std::string GetMsgType() const
96  {
97  M test;
98  google::protobuf::Message *m;
99  if ((m = dynamic_cast<google::protobuf::Message*>(&test))
100  == NULL)
101  gzthrow("Message type must be a google::protobuf type\n");
102  return m->GetTypeName();
103  }
104 
105  // documentation inherited
106  public: virtual bool HandleData(const std::string &_newdata)
107  {
108  boost::shared_ptr<M> m(new M);
109  m->ParseFromString(_newdata);
110  this->callback(m);
111  return true;
112  }
113 
114  // documentation inherited
115  public: virtual bool IsLocal() const
116  {
117  return true;
118  }
119 
120  private: boost::function<void (const boost::shared_ptr<M const> &)>
121  callback;
122  };
123 
127  {
131  const boost::function<void (ConstGzStringPtr &)> &_cb)
132  : callback(_cb)
133  {
134  }
135 
136  // documentation inherited
137  public: std::string GetMsgType() const
138  {
139  msgs::GzString m;
140  return m.GetTypeName();
141  }
142 
143  // documentation inherited
144  public: virtual bool HandleData(const std::string &_newdata)
145  {
146  msgs::Packet packet;
147  packet.ParseFromString(_newdata);
148 
149  boost::shared_ptr<msgs::GzString> m(new msgs::GzString);
150  m->ParseFromString(_newdata);
151  this->callback(m);
152  return true;
153  }
154 
155  // documentation inherited
156  public: virtual bool IsLocal() const
157  {
158  return true;
159  }
160 
161  private: boost::function<void (boost::shared_ptr<msgs::GzString> &)>
162  callback;
163  };
165  }
166 }
167 #endif