Toast Engine
Loading...
Searching...
No Matches
listener.hpp
1
6#pragma once
7
8#include "event.hpp"
9
10#include <any>
11#include <cassert>
12#include <string>
13#include <string_view>
14#include <type_traits>
15#include <typeindex>
16#include <vector>
17
18namespace event {
19
22template<typename F, typename TEvent>
23concept EventCallback = std::is_invocable_r_v<bool, F, TEvent&> || // Returns bool(T&)
24 std::is_invocable_r_v<void, F, TEvent&> || // Returns void(T&)
25 std::is_invocable_r_v<bool, F> || // Returns bool()
26 std::is_invocable_r_v<void, F>; // Returns void()
27
39class TOAST_API Listener {
42 struct Handle {
43 std::type_index type;
44 std::string name;
45 std::any iterator;
46 };
47
48 struct {
49 std::vector<Handle> callbacks;
50 std::atomic<bool>* enabled;
51 } m;
52
53public:
54 Listener();
55 Listener(bool state);
56 ~Listener();
57
60 template<typename TEvent>
61 void unsubscribe(std::string_view name) noexcept;
62
67 template<typename TEvent, EventCallback<TEvent&> F>
68 void subscribe(std::string name, F&& callback, char priority = 0) noexcept;
69
73 template<typename TEvent, EventCallback<TEvent&> F>
74 void subscribe(F&& callback, char priority = 0) noexcept;
75
77 void enabled(bool state);
78
80 [[nodiscard]]
81 auto enabled() const -> bool;
82};
83}
84
85#include "listener.inl"
auto enabled() const -> bool
void unsubscribe(std::string_view name) noexcept
unsubscribes every event in type TEvent with that name
Definition listener.inl:10
void subscribe(std::string name, F &&callback, char priority=0) noexcept
subscribes a callback to an TEvent
Definition listener.inl:24
void enabled(bool state)
enables/disables the callbacks in the listener
valid callbacks you can subscribe
Definition listener.hpp:23