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 2011 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 {
35  namespace transport
36  {
40 
43  {
44  public: CallbackHelper() : latching(false) {}
45  public: virtual ~CallbackHelper() {}
46 
48  public: virtual std::string GetMsgType() const
49  {
50  return std::string();
51  }
52 
53  public: virtual bool HandleData(const std::string &newdata) = 0;
54 
57  public: virtual bool IsLocal() const = 0;
58 
59  public: bool GetLatching() const
60  {return this->latching;}
61  protected: bool latching;
62  };
63 
65  typedef boost::shared_ptr<CallbackHelper> CallbackHelperPtr;
66 
67 
69  template<class M>
71  {
72  public: CallbackHelperT(const boost::function<
73  void (const boost::shared_ptr<M const> &)> &cb) : callback(cb)
74  {
75  // Just some code to make sure we have a google protobuf.
76  /*M test;
77  google::protobuf::Message *m;
78  if ((m =dynamic_cast<google::protobuf::Message*>(&test))
79  == NULL)
80  gzthrow("Message type must be a google::protobuf type\n");
81  */
82  }
83 
85  public: std::string GetMsgType() const
86  {
87  M test;
88  google::protobuf::Message *m;
89  if ((m = dynamic_cast<google::protobuf::Message*>(&test))
90  == NULL)
91  gzthrow("Message type must be a google::protobuf type\n");
92  return m->GetTypeName();
93  }
94 
95  public: virtual bool HandleData(const std::string &newdata)
96  {
97  boost::shared_ptr<M> m(new M);
98  m->ParseFromString(newdata);
99  this->callback(m);
100  return true;
101  }
102 
103  public: virtual bool IsLocal() const
104  {
105  return true;
106  }
107 
108  private: boost::function<void (const boost::shared_ptr<M const> &)>
109  callback;
110  };
111 
113  {
115  const boost::function<void (ConstGzStringPtr &)> &cb)
116  : callback(cb)
117  {
118  }
119 
121  public: std::string GetMsgType() const
122  {
123  msgs::GzString m;
124  return m.GetTypeName();
125  }
126 
127  public: virtual bool HandleData(const std::string &newdata)
128  {
129  msgs::Packet packet;
130  packet.ParseFromString(newdata);
131 
132  boost::shared_ptr<msgs::GzString> m(new msgs::GzString);
133  m->ParseFromString(newdata);
134  this->callback(m);
135  return true;
136  }
137 
138  public: virtual bool IsLocal() const
139  {
140  return true;
141  }
142 
143  private: boost::function<void (boost::shared_ptr<msgs::GzString> &)>
144  callback;
145  };
147  }
148 }
149 #endif