Toast Engine
Loading...
Searching...
No Matches
thread-listener.hpp
1
6#pragma once
7
8#include "listener.hpp"
9
10#include <any>
11#include <cassert>
12#include <map>
13#include <mutex>
14#include <string>
15#include <string_view>
16#include <typeindex>
17#include <vector>
18
19namespace event {
20
21class TOAST_API ThreadListener {
22 using callback_t = std::move_only_function<bool(_detail::IEvent&)>;
23
24 struct Handle {
25 std::type_index type;
26 std::string name;
27 callback_t callback;
28 };
29
30 struct {
31 std::vector<_detail::IEvent*> event_queue;
32 std::mutex queue_mutex;
33 std::map<std::type_index, std::any> recievers;
34 std::multimap<char, Handle, std::greater<>> callbacks;
35 std::atomic<bool>* enabled;
36 } m;
37
38public:
40 ThreadListener(bool state);
42
44 void clear();
45
48 template<typename TEvent>
49 void unsubscribe(std::string_view name) noexcept;
50
55 template<typename TEvent, EventCallback<TEvent&> F>
56 void subscribe(std::string name, F&& callback, char priority = 0) noexcept;
57
61 template<typename TEvent, EventCallback<TEvent&> F>
62 void subscribe(F&& callback, char priority = 0) noexcept;
63
65 void enabled(bool state);
66
68 [[nodiscard]]
69 auto enabled() const -> bool;
70
73 void pollEvents();
74
75private:
77 template<typename TEvent>
78 void listen() noexcept;
79};
80}
81
82#include "thread-listener.inl"
Definition thread-listener.hpp:21
std::multimap< char, Handle, std::greater<> > callbacks
callbacks for the vents
Definition thread-listener.hpp:34
std::vector< _detail::IEvent * > event_queue
copied event queue
Definition thread-listener.hpp:31
std::map< std::type_index, std::any > recievers
callbacks that add events to the event_queue
Definition thread-listener.hpp:33
void pollEvents()
iterates through the event_queue and calls the callbacks
Definition thread-listener.cpp:54
std::atomic< bool > * enabled
enabled bool
Definition thread-listener.hpp:35
valid callbacks you can subscribe
Definition listener.hpp:23
Definition event.hpp:36