Support native_type for tables when using the C++ object API. (#8668)

* Support native_type for tables when using the C++ object API.

If native_type is specified on a table:
- No object API struct type is generated.
- The object API refers to the table by its native_type.
- UnPack and Create<TableName> methods are declared but not defined; as they
  must be user-provided.

* Add tests for native_type on tables.

* Add documentation for native_type on tables.
This commit is contained in:
cosmith-nvidia
2025-11-05 07:50:10 -08:00
committed by GitHub
parent 4173b84d4b
commit 599847236c
7 changed files with 251 additions and 153 deletions

View File

@@ -242,6 +242,41 @@ provide the following functions to aide in the serialization process:
}
```
- `native_type("type")` (on a table): Tables can also be represented with
native types. For example, the following schema:
```cpp
table Matrix (native_type: "NativeMatrix") {
rows: int32;
columns: int32;
values: [float];
}
```
Would be represented by a user-defined C++ class:
```cpp
class NativeMatrix { ... }
```
In this case, the following function declarations are generated by the compiler.
The user must provide and link the matching function definitions:
```cpp
struct Matrix FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
// ...
static ::flatbuffers::Offset<Matrix> Pack(
::flatbuffers::FlatBufferBuilder& _fbb,
const NativeMatrix* _o,
const ::flatbuffers::rehasher_function_t* _rehasher = nullptr);
void UnPackTo(
NativeMatrix* _o,
const ::flatbuffers::resolver_function_t* _resolver = nullptr) const;
}
```
Finally, the following top-level attributes:
- `native_include("path")` (at file level): Because the `native_type` attribute