5#ifndef MALENA_EVENTBUS_H
6#define MALENA_EVENTBUS_H
10#include <unordered_map>
72 using EventKey = std::tuple<std::type_index, int, std::type_index>;
76 std::size_t operator()(
const EventKey& key)
const
78 auto h1 = std::get<0>(key).hash_code();
79 auto h2 = std::hash<int>{}(std::get<1>(key));
80 auto h3 = std::get<2>(key).hash_code();
81 return h1 ^ (h2 << 1) ^ (h3 << 2);
88 std::function<void(
const void*)> callback;
91 static std::unordered_map<EventKey, std::vector<Subscription>, KeyHash> subscribers;
93 static void doUnsubscribe(
const EventKey& key,
void* subscriber);
94 static void doUnsubscribeAll(
void* subscriber);
114 template<
typename DataType,
typename Enum>
116 std::function<
void(
const DataType&)> callback);
132 template<
typename DataType,
typename Enum>
133 static void publish(Enum event,
const DataType& data);
145 template<
typename DataType,
typename Enum>
177 static void forceUnsubscribeAll(
void* subscriber);
183#include "../../../../src/Engine/Messaging/MessageManager.tpp"
CRTP base that gives a manager safe deferred-operation support.
Typed, enum-keyed message bus for structured inter-object communication.
static void publish(Enum event, const DataType &data)
Deliver a typed message to all matching subscribers.
static void unsubscribeAll(void *subscriber)
Remove all subscriptions for a given subscriber.
static void subscribe(Enum event, void *subscriber, std::function< void(const DataType &)> callback)
Register a typed callback for a specific enum event.
static void clear()
Remove all subscriptions for all subscribers and all keys.
static void unsubscribe(Enum event, void *subscriber)
Remove one subscriber's callback for a specific typed event.