ifw  0.0.1-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TopicHandler.hpp
Go to the documentation of this file.
1 
9 #ifndef RAD_TOPIC_HANDLER_HPP
10 #define RAD_TOPIC_HANDLER_HPP
11 
12 #include <rad/Logger.hpp>
13 #include <rad/Exceptions.hpp>
14 
15 #include <memory>
16 #include <string>
17 #include <unordered_map>
18 
19 namespace rad {
20 
24 class TopicHandler {
25 public:
26  virtual ~TopicHandler() {}
34  virtual void handle(const std::string& topicTypeId,
35  const void* pData,
36  size_t dataSize) = 0;
37 };
38 
39 
47 template<typename EventType>
48 EventType ParseTopicAndCreateEvent(const std::string& topicName, const void* pMsg, size_t msgSize)
49 {
50  RAD_LOG_TRACE();
51 
52  // Check that topicName matches the event)
53 #if 0
54  if (strcmp(topicName.c_str(), EventType::id) != 0) {
55  std::string errMsg("Error topicName <" + topicName +
56  ">, does not match eventId <" + EventType::id + ">.");
57  RAD_LOG_ERROR() << errMsg;
58  throw rad::Exception(errMsg);
59  }
60 #endif
61 
62  typename EventType::payload_t topicPayload;
63 
64  if (topicPayload.ParseFromArray(pMsg, msgSize) == false) {
65  std::string errMsg("Error parsing <" + topicName + "> payload.");
66  RAD_LOG_ERROR() << errMsg;
67  throw rad::Exception(errMsg);
68  }
69 
70  return EventType(topicPayload);
71 }
72 
73 } // namespace rad
74 
75 #endif /* #ifndef RAD_TOPIC_HANDLER_HPP */
#define RAD_LOG_ERROR()
Definition: Logger.hpp:266
virtual void handle(const std::string &topicTypeId, const void *pData, size_t dataSize)=0
string id
Definition: requests.proto:15
Definition: Exceptions.hpp:44
virtual ~TopicHandler()
Definition: TopicHandler.hpp:26
#define RAD_LOG_TRACE()
Definition: Logger.hpp:319
EventType ParseTopicAndCreateEvent(const std::string &topicName, const void *pMsg, size_t msgSize)
Definition: TopicHandler.hpp:48
Definition: TopicHandler.hpp:24