#pragma once #include #include #include #include #include "event.h" namespace roro { // Services publish from any task; the UI task calls dispatch() to deliver events to subscribers. // Subscribe only during setup, from the UI task. class EventBus { public: using Handler = std::function; explicit EventBus(size_t capacity = 32); // Thread-safe. Returns false (and counts a drop) if the queue is full. bool publish(const Event& event); void subscribe(EventType type, Handler handler); // Delivers the events queued before this call; events published meanwhile wait for the next one. // Returns how many events were delivered. size_t dispatch(); uint32_t dropped() const; private: std::vector queue_; size_t head_ = 0; size_t count_ = 0; uint32_t dropped_ = 0; mutable std::mutex mutex_; std::array, static_cast(EventType::Count)> handlers_; }; } // namespace roro