mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-04 20:48:59 +00:00
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
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
f7d24f60a2
commit
221193eaa2
@@ -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>(TTable t, int offset) where TTable : Table
|
||||
{
|
||||
offset += bb_pos;
|
||||
t.bb_pos = offset + bb.GetInt(offset);
|
||||
|
||||
@@ -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) + "<TTable>";
|
||||
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 + ")";
|
||||
|
||||
@@ -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>(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; } }
|
||||
|
||||
Reference in New Issue
Block a user