Loading...
Searching...
No Matches
MessageManager.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith. All rights reserved.
2// Malena Framework — Proprietary Software. See LICENSE for terms.
3
4//
5// MessageManager.h
6//
7
8#ifndef MALENA_EVENTBUS_H
9#define MALENA_EVENTBUS_H
10
13#include <algorithm>
14#include <functional>
15#include <optional>
16#include <tuple>
17#include <typeindex>
18#include <unordered_map>
19#include <vector>
20
21namespace ml
22{
74 {
76 using EventKey = std::tuple<std::type_index, int, std::type_index>;
77
78 struct KeyHash
79 {
80 std::size_t operator()(const EventKey& key) const
81 {
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);
86 }
87 };
88
89 struct Subscription
90 {
91 void* subscriber;
92 std::function<void(const void*)> callback;
93 };
94
95 static std::unordered_map<EventKey, std::vector<Subscription>, KeyHash>& subscribers();
96
97 static void doUnsubscribe(const EventKey& key, void* subscriber);
98 static void doUnsubscribeAll(void* subscriber);
100
101 public:
118 template<typename DataType, typename Enum>
119 static void subscribe(Enum event, void* subscriber,
120 std::function<void(const DataType&)> callback);
121
136 template<typename DataType, typename Enum>
137 static void publish(Enum event, const DataType& data);
138
149 template<typename DataType, typename Enum>
150 static void unsubscribe(Enum event, void* subscriber);
151
161 static void unsubscribeAll(void* subscriber);
162
168 static void clear();
169
171
181 static void forceUnsubscribeAll(void* subscriber);
183 };
184
185} // namespace ml
186
187#include "../../../../src/Engine/Messaging/MessageManager.tpp"
188#endif // MALENA_EVENTBUS_H
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.
#define MALENA_API
Definition Component.h:22