subscriber.hpp
Go to the documentation of this file.
1 
6 /*****************************************************************************
7 ** Ifdefs
8 *****************************************************************************/
9 
10 #ifndef mm_mux_demux_SUBSCRIBER_HPP_
11 #define mm_mux_demux_SUBSCRIBER_HPP_
12 
13 /*****************************************************************************
14 ** Includes
15 *****************************************************************************/
16 
18 #include <mm_messages.hpp>
19 #include <sstream>
20 #include <string>
21 #include "demux.hpp"
22 
23 /*****************************************************************************
24 ** Namespaces
25 *****************************************************************************/
26 
27 namespace mm_mux_demux {
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_mux_demux
99 
100 #endif /* mm_mux_demux_SUBSCRIBER_HPP_ */
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: demux.hpp:136
static bool isRegistered(const int &id)
ecl::UnaryFunction< DataType, void > * function
Definition: subscriber.hpp:95
static void unregisterSubscriber(const std::string &name, const unsigned int &id)
Definition: demux.cpp:209
mm_messages::ByteArray ByteArray
Definition: subscriber.hpp:37
Short description of this file.
Subscriber(const std::string &name, void(*f)(DataType))
Definition: subscriber.hpp:46
Subscriber(const std::string &name, void(C::*f)(DataType), C &c)
Definition: subscriber.hpp:74
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_messages::MessageRegistry MessageRegistry
Definition: subscriber.hpp:36


mm_mux_demux
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:52:14