ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
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
11namespace Shit {
12
31public:
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
64
65 EventBus(const EventBus&) = delete;
66 EventBus& operator=(const EventBus&) = delete;
67 EventBus(EventBus&&) = delete;
69
70private:
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
#define SHIT_API
定义 Core.h:12
static EventBus & GetInstance()
static void Clear()
定义 EventBus.h:55
static void Unsubscribe(HandlerID id)
定义 EventBus.h:41
EventBus & operator=(EventBus &&)=delete
uint64_t HandlerID
定义 EventBus.h:32
EventBus(EventBus &&)=delete
static HandlerID Subscribe(std::function< void(const EventType &)> callback)
定义 EventBus.h:35
EventBus & operator=(const EventBus &)=delete
static void Emit(const EventType &event)
定义 EventBus.h:46
static void ProcessEvents()
定义 EventBus.h:50
EventBus(const EventBus &)=delete
static void ClearAll()
定义 EventBus.h:59
定义 AudioPlayer.h:9
事件基类,所有自定义事件须继承此结构体
定义 Event.h:9