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:
faumarayandClaude Fable 5 committed 2026-07-11 23:39:10 +03:00
1 parent 7ca0e1b62e
commit ee4486e1d2
15 files changed
+750 -24

No files matched your search

+29 -6
View File
@@ -47,6 +47,14 @@ namespace Void::Service
{
}
// The reload callback captures `this`; the binding must
// deregister it before dying and therefore cannot move.
PluginServiceBinding(const PluginServiceBinding&) = delete;
PluginServiceBinding&
operator=(const PluginServiceBinding&) = delete;
~PluginServiceBinding() { disable_hot_reload(); }
/// Resolve the current instance's service and (re)register it.
[[nodiscard]] bool bind()
{
@@ -77,22 +85,37 @@ namespace Void::Service
/// Auto-rebind on hot reload (debounced file watching). The
/// manager keeps old instances alive while pinned, so the swap
/// is seamless.
/// is seamless. Idempotent.
void enable_hot_reload()
{
m_manager->on_reloaded([this](std::string_view name) {
if (name == m_name) {
(void)bind();
}
});
if (m_reload_id != 0) {
return;
}
m_reload_id =
m_manager->on_reloaded([this](std::string_view name) {
if (name == m_name) {
(void)bind();
}
});
m_manager->watch(m_name);
}
/// Stop observing reloads (the file watch itself is shared and
/// stays). Idempotent; called by the destructor.
void disable_hot_reload()
{
if (m_reload_id != 0) {
m_manager->remove_on_reloaded(m_reload_id);
m_reload_id = 0;
}
}
private:
Plugin::PluginManager* m_manager;
ServiceRegistry* m_registry;
std::string m_name;
Plugin::ReloadCallbackId m_reload_id{0};
};
} // namespace Void::Service
+16
View File
@@ -80,6 +80,22 @@ namespace Void::Service
m_services.insert_or_assign(key, std::move(entry));
}
/// Withdraw the implementation for interface I from this registry
/// (a parent's entry is unaffected: later lookups fall through to
/// it again). Consumers still holding the shared_ptr keep the
/// instance alive; only new lookups miss. Returns true when an
/// entry was removed.
template <typename I> bool remove()
{
const std::type_index key{typeid(I)};
std::unique_lock lock{m_mutex};
if (m_services.erase(key) == 0) {
return false;
}
std::erase(m_order, key);
return true;
}
/// Resolve I, or nullptr if not provided. Falls through to the
/// parent registry (if any) on a local miss.
template <typename I> [[nodiscard]] std::shared_ptr<I> get() const