Plugin: discovery, unload, config entry, multi-observer reloads, state API
Host-side additions for applications embedding a plugin system: - PluginManager::load_directory(dir) loads every shared-library file in a directory (filename order) with per-file expected results. - PluginManager::unload(name) drops the registry entry; the instance dies with its last pinned handle (RCU semantics, as on reload). - Optional C entry void_plugin_configure(inst, config): fed from PluginManagerOptions::config_provider after create and after each reload swap, before on_load(). - on_reloaded() is now multi-observer: returns a ReloadCallbackId, remove_on_reloaded() unregisters. Manual reload() now notifies observers too (previously only watched reloads did, contradicting the documented contract). PluginServiceBinding deregisters its callback on destruction (was a dangling-callback hazard). - PluginManager::serialize_state/deserialize_state expose the plugin's v2 state exports so an embedder can carry plugin state through its own hot reload. - ServiceRegistry::remove<I>() withdraws a provided service (child removal re-exposes a parent's entry). - EventBus::subscribe_scoped<E>() returns a move-only RAII Subscription that unsubscribes on destruction. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018yj92cCihyaYtf4gKCT3nv
This commit is contained in:
1 parent
7ca0e1b62e
commit
ee4486e1d2
15 files changed
+750
-24
No files matched your search
@@ -79,6 +79,76 @@ namespace Void::EventBus
|
||||
return id;
|
||||
}
|
||||
|
||||
// Move-only RAII handle for a subscription: unsubscribes on
|
||||
// destruction (or reset()). Must not outlive the bus it came
|
||||
// from — the bus is non-movable, so tie the Subscription's
|
||||
// lifetime to a scope the bus outlives.
|
||||
class Subscription
|
||||
{
|
||||
public:
|
||||
|
||||
Subscription() = default;
|
||||
|
||||
Subscription(Subscription&& other) noexcept
|
||||
: bus_(other.bus_), id_(other.id_)
|
||||
{
|
||||
other.bus_ = nullptr;
|
||||
}
|
||||
|
||||
Subscription& operator=(Subscription&& other) noexcept
|
||||
{
|
||||
if (this != &other) {
|
||||
reset();
|
||||
bus_ = other.bus_;
|
||||
id_ = other.id_;
|
||||
other.bus_ = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Subscription(const Subscription&) = delete;
|
||||
Subscription& operator=(const Subscription&) = delete;
|
||||
|
||||
~Subscription() { reset(); }
|
||||
|
||||
// Unsubscribe now; safe to call repeatedly.
|
||||
void reset()
|
||||
{
|
||||
if (bus_ != nullptr) {
|
||||
bus_->unsubscribe(id_);
|
||||
bus_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] explicit operator bool() const noexcept
|
||||
{
|
||||
return bus_ != nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
friend class EventBus;
|
||||
|
||||
Subscription(EventBus* bus, HandlerId id)
|
||||
: bus_(bus), id_(id)
|
||||
{
|
||||
}
|
||||
|
||||
EventBus* bus_ = nullptr;
|
||||
HandlerId id_ = 0;
|
||||
};
|
||||
|
||||
// Subscribe with automatic unsubscription: the returned handle
|
||||
// removes the handler when it goes out of scope. Suits handlers
|
||||
// owned by components (plugins, services) whose lifetime is
|
||||
// shorter than the bus.
|
||||
template <typename Event>
|
||||
[[nodiscard]] Subscription
|
||||
subscribe_scoped(std::function<void(const Event&)> handler)
|
||||
{
|
||||
return Subscription{this, subscribe<Event>(std::move(handler))};
|
||||
}
|
||||
|
||||
// Publish an event to all subscribers of its type.
|
||||
template <typename Event> void publish(const Event& event)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user