Toast Engine
Toggle main menu visibility
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
19
namespace
event {
20
21
class
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
38
public
:
39
ThreadListener
();
40
ThreadListener
(
bool
state);
41
~ThreadListener
();
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
75
private:
77
template<typename TEvent>
78
void
listen() noexcept;
79
};
80
}
81
82
#include "thread-listener.inl"
event::ThreadListener
Definition
thread-listener.hpp:21
event::ThreadListener::callbacks
std::multimap< char, Handle, std::greater<> > callbacks
callbacks for the vents
Definition
thread-listener.hpp:34
event::ThreadListener::event_queue
std::vector< _detail::IEvent * > event_queue
copied event queue
Definition
thread-listener.hpp:31
event::ThreadListener::recievers
std::map< std::type_index, std::any > recievers
callbacks that add events to the event_queue
Definition
thread-listener.hpp:33
event::ThreadListener::pollEvents
void pollEvents()
iterates through the event_queue and calls the callbacks
Definition
thread-listener.cpp:54
event::ThreadListener::enabled
std::atomic< bool > * enabled
enabled bool
Definition
thread-listener.hpp:35
event::EventCallback
valid callbacks you can subscribe
Definition
listener.hpp:23
event::_detail::IEvent
Definition
event.hpp:36
engine
src
toast
events
thread-listener.hpp
Generated by
1.17.0