// Generated by nim-ffi native C++ codegen. Do not edit by hand. // // Native (zero-serialization) wrapper over the C ABI in "echo.h". Struct params/returns cross as flat C-POD structs — no CBOR. For the // inter-process path use the CBOR header (echo_cbor.hpp). #ifndef NIM_FFI_GEN_ECHO_NATIVE_HPP #define NIM_FFI_GEN_ECHO_NATIVE_HPP #include "echo.h" #include #include #include #include #include #include #include #include #include namespace echo { struct EchoConfig { std::string prefix{}; }; struct EchoConfigC { ::EchoConfig c{}; }; inline EchoConfigC toC(const EchoConfig& v) { EchoConfigC h; h.c.prefix = v.prefix.c_str(); return h; } inline EchoConfig fromC(const ::EchoConfig& c) { EchoConfig v{}; v.prefix = c.prefix ? std::string(c.prefix) : std::string(); return v; } struct ShoutRequest { std::string text{}; }; struct ShoutRequestC { ::ShoutRequest c{}; }; inline ShoutRequestC toC(const ShoutRequest& v) { ShoutRequestC h; h.c.text = v.text.c_str(); return h; } inline ShoutRequest fromC(const ::ShoutRequest& c) { ShoutRequest v{}; v.text = c.text ? std::string(c.text) : std::string(); return v; } struct ShoutResponse { std::string shouted{}; std::string prefix{}; }; struct ShoutResponseC { ::ShoutResponse c{}; }; inline ShoutResponseC toC(const ShoutResponse& v) { ShoutResponseC h; h.c.shouted = v.shouted.c_str(); h.c.prefix = v.prefix.c_str(); return h; } inline ShoutResponse fromC(const ::ShoutResponse& c) { ShoutResponse v{}; v.shouted = c.shouted ? std::string(c.shouted) : std::string(); v.prefix = c.prefix ? std::string(c.prefix) : std::string(); return v; } namespace detail { template struct Capture { int ret = RET_ERR; T value{}; std::string err; std::promise done; }; struct AckCapture { int ret = RET_ERR; std::string err; std::promise done; }; inline std::string rawText(const char* msg, std::size_t len) { return (msg && len) ? std::string(msg, len) : std::string(); } } // namespace detail extern "C" { inline void echo_native_ack(int ret, const char* msg, std::size_t len, void* ud) { auto* c = static_cast(ud); c->ret = ret; if (ret == RET_ERR) c->err = detail::rawText(msg, len); c->done.set_value(); } inline void echo_native_str(int ret, const char* msg, std::size_t len, void* ud) { auto* c = static_cast*>(ud); c->ret = ret; if (ret == RET_OK) c->value = detail::rawText(msg, len); else c->err = detail::rawText(msg, len); c->done.set_value(); } inline void echo_native_echo_shout(int ret, const char* msg, std::size_t len, void* ud) { auto* c = static_cast*>(ud); c->ret = ret; if (ret == RET_OK) c->value = fromC(*reinterpret_cast(msg)); else c->err = detail::rawText(msg, len); c->done.set_value(); } } // extern "C" class EchoNode { public: explicit EchoNode(const EchoConfig& config) { detail::AckCapture cap; auto fut = cap.done.get_future(); auto c_config = toC(config); ctx_ = echo_create(c_config.c, echo_native_ack, &cap); if (!ctx_) throw std::runtime_error("echo_create returned null"); fut.wait(); if (cap.ret != RET_OK) throw std::runtime_error(cap.err); } ShoutResponse Shout(const ShoutRequest& req) { detail::Capture cap; auto fut = cap.done.get_future(); auto c_req = toC(req); if (echo_shout(ctx_, echo_native_echo_shout, &cap, c_req.c) != RET_OK) throw std::runtime_error("echo_shout dispatch failed"); fut.wait(); if (cap.ret != RET_OK) throw std::runtime_error(cap.err); return cap.value; } std::string Version() { detail::Capture cap; auto fut = cap.done.get_future(); if (echo_version(ctx_, echo_native_str, &cap) != RET_OK) throw std::runtime_error("echo_version dispatch failed"); fut.wait(); if (cap.ret != RET_OK) throw std::runtime_error(cap.err); return cap.value; } ~EchoNode() { if (ctx_) echo_destroy(ctx_); } EchoNode(const EchoNode&) = delete; EchoNode& operator=(const EchoNode&) = delete; private: void* ctx_ = nullptr; }; } // namespace echo #endif // NIM_FFI_GEN_ECHO_NATIVE_HPP