12#include <unordered_map>
22 std::unordered_map<std::type_index,
23 std::vector<std::function<void(
const void *event)>>>
33 void subscribe(std::function<
void(
const E &event)> func) {
34 auto it = _handlers.find(
typeid(E));
35 if (it != _handlers.end()) {
36 it->second.push_back([fct = std::move(func)](
const void *event) {
37 fct(*
static_cast<const E *
>(event));
41 std::vector<std::function<void(
const void *event)>> functionList;
42 functionList.push_back([fct = std::move(func)](
const void *event) {
43 fct(*
static_cast<const E *
>(event));
45 _handlers.insert({
typeid(E), functionList});
52 template <
typename E>
void publish(
const E &event) {
53 auto it = _handlers.find(
typeid(E));
54 if (it == _handlers.end()) {
57 auto &handler = it->second;
58 for (
const auto &h : handler) {
Type indexed publish and subscribe event bus.
Definition busEvents.hpp:20
void subscribe(std::function< void(const E &event)> func)
Registers a handler for one event type.
Definition busEvents.hpp:33
void publish(const E &event)
Calls every handler registered for the event type.
Definition busEvents.hpp:52