subscriber.hpp
Go to the documentation of this file.
1 
6 /*****************************************************************************
7 ** Ifdefs
8 *****************************************************************************/
9 
10 #ifndef mm_radio_SUBSCRIBER_HPP_
11 #define mm_radio_SUBSCRIBER_HPP_
12 
13 /*****************************************************************************
14 ** Includes
15 *****************************************************************************/
16 
18 #include <mm_messages.hpp>
19 #include <sstream>
20 #include <string>
21 #include "radio.hpp"
22 
23 /*****************************************************************************
24 ** Namespaces
25 *****************************************************************************/
26 
27 namespace mm_radio {
28 
29 /*****************************************************************************
30 ** Interfaces
31 *****************************************************************************/
32 
33 template<unsigned int ID, typename DataType>
34 class Subscriber {
35 public:
38 
46  Subscriber(const std::string& name,
47  void (*f)(DataType)) :
48  name(name),
49  id(ID),
50  function(new ecl::UnaryFreeFunction<DataType>(f))
51  {
52  if ( !MessageRegistry::isRegistered(ID) ) {
53  std::stringstream ss;
54  ss << "id '" << id << "' has not been registered";
55  throw mm_messages::UnregisteredID(ss.str());
56  }
57  if ( !MessageRegistry::isRegisteredWithType<DataType>(ID) ) {
58  std::stringstream ss;
59  ss << "id '" << id << "' is registered, but not with this type";
61  }
63  }
73  template<typename C>
74  Subscriber(const std::string& name,
75  void (C::*f)(DataType), C &c) :
76  name(name),
77  id(ID),
78  function(new ecl::PartiallyBoundUnaryMemberFunction<C, DataType,void>(f,c))
79  {
81  }
82  virtual ~Subscriber() {
84  delete function;
85  }
86 
87  void processPacket(const unsigned char* buffer, const unsigned int& size) {
88  DataType data = mm_messages::Message<DataType>::decode(buffer, size);
89  (*function)(data);
90  }
91 
92 private:
93  std::string name;
94  unsigned int id;
96 };
97 
98 } // namespace mm_radio
99 
100 #endif /* mm_radio_SUBSCRIBER_HPP_ */
Subscriber(const std::string &name, void(*f)(DataType))
Definition: subscriber.hpp:46
mm_messages::MessageRegistry MessageRegistry
Definition: subscriber.hpp:36
ecl::UnaryFunction< DataType, void > * function
Definition: subscriber.hpp:95
static void unregisterSubscriber(const std::string &name, const unsigned int &id)
Definition: lib/radio.cpp:277
Subscriber(const std::string &name, void(C::*f)(DataType), C &c)
Definition: subscriber.hpp:74
static bool isRegistered(const int &id)
mm_messages::ByteArray ByteArray
Definition: subscriber.hpp:37
static void registerSubscriber(const std::string &name, const unsigned int &id, void(C::*processPacket)(const unsigned char *, const unsigned int &), C &s)
Register a callback with the specified demux.
Definition: radio.hpp:153
std::vector< unsigned char > ByteArray
void processPacket(const unsigned char *buffer, const unsigned int &size)
Definition: subscriber.hpp:87
void f(int i) ecl_debug_throw_decl(StandardException)
static T decode(const unsigned char *buffer, const unsigned int &size)


mm_radio
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:52:16