Skip to content

Commit

Permalink
feat: add event iterator api for EventBus
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Jan 2, 2025
1 parent d9bddbc commit 8c3a12b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/ll/api/base/Alias.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,35 @@ struct UntypedStorage {

template <class T>
[[nodiscard]] T& as() & {
return *reinterpret_cast<T*>(data);
if constexpr (std::is_reference_v<T>) {
return **reinterpret_cast<std::remove_reference_t<T>**>(data);
} else {
return *reinterpret_cast<T*>(data);
}
}
template <class T>
[[nodiscard]] T const& as() const& {
return *reinterpret_cast<T const*>(data);
if constexpr (std::is_reference_v<T>) {
return **reinterpret_cast<std::remove_reference_t<T> const**>(data);
} else {
return *reinterpret_cast<T const*>(data);
}
}
template <class T>
[[nodiscard]] T&& as() && {
return std::move(*reinterpret_cast<T*>(data));
if constexpr (std::is_reference_v<T>) {
return std::move(**reinterpret_cast<std::remove_reference_t<T>**>(data));
} else {
return std::move(*reinterpret_cast<T*>(data));
}
}
template <class T>
[[nodiscard]] T const&& as() const&& {
return std::move(*reinterpret_cast<T const*>(data));
if constexpr (std::is_reference_v<T>) {
return std::move(**reinterpret_cast<std::remove_reference_t<T> const**>(data));
} else {
return std::move(*reinterpret_cast<T const*>(data));
}
}
};

Expand Down
21 changes: 21 additions & 0 deletions src/ll/api/event/EventBus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@ void EventBus::publish(std::string_view modName, Event& event, EventIdView const
stream->publish(modName, event);
}
}

coro::Generator<EventIdView> EventBus::events(std::string_view modName) const {
std::lock_guard lock(impl->infoMutex);
if (auto it = impl->modInfos.find(modName); it != impl->modInfos.end()) {
for (auto& id : it->second.ownedEvents) {
co_yield id;
}
}
}

coro::Generator<std::pair<std::string_view, EventIdView>> EventBus::events() const {
std::lock_guard lock(impl->infoMutex);
for (auto& [name, info] : impl->modInfos) {
for (auto& id : info.ownedEvents) {
co_yield {name, id};
}
}
}

bool EventBus::hasEvent(EventIdView const& eventId) const { return impl->events.contains(eventId); }

size_t EventBus::getListenerCount(EventIdView const& eventId) {
if (eventId == EmptyEventId) {
return impl->listenerInfos.size();
Expand Down
7 changes: 7 additions & 0 deletions src/ll/api/event/EventBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "ll/api/base/Concepts.h"
#include "ll/api/base/Macro.h"
#include "ll/api/coro/Generator.h"
#include "ll/api/event/Event.h"
#include "ll/api/event/EventId.h"
#include "ll/api/event/Listener.h"
Expand Down Expand Up @@ -61,6 +62,12 @@ class EventBus {
publish(modName, event, getEventId<T>);
}

LLNDAPI coro::Generator<EventIdView> events(std::string_view modName) const;

LLNDAPI coro::Generator<std::pair<std::string_view, EventIdView>> events() const;

LLNDAPI bool hasEvent(EventIdView const& eventId) const;

LLNDAPI size_t getListenerCount(EventIdView const&);

template <std::derived_from<Event> T>
Expand Down

0 comments on commit 8c3a12b

Please sign in to comment.