From: Frank Benkstein Date: Thu, 1 Nov 2018 19:51:25 +0000 (+0100) Subject: make unions with type aliases more usable (#5019) X-Git-Tag: v1.11.0~137 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=91fe9ba93f3bfe31d08bc7b3fa6c7e96a49af44a;p=platform%2Fupstream%2Fflatbuffers.git make unions with type aliases more usable (#5019) Some generic C++ and Rust code is not generated when unions use type aliases because of potential ambiguity. Actually check for this ambiguity and only disable offending code only if it is found. --- diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index c00bdb9..11427ab 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -327,7 +327,7 @@ struct EnumVal { }; struct EnumDef : public Definition { - EnumDef() : is_union(false), uses_type_aliases(false) {} + EnumDef() : is_union(false), uses_multiple_type_instances(false) {} EnumVal *ReverseLookup(int64_t enum_idx, bool skip_union_default = true) { for (auto it = vals.vec.begin() + @@ -342,7 +342,9 @@ struct EnumDef : public Definition { SymbolTable vals; bool is_union; - bool uses_type_aliases; + // Type is a union which uses type aliases where at least one type is + // available under two different names. + bool uses_multiple_type_instances; Type underlying_type; }; diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 8ab3e93..0d45076 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -1025,7 +1025,7 @@ class CppGenerator : public BaseGenerator { } // Generate type traits for unions to map from a type to union enum value. - if (enum_def.is_union && !enum_def.uses_type_aliases) { + if (enum_def.is_union && !enum_def.uses_multiple_type_instances) { for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); ++it) { const auto &ev = **it; @@ -1074,7 +1074,7 @@ class CppGenerator : public BaseGenerator { code_ += ""; code_ += " void Reset();"; code_ += ""; - if (!enum_def.uses_type_aliases) { + if (!enum_def.uses_multiple_type_instances) { code_ += "#ifndef FLATBUFFERS_CPP98_STL"; code_ += " template "; code_ += " void Set(T&& val) {"; @@ -1773,9 +1773,10 @@ class CppGenerator : public BaseGenerator { if (field.value.type.base_type == BASE_TYPE_UNION) { auto u = field.value.type.enum_def; - code_ += - " template " - "const T *{{NULLABLE_EXT}}{{FIELD_NAME}}_as() const;"; + if (!field.value.type.enum_def->uses_multiple_type_instances) + code_ += + " template " + "const T *{{NULLABLE_EXT}}{{FIELD_NAME}}_as() const;"; for (auto u_it = u->vals.vec.begin(); u_it != u->vals.vec.end(); ++u_it) { @@ -1906,7 +1907,7 @@ class CppGenerator : public BaseGenerator { } auto u = field.value.type.enum_def; - if (u->uses_type_aliases) continue; + if (u->uses_multiple_type_instances) continue; code_.SetValue("FIELD_NAME", Name(field)); diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 6f06975..b530137 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -1307,7 +1307,6 @@ class RustGenerator : public BaseGenerator { } auto u = field.value.type.enum_def; - if (u->uses_type_aliases) continue; code_.SetValue("FIELD_NAME", Name(field)); diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index c4f7c86..50ec760 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -1603,6 +1603,7 @@ CheckedError Parser::ParseEnum(bool is_union, EnumDef **dest) { ECHECK(ParseMetaData(&enum_def->attributes)); EXPECT('{'); if (is_union) enum_def->vals.Add("NONE", new EnumVal("NONE", 0)); + std::set> union_types; for (;;) { if (opts.proto_mode && attribute_ == "option") { ECHECK(ParseProtoOption()); @@ -1633,10 +1634,17 @@ CheckedError Parser::ParseEnum(bool is_union, EnumDef **dest) { if (ev.union_type.base_type != BASE_TYPE_STRUCT && ev.union_type.base_type != BASE_TYPE_STRING) return Error("union value type may only be table/struct/string"); - enum_def->uses_type_aliases = true; } else { ev.union_type = Type(BASE_TYPE_STRUCT, LookupCreateStruct(full_name)); } + if (!enum_def->uses_multiple_type_instances) { + auto union_type_key = std::make_pair(ev.union_type.base_type, ev.union_type.struct_def); + if (union_types.count(union_type_key) > 0) { + enum_def->uses_multiple_type_instances = true; + } else { + union_types.insert(union_type_key); + } + } } if (Is('=')) { NEXT(); diff --git a/tests/FlatBuffers.Test/FlatBuffers.Test.csproj b/tests/FlatBuffers.Test/FlatBuffers.Test.csproj index 7c67274..7510f9d 100644 --- a/tests/FlatBuffers.Test/FlatBuffers.Test.csproj +++ b/tests/FlatBuffers.Test/FlatBuffers.Test.csproj @@ -1,4 +1,4 @@ - + Debug @@ -65,6 +65,12 @@ MyGame\Example\Any.cs + + MyGame\Example\AnyAmbiguousAliases.cs + + + MyGame\Example\AnyUniqueAliases.cs + MyGame\Example\Color.cs @@ -131,4 +137,4 @@ --> - \ No newline at end of file + diff --git a/tests/MyGame/Example/AnyAmbiguousAliases.cs b/tests/MyGame/Example/AnyAmbiguousAliases.cs new file mode 100644 index 0000000..e841518 --- /dev/null +++ b/tests/MyGame/Example/AnyAmbiguousAliases.cs @@ -0,0 +1,17 @@ +// +// automatically generated by the FlatBuffers compiler, do not modify +// + +namespace MyGame.Example +{ + +public enum AnyAmbiguousAliases : byte +{ + NONE = 0, + M1 = 1, + M2 = 2, + M3 = 3, +}; + + +} diff --git a/tests/MyGame/Example/AnyAmbiguousAliases.go b/tests/MyGame/Example/AnyAmbiguousAliases.go new file mode 100644 index 0000000..b5eacde --- /dev/null +++ b/tests/MyGame/Example/AnyAmbiguousAliases.go @@ -0,0 +1,19 @@ +// Code generated by the FlatBuffers compiler. DO NOT EDIT. + +package Example + +type AnyAmbiguousAliases = byte +const ( + AnyAmbiguousAliasesNONE AnyAmbiguousAliases = 0 + AnyAmbiguousAliasesM1 AnyAmbiguousAliases = 1 + AnyAmbiguousAliasesM2 AnyAmbiguousAliases = 2 + AnyAmbiguousAliasesM3 AnyAmbiguousAliases = 3 +) + +var EnumNamesAnyAmbiguousAliases = map[AnyAmbiguousAliases]string{ + AnyAmbiguousAliasesNONE:"NONE", + AnyAmbiguousAliasesM1:"M1", + AnyAmbiguousAliasesM2:"M2", + AnyAmbiguousAliasesM3:"M3", +} + diff --git a/tests/MyGame/Example/AnyAmbiguousAliases.java b/tests/MyGame/Example/AnyAmbiguousAliases.java new file mode 100644 index 0000000..4b699ed --- /dev/null +++ b/tests/MyGame/Example/AnyAmbiguousAliases.java @@ -0,0 +1,18 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +package MyGame.Example; + + +@javax.annotation.Generated(value="flatc") +public final class AnyAmbiguousAliases { + private AnyAmbiguousAliases() { } + public static final byte NONE = 0; + public static final byte M1 = 1; + public static final byte M2 = 2; + public static final byte M3 = 3; + + public static final String[] names = { "NONE", "M1", "M2", "M3", }; + + public static String name(int e) { return names[e]; } +} + diff --git a/tests/MyGame/Example/AnyAmbiguousAliases.lua b/tests/MyGame/Example/AnyAmbiguousAliases.lua new file mode 100644 index 0000000..dbe474b --- /dev/null +++ b/tests/MyGame/Example/AnyAmbiguousAliases.lua @@ -0,0 +1,12 @@ +-- automatically generated by the FlatBuffers compiler, do not modify + +-- namespace: Example + +local AnyAmbiguousAliases = { + NONE = 0, + M1 = 1, + M2 = 2, + M3 = 3, +} + +return AnyAmbiguousAliases -- return the module \ No newline at end of file diff --git a/tests/MyGame/Example/AnyAmbiguousAliases.php b/tests/MyGame/Example/AnyAmbiguousAliases.php new file mode 100644 index 0000000..8d82462 --- /dev/null +++ b/tests/MyGame/Example/AnyAmbiguousAliases.php @@ -0,0 +1,27 @@ + +// automatically generated by the FlatBuffers compiler, do not modify +// + +namespace MyGame.Example +{ + +public enum AnyUniqueAliases : byte +{ + NONE = 0, + M = 1, + T = 2, + M2 = 3, +}; + + +} diff --git a/tests/MyGame/Example/AnyUniqueAliases.go b/tests/MyGame/Example/AnyUniqueAliases.go new file mode 100644 index 0000000..14a2694 --- /dev/null +++ b/tests/MyGame/Example/AnyUniqueAliases.go @@ -0,0 +1,19 @@ +// Code generated by the FlatBuffers compiler. DO NOT EDIT. + +package Example + +type AnyUniqueAliases = byte +const ( + AnyUniqueAliasesNONE AnyUniqueAliases = 0 + AnyUniqueAliasesM AnyUniqueAliases = 1 + AnyUniqueAliasesT AnyUniqueAliases = 2 + AnyUniqueAliasesM2 AnyUniqueAliases = 3 +) + +var EnumNamesAnyUniqueAliases = map[AnyUniqueAliases]string{ + AnyUniqueAliasesNONE:"NONE", + AnyUniqueAliasesM:"M", + AnyUniqueAliasesT:"T", + AnyUniqueAliasesM2:"M2", +} + diff --git a/tests/MyGame/Example/AnyUniqueAliases.java b/tests/MyGame/Example/AnyUniqueAliases.java new file mode 100644 index 0000000..33e42a6 --- /dev/null +++ b/tests/MyGame/Example/AnyUniqueAliases.java @@ -0,0 +1,18 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +package MyGame.Example; + + +@javax.annotation.Generated(value="flatc") +public final class AnyUniqueAliases { + private AnyUniqueAliases() { } + public static final byte NONE = 0; + public static final byte M = 1; + public static final byte T = 2; + public static final byte M2 = 3; + + public static final String[] names = { "NONE", "M", "T", "M2", }; + + public static String name(int e) { return names[e]; } +} + diff --git a/tests/MyGame/Example/AnyUniqueAliases.lua b/tests/MyGame/Example/AnyUniqueAliases.lua new file mode 100644 index 0000000..b4ffc74 --- /dev/null +++ b/tests/MyGame/Example/AnyUniqueAliases.lua @@ -0,0 +1,12 @@ +-- automatically generated by the FlatBuffers compiler, do not modify + +-- namespace: Example + +local AnyUniqueAliases = { + NONE = 0, + M = 1, + T = 2, + M2 = 3, +} + +return AnyUniqueAliases -- return the module \ No newline at end of file diff --git a/tests/MyGame/Example/AnyUniqueAliases.php b/tests/MyGame/Example/AnyUniqueAliases.php new file mode 100644 index 0000000..5d51f82 --- /dev/null +++ b/tests/MyGame/Example/AnyUniqueAliases.php @@ -0,0 +1,27 @@ +(88); } public bool MutateVectorOfNonOwningReferences(int j, ulong vector_of_non_owning_references) { int o = __p.__offset(88); if (o != 0) { __p.bb.PutUlong(__p.__vector(o) + j * 8, vector_of_non_owning_references); return true; } else { return false; } } + public AnyUniqueAliases AnyUniqueType { get { int o = __p.__offset(90); return o != 0 ? (AnyUniqueAliases)__p.bb.Get(o + __p.bb_pos) : AnyUniqueAliases.NONE; } } + public bool MutateAnyUniqueType(AnyUniqueAliases any_unique_type) { int o = __p.__offset(90); if (o != 0) { __p.bb.Put(o + __p.bb_pos, (byte)any_unique_type); return true; } else { return false; } } + public TTable? AnyUnique() where TTable : struct, IFlatbufferObject { int o = __p.__offset(92); return o != 0 ? (TTable?)__p.__union(o) : null; } + public AnyAmbiguousAliases AnyAmbiguousType { get { int o = __p.__offset(94); return o != 0 ? (AnyAmbiguousAliases)__p.bb.Get(o + __p.bb_pos) : AnyAmbiguousAliases.NONE; } } + public bool MutateAnyAmbiguousType(AnyAmbiguousAliases any_ambiguous_type) { int o = __p.__offset(94); if (o != 0) { __p.bb.Put(o + __p.bb_pos, (byte)any_ambiguous_type); return true; } else { return false; } } + public TTable? AnyAmbiguous() where TTable : struct, IFlatbufferObject { int o = __p.__offset(96); return o != 0 ? (TTable?)__p.__union(o) : null; } - public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(43); } + public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(47); } public static void AddPos(FlatBufferBuilder builder, Offset posOffset) { builder.AddStruct(0, posOffset.Value, 0); } public static void AddMana(FlatBufferBuilder builder, short mana) { builder.AddShort(1, mana, 150); } public static void AddHp(FlatBufferBuilder builder, short hp) { builder.AddShort(2, hp, 100); } @@ -261,6 +267,10 @@ public struct Monster : IFlatbufferObject public static VectorOffset CreateVectorOfNonOwningReferencesVector(FlatBufferBuilder builder, ulong[] data) { builder.StartVector(8, data.Length, 8); for (int i = data.Length - 1; i >= 0; i--) builder.AddUlong(data[i]); return builder.EndVector(); } public static VectorOffset CreateVectorOfNonOwningReferencesVectorBlock(FlatBufferBuilder builder, ulong[] data) { builder.StartVector(8, data.Length, 8); builder.Add(data); return builder.EndVector(); } public static void StartVectorOfNonOwningReferencesVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(8, numElems, 8); } + public static void AddAnyUniqueType(FlatBufferBuilder builder, AnyUniqueAliases anyUniqueType) { builder.AddByte(43, (byte)anyUniqueType, 0); } + public static void AddAnyUnique(FlatBufferBuilder builder, int anyUniqueOffset) { builder.AddOffset(44, anyUniqueOffset, 0); } + public static void AddAnyAmbiguousType(FlatBufferBuilder builder, AnyAmbiguousAliases anyAmbiguousType) { builder.AddByte(45, (byte)anyAmbiguousType, 0); } + public static void AddAnyAmbiguous(FlatBufferBuilder builder, int anyAmbiguousOffset) { builder.AddOffset(46, anyAmbiguousOffset, 0); } public static Offset EndMonster(FlatBufferBuilder builder) { int o = builder.EndObject(); builder.Required(o, 10); // name diff --git a/tests/MyGame/Example/Monster.go b/tests/MyGame/Example/Monster.go index 29ca9c0..fefb7b3 100644 --- a/tests/MyGame/Example/Monster.go +++ b/tests/MyGame/Example/Monster.go @@ -656,8 +656,50 @@ func (rcv *Monster) VectorOfNonOwningReferencesLength() int { return 0 } +func (rcv *Monster) AnyUniqueType() byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(90)) + if o != 0 { + return rcv._tab.GetByte(o + rcv._tab.Pos) + } + return 0 +} + +func (rcv *Monster) MutateAnyUniqueType(n byte) bool { + return rcv._tab.MutateByteSlot(90, n) +} + +func (rcv *Monster) AnyUnique(obj *flatbuffers.Table) bool { + o := flatbuffers.UOffsetT(rcv._tab.Offset(92)) + if o != 0 { + rcv._tab.Union(obj, o) + return true + } + return false +} + +func (rcv *Monster) AnyAmbiguousType() byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(94)) + if o != 0 { + return rcv._tab.GetByte(o + rcv._tab.Pos) + } + return 0 +} + +func (rcv *Monster) MutateAnyAmbiguousType(n byte) bool { + return rcv._tab.MutateByteSlot(94, n) +} + +func (rcv *Monster) AnyAmbiguous(obj *flatbuffers.Table) bool { + o := flatbuffers.UOffsetT(rcv._tab.Offset(96)) + if o != 0 { + rcv._tab.Union(obj, o) + return true + } + return false +} + func MonsterStart(builder *flatbuffers.Builder) { - builder.StartObject(43) + builder.StartObject(47) } func MonsterAddPos(builder *flatbuffers.Builder, pos flatbuffers.UOffsetT) { builder.PrependStructSlot(0, flatbuffers.UOffsetT(pos), 0) @@ -836,6 +878,18 @@ func MonsterAddVectorOfNonOwningReferences(builder *flatbuffers.Builder, vectorO func MonsterStartVectorOfNonOwningReferencesVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(8, numElems, 8) } +func MonsterAddAnyUniqueType(builder *flatbuffers.Builder, anyUniqueType byte) { + builder.PrependByteSlot(43, anyUniqueType, 0) +} +func MonsterAddAnyUnique(builder *flatbuffers.Builder, anyUnique flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(44, flatbuffers.UOffsetT(anyUnique), 0) +} +func MonsterAddAnyAmbiguousType(builder *flatbuffers.Builder, anyAmbiguousType byte) { + builder.PrependByteSlot(45, anyAmbiguousType, 0) +} +func MonsterAddAnyAmbiguous(builder *flatbuffers.Builder, anyAmbiguous flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(46, flatbuffers.UOffsetT(anyAmbiguous), 0) +} func MonsterEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } diff --git a/tests/MyGame/Example/Monster.java b/tests/MyGame/Example/Monster.java index d42a995..d477466 100644 --- a/tests/MyGame/Example/Monster.java +++ b/tests/MyGame/Example/Monster.java @@ -148,8 +148,14 @@ public final class Monster extends Table { public ByteBuffer vectorOfNonOwningReferencesAsByteBuffer() { return __vector_as_bytebuffer(88, 8); } public ByteBuffer vectorOfNonOwningReferencesInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 88, 8); } public boolean mutateVectorOfNonOwningReferences(int j, long vector_of_non_owning_references) { int o = __offset(88); if (o != 0) { bb.putLong(__vector(o) + j * 8, vector_of_non_owning_references); return true; } else { return false; } } + public byte anyUniqueType() { int o = __offset(90); return o != 0 ? bb.get(o + bb_pos) : 0; } + public boolean mutateAnyUniqueType(byte any_unique_type) { int o = __offset(90); if (o != 0) { bb.put(o + bb_pos, any_unique_type); return true; } else { return false; } } + public Table anyUnique(Table obj) { int o = __offset(92); return o != 0 ? __union(obj, o) : null; } + public byte anyAmbiguousType() { int o = __offset(94); return o != 0 ? bb.get(o + bb_pos) : 0; } + public boolean mutateAnyAmbiguousType(byte any_ambiguous_type) { int o = __offset(94); if (o != 0) { bb.put(o + bb_pos, any_ambiguous_type); return true; } else { return false; } } + public Table anyAmbiguous(Table obj) { int o = __offset(96); return o != 0 ? __union(obj, o) : null; } - public static void startMonster(FlatBufferBuilder builder) { builder.startObject(43); } + public static void startMonster(FlatBufferBuilder builder) { builder.startObject(47); } public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); } public static void addMana(FlatBufferBuilder builder, short mana) { builder.addShort(1, mana, 150); } public static void addHp(FlatBufferBuilder builder, short hp) { builder.addShort(2, hp, 100); } @@ -223,6 +229,10 @@ public final class Monster extends Table { public static void addVectorOfNonOwningReferences(FlatBufferBuilder builder, int vectorOfNonOwningReferencesOffset) { builder.addOffset(42, vectorOfNonOwningReferencesOffset, 0); } public static int createVectorOfNonOwningReferencesVector(FlatBufferBuilder builder, long[] data) { builder.startVector(8, data.length, 8); for (int i = data.length - 1; i >= 0; i--) builder.addLong(data[i]); return builder.endVector(); } public static void startVectorOfNonOwningReferencesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(8, numElems, 8); } + public static void addAnyUniqueType(FlatBufferBuilder builder, byte anyUniqueType) { builder.addByte(43, anyUniqueType, 0); } + public static void addAnyUnique(FlatBufferBuilder builder, int anyUniqueOffset) { builder.addOffset(44, anyUniqueOffset, 0); } + public static void addAnyAmbiguousType(FlatBufferBuilder builder, byte anyAmbiguousType) { builder.addByte(45, anyAmbiguousType, 0); } + public static void addAnyAmbiguous(FlatBufferBuilder builder, int anyAmbiguousOffset) { builder.addOffset(46, anyAmbiguousOffset, 0); } public static int endMonster(FlatBufferBuilder builder) { int o = builder.endObject(); builder.required(o, 10); // name diff --git a/tests/MyGame/Example/Monster.lua b/tests/MyGame/Example/Monster.lua index a656f9b..7f39cdc 100644 --- a/tests/MyGame/Example/Monster.lua +++ b/tests/MyGame/Example/Monster.lua @@ -477,7 +477,37 @@ function Monster_mt:VectorOfNonOwningReferencesLength() end return 0 end -function Monster.Start(builder) builder:StartObject(43) end +function Monster_mt:AnyUniqueType() + local o = self.view:Offset(90) + if o ~= 0 then + return self.view:Get(flatbuffers.N.Uint8, o + self.view.pos) + end + return 0 +end +function Monster_mt:AnyUnique() + local o = self.view:Offset(92) + if o ~= 0 then + local obj = flatbuffers.view.New(require('flatbuffers.binaryarray').New(0), 0) + self.view:Union(obj, o) + return obj + end +end +function Monster_mt:AnyAmbiguousType() + local o = self.view:Offset(94) + if o ~= 0 then + return self.view:Get(flatbuffers.N.Uint8, o + self.view.pos) + end + return 0 +end +function Monster_mt:AnyAmbiguous() + local o = self.view:Offset(96) + if o ~= 0 then + local obj = flatbuffers.view.New(require('flatbuffers.binaryarray').New(0), 0) + self.view:Union(obj, o) + return obj + end +end +function Monster.Start(builder) builder:StartObject(47) end function Monster.AddPos(builder, pos) builder:PrependStructSlot(0, pos, 0) end function Monster.AddMana(builder, mana) builder:PrependInt16Slot(1, mana, 150) end function Monster.AddHp(builder, hp) builder:PrependInt16Slot(2, hp, 100) end @@ -537,6 +567,10 @@ function Monster.StartVectorOfCoOwningReferencesVector(builder, numElems) return function Monster.AddNonOwningReference(builder, nonOwningReference) builder:PrependUint64Slot(41, nonOwningReference, 0) end function Monster.AddVectorOfNonOwningReferences(builder, vectorOfNonOwningReferences) builder:PrependUOffsetTRelativeSlot(42, vectorOfNonOwningReferences, 0) end function Monster.StartVectorOfNonOwningReferencesVector(builder, numElems) return builder:StartVector(8, numElems, 8) end +function Monster.AddAnyUniqueType(builder, anyUniqueType) builder:PrependUint8Slot(43, anyUniqueType, 0) end +function Monster.AddAnyUnique(builder, anyUnique) builder:PrependUOffsetTRelativeSlot(44, anyUnique, 0) end +function Monster.AddAnyAmbiguousType(builder, anyAmbiguousType) builder:PrependUint8Slot(45, anyAmbiguousType, 0) end +function Monster.AddAnyAmbiguous(builder, anyAmbiguous) builder:PrependUOffsetTRelativeSlot(46, anyAmbiguous, 0) end function Monster.End(builder) return builder:EndObject() end return Monster -- return the module \ No newline at end of file diff --git a/tests/MyGame/Example/Monster.php b/tests/MyGame/Example/Monster.php index a40b90e..d171375 100644 --- a/tests/MyGame/Example/Monster.php +++ b/tests/MyGame/Example/Monster.php @@ -612,21 +612,57 @@ class Monster extends Table } /** + * @return byte + */ + public function getAnyUniqueType() + { + $o = $this->__offset(90); + return $o != 0 ? $this->bb->getByte($o + $this->bb_pos) : \MyGame\Example\AnyUniqueAliases::NONE; + } + + /** + * @returnint + */ + public function getAnyUnique($obj) + { + $o = $this->__offset(92); + return $o != 0 ? $this->__union($obj, $o) : null; + } + + /** + * @return byte + */ + public function getAnyAmbiguousType() + { + $o = $this->__offset(94); + return $o != 0 ? $this->bb->getByte($o + $this->bb_pos) : \MyGame\Example\AnyAmbiguousAliases::NONE; + } + + /** + * @returnint + */ + public function getAnyAmbiguous($obj) + { + $o = $this->__offset(96); + return $o != 0 ? $this->__union($obj, $o) : null; + } + + /** * @param FlatBufferBuilder $builder * @return void */ public static function startMonster(FlatBufferBuilder $builder) { - $builder->StartObject(43); + $builder->StartObject(47); } /** * @param FlatBufferBuilder $builder * @return Monster */ - public static function createMonster(FlatBufferBuilder $builder, $pos, $mana, $hp, $name, $inventory, $color, $test_type, $test, $test4, $testarrayofstring, $testarrayoftables, $enemy, $testnestedflatbuffer, $testempty, $testbool, $testhashs32_fnv1, $testhashu32_fnv1, $testhashs64_fnv1, $testhashu64_fnv1, $testhashs32_fnv1a, $testhashu32_fnv1a, $testhashs64_fnv1a, $testhashu64_fnv1a, $testarrayofbools, $testf, $testf2, $testf3, $testarrayofstring2, $testarrayofsortedstruct, $flex, $test5, $vector_of_longs, $vector_of_doubles, $parent_namespace_test, $vector_of_referrables, $single_weak_reference, $vector_of_weak_references, $vector_of_strong_referrables, $co_owning_reference, $vector_of_co_owning_references, $non_owning_reference, $vector_of_non_owning_references) + public static function createMonster(FlatBufferBuilder $builder, $pos, $mana, $hp, $name, $inventory, $color, $test_type, $test, $test4, $testarrayofstring, $testarrayoftables, $enemy, $testnestedflatbuffer, $testempty, $testbool, $testhashs32_fnv1, $testhashu32_fnv1, $testhashs64_fnv1, $testhashu64_fnv1, $testhashs32_fnv1a, $testhashu32_fnv1a, $testhashs64_fnv1a, $testhashu64_fnv1a, $testarrayofbools, $testf, $testf2, $testf3, $testarrayofstring2, $testarrayofsortedstruct, $flex, $test5, $vector_of_longs, $vector_of_doubles, $parent_namespace_test, $vector_of_referrables, $single_weak_reference, $vector_of_weak_references, $vector_of_strong_referrables, $co_owning_reference, $vector_of_co_owning_references, $non_owning_reference, $vector_of_non_owning_references, $any_unique_type, $any_unique, $any_ambiguous_type, $any_ambiguous) { - $builder->startObject(43); + $builder->startObject(47); self::addPos($builder, $pos); self::addMana($builder, $mana); self::addHp($builder, $hp); @@ -669,6 +705,10 @@ class Monster extends Table self::addVectorOfCoOwningReferences($builder, $vector_of_co_owning_references); self::addNonOwningReference($builder, $non_owning_reference); self::addVectorOfNonOwningReferences($builder, $vector_of_non_owning_references); + self::addAnyUniqueType($builder, $any_unique_type); + self::addAnyUnique($builder, $any_unique); + self::addAnyAmbiguousType($builder, $any_ambiguous_type); + self::addAnyAmbiguous($builder, $any_ambiguous); $o = $builder->endObject(); $builder->required($o, 10); // name return $o; @@ -1499,6 +1539,36 @@ class Monster extends Table /** * @param FlatBufferBuilder $builder + * @param byte + * @return void + */ + public static function addAnyUniqueType(FlatBufferBuilder $builder, $anyUniqueType) + { + $builder->addByteX(43, $anyUniqueType, 0); + } + + public static function addAnyUnique(FlatBufferBuilder $builder, $offset) + { + $builder->addOffsetX(44, $offset, 0); + } + + /** + * @param FlatBufferBuilder $builder + * @param byte + * @return void + */ + public static function addAnyAmbiguousType(FlatBufferBuilder $builder, $anyAmbiguousType) + { + $builder->addByteX(45, $anyAmbiguousType, 0); + } + + public static function addAnyAmbiguous(FlatBufferBuilder $builder, $offset) + { + $builder->addOffsetX(46, $offset, 0); + } + + /** + * @param FlatBufferBuilder $builder * @return int table offset */ public static function endMonster(FlatBufferBuilder $builder) diff --git a/tests/MyGame/Example/Monster.py b/tests/MyGame/Example/Monster.py index 493836e..e68565e 100644 --- a/tests/MyGame/Example/Monster.py +++ b/tests/MyGame/Example/Monster.py @@ -560,7 +560,41 @@ class Monster(object): return self._tab.VectorLen(o) return 0 -def MonsterStart(builder): builder.StartObject(43) + # Monster + def AnyUniqueType(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(90)) + if o != 0: + return self._tab.Get(flatbuffers.number_types.Uint8Flags, o + self._tab.Pos) + return 0 + + # Monster + def AnyUnique(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(92)) + if o != 0: + from flatbuffers.table import Table + obj = Table(bytearray(), 0) + self._tab.Union(obj, o) + return obj + return None + + # Monster + def AnyAmbiguousType(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(94)) + if o != 0: + return self._tab.Get(flatbuffers.number_types.Uint8Flags, o + self._tab.Pos) + return 0 + + # Monster + def AnyAmbiguous(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(96)) + if o != 0: + from flatbuffers.table import Table + obj = Table(bytearray(), 0) + self._tab.Union(obj, o) + return obj + return None + +def MonsterStart(builder): builder.StartObject(47) def MonsterAddPos(builder, pos): builder.PrependStructSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(pos), 0) def MonsterAddMana(builder, mana): builder.PrependInt16Slot(1, mana, 150) def MonsterAddHp(builder, hp): builder.PrependInt16Slot(2, hp, 100) @@ -620,4 +654,8 @@ def MonsterStartVectorOfCoOwningReferencesVector(builder, numElems): return buil def MonsterAddNonOwningReference(builder, nonOwningReference): builder.PrependUint64Slot(41, nonOwningReference, 0) def MonsterAddVectorOfNonOwningReferences(builder, vectorOfNonOwningReferences): builder.PrependUOffsetTRelativeSlot(42, flatbuffers.number_types.UOffsetTFlags.py_type(vectorOfNonOwningReferences), 0) def MonsterStartVectorOfNonOwningReferencesVector(builder, numElems): return builder.StartVector(8, numElems, 8) +def MonsterAddAnyUniqueType(builder, anyUniqueType): builder.PrependUint8Slot(43, anyUniqueType, 0) +def MonsterAddAnyUnique(builder, anyUnique): builder.PrependUOffsetTRelativeSlot(44, flatbuffers.number_types.UOffsetTFlags.py_type(anyUnique), 0) +def MonsterAddAnyAmbiguousType(builder, anyAmbiguousType): builder.PrependUint8Slot(45, anyAmbiguousType, 0) +def MonsterAddAnyAmbiguous(builder, anyAmbiguous): builder.PrependUOffsetTRelativeSlot(46, flatbuffers.number_types.UOffsetTFlags.py_type(anyAmbiguous), 0) def MonsterEnd(builder): return builder.EndObject() diff --git a/tests/monster_test.bfbs b/tests/monster_test.bfbs index f7bb462..ee7d4d3 100644 Binary files a/tests/monster_test.bfbs and b/tests/monster_test.bfbs differ diff --git a/tests/monster_test.fbs b/tests/monster_test.fbs index 866aa47..d4064c4 100644 --- a/tests/monster_test.fbs +++ b/tests/monster_test.fbs @@ -18,6 +18,9 @@ enum Color:byte (bit_flags) { Red = 0, Green, Blue = 3, } union Any { Monster, TestSimpleTableWithEnum, MyGame.Example2.Monster } +union AnyUniqueAliases { M: Monster, T: TestSimpleTableWithEnum, M2: MyGame.Example2.Monster } +union AnyAmbiguousAliases { M1: Monster, M2: Monster, M3: Monster } + struct Test { a:short; b:byte; } table TestSimpleTableWithEnum (csharp_partial, private) { @@ -94,6 +97,8 @@ table Monster { vector_of_co_owning_references:[ulong](id:40, hash:"fnv1a_64", cpp_type:"ReferrableT", cpp_ptr_type:"default_ptr_type", cpp_ptr_type_get:".get()"); //was shared_ptr non_owning_reference:ulong(id:41, hash:"fnv1a_64", cpp_type:"ReferrableT", cpp_ptr_type:"naked", cpp_ptr_type_get:""); //was weak_ptr vector_of_non_owning_references:[ulong](id:42, hash:"fnv1a_64", cpp_type:"ReferrableT", cpp_ptr_type:"naked", cpp_ptr_type_get:""); //was weak_ptr + any_unique:AnyUniqueAliases(id:44); + any_ambiguous:AnyAmbiguousAliases (id:46); } table TypeAliases { diff --git a/tests/monster_test.schema.json b/tests/monster_test.schema.json index b2df316..2db11b2 100644 --- a/tests/monster_test.schema.json +++ b/tests/monster_test.schema.json @@ -13,6 +13,14 @@ "type" : "string", "enum": ["NONE", "Monster", "TestSimpleTableWithEnum", "MyGame_Example2_Monster"] }, + "MyGame_Example_AnyUniqueAliases" : { + "type" : "string", + "enum": ["NONE", "M", "T", "M2"] + }, + "MyGame_Example_AnyAmbiguousAliases" : { + "type" : "string", + "enum": ["NONE", "M1", "M2", "M3"] + }, "MyGame_OtherNameSpace_Unused" : { "type" : "object", "properties" : { @@ -143,7 +151,11 @@ "co_owning_reference" : { "type" : "number" }, "vector_of_co_owning_references" : { "type" : "array", "items" : { "type" : "number" } }, "non_owning_reference" : { "type" : "number" }, - "vector_of_non_owning_references" : { "type" : "array", "items" : { "type" : "number" } } + "vector_of_non_owning_references" : { "type" : "array", "items" : { "type" : "number" } }, + "any_unique_type" : { "$ref" : "#/definitions/MyGame_Example_AnyUniqueAliases" }, + "any_unique" : { "anyOf": [{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_TestSimpleTableWithEnum" },{ "$ref" : "#/definitions/MyGame_Example2_Monster" }] }, + "any_ambiguous_type" : { "$ref" : "#/definitions/MyGame_Example_AnyAmbiguousAliases" }, + "any_ambiguous" : { "anyOf": [{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_Monster" }] } }, "required" : ["name"], "additionalProperties" : false diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index e21b85e..4e63009 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -263,6 +263,251 @@ inline bool operator==(const AnyUnion &lhs, const AnyUnion &rhs) { bool VerifyAny(flatbuffers::Verifier &verifier, const void *obj, Any type); bool VerifyAnyVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types); +enum AnyUniqueAliases { + AnyUniqueAliases_NONE = 0, + AnyUniqueAliases_M = 1, + AnyUniqueAliases_T = 2, + AnyUniqueAliases_M2 = 3, + AnyUniqueAliases_MIN = AnyUniqueAliases_NONE, + AnyUniqueAliases_MAX = AnyUniqueAliases_M2 +}; + +inline const AnyUniqueAliases (&EnumValuesAnyUniqueAliases())[4] { + static const AnyUniqueAliases values[] = { + AnyUniqueAliases_NONE, + AnyUniqueAliases_M, + AnyUniqueAliases_T, + AnyUniqueAliases_M2 + }; + return values; +} + +inline const char * const *EnumNamesAnyUniqueAliases() { + static const char * const names[] = { + "NONE", + "M", + "T", + "M2", + nullptr + }; + return names; +} + +inline const char *EnumNameAnyUniqueAliases(AnyUniqueAliases e) { + if (e < AnyUniqueAliases_NONE || e > AnyUniqueAliases_M2) return ""; + const size_t index = static_cast(e); + return EnumNamesAnyUniqueAliases()[index]; +} + +template struct AnyUniqueAliasesTraits { + static const AnyUniqueAliases enum_value = AnyUniqueAliases_NONE; +}; + +template<> struct AnyUniqueAliasesTraits { + static const AnyUniqueAliases enum_value = AnyUniqueAliases_M; +}; + +template<> struct AnyUniqueAliasesTraits { + static const AnyUniqueAliases enum_value = AnyUniqueAliases_T; +}; + +template<> struct AnyUniqueAliasesTraits { + static const AnyUniqueAliases enum_value = AnyUniqueAliases_M2; +}; + +struct AnyUniqueAliasesUnion { + AnyUniqueAliases type; + void *value; + + AnyUniqueAliasesUnion() : type(AnyUniqueAliases_NONE), value(nullptr) {} + AnyUniqueAliasesUnion(AnyUniqueAliasesUnion&& u) FLATBUFFERS_NOEXCEPT : + type(AnyUniqueAliases_NONE), value(nullptr) + { std::swap(type, u.type); std::swap(value, u.value); } + AnyUniqueAliasesUnion(const AnyUniqueAliasesUnion &) FLATBUFFERS_NOEXCEPT; + AnyUniqueAliasesUnion &operator=(const AnyUniqueAliasesUnion &u) FLATBUFFERS_NOEXCEPT + { AnyUniqueAliasesUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } + AnyUniqueAliasesUnion &operator=(AnyUniqueAliasesUnion &&u) FLATBUFFERS_NOEXCEPT + { std::swap(type, u.type); std::swap(value, u.value); return *this; } + ~AnyUniqueAliasesUnion() { Reset(); } + + void Reset(); + +#ifndef FLATBUFFERS_CPP98_STL + template + void Set(T&& val) { + Reset(); + type = AnyUniqueAliasesTraits::enum_value; + if (type != AnyUniqueAliases_NONE) { + value = new T(std::forward(val)); + } + } +#endif // FLATBUFFERS_CPP98_STL + + static void *UnPack(const void *obj, AnyUniqueAliases type, const flatbuffers::resolver_function_t *resolver); + flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher = nullptr) const; + + MonsterT *AsM() { + return type == AnyUniqueAliases_M ? + reinterpret_cast(value) : nullptr; + } + const MonsterT *AsM() const { + return type == AnyUniqueAliases_M ? + reinterpret_cast(value) : nullptr; + } + TestSimpleTableWithEnumT *AsT() { + return type == AnyUniqueAliases_T ? + reinterpret_cast(value) : nullptr; + } + const TestSimpleTableWithEnumT *AsT() const { + return type == AnyUniqueAliases_T ? + reinterpret_cast(value) : nullptr; + } + MyGame::Example2::MonsterT *AsM2() { + return type == AnyUniqueAliases_M2 ? + reinterpret_cast(value) : nullptr; + } + const MyGame::Example2::MonsterT *AsM2() const { + return type == AnyUniqueAliases_M2 ? + reinterpret_cast(value) : nullptr; + } +}; + + +inline bool operator==(const AnyUniqueAliasesUnion &lhs, const AnyUniqueAliasesUnion &rhs) { + if (lhs.type != rhs.type) return false; + switch (lhs.type) { + case AnyUniqueAliases_NONE: { + return true; + } + case AnyUniqueAliases_M: { + return *(reinterpret_cast(lhs.value)) == + *(reinterpret_cast(rhs.value)); + } + case AnyUniqueAliases_T: { + return *(reinterpret_cast(lhs.value)) == + *(reinterpret_cast(rhs.value)); + } + case AnyUniqueAliases_M2: { + return *(reinterpret_cast(lhs.value)) == + *(reinterpret_cast(rhs.value)); + } + default: { + return false; + } + } +} +bool VerifyAnyUniqueAliases(flatbuffers::Verifier &verifier, const void *obj, AnyUniqueAliases type); +bool VerifyAnyUniqueAliasesVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types); + +enum AnyAmbiguousAliases { + AnyAmbiguousAliases_NONE = 0, + AnyAmbiguousAliases_M1 = 1, + AnyAmbiguousAliases_M2 = 2, + AnyAmbiguousAliases_M3 = 3, + AnyAmbiguousAliases_MIN = AnyAmbiguousAliases_NONE, + AnyAmbiguousAliases_MAX = AnyAmbiguousAliases_M3 +}; + +inline const AnyAmbiguousAliases (&EnumValuesAnyAmbiguousAliases())[4] { + static const AnyAmbiguousAliases values[] = { + AnyAmbiguousAliases_NONE, + AnyAmbiguousAliases_M1, + AnyAmbiguousAliases_M2, + AnyAmbiguousAliases_M3 + }; + return values; +} + +inline const char * const *EnumNamesAnyAmbiguousAliases() { + static const char * const names[] = { + "NONE", + "M1", + "M2", + "M3", + nullptr + }; + return names; +} + +inline const char *EnumNameAnyAmbiguousAliases(AnyAmbiguousAliases e) { + if (e < AnyAmbiguousAliases_NONE || e > AnyAmbiguousAliases_M3) return ""; + const size_t index = static_cast(e); + return EnumNamesAnyAmbiguousAliases()[index]; +} + +struct AnyAmbiguousAliasesUnion { + AnyAmbiguousAliases type; + void *value; + + AnyAmbiguousAliasesUnion() : type(AnyAmbiguousAliases_NONE), value(nullptr) {} + AnyAmbiguousAliasesUnion(AnyAmbiguousAliasesUnion&& u) FLATBUFFERS_NOEXCEPT : + type(AnyAmbiguousAliases_NONE), value(nullptr) + { std::swap(type, u.type); std::swap(value, u.value); } + AnyAmbiguousAliasesUnion(const AnyAmbiguousAliasesUnion &) FLATBUFFERS_NOEXCEPT; + AnyAmbiguousAliasesUnion &operator=(const AnyAmbiguousAliasesUnion &u) FLATBUFFERS_NOEXCEPT + { AnyAmbiguousAliasesUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } + AnyAmbiguousAliasesUnion &operator=(AnyAmbiguousAliasesUnion &&u) FLATBUFFERS_NOEXCEPT + { std::swap(type, u.type); std::swap(value, u.value); return *this; } + ~AnyAmbiguousAliasesUnion() { Reset(); } + + void Reset(); + + static void *UnPack(const void *obj, AnyAmbiguousAliases type, const flatbuffers::resolver_function_t *resolver); + flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher = nullptr) const; + + MonsterT *AsM1() { + return type == AnyAmbiguousAliases_M1 ? + reinterpret_cast(value) : nullptr; + } + const MonsterT *AsM1() const { + return type == AnyAmbiguousAliases_M1 ? + reinterpret_cast(value) : nullptr; + } + MonsterT *AsM2() { + return type == AnyAmbiguousAliases_M2 ? + reinterpret_cast(value) : nullptr; + } + const MonsterT *AsM2() const { + return type == AnyAmbiguousAliases_M2 ? + reinterpret_cast(value) : nullptr; + } + MonsterT *AsM3() { + return type == AnyAmbiguousAliases_M3 ? + reinterpret_cast(value) : nullptr; + } + const MonsterT *AsM3() const { + return type == AnyAmbiguousAliases_M3 ? + reinterpret_cast(value) : nullptr; + } +}; + + +inline bool operator==(const AnyAmbiguousAliasesUnion &lhs, const AnyAmbiguousAliasesUnion &rhs) { + if (lhs.type != rhs.type) return false; + switch (lhs.type) { + case AnyAmbiguousAliases_NONE: { + return true; + } + case AnyAmbiguousAliases_M1: { + return *(reinterpret_cast(lhs.value)) == + *(reinterpret_cast(rhs.value)); + } + case AnyAmbiguousAliases_M2: { + return *(reinterpret_cast(lhs.value)) == + *(reinterpret_cast(rhs.value)); + } + case AnyAmbiguousAliases_M3: { + return *(reinterpret_cast(lhs.value)) == + *(reinterpret_cast(rhs.value)); + } + default: { + return false; + } + } +} +bool VerifyAnyAmbiguousAliases(flatbuffers::Verifier &verifier, const void *obj, AnyAmbiguousAliases type); +bool VerifyAnyAmbiguousAliasesVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types); + FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS { private: int16_t a_; @@ -809,6 +1054,8 @@ struct MonsterT : public flatbuffers::NativeTable { std::vector> vector_of_co_owning_references; ReferrableT *non_owning_reference; std::vector vector_of_non_owning_references; + AnyUniqueAliasesUnion any_unique; + AnyAmbiguousAliasesUnion any_ambiguous; MonsterT() : mana(150), hp(100), @@ -873,7 +1120,9 @@ inline bool operator==(const MonsterT &lhs, const MonsterT &rhs) { (lhs.co_owning_reference == rhs.co_owning_reference) && (lhs.vector_of_co_owning_references == rhs.vector_of_co_owning_references) && (lhs.non_owning_reference == rhs.non_owning_reference) && - (lhs.vector_of_non_owning_references == rhs.vector_of_non_owning_references); + (lhs.vector_of_non_owning_references == rhs.vector_of_non_owning_references) && + (lhs.any_unique == rhs.any_unique) && + (lhs.any_ambiguous == rhs.any_ambiguous); } /// an example documentation comment: monster object @@ -924,7 +1173,11 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VT_CO_OWNING_REFERENCE = 82, VT_VECTOR_OF_CO_OWNING_REFERENCES = 84, VT_NON_OWNING_REFERENCE = 86, - VT_VECTOR_OF_NON_OWNING_REFERENCES = 88 + VT_VECTOR_OF_NON_OWNING_REFERENCES = 88, + VT_ANY_UNIQUE_TYPE = 90, + VT_ANY_UNIQUE = 92, + VT_ANY_AMBIGUOUS_TYPE = 94, + VT_ANY_AMBIGUOUS = 96 }; const Vec3 *pos() const { return GetStruct(VT_POS); @@ -1202,6 +1455,49 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { flatbuffers::Vector *mutable_vector_of_non_owning_references() { return GetPointer *>(VT_VECTOR_OF_NON_OWNING_REFERENCES); } + AnyUniqueAliases any_unique_type() const { + return static_cast(GetField(VT_ANY_UNIQUE_TYPE, 0)); + } + bool mutate_any_unique_type(AnyUniqueAliases _any_unique_type) { + return SetField(VT_ANY_UNIQUE_TYPE, static_cast(_any_unique_type), 0); + } + const void *any_unique() const { + return GetPointer(VT_ANY_UNIQUE); + } + template const T *any_unique_as() const; + const Monster *any_unique_as_M() const { + return any_unique_type() == AnyUniqueAliases_M ? static_cast(any_unique()) : nullptr; + } + const TestSimpleTableWithEnum *any_unique_as_T() const { + return any_unique_type() == AnyUniqueAliases_T ? static_cast(any_unique()) : nullptr; + } + const MyGame::Example2::Monster *any_unique_as_M2() const { + return any_unique_type() == AnyUniqueAliases_M2 ? static_cast(any_unique()) : nullptr; + } + void *mutable_any_unique() { + return GetPointer(VT_ANY_UNIQUE); + } + AnyAmbiguousAliases any_ambiguous_type() const { + return static_cast(GetField(VT_ANY_AMBIGUOUS_TYPE, 0)); + } + bool mutate_any_ambiguous_type(AnyAmbiguousAliases _any_ambiguous_type) { + return SetField(VT_ANY_AMBIGUOUS_TYPE, static_cast(_any_ambiguous_type), 0); + } + const void *any_ambiguous() const { + return GetPointer(VT_ANY_AMBIGUOUS); + } + const Monster *any_ambiguous_as_M1() const { + return any_ambiguous_type() == AnyAmbiguousAliases_M1 ? static_cast(any_ambiguous()) : nullptr; + } + const Monster *any_ambiguous_as_M2() const { + return any_ambiguous_type() == AnyAmbiguousAliases_M2 ? static_cast(any_ambiguous()) : nullptr; + } + const Monster *any_ambiguous_as_M3() const { + return any_ambiguous_type() == AnyAmbiguousAliases_M3 ? static_cast(any_ambiguous()) : nullptr; + } + void *mutable_any_ambiguous() { + return GetPointer(VT_ANY_AMBIGUOUS); + } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, VT_POS) && @@ -1273,6 +1569,12 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VerifyField(verifier, VT_NON_OWNING_REFERENCE) && VerifyOffset(verifier, VT_VECTOR_OF_NON_OWNING_REFERENCES) && verifier.VerifyVector(vector_of_non_owning_references()) && + VerifyField(verifier, VT_ANY_UNIQUE_TYPE) && + VerifyOffset(verifier, VT_ANY_UNIQUE) && + VerifyAnyUniqueAliases(verifier, any_unique(), any_unique_type()) && + VerifyField(verifier, VT_ANY_AMBIGUOUS_TYPE) && + VerifyOffset(verifier, VT_ANY_AMBIGUOUS) && + VerifyAnyAmbiguousAliases(verifier, any_ambiguous(), any_ambiguous_type()) && verifier.EndTable(); } MonsterT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; @@ -1292,6 +1594,18 @@ template<> inline const MyGame::Example2::Monster *Monster::test_as inline const Monster *Monster::any_unique_as() const { + return any_unique_as_M(); +} + +template<> inline const TestSimpleTableWithEnum *Monster::any_unique_as() const { + return any_unique_as_T(); +} + +template<> inline const MyGame::Example2::Monster *Monster::any_unique_as() const { + return any_unique_as_M2(); +} + struct MonsterBuilder { flatbuffers::FlatBufferBuilder &fbb_; flatbuffers::uoffset_t start_; @@ -1421,6 +1735,18 @@ struct MonsterBuilder { void add_vector_of_non_owning_references(flatbuffers::Offset> vector_of_non_owning_references) { fbb_.AddOffset(Monster::VT_VECTOR_OF_NON_OWNING_REFERENCES, vector_of_non_owning_references); } + void add_any_unique_type(AnyUniqueAliases any_unique_type) { + fbb_.AddElement(Monster::VT_ANY_UNIQUE_TYPE, static_cast(any_unique_type), 0); + } + void add_any_unique(flatbuffers::Offset any_unique) { + fbb_.AddOffset(Monster::VT_ANY_UNIQUE, any_unique); + } + void add_any_ambiguous_type(AnyAmbiguousAliases any_ambiguous_type) { + fbb_.AddElement(Monster::VT_ANY_AMBIGUOUS_TYPE, static_cast(any_ambiguous_type), 0); + } + void add_any_ambiguous(flatbuffers::Offset any_ambiguous) { + fbb_.AddOffset(Monster::VT_ANY_AMBIGUOUS, any_ambiguous); + } explicit MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); @@ -1477,7 +1803,11 @@ inline flatbuffers::Offset CreateMonster( uint64_t co_owning_reference = 0, flatbuffers::Offset> vector_of_co_owning_references = 0, uint64_t non_owning_reference = 0, - flatbuffers::Offset> vector_of_non_owning_references = 0) { + flatbuffers::Offset> vector_of_non_owning_references = 0, + AnyUniqueAliases any_unique_type = AnyUniqueAliases_NONE, + flatbuffers::Offset any_unique = 0, + AnyAmbiguousAliases any_ambiguous_type = AnyAmbiguousAliases_NONE, + flatbuffers::Offset any_ambiguous = 0) { MonsterBuilder builder_(_fbb); builder_.add_non_owning_reference(non_owning_reference); builder_.add_co_owning_reference(co_owning_reference); @@ -1486,6 +1816,8 @@ inline flatbuffers::Offset CreateMonster( builder_.add_testhashs64_fnv1a(testhashs64_fnv1a); builder_.add_testhashu64_fnv1(testhashu64_fnv1); builder_.add_testhashs64_fnv1(testhashs64_fnv1); + builder_.add_any_ambiguous(any_ambiguous); + builder_.add_any_unique(any_unique); builder_.add_vector_of_non_owning_references(vector_of_non_owning_references); builder_.add_vector_of_co_owning_references(vector_of_co_owning_references); builder_.add_vector_of_strong_referrables(vector_of_strong_referrables); @@ -1518,6 +1850,8 @@ inline flatbuffers::Offset CreateMonster( builder_.add_pos(pos); builder_.add_hp(hp); builder_.add_mana(mana); + builder_.add_any_ambiguous_type(any_ambiguous_type); + builder_.add_any_unique_type(any_unique_type); builder_.add_testbool(testbool); builder_.add_test_type(test_type); builder_.add_color(color); @@ -1567,7 +1901,11 @@ inline flatbuffers::Offset CreateMonsterDirect( uint64_t co_owning_reference = 0, const std::vector *vector_of_co_owning_references = nullptr, uint64_t non_owning_reference = 0, - const std::vector *vector_of_non_owning_references = nullptr) { + const std::vector *vector_of_non_owning_references = nullptr, + AnyUniqueAliases any_unique_type = AnyUniqueAliases_NONE, + flatbuffers::Offset any_unique = 0, + AnyAmbiguousAliases any_ambiguous_type = AnyAmbiguousAliases_NONE, + flatbuffers::Offset any_ambiguous = 0) { return MyGame::Example::CreateMonster( _fbb, pos, @@ -1611,7 +1949,11 @@ inline flatbuffers::Offset CreateMonsterDirect( co_owning_reference, vector_of_co_owning_references ? _fbb.CreateVector(*vector_of_co_owning_references) : 0, non_owning_reference, - vector_of_non_owning_references ? _fbb.CreateVector(*vector_of_non_owning_references) : 0); + vector_of_non_owning_references ? _fbb.CreateVector(*vector_of_non_owning_references) : 0, + any_unique_type, + any_unique, + any_ambiguous_type, + any_ambiguous); } flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); @@ -2083,6 +2425,10 @@ if (_resolver) (*_resolver)(reinterpret_cast(&_o->vector_of_co_owning_r if (_resolver) (*_resolver)(reinterpret_cast(&_o->non_owning_reference), static_cast(_e)); else _o->non_owning_reference = nullptr; }; { auto _e = vector_of_non_owning_references(); if (_e) { _o->vector_of_non_owning_references.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { //vector resolver, naked if (_resolver) (*_resolver)(reinterpret_cast(&_o->vector_of_non_owning_references[_i]), static_cast(_e->Get(_i))); else _o->vector_of_non_owning_references[_i] = nullptr; } } }; + { auto _e = any_unique_type(); _o->any_unique.type = _e; }; + { auto _e = any_unique(); if (_e) _o->any_unique.value = AnyUniqueAliasesUnion::UnPack(_e, any_unique_type(), _resolver); }; + { auto _e = any_ambiguous_type(); _o->any_ambiguous.type = _e; }; + { auto _e = any_ambiguous(); if (_e) _o->any_ambiguous.value = AnyAmbiguousAliasesUnion::UnPack(_e, any_ambiguous_type(), _resolver); }; } inline flatbuffers::Offset Monster::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT* _o, const flatbuffers::rehasher_function_t *_rehasher) { @@ -2135,6 +2481,10 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder auto _vector_of_co_owning_references = _o->vector_of_co_owning_references.size() ? _fbb.CreateVector(_o->vector_of_co_owning_references.size(), [](size_t i, _VectorArgs *__va) { return __va->__rehasher ? static_cast((*__va->__rehasher)(__va->__o->vector_of_co_owning_references[i].get())) : 0; }, &_va ) : 0; auto _non_owning_reference = _rehasher ? static_cast((*_rehasher)(_o->non_owning_reference)) : 0; auto _vector_of_non_owning_references = _o->vector_of_non_owning_references.size() ? _fbb.CreateVector(_o->vector_of_non_owning_references.size(), [](size_t i, _VectorArgs *__va) { return __va->__rehasher ? static_cast((*__va->__rehasher)(__va->__o->vector_of_non_owning_references[i])) : 0; }, &_va ) : 0; + auto _any_unique_type = _o->any_unique.type; + auto _any_unique = _o->any_unique.Pack(_fbb); + auto _any_ambiguous_type = _o->any_ambiguous.type; + auto _any_ambiguous = _o->any_ambiguous.Pack(_fbb); return MyGame::Example::CreateMonster( _fbb, _pos, @@ -2178,7 +2528,11 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder _co_owning_reference, _vector_of_co_owning_references, _non_owning_reference, - _vector_of_non_owning_references); + _vector_of_non_owning_references, + _any_unique_type, + _any_unique, + _any_ambiguous_type, + _any_ambiguous); } inline TypeAliasesT *TypeAliases::UnPack(const flatbuffers::resolver_function_t *_resolver) const { @@ -2351,6 +2705,228 @@ inline void AnyUnion::Reset() { type = Any_NONE; } +inline bool VerifyAnyUniqueAliases(flatbuffers::Verifier &verifier, const void *obj, AnyUniqueAliases type) { + switch (type) { + case AnyUniqueAliases_NONE: { + return true; + } + case AnyUniqueAliases_M: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case AnyUniqueAliases_T: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case AnyUniqueAliases_M2: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + default: return false; + } +} + +inline bool VerifyAnyUniqueAliasesVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types) { + if (!values || !types) return !values && !types; + if (values->size() != types->size()) return false; + for (flatbuffers::uoffset_t i = 0; i < values->size(); ++i) { + if (!VerifyAnyUniqueAliases( + verifier, values->Get(i), types->GetEnum(i))) { + return false; + } + } + return true; +} + +inline void *AnyUniqueAliasesUnion::UnPack(const void *obj, AnyUniqueAliases type, const flatbuffers::resolver_function_t *resolver) { + switch (type) { + case AnyUniqueAliases_M: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case AnyUniqueAliases_T: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case AnyUniqueAliases_M2: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + default: return nullptr; + } +} + +inline flatbuffers::Offset AnyUniqueAliasesUnion::Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher) const { + switch (type) { + case AnyUniqueAliases_M: { + auto ptr = reinterpret_cast(value); + return CreateMonster(_fbb, ptr, _rehasher).Union(); + } + case AnyUniqueAliases_T: { + auto ptr = reinterpret_cast(value); + return CreateTestSimpleTableWithEnum(_fbb, ptr, _rehasher).Union(); + } + case AnyUniqueAliases_M2: { + auto ptr = reinterpret_cast(value); + return CreateMonster(_fbb, ptr, _rehasher).Union(); + } + default: return 0; + } +} + +inline AnyUniqueAliasesUnion::AnyUniqueAliasesUnion(const AnyUniqueAliasesUnion &u) FLATBUFFERS_NOEXCEPT : type(u.type), value(nullptr) { + switch (type) { + case AnyUniqueAliases_M: { + FLATBUFFERS_ASSERT(false); // MonsterT not copyable. + break; + } + case AnyUniqueAliases_T: { + value = new TestSimpleTableWithEnumT(*reinterpret_cast(u.value)); + break; + } + case AnyUniqueAliases_M2: { + value = new MyGame::Example2::MonsterT(*reinterpret_cast(u.value)); + break; + } + default: + break; + } +} + +inline void AnyUniqueAliasesUnion::Reset() { + switch (type) { + case AnyUniqueAliases_M: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case AnyUniqueAliases_T: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case AnyUniqueAliases_M2: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + default: break; + } + value = nullptr; + type = AnyUniqueAliases_NONE; +} + +inline bool VerifyAnyAmbiguousAliases(flatbuffers::Verifier &verifier, const void *obj, AnyAmbiguousAliases type) { + switch (type) { + case AnyAmbiguousAliases_NONE: { + return true; + } + case AnyAmbiguousAliases_M1: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case AnyAmbiguousAliases_M2: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case AnyAmbiguousAliases_M3: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + default: return false; + } +} + +inline bool VerifyAnyAmbiguousAliasesVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types) { + if (!values || !types) return !values && !types; + if (values->size() != types->size()) return false; + for (flatbuffers::uoffset_t i = 0; i < values->size(); ++i) { + if (!VerifyAnyAmbiguousAliases( + verifier, values->Get(i), types->GetEnum(i))) { + return false; + } + } + return true; +} + +inline void *AnyAmbiguousAliasesUnion::UnPack(const void *obj, AnyAmbiguousAliases type, const flatbuffers::resolver_function_t *resolver) { + switch (type) { + case AnyAmbiguousAliases_M1: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case AnyAmbiguousAliases_M2: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case AnyAmbiguousAliases_M3: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + default: return nullptr; + } +} + +inline flatbuffers::Offset AnyAmbiguousAliasesUnion::Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher) const { + switch (type) { + case AnyAmbiguousAliases_M1: { + auto ptr = reinterpret_cast(value); + return CreateMonster(_fbb, ptr, _rehasher).Union(); + } + case AnyAmbiguousAliases_M2: { + auto ptr = reinterpret_cast(value); + return CreateMonster(_fbb, ptr, _rehasher).Union(); + } + case AnyAmbiguousAliases_M3: { + auto ptr = reinterpret_cast(value); + return CreateMonster(_fbb, ptr, _rehasher).Union(); + } + default: return 0; + } +} + +inline AnyAmbiguousAliasesUnion::AnyAmbiguousAliasesUnion(const AnyAmbiguousAliasesUnion &u) FLATBUFFERS_NOEXCEPT : type(u.type), value(nullptr) { + switch (type) { + case AnyAmbiguousAliases_M1: { + FLATBUFFERS_ASSERT(false); // MonsterT not copyable. + break; + } + case AnyAmbiguousAliases_M2: { + FLATBUFFERS_ASSERT(false); // MonsterT not copyable. + break; + } + case AnyAmbiguousAliases_M3: { + FLATBUFFERS_ASSERT(false); // MonsterT not copyable. + break; + } + default: + break; + } +} + +inline void AnyAmbiguousAliasesUnion::Reset() { + switch (type) { + case AnyAmbiguousAliases_M1: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case AnyAmbiguousAliases_M2: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case AnyAmbiguousAliases_M3: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + default: break; + } + value = nullptr; + type = AnyAmbiguousAliases_NONE; +} + inline const flatbuffers::TypeTable *ColorTypeTable() { static const flatbuffers::TypeCode type_codes[] = { { flatbuffers::ET_CHAR, 0, 0 }, @@ -2396,6 +2972,52 @@ inline const flatbuffers::TypeTable *AnyTypeTable() { return &tt; } +inline const flatbuffers::TypeTable *AnyUniqueAliasesTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_SEQUENCE, 0, -1 }, + { flatbuffers::ET_SEQUENCE, 0, 0 }, + { flatbuffers::ET_SEQUENCE, 0, 1 }, + { flatbuffers::ET_SEQUENCE, 0, 2 } + }; + static const flatbuffers::TypeFunction type_refs[] = { + MonsterTypeTable, + TestSimpleTableWithEnumTypeTable, + MyGame::Example2::MonsterTypeTable + }; + static const char * const names[] = { + "NONE", + "M", + "T", + "M2" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_UNION, 4, type_codes, type_refs, nullptr, names + }; + return &tt; +} + +inline const flatbuffers::TypeTable *AnyAmbiguousAliasesTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_SEQUENCE, 0, -1 }, + { flatbuffers::ET_SEQUENCE, 0, 0 }, + { flatbuffers::ET_SEQUENCE, 0, 0 }, + { flatbuffers::ET_SEQUENCE, 0, 0 } + }; + static const flatbuffers::TypeFunction type_refs[] = { + MonsterTypeTable + }; + static const char * const names[] = { + "NONE", + "M1", + "M2", + "M3" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_UNION, 4, type_codes, type_refs, nullptr, names + }; + return &tt; +} + } // namespace Example inline const flatbuffers::TypeTable *InParentNamespaceTypeTable() { @@ -2568,7 +3190,11 @@ inline const flatbuffers::TypeTable *MonsterTypeTable() { { flatbuffers::ET_ULONG, 0, -1 }, { flatbuffers::ET_ULONG, 1, -1 }, { flatbuffers::ET_ULONG, 0, -1 }, - { flatbuffers::ET_ULONG, 1, -1 } + { flatbuffers::ET_ULONG, 1, -1 }, + { flatbuffers::ET_UTYPE, 0, 9 }, + { flatbuffers::ET_SEQUENCE, 0, 9 }, + { flatbuffers::ET_UTYPE, 0, 10 }, + { flatbuffers::ET_SEQUENCE, 0, 10 } }; static const flatbuffers::TypeFunction type_refs[] = { Vec3TypeTable, @@ -2579,7 +3205,9 @@ inline const flatbuffers::TypeTable *MonsterTypeTable() { StatTypeTable, AbilityTypeTable, MyGame::InParentNamespaceTypeTable, - ReferrableTypeTable + ReferrableTypeTable, + AnyUniqueAliasesTypeTable, + AnyAmbiguousAliasesTypeTable }; static const char * const names[] = { "pos", @@ -2624,10 +3252,14 @@ inline const flatbuffers::TypeTable *MonsterTypeTable() { "co_owning_reference", "vector_of_co_owning_references", "non_owning_reference", - "vector_of_non_owning_references" + "vector_of_non_owning_references", + "any_unique_type", + "any_unique", + "any_ambiguous_type", + "any_ambiguous" }; static const flatbuffers::TypeTable tt = { - flatbuffers::ST_TABLE, 43, type_codes, type_refs, nullptr, names + flatbuffers::ST_TABLE, 47, type_codes, type_refs, nullptr, names }; return &tt; } diff --git a/tests/monster_test_generated.js b/tests/monster_test_generated.js index d6437d5..70e62dd 100644 --- a/tests/monster_test_generated.js +++ b/tests/monster_test_generated.js @@ -44,6 +44,26 @@ MyGame.Example.Any = { }; /** + * @enum + */ +MyGame.Example.AnyUniqueAliases = { + NONE: 0, 0: 'NONE', + M: 1, 1: 'M', + T: 2, 2: 'T', + M2: 3, 3: 'M2' +}; + +/** + * @enum + */ +MyGame.Example.AnyAmbiguousAliases = { + NONE: 0, 0: 'NONE', + M1: 1, 1: 'M1', + M2: 2, 2: 'M2', + M3: 3, 3: 'M3' +}; + +/** * @constructor */ MyGame.InParentNamespace = function() { @@ -1655,10 +1675,74 @@ MyGame.Example.Monster.prototype.vectorOfNonOwningReferencesLength = function() }; /** + * @returns {MyGame.Example.AnyUniqueAliases} + */ +MyGame.Example.Monster.prototype.anyUniqueType = function() { + var offset = this.bb.__offset(this.bb_pos, 90); + return offset ? /** @type {MyGame.Example.AnyUniqueAliases} */ (this.bb.readUint8(this.bb_pos + offset)) : MyGame.Example.AnyUniqueAliases.NONE; +}; + +/** + * @param {MyGame.Example.AnyUniqueAliases} value + * @returns {boolean} + */ +MyGame.Example.Monster.prototype.mutate_any_unique_type = function(value) { + var offset = this.bb.__offset(this.bb_pos, 90); + + if (offset === 0) { + return false; + } + + this.bb.writeUint8(this.bb_pos + offset, value); + return true; +}; + +/** + * @param {flatbuffers.Table} obj + * @returns {?flatbuffers.Table} + */ +MyGame.Example.Monster.prototype.anyUnique = function(obj) { + var offset = this.bb.__offset(this.bb_pos, 92); + return offset ? this.bb.__union(obj, this.bb_pos + offset) : null; +}; + +/** + * @returns {MyGame.Example.AnyAmbiguousAliases} + */ +MyGame.Example.Monster.prototype.anyAmbiguousType = function() { + var offset = this.bb.__offset(this.bb_pos, 94); + return offset ? /** @type {MyGame.Example.AnyAmbiguousAliases} */ (this.bb.readUint8(this.bb_pos + offset)) : MyGame.Example.AnyAmbiguousAliases.NONE; +}; + +/** + * @param {MyGame.Example.AnyAmbiguousAliases} value + * @returns {boolean} + */ +MyGame.Example.Monster.prototype.mutate_any_ambiguous_type = function(value) { + var offset = this.bb.__offset(this.bb_pos, 94); + + if (offset === 0) { + return false; + } + + this.bb.writeUint8(this.bb_pos + offset, value); + return true; +}; + +/** + * @param {flatbuffers.Table} obj + * @returns {?flatbuffers.Table} + */ +MyGame.Example.Monster.prototype.anyAmbiguous = function(obj) { + var offset = this.bb.__offset(this.bb_pos, 96); + return offset ? this.bb.__union(obj, this.bb_pos + offset) : null; +}; + +/** * @param {flatbuffers.Builder} builder */ MyGame.Example.Monster.startMonster = function(builder) { - builder.startObject(43); + builder.startObject(47); }; /** @@ -2317,6 +2401,38 @@ MyGame.Example.Monster.startVectorOfNonOwningReferencesVector = function(builder /** * @param {flatbuffers.Builder} builder + * @param {MyGame.Example.AnyUniqueAliases} anyUniqueType + */ +MyGame.Example.Monster.addAnyUniqueType = function(builder, anyUniqueType) { + builder.addFieldInt8(43, anyUniqueType, MyGame.Example.AnyUniqueAliases.NONE); +}; + +/** + * @param {flatbuffers.Builder} builder + * @param {flatbuffers.Offset} anyUniqueOffset + */ +MyGame.Example.Monster.addAnyUnique = function(builder, anyUniqueOffset) { + builder.addFieldOffset(44, anyUniqueOffset, 0); +}; + +/** + * @param {flatbuffers.Builder} builder + * @param {MyGame.Example.AnyAmbiguousAliases} anyAmbiguousType + */ +MyGame.Example.Monster.addAnyAmbiguousType = function(builder, anyAmbiguousType) { + builder.addFieldInt8(45, anyAmbiguousType, MyGame.Example.AnyAmbiguousAliases.NONE); +}; + +/** + * @param {flatbuffers.Builder} builder + * @param {flatbuffers.Offset} anyAmbiguousOffset + */ +MyGame.Example.Monster.addAnyAmbiguous = function(builder, anyAmbiguousOffset) { + builder.addFieldOffset(46, anyAmbiguousOffset, 0); +}; + +/** + * @param {flatbuffers.Builder} builder * @returns {flatbuffers.Offset} */ MyGame.Example.Monster.endMonster = function(builder) { diff --git a/tests/monster_test_generated.lobster b/tests/monster_test_generated.lobster index d20a1b2..cd64b19 100644 --- a/tests/monster_test_generated.lobster +++ b/tests/monster_test_generated.lobster @@ -15,6 +15,18 @@ enum + Any_TestSimpleTableWithEnum = 2, Any_MyGame_Example2_Monster = 3 +enum + + AnyUniqueAliases_NONE = 0, + AnyUniqueAliases_M = 1, + AnyUniqueAliases_T = 2, + AnyUniqueAliases_M2 = 3 + +enum + + AnyAmbiguousAliases_NONE = 0, + AnyAmbiguousAliases_M1 = 1, + AnyAmbiguousAliases_M2 = 2, + AnyAmbiguousAliases_M3 = 3 + namespace MyGame struct InParentNamespace @@ -297,11 +309,27 @@ struct Monster : flatbuffers_handle buf_.read_int64_le(buf_.flatbuffers_field_vector(pos_, 88) + i * 8) def vector_of_non_owning_references_length(): buf_.flatbuffers_field_vector_len(pos_, 88) + def any_unique_type(): + buf_.flatbuffers_field_int8(pos_, 90, 0) + def any_unique_as_M(): + MyGame_Example_Monster { buf_, buf_.flatbuffers_field_table(pos_, 92) } + def any_unique_as_T(): + MyGame_Example_TestSimpleTableWithEnum { buf_, buf_.flatbuffers_field_table(pos_, 92) } + def any_unique_as_M2(): + MyGame_Example2_Monster { buf_, buf_.flatbuffers_field_table(pos_, 92) } + def any_ambiguous_type(): + buf_.flatbuffers_field_int8(pos_, 94, 0) + def any_ambiguous_as_M1(): + MyGame_Example_Monster { buf_, buf_.flatbuffers_field_table(pos_, 96) } + def any_ambiguous_as_M2(): + MyGame_Example_Monster { buf_, buf_.flatbuffers_field_table(pos_, 96) } + def any_ambiguous_as_M3(): + MyGame_Example_Monster { buf_, buf_.flatbuffers_field_table(pos_, 96) } def GetRootAsMonster(buf:string): Monster { buf, buf.flatbuffers_indirect(0) } def MonsterStart(b_:flatbuffers_builder): - b_.StartObject(43) + b_.StartObject(47) def MonsterAddPos(b_:flatbuffers_builder, pos:int): b_.PrependStructSlot(0, pos, 0) def MonsterAddMana(b_:flatbuffers_builder, mana:int): @@ -476,6 +504,14 @@ def MonsterCreateVectorOfNonOwningReferencesVector(b_:flatbuffers_builder, v_:[i b_.StartVector(8, v_.length, 8) reverse(v_) e_: b_.PrependUint64(e_) b_.EndVector(v_.length) +def MonsterAddAnyUniqueType(b_:flatbuffers_builder, any_unique_type:int): + b_.PrependUint8Slot(43, any_unique_type, 0) +def MonsterAddAnyUnique(b_:flatbuffers_builder, any_unique:int): + b_.PrependUOffsetTRelativeSlot(44, any_unique, 0) +def MonsterAddAnyAmbiguousType(b_:flatbuffers_builder, any_ambiguous_type:int): + b_.PrependUint8Slot(45, any_ambiguous_type, 0) +def MonsterAddAnyAmbiguous(b_:flatbuffers_builder, any_ambiguous:int): + b_.PrependUOffsetTRelativeSlot(46, any_ambiguous, 0) def MonsterEnd(b_:flatbuffers_builder): b_.EndObject() diff --git a/tests/monster_test_generated.rs b/tests/monster_test_generated.rs index 68e61ed..10c6d4a 100644 --- a/tests/monster_test_generated.rs +++ b/tests/monster_test_generated.rs @@ -294,6 +294,140 @@ pub fn enum_name_any(e: Any) -> &'static str { } pub struct AnyUnionTableOffset {} +#[allow(non_camel_case_types)] +#[repr(u8)] +#[derive(Clone, Copy, PartialEq, Debug)] +pub enum AnyUniqueAliases { + NONE = 0, + M = 1, + T = 2, + M2 = 3, + +} + +const ENUM_MIN_ANY_UNIQUE_ALIASES: u8 = 0; +const ENUM_MAX_ANY_UNIQUE_ALIASES: u8 = 3; + +impl<'a> flatbuffers::Follow<'a> for AnyUniqueAliases { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::read_scalar_at::(buf, loc) + } +} + +impl flatbuffers::EndianScalar for AnyUniqueAliases { + #[inline] + fn to_little_endian(self) -> Self { + let n = u8::to_le(self as u8); + let p = &n as *const u8 as *const AnyUniqueAliases; + unsafe { *p } + } + #[inline] + fn from_little_endian(self) -> Self { + let n = u8::from_le(self as u8); + let p = &n as *const u8 as *const AnyUniqueAliases; + unsafe { *p } + } +} + +impl flatbuffers::Push for AnyUniqueAliases { + type Output = AnyUniqueAliases; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + flatbuffers::emplace_scalar::(dst, *self); + } +} + +#[allow(non_camel_case_types)] +const ENUM_VALUES_ANY_UNIQUE_ALIASES:[AnyUniqueAliases; 4] = [ + AnyUniqueAliases::NONE, + AnyUniqueAliases::M, + AnyUniqueAliases::T, + AnyUniqueAliases::M2 +]; + +#[allow(non_camel_case_types)] +const ENUM_NAMES_ANY_UNIQUE_ALIASES:[&'static str; 4] = [ + "NONE", + "M", + "T", + "M2" +]; + +pub fn enum_name_any_unique_aliases(e: AnyUniqueAliases) -> &'static str { + let index: usize = e as usize; + ENUM_NAMES_ANY_UNIQUE_ALIASES[index] +} + +pub struct AnyUniqueAliasesUnionTableOffset {} +#[allow(non_camel_case_types)] +#[repr(u8)] +#[derive(Clone, Copy, PartialEq, Debug)] +pub enum AnyAmbiguousAliases { + NONE = 0, + M1 = 1, + M2 = 2, + M3 = 3, + +} + +const ENUM_MIN_ANY_AMBIGUOUS_ALIASES: u8 = 0; +const ENUM_MAX_ANY_AMBIGUOUS_ALIASES: u8 = 3; + +impl<'a> flatbuffers::Follow<'a> for AnyAmbiguousAliases { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::read_scalar_at::(buf, loc) + } +} + +impl flatbuffers::EndianScalar for AnyAmbiguousAliases { + #[inline] + fn to_little_endian(self) -> Self { + let n = u8::to_le(self as u8); + let p = &n as *const u8 as *const AnyAmbiguousAliases; + unsafe { *p } + } + #[inline] + fn from_little_endian(self) -> Self { + let n = u8::from_le(self as u8); + let p = &n as *const u8 as *const AnyAmbiguousAliases; + unsafe { *p } + } +} + +impl flatbuffers::Push for AnyAmbiguousAliases { + type Output = AnyAmbiguousAliases; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + flatbuffers::emplace_scalar::(dst, *self); + } +} + +#[allow(non_camel_case_types)] +const ENUM_VALUES_ANY_AMBIGUOUS_ALIASES:[AnyAmbiguousAliases; 4] = [ + AnyAmbiguousAliases::NONE, + AnyAmbiguousAliases::M1, + AnyAmbiguousAliases::M2, + AnyAmbiguousAliases::M3 +]; + +#[allow(non_camel_case_types)] +const ENUM_NAMES_ANY_AMBIGUOUS_ALIASES:[&'static str; 4] = [ + "NONE", + "M1", + "M2", + "M3" +]; + +pub fn enum_name_any_ambiguous_aliases(e: AnyAmbiguousAliases) -> &'static str { + let index: usize = e as usize; + ENUM_NAMES_ANY_AMBIGUOUS_ALIASES[index] +} + +pub struct AnyAmbiguousAliasesUnionTableOffset {} // struct Test, aligned to 2 #[repr(C, align(2))] #[derive(Clone, Copy, Debug, PartialEq)] @@ -814,6 +948,8 @@ impl<'a> Monster<'a> { builder.add_testhashs64_fnv1a(args.testhashs64_fnv1a); builder.add_testhashu64_fnv1(args.testhashu64_fnv1); builder.add_testhashs64_fnv1(args.testhashs64_fnv1); + if let Some(x) = args.any_ambiguous { builder.add_any_ambiguous(x); } + if let Some(x) = args.any_unique { builder.add_any_unique(x); } if let Some(x) = args.vector_of_non_owning_references { builder.add_vector_of_non_owning_references(x); } if let Some(x) = args.vector_of_co_owning_references { builder.add_vector_of_co_owning_references(x); } if let Some(x) = args.vector_of_strong_referrables { builder.add_vector_of_strong_referrables(x); } @@ -846,6 +982,8 @@ impl<'a> Monster<'a> { if let Some(x) = args.pos { builder.add_pos(x); } builder.add_hp(args.hp); builder.add_mana(args.mana); + builder.add_any_ambiguous_type(args.any_ambiguous_type); + builder.add_any_unique_type(args.any_unique_type); builder.add_testbool(args.testbool); builder.add_test_type(args.test_type); builder.add_color(args.color); @@ -894,6 +1032,10 @@ impl<'a> Monster<'a> { pub const VT_VECTOR_OF_CO_OWNING_REFERENCES: flatbuffers::VOffsetT = 84; pub const VT_NON_OWNING_REFERENCE: flatbuffers::VOffsetT = 86; pub const VT_VECTOR_OF_NON_OWNING_REFERENCES: flatbuffers::VOffsetT = 88; + pub const VT_ANY_UNIQUE_TYPE: flatbuffers::VOffsetT = 90; + pub const VT_ANY_UNIQUE: flatbuffers::VOffsetT = 92; + pub const VT_ANY_AMBIGUOUS_TYPE: flatbuffers::VOffsetT = 94; + pub const VT_ANY_AMBIGUOUS: flatbuffers::VOffsetT = 96; #[inline] pub fn pos(&self) -> Option<&'a Vec3> { @@ -1085,6 +1227,22 @@ impl<'a> Monster<'a> { self._tab.get::>>(Monster::VT_VECTOR_OF_NON_OWNING_REFERENCES, None) } #[inline] + pub fn any_unique_type(&self) -> AnyUniqueAliases { + self._tab.get::(Monster::VT_ANY_UNIQUE_TYPE, Some(AnyUniqueAliases::NONE)).unwrap() + } + #[inline] + pub fn any_unique(&self) -> Option> { + self._tab.get::>>(Monster::VT_ANY_UNIQUE, None) + } + #[inline] + pub fn any_ambiguous_type(&self) -> AnyAmbiguousAliases { + self._tab.get::(Monster::VT_ANY_AMBIGUOUS_TYPE, Some(AnyAmbiguousAliases::NONE)).unwrap() + } + #[inline] + pub fn any_ambiguous(&self) -> Option> { + self._tab.get::>>(Monster::VT_ANY_AMBIGUOUS, None) + } + #[inline] #[allow(non_snake_case)] pub fn test_as_monster(&'a self) -> Option { if self.test_type() == Any::Monster { @@ -1114,6 +1272,66 @@ impl<'a> Monster<'a> { } } + #[inline] + #[allow(non_snake_case)] + pub fn any_unique_as_m(&'a self) -> Option { + if self.any_unique_type() == AnyUniqueAliases::M { + self.any_unique().map(|u| Monster::init_from_table(u)) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn any_unique_as_t(&'a self) -> Option { + if self.any_unique_type() == AnyUniqueAliases::T { + self.any_unique().map(|u| TestSimpleTableWithEnum::init_from_table(u)) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn any_unique_as_m2(&'a self) -> Option { + if self.any_unique_type() == AnyUniqueAliases::M2 { + self.any_unique().map(|u| super::example_2::Monster::init_from_table(u)) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn any_ambiguous_as_m1(&'a self) -> Option { + if self.any_ambiguous_type() == AnyAmbiguousAliases::M1 { + self.any_ambiguous().map(|u| Monster::init_from_table(u)) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn any_ambiguous_as_m2(&'a self) -> Option { + if self.any_ambiguous_type() == AnyAmbiguousAliases::M2 { + self.any_ambiguous().map(|u| Monster::init_from_table(u)) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn any_ambiguous_as_m3(&'a self) -> Option { + if self.any_ambiguous_type() == AnyAmbiguousAliases::M3 { + self.any_ambiguous().map(|u| Monster::init_from_table(u)) + } else { + None + } + } + } pub struct MonsterArgs<'a> { @@ -1159,6 +1377,10 @@ pub struct MonsterArgs<'a> { pub vector_of_co_owning_references: Option>>, pub non_owning_reference: u64, pub vector_of_non_owning_references: Option>>, + pub any_unique_type: AnyUniqueAliases, + pub any_unique: Option>, + pub any_ambiguous_type: AnyAmbiguousAliases, + pub any_ambiguous: Option>, } impl<'a> Default for MonsterArgs<'a> { #[inline] @@ -1206,6 +1428,10 @@ impl<'a> Default for MonsterArgs<'a> { vector_of_co_owning_references: None, non_owning_reference: 0, vector_of_non_owning_references: None, + any_unique_type: AnyUniqueAliases::NONE, + any_unique: None, + any_ambiguous_type: AnyAmbiguousAliases::NONE, + any_ambiguous: None, } } } @@ -1383,6 +1609,22 @@ impl<'a: 'b, 'b> MonsterBuilder<'a, 'b> { self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_NON_OWNING_REFERENCES, vector_of_non_owning_references); } #[inline] + pub fn add_any_unique_type(&mut self, any_unique_type: AnyUniqueAliases) { + self.fbb_.push_slot::(Monster::VT_ANY_UNIQUE_TYPE, any_unique_type, AnyUniqueAliases::NONE); + } + #[inline] + pub fn add_any_unique(&mut self, any_unique: flatbuffers::WIPOffset) { + self.fbb_.push_slot_always::>(Monster::VT_ANY_UNIQUE, any_unique); + } + #[inline] + pub fn add_any_ambiguous_type(&mut self, any_ambiguous_type: AnyAmbiguousAliases) { + self.fbb_.push_slot::(Monster::VT_ANY_AMBIGUOUS_TYPE, any_ambiguous_type, AnyAmbiguousAliases::NONE); + } + #[inline] + pub fn add_any_ambiguous(&mut self, any_ambiguous: flatbuffers::WIPOffset) { + self.fbb_.push_slot_always::>(Monster::VT_ANY_AMBIGUOUS, any_ambiguous); + } + #[inline] pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> MonsterBuilder<'a, 'b> { let start = _fbb.start_table(); MonsterBuilder { diff --git a/tests/monster_test_generated.ts b/tests/monster_test_generated.ts index 73d696e..edb7492 100644 --- a/tests/monster_test_generated.ts +++ b/tests/monster_test_generated.ts @@ -22,6 +22,28 @@ export enum Any{ }}; /** + * @enum + */ +export namespace MyGame.Example{ +export enum AnyUniqueAliases{ + NONE= 0, + M= 1, + T= 2, + M2= 3 +}}; + +/** + * @enum + */ +export namespace MyGame.Example{ +export enum AnyAmbiguousAliases{ + NONE= 0, + M1= 1, + M2= 2, + M3= 3 +}}; + +/** * @constructor */ export namespace MyGame{ @@ -1594,10 +1616,74 @@ vectorOfNonOwningReferencesLength():number { }; /** + * @returns MyGame.Example.AnyUniqueAliases + */ +anyUniqueType():MyGame.Example.AnyUniqueAliases { + var offset = this.bb!.__offset(this.bb_pos, 90); + return offset ? /** */ (this.bb!.readUint8(this.bb_pos + offset)) : MyGame.Example.AnyUniqueAliases.NONE; +}; + +/** + * @param MyGame.Example.AnyUniqueAliases value + * @returns boolean + */ +mutate_any_unique_type(value:MyGame.Example.AnyUniqueAliases):boolean { + var offset = this.bb!.__offset(this.bb_pos, 90); + + if (offset === 0) { + return false; + } + + this.bb!.writeUint8(this.bb_pos + offset, value); + return true; +}; + +/** + * @param flatbuffers.Table obj + * @returns ?flatbuffers.Table + */ +anyUnique(obj:T):T|null { + var offset = this.bb!.__offset(this.bb_pos, 92); + return offset ? this.bb!.__union(obj, this.bb_pos + offset) : null; +}; + +/** + * @returns MyGame.Example.AnyAmbiguousAliases + */ +anyAmbiguousType():MyGame.Example.AnyAmbiguousAliases { + var offset = this.bb!.__offset(this.bb_pos, 94); + return offset ? /** */ (this.bb!.readUint8(this.bb_pos + offset)) : MyGame.Example.AnyAmbiguousAliases.NONE; +}; + +/** + * @param MyGame.Example.AnyAmbiguousAliases value + * @returns boolean + */ +mutate_any_ambiguous_type(value:MyGame.Example.AnyAmbiguousAliases):boolean { + var offset = this.bb!.__offset(this.bb_pos, 94); + + if (offset === 0) { + return false; + } + + this.bb!.writeUint8(this.bb_pos + offset, value); + return true; +}; + +/** + * @param flatbuffers.Table obj + * @returns ?flatbuffers.Table + */ +anyAmbiguous(obj:T):T|null { + var offset = this.bb!.__offset(this.bb_pos, 96); + return offset ? this.bb!.__union(obj, this.bb_pos + offset) : null; +}; + +/** * @param flatbuffers.Builder builder */ static startMonster(builder:flatbuffers.Builder) { - builder.startObject(43); + builder.startObject(47); }; /** @@ -2256,6 +2342,38 @@ static startVectorOfNonOwningReferencesVector(builder:flatbuffers.Builder, numEl /** * @param flatbuffers.Builder builder + * @param MyGame.Example.AnyUniqueAliases anyUniqueType + */ +static addAnyUniqueType(builder:flatbuffers.Builder, anyUniqueType:MyGame.Example.AnyUniqueAliases) { + builder.addFieldInt8(43, anyUniqueType, MyGame.Example.AnyUniqueAliases.NONE); +}; + +/** + * @param flatbuffers.Builder builder + * @param flatbuffers.Offset anyUniqueOffset + */ +static addAnyUnique(builder:flatbuffers.Builder, anyUniqueOffset:flatbuffers.Offset) { + builder.addFieldOffset(44, anyUniqueOffset, 0); +}; + +/** + * @param flatbuffers.Builder builder + * @param MyGame.Example.AnyAmbiguousAliases anyAmbiguousType + */ +static addAnyAmbiguousType(builder:flatbuffers.Builder, anyAmbiguousType:MyGame.Example.AnyAmbiguousAliases) { + builder.addFieldInt8(45, anyAmbiguousType, MyGame.Example.AnyAmbiguousAliases.NONE); +}; + +/** + * @param flatbuffers.Builder builder + * @param flatbuffers.Offset anyAmbiguousOffset + */ +static addAnyAmbiguous(builder:flatbuffers.Builder, anyAmbiguousOffset:flatbuffers.Offset) { + builder.addFieldOffset(46, anyAmbiguousOffset, 0); +}; + +/** + * @param flatbuffers.Builder builder * @returns flatbuffers.Offset */ static endMonster(builder:flatbuffers.Builder):flatbuffers.Offset { diff --git a/tests/monster_test_my_game.example_generated.dart b/tests/monster_test_my_game.example_generated.dart index d5c201b..c85bae4 100644 --- a/tests/monster_test_my_game.example_generated.dart +++ b/tests/monster_test_my_game.example_generated.dart @@ -88,6 +88,88 @@ class _AnyTypeIdReader extends fb.Reader { new AnyTypeId.fromValue(const fb.Uint8Reader().read(bc, offset)); } +class AnyUniqueAliasesTypeId { + final int value; + const AnyUniqueAliasesTypeId._(this.value); + + factory AnyUniqueAliasesTypeId.fromValue(int value) { + if (value == null) value = 0; + if (!values.containsKey(value)) { + throw new StateError('Invalid value $value for bit flag enum AnyUniqueAliasesTypeId'); + } + return values[value]; + } + + static const int minValue = 0; + static const int maxValue = 3; + static bool containsValue(int value) => values.containsKey(value); + + static const AnyUniqueAliasesTypeId NONE = const AnyUniqueAliasesTypeId._(0); + static const AnyUniqueAliasesTypeId M = const AnyUniqueAliasesTypeId._(1); + static const AnyUniqueAliasesTypeId T = const AnyUniqueAliasesTypeId._(2); + static const AnyUniqueAliasesTypeId M2 = const AnyUniqueAliasesTypeId._(3); + static get values => {0: NONE,1: M,2: T,3: M2,}; + + static const fb.Reader reader = const _AnyUniqueAliasesTypeIdReader(); + + @override + String toString() { + return 'AnyUniqueAliasesTypeId{value: $value}'; + } +} + +class _AnyUniqueAliasesTypeIdReader extends fb.Reader { + const _AnyUniqueAliasesTypeIdReader(); + + @override + int get size => 1; + + @override + AnyUniqueAliasesTypeId read(fb.BufferContext bc, int offset) => + new AnyUniqueAliasesTypeId.fromValue(const fb.Uint8Reader().read(bc, offset)); +} + +class AnyAmbiguousAliasesTypeId { + final int value; + const AnyAmbiguousAliasesTypeId._(this.value); + + factory AnyAmbiguousAliasesTypeId.fromValue(int value) { + if (value == null) value = 0; + if (!values.containsKey(value)) { + throw new StateError('Invalid value $value for bit flag enum AnyAmbiguousAliasesTypeId'); + } + return values[value]; + } + + static const int minValue = 0; + static const int maxValue = 3; + static bool containsValue(int value) => values.containsKey(value); + + static const AnyAmbiguousAliasesTypeId NONE = const AnyAmbiguousAliasesTypeId._(0); + static const AnyAmbiguousAliasesTypeId M1 = const AnyAmbiguousAliasesTypeId._(1); + static const AnyAmbiguousAliasesTypeId M2 = const AnyAmbiguousAliasesTypeId._(2); + static const AnyAmbiguousAliasesTypeId M3 = const AnyAmbiguousAliasesTypeId._(3); + static get values => {0: NONE,1: M1,2: M2,3: M3,}; + + static const fb.Reader reader = const _AnyAmbiguousAliasesTypeIdReader(); + + @override + String toString() { + return 'AnyAmbiguousAliasesTypeId{value: $value}'; + } +} + +class _AnyAmbiguousAliasesTypeIdReader extends fb.Reader { + const _AnyAmbiguousAliasesTypeIdReader(); + + @override + int get size => 1; + + @override + AnyAmbiguousAliasesTypeId read(fb.BufferContext bc, int offset) => + new AnyAmbiguousAliasesTypeId.fromValue(const fb.Uint8Reader().read(bc, offset)); +} + class Test { Test._(this._bc, this._bcOffset); @@ -652,10 +734,28 @@ class Monster { List get vectorOfCoOwningReferences => const fb.ListReader(const fb.Uint64Reader()).vTableGet(_bc, _bcOffset, 84, null); int get nonOwningReference => const fb.Uint64Reader().vTableGet(_bc, _bcOffset, 86, 0); List get vectorOfNonOwningReferences => const fb.ListReader(const fb.Uint64Reader()).vTableGet(_bc, _bcOffset, 88, null); + AnyUniqueAliasesTypeId get anyUniqueType => new AnyUniqueAliasesTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 90, 0)); + dynamic get anyUnique { + switch (anyUniqueType?.value) { + case 1: return M.reader.vTableGet(_bc, _bcOffset, 92, null); + case 2: return T.reader.vTableGet(_bc, _bcOffset, 92, null); + case 3: return M2.reader.vTableGet(_bc, _bcOffset, 92, null); + default: return null; + } + } + AnyAmbiguousAliasesTypeId get anyAmbiguousType => new AnyAmbiguousAliasesTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 94, 0)); + dynamic get anyAmbiguous { + switch (anyAmbiguousType?.value) { + case 1: return M1.reader.vTableGet(_bc, _bcOffset, 96, null); + case 2: return M2.reader.vTableGet(_bc, _bcOffset, 96, null); + case 3: return M3.reader.vTableGet(_bc, _bcOffset, 96, null); + default: return null; + } + } @override String toString() { - return 'Monster{pos: $pos, mana: $mana, hp: $hp, name: $name, inventory: $inventory, color: $color, testType: $testType, test: $test, test4: $test4, testarrayofstring: $testarrayofstring, testarrayoftables: $testarrayoftables, enemy: $enemy, testnestedflatbuffer: $testnestedflatbuffer, testempty: $testempty, testbool: $testbool, testhashs32Fnv1: $testhashs32Fnv1, testhashu32Fnv1: $testhashu32Fnv1, testhashs64Fnv1: $testhashs64Fnv1, testhashu64Fnv1: $testhashu64Fnv1, testhashs32Fnv1a: $testhashs32Fnv1a, testhashu32Fnv1a: $testhashu32Fnv1a, testhashs64Fnv1a: $testhashs64Fnv1a, testhashu64Fnv1a: $testhashu64Fnv1a, testarrayofbools: $testarrayofbools, testf: $testf, testf2: $testf2, testf3: $testf3, testarrayofstring2: $testarrayofstring2, testarrayofsortedstruct: $testarrayofsortedstruct, flex: $flex, test5: $test5, vectorOfLongs: $vectorOfLongs, vectorOfDoubles: $vectorOfDoubles, parentNamespaceTest: $parentNamespaceTest, vectorOfReferrables: $vectorOfReferrables, singleWeakReference: $singleWeakReference, vectorOfWeakReferences: $vectorOfWeakReferences, vectorOfStrongReferrables: $vectorOfStrongReferrables, coOwningReference: $coOwningReference, vectorOfCoOwningReferences: $vectorOfCoOwningReferences, nonOwningReference: $nonOwningReference, vectorOfNonOwningReferences: $vectorOfNonOwningReferences}'; + return 'Monster{pos: $pos, mana: $mana, hp: $hp, name: $name, inventory: $inventory, color: $color, testType: $testType, test: $test, test4: $test4, testarrayofstring: $testarrayofstring, testarrayoftables: $testarrayoftables, enemy: $enemy, testnestedflatbuffer: $testnestedflatbuffer, testempty: $testempty, testbool: $testbool, testhashs32Fnv1: $testhashs32Fnv1, testhashu32Fnv1: $testhashu32Fnv1, testhashs64Fnv1: $testhashs64Fnv1, testhashu64Fnv1: $testhashu64Fnv1, testhashs32Fnv1a: $testhashs32Fnv1a, testhashu32Fnv1a: $testhashu32Fnv1a, testhashs64Fnv1a: $testhashs64Fnv1a, testhashu64Fnv1a: $testhashu64Fnv1a, testarrayofbools: $testarrayofbools, testf: $testf, testf2: $testf2, testf3: $testf3, testarrayofstring2: $testarrayofstring2, testarrayofsortedstruct: $testarrayofsortedstruct, flex: $flex, test5: $test5, vectorOfLongs: $vectorOfLongs, vectorOfDoubles: $vectorOfDoubles, parentNamespaceTest: $parentNamespaceTest, vectorOfReferrables: $vectorOfReferrables, singleWeakReference: $singleWeakReference, vectorOfWeakReferences: $vectorOfWeakReferences, vectorOfStrongReferrables: $vectorOfStrongReferrables, coOwningReference: $coOwningReference, vectorOfCoOwningReferences: $vectorOfCoOwningReferences, nonOwningReference: $nonOwningReference, vectorOfNonOwningReferences: $vectorOfNonOwningReferences, anyUniqueType: $anyUniqueType, anyUnique: $anyUnique, anyAmbiguousType: $anyAmbiguousType, anyAmbiguous: $anyAmbiguous}'; } } @@ -846,6 +946,22 @@ class MonsterBuilder { fbBuilder.addOffset(42, offset); return fbBuilder.offset; } + int addAnyUniqueType(AnyUniqueAliasesTypeId anyUniqueType) { + fbBuilder.addUint8(43, anyUniqueType?.value); + return fbBuilder.offset; + } + int addAnyUniqueOffset(int offset) { + fbBuilder.addOffset(44, offset); + return fbBuilder.offset; + } + int addAnyAmbiguousType(AnyAmbiguousAliasesTypeId anyAmbiguousType) { + fbBuilder.addUint8(45, anyAmbiguousType?.value); + return fbBuilder.offset; + } + int addAnyAmbiguousOffset(int offset) { + fbBuilder.addOffset(46, offset); + return fbBuilder.offset; + } int finish() { return fbBuilder.endTable(); @@ -895,6 +1011,10 @@ class MonsterObjectBuilder extends fb.ObjectBuilder { final List _vectorOfCoOwningReferences; final int _nonOwningReference; final List _vectorOfNonOwningReferences; + final AnyUniqueAliasesTypeId _anyUniqueType; + final dynamic _anyUnique; + final AnyAmbiguousAliasesTypeId _anyAmbiguousType; + final dynamic _anyAmbiguous; MonsterObjectBuilder({ Vec3ObjectBuilder pos, @@ -939,6 +1059,10 @@ class MonsterObjectBuilder extends fb.ObjectBuilder { List vectorOfCoOwningReferences, int nonOwningReference, List vectorOfNonOwningReferences, + AnyUniqueAliasesTypeId anyUniqueType, + dynamic anyUnique, + AnyAmbiguousAliasesTypeId anyAmbiguousType, + dynamic anyAmbiguous, }) : _pos = pos, _mana = mana, @@ -981,7 +1105,11 @@ class MonsterObjectBuilder extends fb.ObjectBuilder { _coOwningReference = coOwningReference, _vectorOfCoOwningReferences = vectorOfCoOwningReferences, _nonOwningReference = nonOwningReference, - _vectorOfNonOwningReferences = vectorOfNonOwningReferences; + _vectorOfNonOwningReferences = vectorOfNonOwningReferences, + _anyUniqueType = anyUniqueType, + _anyUnique = anyUnique, + _anyAmbiguousType = anyAmbiguousType, + _anyAmbiguous = anyAmbiguous; /// Finish building, and store into the [fbBuilder]. @override @@ -1044,6 +1172,8 @@ class MonsterObjectBuilder extends fb.ObjectBuilder { final int vectorOfNonOwningReferencesOffset = _vectorOfNonOwningReferences?.isNotEmpty == true ? fbBuilder.writeListUint64(_vectorOfNonOwningReferences) : null; + final int anyUniqueOffset = _anyUnique?.getOrCreateOffset(fbBuilder); + final int anyAmbiguousOffset = _anyAmbiguous?.getOrCreateOffset(fbBuilder); fbBuilder.startTable(); if (_pos != null) { @@ -1134,6 +1264,14 @@ class MonsterObjectBuilder extends fb.ObjectBuilder { if (vectorOfNonOwningReferencesOffset != null) { fbBuilder.addOffset(42, vectorOfNonOwningReferencesOffset); } + fbBuilder.addUint8(43, _anyUniqueType?.value); + if (anyUniqueOffset != null) { + fbBuilder.addOffset(44, anyUniqueOffset); + } + fbBuilder.addUint8(45, _anyAmbiguousType?.value); + if (anyAmbiguousOffset != null) { + fbBuilder.addOffset(46, anyAmbiguousOffset); + } return fbBuilder.endTable(); } diff --git a/tests/union_vector/union_vector_generated.h b/tests/union_vector/union_vector_generated.h index a7e3cd4..6645ca2 100644 --- a/tests/union_vector/union_vector_generated.h +++ b/tests/union_vector/union_vector_generated.h @@ -332,7 +332,6 @@ struct Movie FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { const void *main_character() const { return GetPointer(VT_MAIN_CHARACTER); } - template const T *main_character_as() const; const Attacker *main_character_as_MuLan() const { return main_character_type() == Character_MuLan ? static_cast(main_character()) : nullptr; }