FCT
载入中...
搜索中...
未找到
IEventSystem.h
浏览该文件的文档.
1//
2// Created by Administrator on 2025/7/27.
3//
4
5#ifndef EVENTSYSTEM_H
6#define EVENTSYSTEM_H
7#include "../ThirdParty.h"
8#include <unordered_map>
9#include <vector>
10#include <functional>
11#include <memory>
12
13namespace FCT
14{
16 {
17 struct QueueOnly {
18 static constexpr bool EnableQueue = true;
19 static constexpr bool EnableTrigger = false;
20 static constexpr bool EventAsData = true;
21 };
22 struct TriggerOnly {
23 static constexpr bool EnableQueue = false;
24 static constexpr bool EnableTrigger = true;
25 static constexpr bool EventAsData = true;
26 };
27 struct Full {
28 static constexpr bool EnableQueue = true;
29 static constexpr bool EnableTrigger = true;
30 static constexpr bool EventAsData = true;
31 };
33 static constexpr bool EnableQueue = true;
34 static constexpr bool EnableTrigger = true;
35 static constexpr bool EventAsData = false;
36 };
38 static constexpr bool EnableQueue = true;
39 static constexpr bool EnableTrigger = true;
40 static constexpr bool EventAsData = false;
41 };
42 }
43 using SubscribeId = std::size_t;
44
45 template<typename Config = EventSystemConfig::Full>
47 private:
48 template<typename Event>
49 static constexpr auto getEventTypeId() {
50 return entt::type_hash<Event>::value();
51 }
52
54 virtual ~IEventHandler() = default;
55 virtual void handle(const void* event) = 0;
56 };
57
58 template<typename Event>
60 std::function<void(const Event&)> handler;
61
62 EventHandler(std::function<void(const Event&)> h) : handler(std::move(h)) {}
63
64 void handle(const void* event) override {
65 handler(*static_cast<const Event*>(event));
66 }
67 };
68
69 struct QueuedEvent {
70 std::unique_ptr<void, void(*)(void*)> data;
71 std::function<void(const void*)> trigger_func;
72
73 template<typename Event>
74 QueuedEvent(Event&& event)
75 : data(new Event(std::forward<Event>(event)), [](void* ptr) { delete static_cast<Event*>(ptr); })
76 , trigger_func([this](const void*) {
77 auto* evt = static_cast<const Event*>(data.get());
78 }) {}
79 };
80
81
83 virtual ~IEventIdentifierHandler() = default;
84 virtual void handle() = 0;
85 };
86
87 template<typename Event>
89 std::function<void()> handler;
90
91 EventIdentifierHandler(std::function<void()> h) : handler(std::move(h)) {}
92
93 void handle() override {
94 handler();
95 }
96 };
97
98 std::conditional_t<Config::EventAsData,
99 std::unordered_map<entt::id_type, std::unordered_map<SubscribeId, std::unique_ptr<IEventHandler>>>,
100 std::unordered_map<entt::id_type, std::unordered_map<SubscribeId, std::unique_ptr<IEventIdentifierHandler>>>> m_handlers;
101
102 std::conditional_t<Config::EnableQueue,
103 std::unordered_map<entt::id_type, std::vector<std::function<void()>>>,
104 std::monostate> m_eventQueue;
105
107
108 public:
109 template<typename Event>
110 void trigger(const Event& event)
111 requires (Config::EnableTrigger && Config::EventAsData)
112 {
113 constexpr auto eventTypeId = getEventTypeId<Event>();
114
115 auto it = m_handlers.find(eventTypeId);
116 if (it != m_handlers.end()) {
117 for (const auto& [id, handler] : it->second) {
118 handler->handle(&event);
119 }
120 }
121 }
122
123 template<typename Event>
124 void trigger()
125 requires (Config::EnableTrigger && !Config::EventAsData)
126 {
127 constexpr auto eventTypeId = getEventTypeId<Event>();
128
129 auto it = m_handlers.find(eventTypeId);
130 if (it != m_handlers.end()) {
131 for (const auto& [id, handler] : it->second) {
132 handler->handle();
133 }
134 }
135 }
136
137 template<typename Event>
138 void enqueue(Event&& event)
139 requires (Config::EnableQueue && Config::EventAsData)
140 {
141 constexpr auto eventTypeId = getEventTypeId<Event>();
142
143 auto triggerLambda = [this, evt = std::forward<Event>(event)]() mutable {
144 this->trigger(evt);
145 };
146
147 m_eventQueue[eventTypeId].emplace_back(std::move(triggerLambda));
148 }
149
150 template<typename Event>
151 void enqueue()
152 requires (Config::EnableQueue && !Config::EventAsData)
153 {
154 constexpr auto eventTypeId = getEventTypeId<Event>();
155
156 auto triggerLambda = [this]() {
157 this->template trigger<Event>();
158 };
159
160 m_eventQueue[eventTypeId].emplace_back(std::move(triggerLambda));
161 }
162
163
164 template<typename Event>
165 void update()
166 requires Config::EnableQueue
167 {
168 constexpr auto eventTypeId = getEventTypeId<Event>();
169
170 auto it = m_eventQueue.find(eventTypeId);
171 if (it != m_eventQueue.end()) {
172 auto& queue = it->second;
173 for (auto& triggerFunc : queue) {
174 triggerFunc();
175 }
176 queue.clear();
177 }
178 }
179
181 requires Config::EnableQueue
182 {
183 for (auto& [typeId, queue] : m_eventQueue) {
184 for (auto& triggerFunc : queue) {
185 triggerFunc();
186 }
187 queue.clear();
188 }
189 }
190
191 template<typename Event, typename Func>
193 requires Config::EventAsData
194 {
195 constexpr auto eventTypeId = getEventTypeId<Event>();
196
197 auto subscribeId = m_nextSubscribeId++;
198
199 auto handler = std::make_unique<EventHandler<Event>>(
200 std::function<void(const Event&)>(std::forward<Func>(func))
201 );
202
203 m_handlers[eventTypeId][subscribeId] = std::move(handler);
204
205 return subscribeId;
206 }
207 template<typename Event, typename Func>
209 requires (!Config::EventAsData)
210 {
211 constexpr auto eventTypeId = getEventTypeId<Event>();
212
213 auto subscribeId = m_nextSubscribeId++;
214
215 auto handler = std::make_unique<EventIdentifierHandler<Event>>(
216 std::function<void()>(std::forward<Func>(func))
217 );
218
219 m_handlers[eventTypeId][subscribeId] = std::move(handler);
220
221 return subscribeId;
222 }
223
224
225 template<typename Event>
226 void unsubscribe(SubscribeId subscribeId) {
227 constexpr auto eventTypeId = getEventTypeId<Event>();
228
229 auto it = m_handlers.find(eventTypeId);
230 if (it != m_handlers.end()) {
231 it->second.erase(subscribeId);
232 if (it->second.empty()) {
233 m_handlers.erase(it);
234 }
235 }
236 }
237
238 template<typename Event>
240 constexpr auto eventTypeId = getEventTypeId<Event>();
241 m_handlers.erase(eventTypeId);
242 }
243
244 void unsubscribe(SubscribeId subscribeId) {
245 for (auto& [typeId, handlers] : m_handlers) {
246 auto it = handlers.find(subscribeId);
247 if (it != handlers.end()) {
248 handlers.erase(it);
249 if (handlers.empty()) {
250 m_handlers.erase(typeId);
251 }
252 return;
253 }
254 }
255 }
256 };
257 template<typename Config = EventSystemConfig::Full>
259}
260#endif //EVENTSYSTEM_H
void enqueue(Event &&event)
void unsubscribe(SubscribeId subscribeId)
void trigger(const Event &event)
SubscribeId subscribe(Func &&func)
SubscribeId subscribe(Func &&func)
static constexpr auto getEventTypeId()
std::conditional_t< Config::EnableQueue, std::unordered_map< entt::id_type, std::vector< std::function< void()> > >, std::monostate > m_eventQueue
std::conditional_t< Config::EventAsData, std::unordered_map< entt::id_type, std::unordered_map< SubscribeId, std::unique_ptr< IEventHandler > > >, std::unordered_map< entt::id_type, std::unordered_map< SubscribeId, std::unique_ptr< IEventIdentifierHandler > > > > m_handlers
void unsubscribe(SubscribeId subscribeId)
IEventSystem< Config > EventDispatcher
std::size_t SubscribeId
static constexpr bool EnableQueue
static constexpr bool EventAsData
static constexpr bool EnableTrigger
std::function< void(const Event &)> handler
EventHandler(std::function< void(const Event &)> h)
void handle(const void *event) override
EventIdentifierHandler(std::function< void()> h)
virtual ~IEventHandler()=default
virtual void handle(const void *event)=0
std::unique_ptr< void, void(*)(void *)> data
std::function< void(const void *)> trigger_func