Implementation:Alibaba MNN FlatBuffers Reflection Header
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Serialization, Reflection |
| Last Updated | 2026-02-10 12:00 GMT |
Overview
Full reflection API enabling runtime schema introspection and dynamic field access on FlatBuffer objects.
Description
reflection.h provides utilities for accessing FlatBuffer data dynamically using a binary schema (a .bfbs file compiled by flatc). It includes functions to get and set fields by name or index at runtime, resize vectors, and copy tables between buffers. This is more powerful than mini-reflection but requires shipping the binary schema alongside the data.
Usage
Vendored dependency used internally by MNN for model serialization. Not directly imported by end users.
Code Reference
Source Location
- Repository: Alibaba_MNN
- File:
3rd_party/flatbuffers/include/flatbuffers/reflection.h - Lines: 1-477
Signature
const Table *GetAnyRoot(const uint8_t *flatbuf);
int64_t GetAnyFieldI(const Table &table, const reflection::Field &field);
std::string GetAnyFieldS(const Table &table, const reflection::Field &field,
const reflection::Schema &schema);
void SetAnyFieldI(Table *table, const reflection::Field &field, int64_t val);
Import
#include "flatbuffers/reflection.h"
I/O Contract
| Input | Output |
|---|---|
FlatBuffer bytes + binary reflection schema (.bfbs) |
Dynamic field access by name or index at runtime |
| Table reference + Field descriptor + new value | Mutated FlatBuffer field in-place |
Usage Examples
#include "flatbuffers/reflection.h"
#include "flatbuffers/reflection_generated.h"
auto &schema = *reflection::GetSchema(schema_bfbs);
auto root_table = schema.root_table();
auto *field = root_table->fields()->LookupByKey("hp");
auto root = flatbuffers::GetAnyRoot(buffer);
int64_t hp = flatbuffers::GetAnyFieldI(*root, *field);
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment