LeviLamina
Loading...
Searching...
No Matches
Listener.h
1#pragma once
2
3#include "ll/api/base/Macro.h"
4#include "ll/api/event/ListenerBase.h"
5#include "ll/api/reflection/Reflection.h"
6
7namespace ll::event {
8template <std::derived_from<Event> T>
9class Listener : public ListenerBase {
10public:
11 using event_type = T;
12 using callback_fn = std::function<void(event_type&)>;
13
14 explicit Listener(
15 callback_fn fn,
16 EventPriority priority = EventPriority::Normal,
17 std::weak_ptr<mod::Mod> mod = mod::NativeMod::current()
18 )
19 : ListenerBase(priority, std::move(mod)),
20 callback(std::move(fn)) {}
21
22 ~Listener() override = default;
23
24 void call(Event& event) override { callback(static_cast<event_type&>(event)); }
25
26 static std::shared_ptr<Listener> create(
27 callback_fn fn,
28 EventPriority priority = EventPriority::Normal,
29 std::weak_ptr<mod::Mod> mod = mod::NativeMod::current()
30 ) {
31 return std::make_shared<Listener>(std::move(fn), priority, std::move(mod));
32 }
33
34private:
35 callback_fn callback;
36};
37} // namespace ll::event
Definition Event.h:14
Definition ListenerBase.h:24
Definition Listener.h:9