#include "event_bus.h" namespace roro { EventBus::EventBus(size_t capacity) : queue_(capacity) {} bool EventBus::publish(const Event& event) { std::lock_guard lock(mutex_); if (count_ == queue_.size()) { dropped_++; return false; } queue_[(head_ + count_) % queue_.size()] = event; count_++; return true; } void EventBus::subscribe(EventType type, Handler handler) { handlers_[static_cast(type)].push_back(std::move(handler)); } size_t EventBus::dispatch() { size_t pending; { std::lock_guard lock(mutex_); pending = count_; } for (size_t i = 0; i < pending; i++) { Event event; { std::lock_guard lock(mutex_); event = queue_[head_]; head_ = (head_ + 1) % queue_.size(); count_--; } for (auto& handler : handlers_[static_cast(event.type)]) handler(event); } return pending; } uint32_t EventBus::dropped() const { std::lock_guard lock(mutex_); return dropped_; } } // namespace roro