Files
Library/include/Void/Plugin/Plugin.hpp
T
faumaray f94366104e
CI / GCC 16 / C++26 / default (pull_request) Has been cancelled
CI / GCC 16 / C++26 / sanitizers (pull_request) Has been cancelled
CI / GCC 16 / C++26 / werror (pull_request) Has been cancelled
Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-12 02:37:49 +03:00

102 lines
5.1 KiB
C++

#pragma once
#include <cstdint>
#include <string_view>
namespace Void::Plugin
{
// ── ABI version ───────────────────────────────────────────────────────────────
// Bump whenever IPlugin's vtable order changes. Loader rejects mismatches.
inline constexpr std::uint32_t ABI_VERSION = 1;
// ── PluginInfo ────────────────────────────────────────────────────────────────
// Plain-C struct returned by the info entry point. All fields are C strings so
// they cross the ABI boundary without C++ allocator involvement.
struct PluginInfo {
std::uint32_t abi_version {ABI_VERSION};
const char* name {""};
const char* plugin_version{"0.0.0"};
const char* description {""};
};
// ── IPlugin ───────────────────────────────────────────────────────────────────
// Abstract interface every plugin must implement.
// Vtable layout is FROZEN: new pure-virtual methods go only at the END.
class IPlugin {
public:
virtual ~IPlugin() = default;
[[nodiscard]] virtual std::string_view name() const noexcept = 0;
// Called once after the plugin is successfully created and verified.
virtual void on_load() {}
// Called once before the plugin is destroyed (including during hot-reload).
virtual void on_unload() {}
};
// ── C ABI entry-point typedefs ────────────────────────────────────────────────
// The .so MUST export these symbols via extern "C":
//
// void* void_plugin_create() — construct and return IPlugin*
// void void_plugin_destroy(void*) — destruct the IPlugin*
// PluginInfo void_plugin_info() — return metadata + ABI version
//
// Optional (enables hot-reload state transfer):
// void void_plugin_transfer_state(void* old, void* new)
//
// Optional (enables host-supplied configuration):
// void void_plugin_configure(void* instance, const char* config)
// — called by the host with an opaque configuration string (typically
// JSON) after create and after each hot-reload swap, before
// on_load(). The string is only valid for the duration of the call.
//
// Optional (transactional activation check):
// int void_plugin_ready(void* instance)
// — called after on_load() and state restoration, immediately before
// the instance becomes visible. Return 0 when the plugin is ready.
// Any other value aborts load/reload and keeps the old instance.
//
// Optional v2 state protocol (preferred over transfer_state when both
// sides provide it; survives layout changes between plugin versions):
//
// uint32_t void_plugin_state_version()
// — schema version of the state this build serializes
// const char* void_plugin_serialize_state(void* instance)
// — heap-allocated JSON; release with void_plugin_free_string
// void void_plugin_free_string(const char*)
// — frees serialize output (allocator stays inside the .so)
// int void_plugin_deserialize_state(void* instance,
// const char* json, uint32_t from_version)
// — 0 on success; migrations from historical versions live in the
// NEW .so (it knows the old formats; Json::MigrationChain helps)
using create_fn = void* (*)(void);
using destroy_fn = void (*)(void*);
using info_fn = PluginInfo (*)(void);
using transfer_fn = void (*)(void* old_instance, void* new_instance);
using configure_fn = void (*)(void* instance, const char* config);
using ready_fn = int (*)(void* instance);
using state_version_fn = std::uint32_t (*)(void);
using serialize_fn = const char* (*)(void* instance);
using free_string_fn = void (*)(const char*);
using deserialize_fn = int (*)(void* instance, const char* json,
std::uint32_t from_version);
inline constexpr const char* kCreateSymbol = "void_plugin_create";
inline constexpr const char* kDestroySymbol = "void_plugin_destroy";
inline constexpr const char* kInfoSymbol = "void_plugin_info";
inline constexpr const char* kTransferSymbol = "void_plugin_transfer_state";
inline constexpr const char* kConfigureSymbol = "void_plugin_configure";
inline constexpr const char* kReadySymbol = "void_plugin_ready";
inline constexpr const char* kStateVersionSymbol = "void_plugin_state_version";
inline constexpr const char* kSerializeSymbol = "void_plugin_serialize_state";
inline constexpr const char* kFreeStringSymbol = "void_plugin_free_string";
inline constexpr const char* kDeserializeSymbol = "void_plugin_deserialize_state";
} // namespace Void::Plugin