#pragma once #include #include #include #include #include namespace whetstone { class DAPEventBroadcaster { public: using EventHandler = std::function; // Subscribe to events of eventType. Returns a subscription id. int subscribe(const std::string& eventType, EventHandler handler) { int id = nextId_++; subs_.push_back({id, eventType, std::move(handler)}); return id; } // Remove the subscription with the given id. void unsubscribe(int id) { subs_.erase( std::remove_if(subs_.begin(), subs_.end(), [id](const Sub& s){ return s.id == id; }), subs_.end()); } // Call all handlers registered for eventType, passing body. void broadcast(const std::string& eventType, const nlohmann::json& body) const { for (const auto& s : subs_) if (s.eventType == eventType) s.handler(body); } // Broadcast using event["event"] as the type, event itself as the body. void broadcastAll(const nlohmann::json& event) const { if (!event.contains("event") || !event["event"].is_string()) return; broadcast(event["event"].get(), event); } int subscriberCount(const std::string& eventType) const { int n = 0; for (const auto& s : subs_) if (s.eventType == eventType) ++n; return n; } private: struct Sub { int id; std::string eventType; EventHandler handler; }; std::vector subs_; int nextId_ = 1; }; } // namespace whetstone