#pragma once #include #include #include #include #include #include #include namespace JuiceAgent { namespace Services { class IService { public: virtual ~IService() = default; virtual const char* name() const = 0; virtual bool onInitialize() { return true; } virtual void onShutdown() {} protected: // Register `handler` to run after previously registered listeners. template void listen(void (ServiceT::*handler)(const EventT&)) { static_assert(std::is_base_of::value, "ServiceT must derive from IService"); auto* self = static_cast(this); trackListener( [self, handler](const EventT& event) { (self->*handler)(event); }, false); } // Register `handler` to run before previously registered listeners. // Use it when the ordering between listeners matters (e.g. a transformer // pipeline that must run before other consumers). template void listenFront(void (ServiceT::*handler)(const EventT&)) { static_assert(std::is_base_of::value, "ServiceT must derive from IService"); auto* self = static_cast(this); trackListener( [self, handler](const EventT& event) { (self->*handler)(event); }, true); } // Remove all listeners registered by this service. Call it in onShutdown() // so no callback outlives the service instance. void unlistenAll() { auto& bus = EventBus::getInstance(); for (auto& [type, handle] : handles_) { bus.unsubscribe(type, handle); } handles_.clear(); } private: template void trackListener(std::function callback, bool front) { auto& bus = EventBus::getInstance(); const EventBus::Handle handle = front ? bus.subscribeFront(std::move(callback)) : bus.subscribe(std::move(callback)); handles_.push_back({std::type_index(typeid(EventT)), handle}); } std::vector> handles_; }; } }