Goby v2
subscription.h
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef SUBSCRIPTION20110412H
24 #define SUBSCRIPTION20110412H
25 
26 #include <boost/function.hpp>
27 #include <boost/shared_ptr.hpp>
28 #include <google/protobuf/descriptor.h>
29 #include <google/protobuf/dynamic_message.h>
30 
31 namespace goby
32 {
33 namespace pb
34 {
35 // forms a non-template base for the Subscription class, allowing us
36 // use a common pointer type.
38 {
39  public:
40  virtual void post(const std::string& body) = 0;
41  virtual const google::protobuf::Message& newest() const = 0;
42  virtual const std::string& type_name() const = 0;
43  virtual const std::string& group() const = 0;
44  virtual bool has_valid_handler() const = 0;
45 };
46 
47 // forms the concept of a subscription to a given Google Protocol Buffers
48 // type ProtoBufMessage
49 // An instantiation of this is created for each call to ApplicationBase::subscribe()
50 template <typename ProtoBufMessage> class Subscription : public SubscriptionBase
51 {
52  public:
53  typedef boost::function<void(const ProtoBufMessage&)> HandlerType;
54 
55  Subscription(HandlerType& handler, const std::string& type_name, const std::string& group = "")
56  : handler_(handler), type_name_(type_name), group_(group)
57  {
58  }
59 
60  // handle an incoming message (serialized using the google::protobuf
61  // library calls)
62  void post(const std::string& body)
63  {
64  newest_msg_.ParseFromString(body);
65  if (handler_)
66  handler_(newest_msg_);
67  }
68 
69  // getters
70  const google::protobuf::Message& newest() const { return newest_msg_; }
71  const std::string& type_name() const { return type_name_; }
72  const std::string& group() const { return group_; }
73  bool has_valid_handler() const { return handler_; }
74 
75  private:
76  HandlerType handler_;
77  ProtoBufMessage newest_msg_;
78  const std::string type_name_;
79  const std::string group_;
80 };
81 } // namespace pb
82 } // namespace goby
83 
84 #endif
The global namespace for the Goby project.