LeviLamina
Loading...
Searching...
No Matches
MultiListener.h
1#pragma once
2
3#include <typeindex>
4
5#include "ll/api/base/Containers.h"
6#include "ll/api/event/Event.h"
7#include "ll/api/event/Listener.h"
8
9namespace ll::event {
10template <class... Ts>
11 requires(sizeof...(Ts) > 1 && (std::derived_from<Ts, Event> && ...))
13public:
14 using callback_fn = std::function<void(Event&)>;
15
16 template <class Callable>
17 explicit MultiListener(
18 Callable const& fn,
19 EventPriority priority = EventPriority::Normal,
20 std::weak_ptr<mod::Mod> mod = mod::NativeMod::current()
21 )
22 : ListenerBase(priority, std::move(mod)) {
23 (callback.emplace(getEventId<Ts>, [fn](Event& ev) { static_cast<void>(fn(static_cast<Ts&>(ev))); }), ...);
24 }
25
26 ~MultiListener() override = default;
27
28 void call(Event& event) override { callback.at(event.getId())(event); }
29
30 template <class Callable>
31 static std::shared_ptr<MultiListener> create(
32 Callable const& fn,
33 EventPriority priority = EventPriority::Normal,
34 std::weak_ptr<mod::Mod> mod = mod::NativeMod::current()
35 ) {
36 return std::make_shared<MultiListener>(fn, priority, std::move(mod));
37 }
38
39private:
40 SmallDenseMap<EventIdView, callback_fn> callback;
41};
42} // namespace ll::event
Definition Event.h:14
Definition ListenerBase.h:24
Definition MultiListener.h:12