templated type

This commit is contained in:
2026-07-29 22:57:58 +02:00
parent 81e5f093f9
commit 4f6ad45b0e
10 changed files with 478 additions and 25 deletions
+44
View File
@@ -242,6 +242,50 @@ provide the following functions to aide in the serialization process:
}
```
- `native_type_template("type<{{T}}>")` (on a struct) together with
`native_type_template_arg("type")` (on a field of that struct type):
lets a single struct declaration back a native type that is a C++
template, instantiated differently per field, instead of requiring one
struct declaration (and `native_type`) per instantiation. For example:
```cpp
struct Box (native_type_template: "Native::Box<{{T}}>") {
value: int32;
}
table Example {
a: Box (native_inline, native_type_template_arg: "Native::TypeA");
b: Box (native_inline, native_type_template_arg: "Native::TypeB");
}
```
is equivalent to writing, on each field itself:
```cpp
a: Box (native_inline, native_type: "Native::Box<Native::TypeA>", native_type_pack_name: "BoxTypeA");
b: Box (native_inline, native_type: "Native::Box<Native::TypeB>", native_type_pack_name: "BoxTypeB");
```
`native_type_template_arg` also auto-derives a `native_type_pack_name` of
`<StructName><ArgShortName>` (here, `BoxTypeA`/`BoxTypeB`) so the Pack/UnPack
functions stay unique across instantiations without spelling it out
yourself; an explicit `native_type_pack_name` on the field still overrides
this. More than one template parameter is supported by separating them with
commas and using indexed placeholders:
```cpp
struct Pair (native_type_template: "Native::Pair<{{T0}}, {{T1}}>") {
value: int32;
}
table Example2 {
p: Pair (native_inline, native_type_template_arg: "Native::Key, Native::Value");
}
```
`native_type_template_arg` is only valid on fields whose type is a struct
(or vector of structs) that declares `native_type_template`.
- `native_type("type")` (on a table): Tables can also be represented with
native types. For example, the following schema: