208 lines
5.6 KiB
C++
208 lines
5.6 KiB
C++
#pragma once
|
|
|
|
#include <echoion/api/module/Module.hpp>
|
|
#include <echoion/api/module/ModuleAbi.h>
|
|
|
|
#include <Void/Data/Json.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <limits>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
namespace Echoion::Api::Module
|
|
{
|
|
namespace Detail
|
|
{
|
|
inline echoion_string_view_v1
|
|
stringView(const std::string_view value) noexcept
|
|
{
|
|
return {.data = value.data(), .size = value.size()};
|
|
}
|
|
|
|
inline echoion_status_v1
|
|
status(const Void::Result::Result<void>& result) noexcept
|
|
{
|
|
return result ? ECHOION_STATUS_OK_V1 : ECHOION_STATUS_INTERNAL_V1;
|
|
}
|
|
} // namespace Detail
|
|
|
|
// Transitional adapter for existing C++ lifecycle implementations. The
|
|
// exported descriptor and calls are pure C. C++ ownership and exceptions
|
|
// remain confined to the module that created the opaque instance.
|
|
template <Void::Plugin::module_factory_fn Factory>
|
|
struct LegacyModuleAbi final {
|
|
struct Instance {
|
|
std::unique_ptr<Void::Plugin::IModule> module;
|
|
std::string readinessDetail;
|
|
};
|
|
|
|
static echoion_status_v1
|
|
create(const echoion_module_host_v1* host,
|
|
echoion_module_instance_v1* output) noexcept
|
|
{
|
|
static_cast<void>(host);
|
|
if (output == nullptr) {
|
|
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
|
}
|
|
try {
|
|
auto module = Factory();
|
|
if (!module) {
|
|
return ECHOION_STATUS_INTERNAL_V1;
|
|
}
|
|
auto instance = std::make_unique<Instance>(Instance{
|
|
.module = std::move(module), .readinessDetail = {}});
|
|
*output = instance.release();
|
|
return ECHOION_STATUS_OK_V1;
|
|
} catch (...) {
|
|
return ECHOION_STATUS_INTERNAL_V1;
|
|
}
|
|
}
|
|
|
|
static void destroy(echoion_module_instance_v1 value) noexcept
|
|
{
|
|
std::unique_ptr<Instance> instance{
|
|
static_cast<Instance*>(value)};
|
|
}
|
|
|
|
static echoion_status_v1
|
|
activate(echoion_module_instance_v1 value) noexcept
|
|
{
|
|
Instance* instance = checked(value);
|
|
if (instance == nullptr) {
|
|
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
|
}
|
|
try {
|
|
instance->module->on_load();
|
|
return ECHOION_STATUS_OK_V1;
|
|
} catch (...) {
|
|
return ECHOION_STATUS_INTERNAL_V1;
|
|
}
|
|
}
|
|
|
|
static echoion_status_v1
|
|
quiesce(echoion_module_instance_v1 value,
|
|
const std::int64_t deadlineNanoseconds) noexcept
|
|
{
|
|
Instance* instance = checked(value);
|
|
if (instance == nullptr || deadlineNanoseconds < 0) {
|
|
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
|
}
|
|
try {
|
|
const auto deadline = std::chrono::steady_clock::time_point{
|
|
std::chrono::nanoseconds{deadlineNanoseconds}};
|
|
return Detail::status(instance->module->quiesce(deadline));
|
|
} catch (...) {
|
|
return ECHOION_STATUS_INTERNAL_V1;
|
|
}
|
|
}
|
|
|
|
static echoion_status_v1
|
|
checkpoint(echoion_module_instance_v1 value,
|
|
echoion_write_bytes_fn_v1 writer,
|
|
void* writerContext) noexcept
|
|
{
|
|
Instance* instance = checked(value);
|
|
if (instance == nullptr || writer == nullptr) {
|
|
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
|
}
|
|
try {
|
|
const std::string encoded =
|
|
Void::Json::serialize(instance->module->checkpoint());
|
|
return writer(
|
|
writerContext,
|
|
{.data = reinterpret_cast<const std::uint8_t*>(
|
|
encoded.data()),
|
|
.size = encoded.size()});
|
|
} catch (...) {
|
|
return ECHOION_STATUS_INTERNAL_V1;
|
|
}
|
|
}
|
|
|
|
static echoion_status_v1
|
|
restore(echoion_module_instance_v1 value,
|
|
const echoion_bytes_view_v1 state) noexcept
|
|
{
|
|
Instance* instance = checked(value);
|
|
if (instance == nullptr ||
|
|
(state.data == nullptr && state.size != 0)) {
|
|
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
|
}
|
|
try {
|
|
const std::string_view encoded =
|
|
state.size == 0
|
|
? std::string_view{}
|
|
: std::string_view{
|
|
reinterpret_cast<const char*>(state.data),
|
|
state.size};
|
|
const auto parsed = Void::Json::parse(encoded);
|
|
if (!parsed) {
|
|
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
|
}
|
|
return Detail::status(instance->module->restore(*parsed));
|
|
} catch (...) {
|
|
return ECHOION_STATUS_INTERNAL_V1;
|
|
}
|
|
}
|
|
|
|
static echoion_status_v1
|
|
readiness(echoion_module_instance_v1 value,
|
|
echoion_module_readiness_v1* output) noexcept
|
|
{
|
|
Instance* instance = checked(value);
|
|
if (instance == nullptr || output == nullptr ||
|
|
output->struct_size < sizeof(echoion_module_readiness_v1)) {
|
|
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
|
}
|
|
try {
|
|
const auto result = instance->module->readiness();
|
|
instance->readinessDetail = result.detail;
|
|
output->ready = result.ready ? 1U : 0U;
|
|
output->detail =
|
|
Detail::stringView(instance->readinessDetail);
|
|
return ECHOION_STATUS_OK_V1;
|
|
} catch (...) {
|
|
return ECHOION_STATUS_INTERNAL_V1;
|
|
}
|
|
}
|
|
|
|
static echoion_status_v1
|
|
deactivate(echoion_module_instance_v1 value) noexcept
|
|
{
|
|
Instance* instance = checked(value);
|
|
if (instance == nullptr) {
|
|
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
|
}
|
|
try {
|
|
return Detail::status(instance->module->deactivate());
|
|
} catch (...) {
|
|
return ECHOION_STATUS_INTERNAL_V1;
|
|
}
|
|
}
|
|
|
|
inline static const echoion_module_api_v1 api{
|
|
.struct_size = sizeof(echoion_module_api_v1),
|
|
.abi_version = ECHOION_MODULE_ABI_VERSION,
|
|
.create = create,
|
|
.destroy = destroy,
|
|
.activate = activate,
|
|
.quiesce = quiesce,
|
|
.checkpoint = checkpoint,
|
|
.restore = restore,
|
|
.readiness = readiness,
|
|
.deactivate = deactivate,
|
|
.reserved = {},
|
|
};
|
|
|
|
private:
|
|
|
|
[[nodiscard]] static Instance*
|
|
checked(echoion_module_instance_v1 value) noexcept
|
|
{
|
|
return static_cast<Instance*>(value);
|
|
}
|
|
};
|
|
|
|
} // namespace Echoion::Api::Module
|