nim-ffi/examples/timer/cpp_native_bindings
Ivan FB 2302e5fb7d
feat(codegen): native C++ generator handles seq / Option / nested
Extends the native C++ marshalling to sequences and optionals: a C++ field maps
to std::vector<T> / std::optional<T>, and `toC` returns a holder that owns the
C-array backing (move/NRVO-safe std::vectors) while string pointers borrow the
C++ argument — valid for the call, which the library deep-copies. `fromC` reads
seq/Option back out of the C-POD.

Unblocks the timer's complex (seq-of-structs / seq-of-strings / two optionals)
and schedule (three struct params) methods — they now generate and round-trip
typed results. Verified end-to-end and ASAN-clean.

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

Requests are fully supported: scalar / string / bool / nested struct and now sequences (std::vector) and optionals (std::optional) — create, version, echo, complex, schedule all generate and round-trip typed values (ASAN-clean). toC uses a holder that owns the C-array backing while string pointers borrow the C++ argument (valid for the call's duration; the library deep-copies).

Still to come: native typed events (On<Event> handlers) and the native-bare / _cbor filename reconciliation (matching the C headers). Today this emits my_timer_native.hpp so it coexists with the CBOR my_timer.hpp.