#pragma once #include #include #include #include #include #include #include namespace XCEngine { namespace Editor { class EventBus { public: using EventHandler = std::function; template uint64_t Subscribe(std::function handler) { static_assert(sizeof(T) > 0, "Event type must be defined"); size_t typeId = typeid(T).hash_code(); uint64_t handlerId = m_nextHandlerId++; auto it = m_handlers.find(typeId); if (it == m_handlers.end()) { m_handlers[typeId] = std::vector(); } HandlerEntry entry; entry.id = handlerId; entry.handler = [handler](const void* data) { handler(*static_cast(data)); }; m_handlers[typeId].push_back(entry); return handlerId; } template void Unsubscribe(uint64_t handlerId) { static_assert(sizeof(T) > 0, "Event type must be defined"); size_t typeId = typeid(T).hash_code(); auto it = m_handlers.find(typeId); if (it != m_handlers.end()) { auto& handlers = it->second; handlers.erase( std::remove_if(handlers.begin(), handlers.end(), [handlerId](const HandlerEntry& entry) { return entry.id == handlerId; }), handlers.end() ); } } template void Publish(const T& event) { static_assert(sizeof(T) > 0, "Event type must be defined"); size_t typeId = typeid(T).hash_code(); auto it = m_handlers.find(typeId); if (it != m_handlers.end()) { for (const auto& entry : it->second) { entry.handler(&event); } } } void Clear() { m_handlers.clear(); } private: struct HandlerEntry { uint64_t id; std::function handler; }; std::unordered_map> m_handlers; uint64_t m_nextHandlerId = 0; }; } }