假设我有以下Flatbuffers模式文件:
union WeaponProperties {
sword: PSword,
axe: PAxe,
mace: PMace,
}
struct PSword { length: float32; straight: bool; }
struct PAxe { bearded: bool; }
struct PMace { hardness: int8; }
table Weapon {
name: string;
mindamage: int32;
maxdamage: int32;
swingtime: float32;
weight: float32;
properties: WeaponProperties;
}
root_type Weapon;
在此架构文件上运行flatc
编译器后,生成的weapon_generated.h
文件将大致具有以下结构:
enum WeaponProperties {
WeaponProperties_NONE = 0,
WeaponProperties_sword = 1,
WeaponProperties_axe = 2,
WeaponProperties_mace = 3,
...
};
struct PSword { ... }
struct PAxe { ... }
struct PMace { ... }
class Weapon { ... }
class WeaponBuilder { ... }
inline flatbuffers::Offset<Weapon> CreateWeaponDirect(
flatbuffers::FlatBufferBuilder &_fbb,
const char *name = nullptr,
int32_t mindamage = 0,
int32_t maxdamage = 0,
float swingtime = 0.0f,
float weight = 0.0f,
WeaponProperties properties_type = WeaponProperties_NONE,
flatbuffers::Offset<void> properties = 0) { ... }
值得注意的是,为了使用CreateWeaponDirect()
或WeaponBuilder::add_properties()
,我必须传递properties
参数,该参数必须为flatbuffers::Offset<void>
类型。但是我不了解的是如何首先创建此对象。据我所知,在生成的.h
文件中没有函数/方法可以返回这种类型的对象。我当然可以创建类型为PSword
/ PAxe
/ PMace
的对象,但是如何将其转换为Flatbuffers偏移量?
答案 0 :(得分:1)
您想要FlatBufferBuilder::CreateStruct
。与通常序列化结构(内联)的方式相比,这确实有点怪异,这是由于工会希望所有工会成员的大小相同,因此要在偏移量上引用它们。