2#include <EASTL/functional.h>
3#include <EASTL/unordered_map.h>
4#include <EASTL/vector.h>
5#include <EASTL/queue.h>
6#include <EASTL/unique_ptr.h>
60 QEventBus(
const std::string_view& eventNamespace);
68 template<
typename EventType>
69 using Handler = eastl::function<void(
const EventType&)>;
81 template<
typename EventType>
93 template<
typename EventType>
105 template<
typename EventType>
106 void post(EventType&& event);
133 size_t getQueueSize()
const;
142 const std::string_view& getNamespace()
const;
152 void dispatch(
const IEvent& e);
155 struct TypeIndexHash {
156 size_t operator()(
const std::type_index& t)
const noexcept {
157 return t.hash_code();
161 struct TypeIndexEqual {
162 bool operator()(
const std::type_index& a,
const std::type_index& b)
const noexcept {
174 eastl::unordered_map<
176 eastl::vector<eastl::function<void(
const IEvent&)>>,
187 eastl::queue<eastl::unique_ptr<IEvent>> queue;
197 std::mutex queueMutex;
202 std::condition_variable conditionalVariable;
207 std::atomic_bool bIsRunning =
false;
214 const std::string_view eventNamespace;
216 template<
typename EventType>
218 std::lock_guard<std::mutex> lock(QEventBus::mutex);
220 auto& handlers = QEventBus::handlers[
typeid(EventType)];
222 handlers.emplace_back([handler](
const IEvent& e) {
223 handler(
static_cast<const EventType&
>(e));
226 auto it = eastl::prev(handlers.end());
227 std::type_index typeIdx = std::type_index(
typeid(EventType));
230 std::lock_guard<std::mutex> lock(QEventBus::mutex);
231 auto mapIt = QEventBus::handlers.find(typeIdx);
232 if (mapIt != QEventBus::handlers.end()) {
233 auto& vec = mapIt->second;
238 QEventBus::handlers.erase(mapIt);
245 template<
typename EventType>
247 std::lock_guard<std::mutex> lock(QEventBus::mutex);
250 auto& handlers = QEventBus::handlers[
typeid(EventType)];
251 handlers.emplace_back([handler](
const IEvent& e) {
252 handler(
static_cast<const EventType&
>(e));
256 template<
typename EventType>
259 using Decayed = std::decay_t<EventType>;
260 auto ptr = eastl::make_unique<Decayed>(eastl::forward<EventType>(event));
262 std::lock_guard<std::mutex> lock(QEventBus::queueMutex);
263 QEventBus::queue.push(eastl::move(ptr));
265 QEventBus::conditionalVariable.notify_one();
~QEventBus()
Destructor.
Definition QEventBus.cpp:47
void stop()
Stop asynchronous processing and join the worker thread.
Definition QEventBus.cpp:37
void post(EventType &&event)
Post an event instance to the bus for later processing.
Definition QEventBus.hpp:257
void runAsync()
Start asynchronous processing of queued events.
Definition QEventBus.cpp:18
void process()
Process and dispatch all currently queued events.
Definition QEventBus.cpp:5
eastl::function< void(const EventType &)> Handler
Alias for an event handler function.
Definition QEventBus.hpp:69
SubscriptionToken subscribeRAII(Handler< EventType > handler)
Subscribe a handler to EventType and receive a RAII token.
Definition QEventBus.hpp:217
void subscribe(Handler< EventType > handler)
Subscribe a handler to EventType without RAII token.
Definition QEventBus.hpp:246
Token object that controls the lifetime of an event subscription.
Definition QEventBus.hpp:27
eastl::function< void()> unsubscribe
Callable invoked to perform the unsubscribe action.
Definition QEventBus.hpp:33
~SubscriptionToken()
Destructor.
Definition QEventBus.hpp:40