739 lines
31 KiB
C++
739 lines
31 KiB
C++
#pragma once
|
||
// VDE 插件 C++ SDK(header-only,C++17)。
|
||
//
|
||
// 目标:在纯 C ABI(vde/plugin/vde_plugin_api.h)之上提供轻量 C++17 便利层,
|
||
// **不链接内核、不引入外部依赖**,可被官方/第三方插件直接 include:
|
||
// - HostServices:宿主服务表校验 + RAII(内存/错误/实体/三角化网格释放);
|
||
// - BufferOwner:host 返回缓冲(VdeBuffer)的 RAII 所有权(free_buffer/host->free);
|
||
// - ErrorMessage:host->set_error 包装与 printf 风格格式化;
|
||
// - JsonWriter:确定性 UTF-8 JSON 写出(对象/数组/字符串转义/数字/bool/null);
|
||
// - PluginInfo / Registration<T>:vde_plugin_query / vde_plugin_init 注册辅助。
|
||
//
|
||
// 使用约定(与 ABI 文档一致):
|
||
// - 插件 C++ 代码可自由使用本头文件;导出符号仍需 extern "C" vde_plugin_query/init/shutdown
|
||
// (或使用本头的 query_plugin()/initialize_plugin() 辅助实现);
|
||
// - 跨 ABI 边界的字符串指针(PluginInfo 的 id/name/version/...)必须比插件实例存活更久,
|
||
// 推荐放在 static/常量区;
|
||
// - 所有 host->alloc 分配的内存必须由 host->free 释放;host 服务返回的 VdeBuffer/VdeMeshData
|
||
// 必须由 free_buffer 释放(BufferOwner / HostServices::MeshData 自动处理)。
|
||
//
|
||
// 本头文件保持 ABI 无状态(无全局对象、无固定缓冲),可安全用于多插件同进程。
|
||
#include <charconv>
|
||
#include <cmath>
|
||
#include <cstdarg>
|
||
#include <system_error>
|
||
#include <cstddef>
|
||
#include <cstdint>
|
||
#include <cstdio>
|
||
#include <cstring>
|
||
#include <string>
|
||
#include <string_view>
|
||
#include <type_traits>
|
||
#include <utility>
|
||
#include <vector>
|
||
|
||
#include "vde/plugin/vde_plugin_api.h"
|
||
|
||
namespace vde::plugin::sdk {
|
||
|
||
/// SDK 头文件版本(供插件在 describe/日志中标注;与 C ABI 版本独立)。
|
||
inline constexpr std::uint32_t kSdkVersionMajor = 1;
|
||
inline constexpr std::uint32_t kSdkVersionMinor = 0;
|
||
|
||
/// struct_size 是否覆盖 [offset, offset+field_size)。
|
||
inline constexpr bool struct_covers(std::size_t struct_size, std::size_t offset,
|
||
std::size_t field_size) noexcept {
|
||
return struct_size >= offset + field_size;
|
||
}
|
||
|
||
// ──────────────────────────────────────────────────────────────────────────
|
||
// HostServices:宿主服务表校验与 RAII 包装
|
||
// ──────────────────────────────────────────────────────────────────────────
|
||
class HostServices {
|
||
public:
|
||
HostServices() noexcept = default;
|
||
explicit HostServices(const VdePluginHost* host) noexcept : host_(host) {}
|
||
|
||
static HostServices from(const VdePluginHost* host) noexcept {
|
||
return HostServices(host);
|
||
}
|
||
|
||
/// 宿主表指针(可能为 nullptr;调用方自行判空)。
|
||
const VdePluginHost* raw() const noexcept { return host_; }
|
||
|
||
/// 是否可用于基础宿主服务(非空 + ABI major 匹配 + core 函数齐备)。
|
||
bool valid() const noexcept { return abi_major_ok() && has_core(); }
|
||
explicit operator bool() const noexcept { return valid(); }
|
||
|
||
bool abi_major_ok() const noexcept {
|
||
return host_ != nullptr && host_->abi_major == VDE_PLUGIN_ABI_MAJOR;
|
||
}
|
||
|
||
std::uint32_t abi_minor() const noexcept {
|
||
return host_ != nullptr ? host_->abi_minor : 0u;
|
||
}
|
||
|
||
/// 基础服务:alloc/free/set_error/body_destroy/free_buffer 均可用。
|
||
bool has_core() const noexcept {
|
||
if (host_ == nullptr) return false;
|
||
const std::size_t size = host_->struct_size;
|
||
return struct_covers(size, offsetof(VdePluginHost, alloc), sizeof(host_->alloc)) &&
|
||
struct_covers(size, offsetof(VdePluginHost, free), sizeof(host_->free)) &&
|
||
struct_covers(size, offsetof(VdePluginHost, set_error),
|
||
sizeof(host_->set_error)) &&
|
||
struct_covers(size, offsetof(VdePluginHost, body_destroy),
|
||
sizeof(host_->body_destroy)) &&
|
||
struct_covers(size, offsetof(VdePluginHost, free_buffer),
|
||
sizeof(host_->free_buffer)) &&
|
||
host_->alloc != nullptr && host_->free != nullptr &&
|
||
host_->set_error != nullptr && host_->body_destroy != nullptr &&
|
||
host_->free_buffer != nullptr;
|
||
}
|
||
|
||
/// 读取服务:body_from_mesh(ABI minor >= 1)。
|
||
bool has_body_from_mesh() const noexcept {
|
||
if (host_ == nullptr) return false;
|
||
return struct_covers(host_->struct_size, offsetof(VdePluginHost, body_from_mesh),
|
||
sizeof(host_->body_from_mesh)) &&
|
||
host_->body_from_mesh != nullptr;
|
||
}
|
||
|
||
/// 写出服务:body_tessellate(P0 core 几何服务)。
|
||
bool has_body_tessellate() const noexcept {
|
||
if (host_ == nullptr) return false;
|
||
return struct_covers(host_->struct_size,
|
||
offsetof(VdePluginHost, body_tessellate),
|
||
sizeof(host_->body_tessellate)) &&
|
||
host_->body_tessellate != nullptr;
|
||
}
|
||
|
||
/// 测量服务:body_query/body_volume/body_area/body_bbox 齐备。
|
||
bool has_measure() const noexcept {
|
||
if (host_ == nullptr) return false;
|
||
const std::size_t size = host_->struct_size;
|
||
return struct_covers(size, offsetof(VdePluginHost, body_query),
|
||
sizeof(host_->body_query)) &&
|
||
struct_covers(size, offsetof(VdePluginHost, body_volume),
|
||
sizeof(host_->body_volume)) &&
|
||
struct_covers(size, offsetof(VdePluginHost, body_area),
|
||
sizeof(host_->body_area)) &&
|
||
struct_covers(size, offsetof(VdePluginHost, body_bbox),
|
||
sizeof(host_->body_bbox)) &&
|
||
host_->body_query != nullptr && host_->body_volume != nullptr &&
|
||
host_->body_area != nullptr && host_->body_bbox != nullptr;
|
||
}
|
||
|
||
// ── 基础服务 ──
|
||
void* alloc(std::size_t size) const noexcept {
|
||
return (host_ != nullptr && host_->alloc != nullptr) ? host_->alloc(size) : nullptr;
|
||
}
|
||
|
||
void free(void* pointer) const noexcept {
|
||
if (host_ != nullptr && host_->free != nullptr && pointer != nullptr)
|
||
host_->free(pointer);
|
||
}
|
||
|
||
bool set_error(const char* message) const noexcept {
|
||
if (host_ == nullptr || host_->set_error == nullptr) return false;
|
||
host_->set_error(message != nullptr ? message : "");
|
||
return true;
|
||
}
|
||
|
||
bool set_error(const std::string& message) const noexcept {
|
||
return set_error(message.c_str());
|
||
}
|
||
|
||
// ── 实体 ──
|
||
/// 由三角网格创建实体(需要 body_from_mesh;失败返回 nullptr,宿主已 set_error)。
|
||
VdeBody body_from_mesh(const double* positions, std::size_t num_vertices,
|
||
const std::uint32_t* indices,
|
||
std::size_t num_triangles) const noexcept {
|
||
if (!has_body_from_mesh()) return nullptr;
|
||
return host_->body_from_mesh(positions, num_vertices, indices, num_triangles);
|
||
}
|
||
|
||
bool body_destroy(VdeBody body) const noexcept {
|
||
if (host_ == nullptr || host_->body_destroy == nullptr || body == nullptr)
|
||
return false;
|
||
host_->body_destroy(body);
|
||
return true;
|
||
}
|
||
|
||
// ── body_tessellate 输出 RAII(free_buffer 自动释放)──
|
||
class MeshData {
|
||
public:
|
||
MeshData() noexcept = default;
|
||
MeshData(const VdePluginHost* host, VdeMeshData data) noexcept
|
||
: host_(host), data_(data) {}
|
||
~MeshData() { release(); }
|
||
|
||
MeshData(const MeshData&) = delete;
|
||
MeshData& operator=(const MeshData&) = delete;
|
||
|
||
MeshData(MeshData&& other) noexcept : host_(other.host_), data_(other.data_) {
|
||
other.reset_storage();
|
||
}
|
||
MeshData& operator=(MeshData&& other) noexcept {
|
||
if (this != &other) {
|
||
release();
|
||
host_ = other.host_;
|
||
data_ = other.data_;
|
||
other.reset_storage();
|
||
}
|
||
return *this;
|
||
}
|
||
|
||
const VdeMeshData& get() const noexcept { return data_; }
|
||
const double* positions() const noexcept { return data_.positions; }
|
||
std::size_t num_vertices() const noexcept { return data_.num_vertices; }
|
||
const std::uint32_t* indices() const noexcept { return data_.indices; }
|
||
std::size_t num_triangles() const noexcept { return data_.num_triangles; }
|
||
const double* normals() const noexcept { return data_.normals; }
|
||
bool empty() const noexcept {
|
||
return data_.positions == nullptr || data_.num_vertices == 0 ||
|
||
data_.indices == nullptr || data_.num_triangles == 0;
|
||
}
|
||
explicit operator bool() const noexcept { return !empty(); }
|
||
|
||
/// 释放宿主分配的网格缓冲(幂等)。
|
||
void release() noexcept {
|
||
if (host_ != nullptr && host_->free_buffer != nullptr) {
|
||
if (data_.positions != nullptr) host_->free_buffer(data_.positions);
|
||
if (data_.indices != nullptr) host_->free_buffer(data_.indices);
|
||
if (data_.normals != nullptr) host_->free_buffer(data_.normals);
|
||
}
|
||
reset_storage();
|
||
}
|
||
|
||
/// 交出数据(调用方负责 free_buffer;本对象不再释放)。
|
||
VdeMeshData detach() noexcept {
|
||
VdeMeshData data = data_;
|
||
reset_storage();
|
||
return data;
|
||
}
|
||
|
||
private:
|
||
void reset_storage() noexcept {
|
||
data_ = VdeMeshData{};
|
||
host_ = nullptr;
|
||
}
|
||
|
||
const VdePluginHost* host_ = nullptr;
|
||
VdeMeshData data_{};
|
||
};
|
||
|
||
/// 三角化实体;成功填充 out(自动 free_buffer),失败返回 false。
|
||
bool tessellate(VdeBody body, double deflection, MeshData* out) const noexcept {
|
||
if (out == nullptr || !has_body_tessellate()) return false;
|
||
VdeMeshData data{};
|
||
data.struct_size = static_cast<std::uint32_t>(sizeof(VdeMeshData));
|
||
const int rc = host_->body_tessellate(body, deflection, &data);
|
||
if (rc != VDE_PLUGIN_OK) {
|
||
// 防御:部分宿主失败时也可能已分配缓冲,交 RAII 释放。
|
||
*out = MeshData(host_, data);
|
||
return false;
|
||
}
|
||
*out = MeshData(host_, data);
|
||
return true;
|
||
}
|
||
|
||
private:
|
||
const VdePluginHost* host_ = nullptr;
|
||
};
|
||
|
||
// ──────────────────────────────────────────────────────────────────────────
|
||
// BufferOwner:host 返回 VdeBuffer 的 RAII 所有权
|
||
// ──────────────────────────────────────────────────────────────────────────
|
||
class BufferOwner {
|
||
public:
|
||
/// 释放策略:free_buffer(host 服务返回的缓冲);host_free(host->alloc 分配的缓冲)。
|
||
enum class Deallocator { FreeBuffer, HostFree };
|
||
|
||
BufferOwner() noexcept = default;
|
||
BufferOwner(const VdePluginHost* host, VdeBuffer buffer,
|
||
Deallocator deallocator = Deallocator::FreeBuffer) noexcept
|
||
: host_(host), buffer_(buffer), deallocator_(deallocator) {}
|
||
~BufferOwner() { release(); }
|
||
|
||
BufferOwner(const BufferOwner&) = delete;
|
||
BufferOwner& operator=(const BufferOwner&) = delete;
|
||
|
||
BufferOwner(BufferOwner&& other) noexcept
|
||
: host_(other.host_), buffer_(other.buffer_), deallocator_(other.deallocator_) {
|
||
other.buffer_ = VdeBuffer{};
|
||
}
|
||
BufferOwner& operator=(BufferOwner&& other) noexcept {
|
||
if (this != &other) {
|
||
release();
|
||
host_ = other.host_;
|
||
buffer_ = other.buffer_;
|
||
deallocator_ = other.deallocator_;
|
||
other.buffer_ = VdeBuffer{};
|
||
}
|
||
return *this;
|
||
}
|
||
|
||
const VdeBuffer& get() const noexcept { return buffer_; }
|
||
void* data() const noexcept { return buffer_.data; }
|
||
std::size_t size() const noexcept { return buffer_.size; }
|
||
bool empty() const noexcept { return buffer_.data == nullptr || buffer_.size == 0; }
|
||
explicit operator bool() const noexcept { return !empty(); }
|
||
|
||
/// 是否持有缓冲(任意非空 data)。
|
||
bool owns() const noexcept { return buffer_.data != nullptr; }
|
||
|
||
/// 交出 VdeBuffer(调用方负责释放;本对象不再释放)。
|
||
VdeBuffer release_buffer() noexcept {
|
||
VdeBuffer buffer = buffer_;
|
||
buffer_ = VdeBuffer{};
|
||
return buffer;
|
||
}
|
||
|
||
/// 立即释放(幂等)。
|
||
void release() noexcept {
|
||
if (buffer_.data != nullptr && host_ != nullptr) {
|
||
if (deallocator_ == Deallocator::FreeBuffer) {
|
||
if (host_->free_buffer != nullptr) host_->free_buffer(buffer_.data);
|
||
} else {
|
||
if (host_->free != nullptr) host_->free(buffer_.data);
|
||
}
|
||
}
|
||
buffer_ = VdeBuffer{};
|
||
}
|
||
|
||
private:
|
||
const VdePluginHost* host_ = nullptr;
|
||
VdeBuffer buffer_{};
|
||
Deallocator deallocator_ = Deallocator::FreeBuffer;
|
||
};
|
||
|
||
// ──────────────────────────────────────────────────────────────────────────
|
||
// ErrorMessage:host->set_error 包装与 printf 风格格式化
|
||
// ──────────────────────────────────────────────────────────────────────────
|
||
class ErrorMessage {
|
||
public:
|
||
ErrorMessage() noexcept = default;
|
||
explicit ErrorMessage(const VdePluginHost* host) noexcept : host_(host) {}
|
||
|
||
const VdePluginHost* host() const noexcept { return host_; }
|
||
bool available() const noexcept {
|
||
return host_ != nullptr && host_->set_error != nullptr;
|
||
}
|
||
|
||
bool set(const char* message) const noexcept {
|
||
if (!available()) return false;
|
||
host_->set_error(message != nullptr ? message : "");
|
||
return true;
|
||
}
|
||
|
||
bool set(const std::string& message) const noexcept { return set(message.c_str()); }
|
||
|
||
bool set(std::string_view message) const noexcept {
|
||
return set(std::string(message));
|
||
}
|
||
|
||
/// printf 风格设置错误(截断安全;格式化后经 host->set_error 复制给宿主)。
|
||
bool setf(const char* format, ...) const noexcept {
|
||
if (!available() || format == nullptr) return false;
|
||
va_list args;
|
||
va_start(args, format);
|
||
const std::string message = vformat(format, args);
|
||
va_end(args);
|
||
host_->set_error(message.c_str());
|
||
return true;
|
||
}
|
||
|
||
/// printf 风格格式化(本 SDK 内部/插件日志复用)。
|
||
static std::string format(const char* format, ...) {
|
||
if (format == nullptr) return std::string();
|
||
va_list args;
|
||
va_start(args, format);
|
||
const std::string message = vformat(format, args);
|
||
va_end(args);
|
||
return message;
|
||
}
|
||
|
||
private:
|
||
static std::string vformat(const char* format, va_list args) {
|
||
va_list copy;
|
||
va_copy(copy, args);
|
||
char stack_buffer[256];
|
||
const int needed = std::vsnprintf(stack_buffer, sizeof(stack_buffer), format, copy);
|
||
va_end(copy);
|
||
if (needed <= 0) return std::string();
|
||
if (static_cast<std::size_t>(needed) < sizeof(stack_buffer))
|
||
return std::string(stack_buffer, static_cast<std::size_t>(needed));
|
||
std::string message(static_cast<std::size_t>(needed), '\0');
|
||
std::vsnprintf(&message[0], message.size() + 1, format, args);
|
||
return message;
|
||
}
|
||
|
||
const VdePluginHost* host_ = nullptr;
|
||
};
|
||
|
||
// ──────────────────────────────────────────────────────────────────────────
|
||
// JsonWriter:确定性 UTF-8 JSON 写出(无外部依赖)
|
||
// ──────────────────────────────────────────────────────────────────────────
|
||
class JsonWriter {
|
||
public:
|
||
JsonWriter() = default;
|
||
|
||
bool ok() const noexcept { return error_.empty(); }
|
||
const std::string& error() const noexcept { return error_; }
|
||
const std::string& str() const noexcept { return out_; }
|
||
|
||
/// 结束并取出 JSON(不清除错误状态;错误时返回值可能不完整/为空约定由调用方检查 ok())。
|
||
std::string take() {
|
||
std::string result = std::move(out_);
|
||
out_.clear();
|
||
stack_.clear();
|
||
root_started_ = false;
|
||
pending_key_ = false;
|
||
error_.clear();
|
||
return result;
|
||
}
|
||
|
||
void clear() noexcept {
|
||
out_.clear();
|
||
stack_.clear();
|
||
root_started_ = false;
|
||
pending_key_ = false;
|
||
error_.clear();
|
||
}
|
||
|
||
JsonWriter& begin_object() {
|
||
if (!begin_value()) return *this;
|
||
out_.push_back('{');
|
||
stack_.push_back(Level{Container::Object, true});
|
||
pending_key_ = false;
|
||
return *this;
|
||
}
|
||
|
||
JsonWriter& end_object() { return end_container(Container::Object, '}'); }
|
||
|
||
JsonWriter& begin_array() {
|
||
if (!begin_value()) return *this;
|
||
out_.push_back('[');
|
||
stack_.push_back(Level{Container::Array, true});
|
||
pending_key_ = false;
|
||
return *this;
|
||
}
|
||
|
||
JsonWriter& end_array() { return end_container(Container::Array, ']'); }
|
||
|
||
/// 对象键(仅对象内、且上一个键已有值后)。
|
||
JsonWriter& key(std::string_view key_text) {
|
||
if (!ok()) return *this;
|
||
if (stack_.empty() || stack_.back().type != Container::Object) {
|
||
fail("JsonWriter: key outside object");
|
||
return *this;
|
||
}
|
||
if (pending_key_) {
|
||
fail("JsonWriter: duplicate key without value");
|
||
return *this;
|
||
}
|
||
if (!stack_.back().first) out_.push_back(',');
|
||
write_escaped_string(key_text);
|
||
out_.push_back(':');
|
||
pending_key_ = true;
|
||
return *this;
|
||
}
|
||
|
||
JsonWriter& value(std::string_view text) {
|
||
if (!begin_value()) return *this;
|
||
write_escaped_string(text);
|
||
return *this;
|
||
}
|
||
JsonWriter& value(const char* text) {
|
||
return value(std::string_view(text != nullptr ? text : ""));
|
||
}
|
||
JsonWriter& value(bool boolean) {
|
||
if (!begin_value()) return *this;
|
||
out_.append(boolean ? "true" : "false");
|
||
return *this;
|
||
}
|
||
JsonWriter& null() {
|
||
if (!begin_value()) return *this;
|
||
out_.append("null");
|
||
return *this;
|
||
}
|
||
|
||
template <typename Integer,
|
||
typename = std::enable_if_t<std::is_integral_v<Integer> &&
|
||
!std::is_same_v<Integer, bool>>>
|
||
JsonWriter& value(Integer number) {
|
||
if (!begin_value()) return *this;
|
||
char buffer[32];
|
||
using Unsigned = std::make_unsigned_t<Integer>;
|
||
if constexpr (std::is_signed_v<Integer>) {
|
||
const auto converted = static_cast<long long>(number);
|
||
const auto result = std::to_chars(buffer, buffer + sizeof(buffer), converted);
|
||
append_number(buffer, result);
|
||
} else {
|
||
const auto converted = static_cast<unsigned long long>(
|
||
static_cast<Unsigned>(number));
|
||
const auto result = std::to_chars(buffer, buffer + sizeof(buffer), converted);
|
||
append_number(buffer, result);
|
||
}
|
||
return *this;
|
||
}
|
||
|
||
JsonWriter& value(float number) {
|
||
return value(static_cast<double>(number));
|
||
}
|
||
|
||
JsonWriter& value(double number) {
|
||
if (!begin_value()) return *this;
|
||
if (!std::isfinite(number)) {
|
||
out_.append("null");
|
||
fail("JsonWriter: non-finite number");
|
||
return *this;
|
||
}
|
||
char buffer[64];
|
||
const auto result = std::to_chars(buffer, buffer + sizeof(buffer), number);
|
||
if (result.ec != std::errc()) {
|
||
// 非有限数(NaN/Inf)不是合法 JSON:保持输出合法并置错误。
|
||
out_.append("null");
|
||
fail("JsonWriter: non-finite number");
|
||
return *this;
|
||
}
|
||
append_number(buffer, result);
|
||
return *this;
|
||
}
|
||
|
||
private:
|
||
enum class Container { Object, Array };
|
||
struct Level {
|
||
Container type = Container::Object;
|
||
bool first = true;
|
||
};
|
||
|
||
template <typename Result>
|
||
void append_number(const char* buffer, const Result& result) {
|
||
out_.append(buffer, static_cast<std::size_t>(result.ptr - buffer));
|
||
}
|
||
|
||
bool begin_value() {
|
||
if (!ok()) return false;
|
||
if (stack_.empty()) {
|
||
if (root_started_) {
|
||
fail("JsonWriter: multiple root values");
|
||
return false;
|
||
}
|
||
root_started_ = true;
|
||
return true;
|
||
}
|
||
Level& level = stack_.back();
|
||
if (level.type == Container::Object) {
|
||
if (!pending_key_) {
|
||
fail("JsonWriter: value without key in object");
|
||
return false;
|
||
}
|
||
// key() 已按需输出分隔逗号;此处只消费“等待值”状态。
|
||
pending_key_ = false;
|
||
level.first = false;
|
||
return true;
|
||
}
|
||
if (!level.first) out_.push_back(',');
|
||
level.first = false;
|
||
return true;
|
||
}
|
||
|
||
JsonWriter& end_container(Container expected, char close_char) {
|
||
if (!ok()) return *this;
|
||
if (stack_.empty() || stack_.back().type != expected) {
|
||
fail("JsonWriter: mismatched closing");
|
||
return *this;
|
||
}
|
||
if (pending_key_) {
|
||
fail("JsonWriter: object key without value");
|
||
return *this;
|
||
}
|
||
stack_.pop_back();
|
||
out_.push_back(close_char);
|
||
return *this;
|
||
}
|
||
|
||
JsonWriter& fail(const char* message) {
|
||
if (error_.empty()) error_ = message != nullptr ? message : "JsonWriter: error";
|
||
return *this;
|
||
}
|
||
|
||
void write_escaped_string(std::string_view text) {
|
||
out_.push_back('"');
|
||
for (const char raw : text) {
|
||
const auto byte = static_cast<unsigned char>(raw);
|
||
switch (raw) {
|
||
case '"': out_.append("\\\""); break;
|
||
case '\\': out_.append("\\\\"); break;
|
||
case '\b': out_.append("\\b"); break;
|
||
case '\f': out_.append("\\f"); break;
|
||
case '\n': out_.append("\\n"); break;
|
||
case '\r': out_.append("\\r"); break;
|
||
case '\t': out_.append("\\t"); break;
|
||
default:
|
||
if (byte < 0x20) {
|
||
char buffer[8];
|
||
std::snprintf(buffer, sizeof(buffer), "\\u%04x",
|
||
static_cast<unsigned>(byte));
|
||
out_.append(buffer);
|
||
} else {
|
||
out_.push_back(raw);
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
out_.push_back('"');
|
||
}
|
||
|
||
std::string out_;
|
||
std::string error_;
|
||
std::vector<Level> stack_;
|
||
bool root_started_ = false;
|
||
bool pending_key_ = false;
|
||
};
|
||
|
||
// ──────────────────────────────────────────────────────────────────────────
|
||
// PluginInfo / Registration<T>:vde_plugin_query/init 注册辅助
|
||
// ──────────────────────────────────────────────────────────────────────────
|
||
struct PluginInfo {
|
||
std::string id;
|
||
std::string name;
|
||
std::string version;
|
||
std::string capabilities; ///< 分号分隔能力 ID(如 "format:x.read;format:x.write")
|
||
std::string license; ///< SPDX 标识
|
||
VdePluginKind kind = VDE_PLUGIN_KIND_FORMAT;
|
||
};
|
||
|
||
/// 填充 VdePluginInfo(校验 struct_size/ABI major/id/version;指针指向 info 内部字符串,
|
||
/// 调用方必须保证 info 在本插件实例生命周期内存活,推荐 static)。
|
||
inline bool fill_plugin_info(const PluginInfo& plugin, std::uint32_t host_abi_major,
|
||
VdePluginInfo* out) noexcept {
|
||
if (out == nullptr || out->struct_size < sizeof(VdePluginInfo)) return false;
|
||
if (host_abi_major != VDE_PLUGIN_ABI_MAJOR) return false;
|
||
if (plugin.id.empty() || plugin.version.empty()) return false;
|
||
out->id = plugin.id.c_str();
|
||
out->name = plugin.name.empty() ? plugin.id.c_str() : plugin.name.c_str();
|
||
out->version = plugin.version.c_str();
|
||
out->kind = static_cast<std::uint32_t>(plugin.kind);
|
||
out->abi_major = VDE_PLUGIN_ABI_MAJOR;
|
||
out->abi_minor = VDE_PLUGIN_ABI_MINOR;
|
||
out->capabilities = plugin.capabilities.c_str();
|
||
out->license = plugin.license.c_str();
|
||
return true;
|
||
}
|
||
|
||
/// 按扩展点类型设置 VdePluginRegistration 对应字段(仅在宿主 struct_size 覆盖字段时写入)。
|
||
template <typename Extension>
|
||
class Registration {
|
||
public:
|
||
Registration(PluginInfo info, const Extension* extension) noexcept
|
||
: info_(std::move(info)), extension_(extension) {}
|
||
|
||
const PluginInfo& info() const noexcept { return info_; }
|
||
const Extension* extension() const noexcept { return extension_; }
|
||
|
||
bool fill_info(std::uint32_t host_abi_major, VdePluginInfo* out) const noexcept {
|
||
return fill_plugin_info(info_, host_abi_major, out);
|
||
}
|
||
|
||
/// 校验宿主并填充注册表(kind + 扩展点指针);失败不修改 reg。
|
||
bool initialize(const VdePluginHost* host, VdePluginRegistration* reg) const noexcept {
|
||
if (reg == nullptr || extension_ == nullptr) return false;
|
||
if (reg->struct_size < offsetof(VdePluginRegistration, kind) +
|
||
sizeof(reg->kind))
|
||
return false;
|
||
const HostServices services(host);
|
||
if (!services.has_core()) return false;
|
||
reg->kind = static_cast<std::uint32_t>(info_.kind);
|
||
set_extension(reg);
|
||
return true;
|
||
}
|
||
|
||
private:
|
||
void set_extension(VdePluginRegistration* reg) const noexcept {
|
||
if constexpr (std::is_same_v<Extension, VdeFormatPlugin>) {
|
||
if (reg->struct_size >= offsetof(VdePluginRegistration, format) +
|
||
sizeof(reg->format))
|
||
reg->format = extension_;
|
||
} else if constexpr (std::is_same_v<Extension, VdeCaeSolverPlugin>) {
|
||
if (reg->struct_size >= offsetof(VdePluginRegistration, cae_solver) +
|
||
sizeof(reg->cae_solver))
|
||
reg->cae_solver = extension_;
|
||
} else if constexpr (std::is_same_v<Extension, VdeCamStrategyPlugin>) {
|
||
if (reg->struct_size >= offsetof(VdePluginRegistration, cam_strategy) +
|
||
sizeof(reg->cam_strategy))
|
||
reg->cam_strategy = extension_;
|
||
} else if constexpr (std::is_same_v<Extension, VdeGcsSolverPlugin>) {
|
||
if (reg->struct_size >= offsetof(VdePluginRegistration, gcs_solver) +
|
||
sizeof(reg->gcs_solver))
|
||
reg->gcs_solver = extension_;
|
||
} else if constexpr (std::is_same_v<Extension, VdeMesherPlugin>) {
|
||
if (reg->struct_size >= offsetof(VdePluginRegistration, mesher) +
|
||
sizeof(reg->mesher))
|
||
reg->mesher = extension_;
|
||
} else if constexpr (std::is_same_v<Extension, VdeOptimizerPlugin>) {
|
||
if (reg->struct_size >= offsetof(VdePluginRegistration, optimizer) +
|
||
sizeof(reg->optimizer))
|
||
reg->optimizer = extension_;
|
||
} else if constexpr (std::is_same_v<Extension, VdeIoBatchPlugin>) {
|
||
if (reg->struct_size >= offsetof(VdePluginRegistration, io_batch) +
|
||
sizeof(reg->io_batch))
|
||
reg->io_batch = extension_;
|
||
} else if constexpr (std::is_same_v<Extension, VdeGeomOpPlugin>) {
|
||
if (reg->struct_size >= offsetof(VdePluginRegistration, geom_op) +
|
||
sizeof(reg->geom_op))
|
||
reg->geom_op = extension_;
|
||
} else if constexpr (std::is_same_v<Extension, VdeUiPlugin>) {
|
||
if (reg->struct_size >= offsetof(VdePluginRegistration, ui) +
|
||
sizeof(reg->ui))
|
||
reg->ui = extension_;
|
||
} else if constexpr (std::is_same_v<Extension, VdeBindingPlugin>) {
|
||
if (reg->struct_size >= offsetof(VdePluginRegistration, binding) +
|
||
sizeof(reg->binding))
|
||
reg->binding = extension_;
|
||
} else {
|
||
// 未知扩展:仅写 generic(保留字段),保证可扩展性。
|
||
if (reg->struct_size >= offsetof(VdePluginRegistration, generic) +
|
||
sizeof(reg->generic))
|
||
reg->generic = extension_;
|
||
}
|
||
}
|
||
|
||
PluginInfo info_;
|
||
const Extension* extension_ = nullptr;
|
||
};
|
||
|
||
using FormatRegistration = Registration<VdeFormatPlugin>;
|
||
using CaeSolverRegistration = Registration<VdeCaeSolverPlugin>;
|
||
using CamStrategyRegistration = Registration<VdeCamStrategyPlugin>;
|
||
using GcsSolverRegistration = Registration<VdeGcsSolverPlugin>;
|
||
using MesherRegistration = Registration<VdeMesherPlugin>;
|
||
using OptimizerRegistration = Registration<VdeOptimizerPlugin>;
|
||
using IoBatchRegistration = Registration<VdeIoBatchPlugin>;
|
||
using GeomOpRegistration = Registration<VdeGeomOpPlugin>;
|
||
using UiRegistration = Registration<VdeUiPlugin>;
|
||
using BindingRegistration = Registration<VdeBindingPlugin>;
|
||
|
||
/// vde_plugin_query 的便利实现:成功返回 VDE_PLUGIN_OK,否则 VDE_PLUGIN_ERR_ABI。
|
||
template <typename Extension>
|
||
inline int query_plugin(const Registration<Extension>& registration,
|
||
std::uint32_t host_abi_major, VdePluginInfo* info) noexcept {
|
||
return registration.fill_info(host_abi_major, info) ? VDE_PLUGIN_OK
|
||
: VDE_PLUGIN_ERR_ABI;
|
||
}
|
||
|
||
/// vde_plugin_init 的便利实现:失败返回 VDE_PLUGIN_ERR_INVALID_ARG/ABI。
|
||
template <typename Extension>
|
||
inline int initialize_plugin(const Registration<Extension>& registration,
|
||
const VdePluginHost* host,
|
||
VdePluginRegistration* reg) noexcept {
|
||
if (reg == nullptr) return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
if (reg->struct_size < sizeof(VdePluginRegistration))
|
||
return VDE_PLUGIN_ERR_ABI;
|
||
return registration.initialize(host, reg) ? VDE_PLUGIN_OK
|
||
: VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
|
||
} // namespace vde::plugin::sdk
|