nim-ffi/examples/timer/cpp_native_bindings
Ivan FB 725a7b6551
feat(codegen): native (non-CBOR) C++ generator — core
A native C++ binding generator (`cpp_native.nim`), the C++ counterpart of the C
and Go native paths and companion to the CBOR `cpp.nim`. It emits
`<lib>_native.hpp`: an idiomatic C++ struct + `toC`/`fromC` per `{.ffi.}` type,
and a `<Lib>Node` class whose methods marshal typed args into / read typed
struct returns out of the native ABI (`<name>` entry points + flat C structs in
`<lib>.h`) — zero serialization. Wired into genBindings under
`targetLang=cpp` + `-d:ffiMode=native`; emits the native C header alongside so
the binding is self-contained. Task: `genbindings_cpp_native`.

First cut covers scalar/string/bool/nested-struct fields (create/version/echo);
seq/Option params are `// SKIPPED`, and native typed events are next. Filename
is `_native.hpp` for now to coexist with the CBOR `.hpp` (rename is a follow-up).

Verified end-to-end: the generated example builds and round-trips a typed
EchoResponse (`make run`).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-05-31 18:39:19 +02:00
..

C++ bindings — native (generated)

Generated native (zero-serialization) C++ bindings for the timer library — the C++ counterpart of c_bindings / go_bindings. The CBOR C++ bindings live in ../cpp_bindings.

File Description
my_timer_native.hpp Generated wrapper: a C++ struct + toC/fromC per {.ffi.} type, and a My_timerNode class whose methods marshal typed args into / read typed struct returns out of the native ABI — no CBOR.
my_timer.h Native C header (structs + entry points) the .hpp includes.
main.cpp, Makefile A driver + build.
my_timer::My_timerNode node(my_timer::TimerConfig{"my-app"});
std::cout << node.Version();
auto r = node.Echo(my_timer::EchoRequest{"hello", 5});  // -> EchoResponse
std::cout << r.echoed << " / " << r.timerName;

Regenerate with nimble genbindings_cpp_native (from the repo root).

Build & run

cd examples/timer/cpp_native_bindings
make run

Status

First cut. Methods whose params/returns use only scalar / string / bool / nested-struct fields are generated (create, version, echo). Methods using sequences or optionals are emitted as // SKIPPED for now (complex, schedule) — those plus native typed events are the next increments. The native-bare / _cbor filename reconciliation (matching the C headers) is also a follow-up; today this emits my_timer_native.hpp so it coexists with the CBOR my_timer.hpp.