mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-22 02:08:28 +00:00
Add pointer reference to sibling union field on FieldDef (#7755)
To make it simple to map between a union field and its union type field we are adding a pointer to FieldDef to point to each other. For all other types the pointer will be nullptr. Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
@@ -297,7 +297,8 @@ struct FieldDef : public Definition {
|
|||||||
flexbuffer(false),
|
flexbuffer(false),
|
||||||
presence(kDefault),
|
presence(kDefault),
|
||||||
nested_flatbuffer(nullptr),
|
nested_flatbuffer(nullptr),
|
||||||
padding(0) {}
|
padding(0),
|
||||||
|
sibling_union_field(nullptr){}
|
||||||
|
|
||||||
Offset<reflection::Field> Serialize(FlatBufferBuilder *builder, uint16_t id,
|
Offset<reflection::Field> Serialize(FlatBufferBuilder *builder, uint16_t id,
|
||||||
const Parser &parser) const;
|
const Parser &parser) const;
|
||||||
@@ -342,6 +343,12 @@ struct FieldDef : public Definition {
|
|||||||
|
|
||||||
StructDef *nested_flatbuffer; // This field contains nested FlatBuffer data.
|
StructDef *nested_flatbuffer; // This field contains nested FlatBuffer data.
|
||||||
size_t padding; // Bytes to always pad after this field.
|
size_t padding; // Bytes to always pad after this field.
|
||||||
|
|
||||||
|
// sibling_union_field is always set to nullptr. The only exception is
|
||||||
|
// when FieldDef is a union field or an union type field. Therefore,
|
||||||
|
// sibling_union_field on a union field points to the union type field
|
||||||
|
// and vice-versa.
|
||||||
|
FieldDef *sibling_union_field;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct StructDef : public Definition {
|
struct StructDef : public Definition {
|
||||||
|
|||||||
@@ -970,6 +970,14 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
|
|||||||
FieldDef *field;
|
FieldDef *field;
|
||||||
ECHECK(AddField(struct_def, name, type, &field));
|
ECHECK(AddField(struct_def, name, type, &field));
|
||||||
|
|
||||||
|
if (typefield) {
|
||||||
|
// We preserve the relation between the typefield
|
||||||
|
// and field, so we can easily map it in the code
|
||||||
|
// generators.
|
||||||
|
typefield->sibling_union_field = field;
|
||||||
|
field->sibling_union_field = typefield;
|
||||||
|
}
|
||||||
|
|
||||||
if (token_ == '=') {
|
if (token_ == '=') {
|
||||||
NEXT();
|
NEXT();
|
||||||
ECHECK(ParseSingleValue(&field->name, field->value, true));
|
ECHECK(ParseSingleValue(&field->name, field->value, true));
|
||||||
|
|||||||
Reference in New Issue
Block a user