diff --git a/lib/libimhex/include/hex/api/event_manager.hpp b/lib/libimhex/include/hex/api/event_manager.hpp index 1bc1287c9..340590253 100644 --- a/lib/libimhex/include/hex/api/event_manager.hpp +++ b/lib/libimhex/include/hex/api/event_manager.hpp @@ -14,17 +14,37 @@ #include -#define EVENT_DEF_IMPL(event_name, event_name_string, should_log, ...) \ - struct event_name final : public hex::impl::Event<__VA_ARGS__> { \ - constexpr static auto Id = [] { return hex::impl::EventId(event_name_string); }(); \ - constexpr static auto ShouldLog = (should_log); \ - explicit event_name(Callback func) noexcept : Event(std::move(func)) { } \ - \ - static EventManager::EventList::iterator subscribe(Event::Callback function) { return EventManager::subscribe(std::move(function)); } \ - static void subscribe(void *token, Event::Callback function) { EventManager::subscribe(token, std::move(function)); } \ - static void unsubscribe(const EventManager::EventList::iterator &token) noexcept { EventManager::unsubscribe(token); } \ - static void unsubscribe(void *token) noexcept { EventManager::unsubscribe(token); } \ - static void post(auto &&...args) { EventManager::post(std::forward(args)...); } \ +#define EVENT_DEF_IMPL(event_name, event_name_string, should_log, ...) \ + struct event_name final : public hex::impl::Event<__VA_ARGS__> { \ + constexpr static auto Id = [] { return hex::impl::EventId(event_name_string); }(); \ + constexpr static auto ShouldLog = (should_log); \ + explicit event_name(Callback func) noexcept : Event(std::move(func)) { } \ + \ + static EventManager::EventList::iterator subscribe(Event::Callback function) { \ + return EventManager::subscribe(std::move(function)); \ + } \ + template \ + static EventManager::EventList::iterator subscribe(Event::BaseCallback function) \ + requires (!std::same_as) { \ + return EventManager::subscribe([function = std::move(function)](auto && ...) { function(); }); \ + } \ + static void subscribe(void *token, Event::Callback function) { \ + EventManager::subscribe(token, std::move(function)); \ + } \ + template \ + static void subscribe(void *token, Event::BaseCallback function) \ + requires (!std::same_as) { \ + return EventManager::subscribe(token, [function = std::move(function)](auto && ...) { function(); }); \ + } \ + static void unsubscribe(const EventManager::EventList::iterator &token) noexcept { \ + EventManager::unsubscribe(token); \ + } \ + static void unsubscribe(void *token) noexcept { \ + EventManager::unsubscribe(token); \ + } \ + static void post(auto &&...args) { \ + EventManager::post(std::forward(args)...); \ + } \ } #define EVENT_DEF(event_name, ...) EVENT_DEF_IMPL(event_name, #event_name, true, __VA_ARGS__) @@ -65,6 +85,7 @@ EXPORT_MODULE namespace hex { template struct Event : EventBase { using Callback = std::function; + using BaseCallback = std::function; explicit Event(Callback func) noexcept : m_func(std::move(func)) { }