From 221193eaa2a92d6c8f1e8c149c418b0add55b2a5 Mon Sep 17 00:00:00 2001 From: Mormegil Date: Wed, 15 Apr 2015 17:51:20 +0200 Subject: [PATCH] Union accessors in C# should use generic type for the table When accessing a union field, we should return the same object type as was given to the method, i.e. the parameter should have a generic type for any Table-derived type. This way, we do not need to make superfluous casts (which also reduce type safety) like var myUnionType = (MyUnionType)buff.GetUnionField(new MyUnionType()); when we can do just var myUnionType = buff.GetUnionField(new MyUnionType()); Change-Id: Idac1b638e46cc50b1f2dc19f10741481202b1515 --- net/FlatBuffers/Table.cs | 2 +- src/idl_gen_general.cpp | 44 +++++++++++++++++++++++++++++------------ tests/MyGame/Example/Monster.cs | 2 +- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/net/FlatBuffers/Table.cs b/net/FlatBuffers/Table.cs index 218258a..1a097d3 100644 --- a/net/FlatBuffers/Table.cs +++ b/net/FlatBuffers/Table.cs @@ -66,7 +66,7 @@ namespace FlatBuffers } // Initialize any Table-derived type to point to the union at the given offset. - protected Table __union(Table t, int offset) + protected TTable __union(TTable t, int offset) where TTable : Table { offset += bb_pos; t.bb_pos = offset + bb.GetInt(offset); diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index 0b399e5..e3d994d 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -258,6 +258,23 @@ static Type DestinationType(const LanguageParameters &lang, const Type &type, } } +// Generate destination type name +static std::string GenTypeNameDest(const LanguageParameters &lang, const Type &type) +{ + if (lang.language == GeneratorOptions::kCSharp) { + // C# enums are represented by themselves + if (type.enum_def != nullptr && type.base_type != BASE_TYPE_UNION) + return type.enum_def->name; + + // Unions in C# use a generic Table-derived type for better type safety + if (type.base_type == BASE_TYPE_UNION) + return "TTable"; + } + + // default behavior + return GenTypeGet(lang, DestinationType(lang, type, true)); +} + // Mask to turn serialized value into destination type value. static std::string DestinationMask(const LanguageParameters &lang, const Type &type, bool vectorelem) { @@ -533,16 +550,16 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, if (field.deprecated) continue; GenComment(field.doc_comment, code_ptr, &lang.comment_config, " "); std::string type_name = GenTypeGet(lang, field.value.type); - std::string type_name_dest = - lang.language == GeneratorOptions::kCSharp && - field.value.type.enum_def != nullptr && - field.value.type.base_type != BASE_TYPE_UNION - ? field.value.type.enum_def->name - : GenTypeGet(lang, DestinationType(lang, field.value.type, true)); + std::string type_name_dest = GenTypeNameDest(lang, field.value.type); std::string dest_mask = DestinationMask(lang, field.value.type, true); std::string dest_cast = DestinationCast(lang, field.value.type); std::string method_start = " public " + type_name_dest + " " + MakeCamel(field.name, lang.first_camel_upper); + // Most field accessors need to retrieve and test the field offset first, + // this is the prefix code for that: + auto offset_prefix = " { int o = __offset(" + + NumToString(field.value.offset) + + "); return o != 0 ? "; // Generate the accessors that don't do object reuse. if (field.value.type.base_type == BASE_TYPE_STRUCT) { // Calls the accessor that takes an accessor object with a new object. @@ -572,19 +589,20 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, code += MakeCamel(field.name, lang.first_camel_upper); code += "(new "; code += type_name + "(), j); }\n"; - } else if (field.value.type.base_type == BASE_TYPE_UNION || - field.value.type.base_type == BASE_TYPE_VECTOR) { + } else if (field.value.type.base_type == BASE_TYPE_VECTOR) { if (lang.language == GeneratorOptions::kCSharp) { method_start = " public " + type_name_dest + " Get" + MakeCamel(field.name, lang.first_camel_upper); } + } else if (field.value.type.base_type == BASE_TYPE_UNION) { + if (lang.language == GeneratorOptions::kCSharp) { + // union types in C# use generic Table-derived type for better type safety + method_start = " public " + type_name_dest + " Get" + MakeCamel(field.name, lang.first_camel_upper) + ""; + offset_prefix = " where TTable : Table" + offset_prefix; + type_name = type_name_dest; + } } std::string getter = dest_cast + GenGetter(lang, field.value.type); code += method_start; - // Most field accessors need to retrieve and test the field offset first, - // this is the prefix code for that: - auto offset_prefix = " { int o = __offset(" + - NumToString(field.value.offset) + - "); return o != 0 ? "; std::string default_cast = ""; if (lang.language == GeneratorOptions::kCSharp) default_cast = "(" + type_name_dest + ")"; diff --git a/tests/MyGame/Example/Monster.cs b/tests/MyGame/Example/Monster.cs index b999c5f..a427c66 100644 --- a/tests/MyGame/Example/Monster.cs +++ b/tests/MyGame/Example/Monster.cs @@ -20,7 +20,7 @@ public sealed class Monster : Table { public int InventoryLength { get { int o = __offset(14); return o != 0 ? __vector_len(o) : 0; } } public Color Color { get { int o = __offset(16); return o != 0 ? (Color)bb.GetSbyte(o + bb_pos) : (Color)8; } } public Any TestType { get { int o = __offset(18); return o != 0 ? (Any)bb.Get(o + bb_pos) : (Any)0; } } - public Table GetTest(Table obj) { int o = __offset(20); return o != 0 ? __union(obj, o) : null; } + public TTable GetTest(TTable obj) where TTable : Table { int o = __offset(20); return o != 0 ? __union(obj, o) : null; } public Test GetTest4(int j) { return GetTest4(new Test(), j); } public Test GetTest4(Test obj, int j) { int o = __offset(22); return o != 0 ? obj.__init(__vector(o) + j * 4, bb) : null; } public int Test4Length { get { int o = __offset(22); return o != 0 ? __vector_len(o) : 0; } } -- 2.7.4