SatDump 2.0.0-alpha-76a915210
Loading...
Searching...
No Matches
event_bus.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <functional>
9#include <string>
10#include <typeinfo>
11#include <vector>
12
13namespace satdump
14{
28 {
29 private:
30 std::vector<std::pair<std::string, std::function<void(void *)>>> all_handlers;
31
32 public:
39 template <typename T>
40 void register_handler(std::function<void(T)> handler_fun)
41 {
42 all_handlers.push_back({std::string(typeid(T).name()), [handler_fun](void *raw)
43 {
44 T evt = *((T *)raw); // Cast struct to original type
45 handler_fun(evt); // Call real handler
46 }});
47 }
48
55 template <typename T>
56 void fire_event(T evt)
57 {
58 for (std::pair<std::string, std::function<void(void *)>> h : all_handlers) // Iterate through all registered functions
59 if (std::string(typeid(T).name()) == h.first) // Check struct type is the same
60 h.second((void *)&evt); // Fire handler up
61 }
62
71 void fire_event(void *evt, std::string evt_name)
72 {
73 for (std::pair<std::string, std::function<void(void *)>> h : all_handlers) // Iterate through all registered functions
74 if (evt_name == h.first) // Check struct type is the same
75 h.second(evt); // Fire handler up
76 }
77 };
78} // namespace satdump
Very simple event bus implementation using std::function and typeid. All this does is fire any regist...
Definition event_bus.h:28
void fire_event(T evt)
Trigger an event, called every registered handler.
Definition event_bus.h:56
void fire_event(void *evt, std::string evt_name)
Trigger an event, called every registered handler. Allows specifying the event name....
Definition event_bus.h:71
void register_handler(std::function< void(T)> handler_fun)
Register a handler function to be called when a specific event is fired.
Definition event_bus.h:40