ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
Toggle main menu visibility
载入中...
搜索中...
未找到
EventBus.h
浏览该文件的文档.
1
#pragma once
2
#include "
Event.h
"
3
#include <functional>
4
#include <typeindex>
5
#include <unordered_map>
6
#include <vector>
7
#include <memory>
8
#include <queue>
9
#include <mutex>
10
11
namespace
Shit
{
12
30
class
SHIT_API
EventBus
{
31
public
:
32
using
HandlerID
= uint64_t;
33
34
template
<
typename
EventType>
35
static
HandlerID
Subscribe
(std::function<
void
(
const
EventType&)> callback) {
36
static_assert
(std::is_base_of_v<Event, EventType>,
"EventType must inherit from Event"
);
37
return
GetInstance
().subscribe<EventType>(std::move(callback));
38
}
39
40
template
<
typename
EventType>
41
static
void
Unsubscribe
(
HandlerID
id
) {
42
GetInstance
().unsubscribe<EventType>(id);
43
}
44
45
template
<
typename
EventType>
46
static
void
Emit
(
const
EventType& event) {
47
GetInstance
().emit<EventType>(event);
48
}
49
50
static
void
ProcessEvents
() {
51
GetInstance
().processEvents();
52
}
53
54
template
<
typename
EventType>
55
static
void
Clear
() {
56
GetInstance
().clear<EventType>();
57
}
58
59
static
void
ClearAll
() {
60
GetInstance
().clearAll();
61
}
62
63
static
EventBus
&
GetInstance
();
64
65
EventBus
(
const
EventBus
&) =
delete
;
66
EventBus
&
operator=
(
const
EventBus
&) =
delete
;
67
EventBus
(
EventBus
&&) =
delete
;
68
EventBus
&
operator=
(
EventBus
&&) =
delete
;
69
70
private
:
71
EventBus
() =
default
;
72
~EventBus
() =
default
;
73
74
struct
ListenerEntry {
75
HandlerID
id;
76
std::function<void(
const
Event
&)> callback;
77
};
78
79
template
<
typename
EventType>
80
HandlerID subscribe(std::function<
void
(
const
EventType&)> callback) {
81
std::lock_guard<std::mutex> lock(m_mutex);
82
HandlerID
id
= m_nextID++;
83
m_listeners[std::type_index(
typeid
(EventType))].push_back({
84
id,
85
[cb = std::move(callback)](
const
Event
& e) {
86
cb(
static_cast<
const
EventType&
>
(e));
87
}
88
});
89
return
id;
90
}
91
92
template
<
typename
EventType>
93
void
unsubscribe(HandlerID
id
) {
94
std::lock_guard<std::mutex> lock(m_mutex);
95
auto
it = m_listeners.find(std::type_index(
typeid
(EventType)));
96
if
(it == m_listeners.end())
return
;
97
auto
& vec = it->second;
98
vec.erase(std::remove_if(vec.begin(), vec.end(),
99
[
id
](
const
ListenerEntry& entry) { return entry.id == id; }),
100
vec.end());
101
}
102
103
template
<
typename
EventType>
104
void
emit(
const
EventType& event) {
105
std::lock_guard<std::mutex> lock(m_mutex);
106
m_eventQueue.push(std::make_shared<EventType>(event));
107
}
108
109
void
processEvents();
110
111
template
<
typename
EventType>
112
void
clear() {
113
std::lock_guard<std::mutex> lock(m_mutex);
114
m_listeners.erase(std::type_index(
typeid
(EventType)));
115
}
116
117
void
clearAll();
118
119
std::unordered_map<std::type_index, std::vector<ListenerEntry>> m_listeners;
120
std::queue<std::shared_ptr<Event>> m_eventQueue;
121
std::mutex m_mutex;
122
HandlerID m_nextID = 0;
123
};
124
125
}
// namespace Shit
SHIT_API
#define SHIT_API
定义
Core.h:12
Event.h
Shit::EventBus::GetInstance
static EventBus & GetInstance()
Shit::EventBus::Clear
static void Clear()
定义
EventBus.h:55
Shit::EventBus::Unsubscribe
static void Unsubscribe(HandlerID id)
定义
EventBus.h:41
Shit::EventBus::operator=
EventBus & operator=(EventBus &&)=delete
Shit::EventBus::HandlerID
uint64_t HandlerID
定义
EventBus.h:32
Shit::EventBus::EventBus
EventBus(EventBus &&)=delete
Shit::EventBus::Subscribe
static HandlerID Subscribe(std::function< void(const EventType &)> callback)
定义
EventBus.h:35
Shit::EventBus::operator=
EventBus & operator=(const EventBus &)=delete
Shit::EventBus::Emit
static void Emit(const EventType &event)
定义
EventBus.h:46
Shit::EventBus::ProcessEvents
static void ProcessEvents()
定义
EventBus.h:50
Shit::EventBus::EventBus
EventBus(const EventBus &)=delete
Shit::EventBus::ClearAll
static void ClearAll()
定义
EventBus.h:59
Shit
定义
AudioPlayer.h:9
Shit::Event
事件基类,所有自定义事件须继承此结构体
定义
Event.h:9
ShitEngine
Event
EventBus.h
制作者
1.17.0