mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-29 00:50:01 +00:00
Union As Accessors for C# (#6251)
* Union As Accessors for C# * Changed loop to be compatible with older compilers * errant change fix
This commit is contained in:
@@ -618,8 +618,9 @@ class CSharpGenerator : public BaseGenerator {
|
|||||||
std::string dest_mask = "";
|
std::string dest_mask = "";
|
||||||
std::string dest_cast = DestinationCast(field.value.type);
|
std::string dest_cast = DestinationCast(field.value.type);
|
||||||
std::string src_cast = SourceCast(field.value.type);
|
std::string src_cast = SourceCast(field.value.type);
|
||||||
std::string method_start = " public " + type_name_dest + optional + " " +
|
std::string field_name_camel = MakeCamel(field.name, true);
|
||||||
MakeCamel(field.name, true);
|
std::string method_start =
|
||||||
|
" public " + type_name_dest + optional + " " + field_name_camel;
|
||||||
std::string obj = "(new " + type_name + "())";
|
std::string obj = "(new " + type_name + "())";
|
||||||
|
|
||||||
// Most field accessors need to retrieve and test the field offset first,
|
// Most field accessors need to retrieve and test the field offset first,
|
||||||
@@ -763,6 +764,30 @@ class CSharpGenerator : public BaseGenerator {
|
|||||||
code += offset_prefix + GenGetter(Type(BASE_TYPE_STRING));
|
code += offset_prefix + GenGetter(Type(BASE_TYPE_STRING));
|
||||||
code += "(o + __p.bb_pos) : null";
|
code += "(o + __p.bb_pos) : null";
|
||||||
}
|
}
|
||||||
|
// As<> accesors for Unions
|
||||||
|
// Loop through all the possible union types and generate an As
|
||||||
|
// accessor that casts to the correct type.
|
||||||
|
for (auto uit = field.value.type.enum_def->Vals().begin();
|
||||||
|
uit != field.value.type.enum_def->Vals().end(); ++uit) {
|
||||||
|
auto val = *uit;
|
||||||
|
if (val->union_type.base_type == BASE_TYPE_NONE) { continue; }
|
||||||
|
auto union_field_type_name = GenTypeGet(val->union_type);
|
||||||
|
code += member_suffix + "}\n";
|
||||||
|
if (val->union_type.base_type == BASE_TYPE_STRUCT &&
|
||||||
|
val->union_type.struct_def->attributes.Lookup("private")) {
|
||||||
|
code += " internal ";
|
||||||
|
} else {
|
||||||
|
code += " public ";
|
||||||
|
}
|
||||||
|
code += union_field_type_name + " ";
|
||||||
|
code += field_name_camel + "As" + val->name + "() { return ";
|
||||||
|
code += field_name_camel;
|
||||||
|
if (IsString(val->union_type)) {
|
||||||
|
code += "AsString()";
|
||||||
|
} else {
|
||||||
|
code += "<" + union_field_type_name + ">().Value";
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default: FLATBUFFERS_ASSERT(0);
|
default: FLATBUFFERS_ASSERT(0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,9 @@ public struct Monster : IFlatbufferObject
|
|||||||
public bool MutateColor(MyGame.Example.Color color) { int o = __p.__offset(16); if (o != 0) { __p.bb.Put(o + __p.bb_pos, (byte)color); return true; } else { return false; } }
|
public bool MutateColor(MyGame.Example.Color color) { int o = __p.__offset(16); if (o != 0) { __p.bb.Put(o + __p.bb_pos, (byte)color); return true; } else { return false; } }
|
||||||
public MyGame.Example.Any TestType { get { int o = __p.__offset(18); return o != 0 ? (MyGame.Example.Any)__p.bb.Get(o + __p.bb_pos) : MyGame.Example.Any.NONE; } }
|
public MyGame.Example.Any TestType { get { int o = __p.__offset(18); return o != 0 ? (MyGame.Example.Any)__p.bb.Get(o + __p.bb_pos) : MyGame.Example.Any.NONE; } }
|
||||||
public TTable? Test<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(20); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
|
public TTable? Test<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(20); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
|
||||||
|
public MyGame.Example.Monster TestAsMonster() { return Test<MyGame.Example.Monster>().Value; }
|
||||||
|
internal MyGame.Example.TestSimpleTableWithEnum TestAsTestSimpleTableWithEnum() { return Test<MyGame.Example.TestSimpleTableWithEnum>().Value; }
|
||||||
|
public MyGame.Example2.Monster TestAsMyGame_Example2_Monster() { return Test<MyGame.Example2.Monster>().Value; }
|
||||||
public MyGame.Example.Test? Test4(int j) { int o = __p.__offset(22); return o != 0 ? (MyGame.Example.Test?)(new MyGame.Example.Test()).__assign(__p.__vector(o) + j * 4, __p.bb) : null; }
|
public MyGame.Example.Test? Test4(int j) { int o = __p.__offset(22); return o != 0 ? (MyGame.Example.Test?)(new MyGame.Example.Test()).__assign(__p.__vector(o) + j * 4, __p.bb) : null; }
|
||||||
public int Test4Length { get { int o = __p.__offset(22); return o != 0 ? __p.__vector_len(o) : 0; } }
|
public int Test4Length { get { int o = __p.__offset(22); return o != 0 ? __p.__vector_len(o) : 0; } }
|
||||||
public string Testarrayofstring(int j) { int o = __p.__offset(24); return o != 0 ? __p.__string(__p.__vector(o) + j * 4) : null; }
|
public string Testarrayofstring(int j) { int o = __p.__offset(24); return o != 0 ? __p.__string(__p.__vector(o) + j * 4) : null; }
|
||||||
@@ -175,8 +178,14 @@ public struct Monster : IFlatbufferObject
|
|||||||
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 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 MyGame.Example.AnyUniqueAliases AnyUniqueType { get { int o = __p.__offset(90); return o != 0 ? (MyGame.Example.AnyUniqueAliases)__p.bb.Get(o + __p.bb_pos) : MyGame.Example.AnyUniqueAliases.NONE; } }
|
public MyGame.Example.AnyUniqueAliases AnyUniqueType { get { int o = __p.__offset(90); return o != 0 ? (MyGame.Example.AnyUniqueAliases)__p.bb.Get(o + __p.bb_pos) : MyGame.Example.AnyUniqueAliases.NONE; } }
|
||||||
public TTable? AnyUnique<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(92); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
|
public TTable? AnyUnique<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(92); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
|
||||||
|
public MyGame.Example.Monster AnyUniqueAsM() { return AnyUnique<MyGame.Example.Monster>().Value; }
|
||||||
|
internal MyGame.Example.TestSimpleTableWithEnum AnyUniqueAsTS() { return AnyUnique<MyGame.Example.TestSimpleTableWithEnum>().Value; }
|
||||||
|
public MyGame.Example2.Monster AnyUniqueAsM2() { return AnyUnique<MyGame.Example2.Monster>().Value; }
|
||||||
public MyGame.Example.AnyAmbiguousAliases AnyAmbiguousType { get { int o = __p.__offset(94); return o != 0 ? (MyGame.Example.AnyAmbiguousAliases)__p.bb.Get(o + __p.bb_pos) : MyGame.Example.AnyAmbiguousAliases.NONE; } }
|
public MyGame.Example.AnyAmbiguousAliases AnyAmbiguousType { get { int o = __p.__offset(94); return o != 0 ? (MyGame.Example.AnyAmbiguousAliases)__p.bb.Get(o + __p.bb_pos) : MyGame.Example.AnyAmbiguousAliases.NONE; } }
|
||||||
public TTable? AnyAmbiguous<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(96); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
|
public TTable? AnyAmbiguous<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(96); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
|
||||||
|
public MyGame.Example.Monster AnyAmbiguousAsM1() { return AnyAmbiguous<MyGame.Example.Monster>().Value; }
|
||||||
|
public MyGame.Example.Monster AnyAmbiguousAsM2() { return AnyAmbiguous<MyGame.Example.Monster>().Value; }
|
||||||
|
public MyGame.Example.Monster AnyAmbiguousAsM3() { return AnyAmbiguous<MyGame.Example.Monster>().Value; }
|
||||||
public MyGame.Example.Color VectorOfEnums(int j) { int o = __p.__offset(98); return o != 0 ? (MyGame.Example.Color)__p.bb.Get(__p.__vector(o) + j * 1) : (MyGame.Example.Color)0; }
|
public MyGame.Example.Color VectorOfEnums(int j) { int o = __p.__offset(98); return o != 0 ? (MyGame.Example.Color)__p.bb.Get(__p.__vector(o) + j * 1) : (MyGame.Example.Color)0; }
|
||||||
public int VectorOfEnumsLength { get { int o = __p.__offset(98); return o != 0 ? __p.__vector_len(o) : 0; } }
|
public int VectorOfEnumsLength { get { int o = __p.__offset(98); return o != 0 ? __p.__vector_len(o) : 0; } }
|
||||||
#if ENABLE_SPAN_T
|
#if ENABLE_SPAN_T
|
||||||
|
|||||||
@@ -20,6 +20,12 @@ public struct Movie : IFlatbufferObject
|
|||||||
public Character MainCharacterType { get { int o = __p.__offset(4); return o != 0 ? (Character)__p.bb.Get(o + __p.bb_pos) : Character.NONE; } }
|
public Character MainCharacterType { get { int o = __p.__offset(4); return o != 0 ? (Character)__p.bb.Get(o + __p.bb_pos) : Character.NONE; } }
|
||||||
public TTable? MainCharacter<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(6); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
|
public TTable? MainCharacter<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(6); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
|
||||||
public string MainCharacterAsString() { int o = __p.__offset(6); return o != 0 ? __p.__string(o + __p.bb_pos) : null; }
|
public string MainCharacterAsString() { int o = __p.__offset(6); return o != 0 ? __p.__string(o + __p.bb_pos) : null; }
|
||||||
|
public Attacker MainCharacterAsMuLan() { return MainCharacter<Attacker>().Value; }
|
||||||
|
public Rapunzel MainCharacterAsRapunzel() { return MainCharacter<Rapunzel>().Value; }
|
||||||
|
public BookReader MainCharacterAsBelle() { return MainCharacter<BookReader>().Value; }
|
||||||
|
public BookReader MainCharacterAsBookFan() { return MainCharacter<BookReader>().Value; }
|
||||||
|
public string MainCharacterAsOther() { return MainCharacterAsString(); }
|
||||||
|
public string MainCharacterAsUnused() { return MainCharacterAsString(); }
|
||||||
public Character CharactersType(int j) { int o = __p.__offset(8); return o != 0 ? (Character)__p.bb.Get(__p.__vector(o) + j * 1) : (Character)0; }
|
public Character CharactersType(int j) { int o = __p.__offset(8); return o != 0 ? (Character)__p.bb.Get(__p.__vector(o) + j * 1) : (Character)0; }
|
||||||
public int CharactersTypeLength { get { int o = __p.__offset(8); return o != 0 ? __p.__vector_len(o) : 0; } }
|
public int CharactersTypeLength { get { int o = __p.__offset(8); return o != 0 ? __p.__vector_len(o) : 0; } }
|
||||||
#if ENABLE_SPAN_T
|
#if ENABLE_SPAN_T
|
||||||
|
|||||||
Reference in New Issue
Block a user