26 typename EventVariant,
27 size_t MaxSubscribersPerTopic = 8,
42 for (
size_t i = 0; i < m_numTopics; ++i) {
43 if (m_topics[i].topicId == topicId) {
44 if (m_topics[i].subscriberCount >= MaxSubscribersPerTopic) {
47 m_topics[i].subscribers[m_topics[i].subscriberCount++] = std::move(subscriber);
53 if (m_numTopics >= MaxTopics) {
57 auto& newTopic = m_topics[m_numTopics++];
58 newTopic.topicId = topicId;
59 newTopic.subscriberCount = 1;
60 newTopic.subscribers[0] = std::move(subscriber);
70 template <
typename Event,
typename Callable>
73 if (std::holds_alternative<Event>(var)) {
74 c(std::get<Event>(var));
83 [[nodiscard]]
size_t publish(uint32_t topicId,
const EventVariant& event)
const noexcept {
84 for (
size_t i = 0; i < m_numTopics; ++i) {
85 if (m_topics[i].topicId == topicId) {
86 for (
size_t j = 0; j < m_topics[i].subscriberCount; ++j) {
87 m_topics[i].subscribers[j](event);
89 return m_topics[i].subscriberCount;
100 template <
typename Event>
102 return publish(topicId, EventVariant{
event});
108 for (
auto& topic : m_topics) {
109 topic.subscriberCount = 0;
121 size_t subscriberCount{0};
122 std::array<DelegateType, MaxSubscribersPerTopic> subscribers{};
125 std::array<TopicSlot, MaxTopics> m_topics{};
126 size_t m_numTopics{0};
Zero-allocating Small Buffer Optimized (SBO) static delegate dispatcher.
Zero-heap static topic-based publish/subscribe router. Fans out events to multiple registered delegat...
Definition EventRouter.hpp:30
size_t publish(uint32_t topicId, const EventVariant &event) const noexcept
Publish an event variant to all subscribers of a specific topic.
Definition EventRouter.hpp:83
constexpr EventRouter() noexcept=default
void clear() noexcept
Reset all topic subscriptions.
Definition EventRouter.hpp:106
size_t publishEvent(uint32_t topicId, const Event &event) const noexcept
Publish a concrete event to all subscribers of a specific topic.
Definition EventRouter.hpp:101
size_t topicCount() const noexcept
Total active topics currently configured.
Definition EventRouter.hpp:114
bool subscribeEvent(uint32_t topicId, Callable callable) noexcept
Subscribe a typed event handler lambda to a specific topic ID.
Definition EventRouter.hpp:71
internal::EventHandlerDelegate< EventVariant, 32 > DelegateType
Definition EventRouter.hpp:32
bool subscribe(uint32_t topicId, DelegateType subscriber) noexcept
Subscribe a delegate callback to a specific topic ID.
Definition EventRouter.hpp:40
Lightweight non-allocating delegate wrapper with 32-byte Small Buffer Optimization (SBO)....
Definition FastDelegate.hpp:22
Definition Application.hpp:16
DefaultEvents Event
Alias for DefaultEvents.
Definition Events.hpp:65