一、FlatBuffers root_type 至多1个
- root_type 关键字用于指定根类型,而且只能指定一个根类型。
- 每个 FlatBuffers schema 文件只能有一个根类型。
- root_type 声明。这个声明指定了 FlatBuffers 文件的顶层结构,也就是文件的入口点,用于表示整个数据结构的起始点。
- 只有根节点才会使用相关GetRoot的方法
- 如果有union,需要判断类型再做实例
- 观察生成python脚本,两个重要的理念就是Bytes(字节),Pos(偏移量)
二、使用示例
sudo apt install flatbuffers-compiler
flatc --python AdcBridge.fbs
pip install flatbuffers
三、python示例,网络教程比较简单,这里嵌套比较深入
namespace map.adas.flat;
table GPSLocation {
latitude: double;
longitude: double;
bearing: double;
altitude: double;
locationType: GPSLocationType = G_NOT_VALID;
}
table NavMapLocVersion2 {
egoLocAfterMatching: GPSLocation;
angle: float;
distance: int;
roadName: string;
usedRoadName: string;
eRoadName: string;
linkLength: double;
linkId: uint64;
speedLimit: int;
formOfWay: int;
matchState: int;
roadClass: RoadClass = R_NONE;
timestamp: uint64;
last_route_change_timestamp: uint64;
last_route_count: int32;
link_remain_dist: float;
linkIndex: int;
}
union SDMapInfo {
nodeInfo: NodeInfoData,
adasInfo: AdasInfoData,
nopInfo: NopInfoData,
mapLoc: NavMapLocVersion2,
routeMatchRequest: RouteMatchRequest,
routeMatchResponse: RouteMatchResponse,
nadInfo: NADInfo,
roadConditionInfo: RoadConditionInfo,
resetPredictiveTree: ResetPredictiveTree,
hdRoutesEvents: HDRoutesEvents,
bumpyPois: BumpyPoisInfo,
trafficLightCountdown: TrafficLightCountdownList,
}
table SDMapTable {
requestId: int;
data: SDMapInfo;
engineType:int;
}
root_type SDMapTable;
编译:
flatc --python AdcBridge.fbs
因为嵌套比较多,会生成很多脚本,使用时接口需要参照生成脚本,python示例(示例仅供理解,代码无法直接使用)
from map.adas.flat import SDMapTable,NavMapLocVersion2
monster = SDMapTable.SDMapTable.GetRootAsSDMapTable(SdMapPackage_one, 0)
if monster.DataType() == SDMapInfo.SDMapInfo().mapLoc:
mapLoc_class = NavMapLocVersion2.NavMapLocVersion2()
mapLoc_class.Init(monster.Data().Bytes, monster.Data().Pos)
else:
pass