diff options
author | stefan301 <32997632+stefan301@users.noreply.github.com> | 2020-03-23 17:39:26 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-23 09:39:26 -0700 |
commit | 3af73593453d70585542aad617f62e4f041b6ada (patch) | |
tree | b627b45f40df5caa2cdf9ca0ae27f33b9ea99d1f /src/idl_gen_csharp.cpp | |
parent | c4231c3cb99159978ca5f0669a07ef1aa02b88e3 (diff) | |
download | flatbuffers-3af73593453d70585542aad617f62e4f041b6ada.tar.gz flatbuffers-3af73593453d70585542aad617f62e4f041b6ada.tar.bz2 flatbuffers-3af73593453d70585542aad617f62e4f041b6ada.zip |
[csharp] flatc should generate a 'Create…' method for tables with struct field… (#5818)
* flatc should generate a 'Create…' method for tables with struct fields when also generating the object based api (C#)
https://stackoverflow.com/questions/60724317/flatc-should-generate-a-create-method-for-tables-with-struct-fields-when-al
* missing namespace fixed: C:\projects\flatbuffers\tests\namespace_test\NamespaceA\TableInFirstNS.cs(30,7): error CS0246: The type or namespace name 'StructInNestedNST' could not be found (are you missing a using directive or an assembly reference?) [C:\projects\flatbuffers\tests\FlatBuffers.Test\FlatBuffers.Test.csproj]
Co-authored-by: stefan301 <Stefan.Felkel@de.Zuken.com>
Diffstat (limited to 'src/idl_gen_csharp.cpp')
-rw-r--r-- | src/idl_gen_csharp.cpp | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/src/idl_gen_csharp.cpp b/src/idl_gen_csharp.cpp index 7db40fab..0bcd132c 100644 --- a/src/idl_gen_csharp.cpp +++ b/src/idl_gen_csharp.cpp @@ -947,7 +947,8 @@ class CSharpGenerator : public BaseGenerator { } // JVM specifications restrict default constructor params to be < 255. // Longs and doubles take up 2 units, so we set the limit to be < 127. - if (has_no_struct_fields && num_fields && num_fields < 127) { + if ((has_no_struct_fields || opts.generate_object_based_api) && + num_fields && num_fields < 127) { struct_has_create = true; // Generate a table constructor of the form: // public static int createName(FlatBufferBuilder builder, args...) @@ -959,13 +960,22 @@ class CSharpGenerator : public BaseGenerator { auto &field = **it; if (field.deprecated) continue; code += ",\n "; - code += GenTypeBasic(field.value.type); - code += " "; - code += field.name; - if (!IsScalar(field.value.type.base_type)) code += "Offset"; + if (IsStruct(field.value.type) && opts.generate_object_based_api) { + code += WrapInNameSpace( + field.value.type.struct_def->defined_namespace, + GenTypeName_ObjectAPI(field.value.type.struct_def->name, opts)); + code += " "; + code += field.name; + code += " = null"; + } else { + code += GenTypeBasic(field.value.type); + code += " "; + code += field.name; + if (!IsScalar(field.value.type.base_type)) code += "Offset"; - code += " = "; - code += GenDefaultValueBasic(field); + code += " = "; + code += GenDefaultValueBasic(field); + } } code += ") {\n builder."; code += "StartTable("; @@ -980,8 +990,16 @@ class CSharpGenerator : public BaseGenerator { size == SizeOf(field.value.type.base_type))) { code += " " + struct_def.name + "."; code += "Add"; - code += MakeCamel(field.name) + "(builder, " + field.name; - if (!IsScalar(field.value.type.base_type)) code += "Offset"; + code += MakeCamel(field.name) + "(builder, "; + if (IsStruct(field.value.type) && + opts.generate_object_based_api) { + code += GenTypePointer(field.value.type) + ".Pack(builder, " + + field.name + ")"; + } else { + code += field.name; + if (!IsScalar(field.value.type.base_type)) code += "Offset"; + } + code += ");\n"; } } @@ -1643,8 +1661,11 @@ class CSharpGenerator : public BaseGenerator { } else { code += ",\n"; if (field.value.type.struct_def->fixed) { - code += " " + GenTypeGet(field.value.type) + - ".Pack(builder, _o." + camel_name + ")"; + if (opts.generate_object_based_api) + code += " _o." + camel_name; + else + code += " " + GenTypeGet(field.value.type) + + ".Pack(builder, _o." + camel_name + ")"; } else { code += " _" + field.name; } |