8#ifndef MALENA_EVENTBUS_H
9#define MALENA_EVENTBUS_H
18#include <unordered_map>
76 using EventKey = std::tuple<std::type_index, int, std::type_index>;
80 std::size_t operator()(
const EventKey& key)
const
82 auto h1 = std::get<0>(key).hash_code();
83 auto h2 = std::hash<int>{}(std::get<1>(key));
84 auto h3 = std::get<2>(key).hash_code();
85 return h1 ^ (h2 << 1) ^ (h3 << 2);
92 std::function<void(
const void*)> callback;
95 static std::unordered_map<EventKey, std::vector<Subscription>, KeyHash>& subscribers();
97 static void doUnsubscribe(
const EventKey& key,
void* subscriber);
98 static void doUnsubscribeAll(
void* subscriber);
118 template<
typename DataType,
typename Enum>
120 std::function<
void(
const DataType&)> callback);
136 template<
typename DataType,
typename Enum>
137 static void publish(Enum event,
const DataType& data);
149 template<
typename DataType,
typename Enum>
181 static void forceUnsubscribeAll(
void* subscriber);
187#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.