From e083e466b85c1ae512193cee74e0dbd60be5ab87 Mon Sep 17 00:00:00 2001 From: Michael Collins Date: Sun, 4 Oct 2015 19:57:39 -0700 Subject: Add Get Bytes Method Generator for C# I updated idl_gen_general.cpp to add support for generating a Get Bytes method for a vector to the generated C# source code. Given a byte vector field named Foo, a method named GetFooBytes() will be generated in the C# source code that will return an ArraySegment value referencing the vector data in the underlying ByteBuffer. I added a method to Table.cs named __vector_as_arraysegment that is used by the code generated by the change to the C# generator. __vector_as_arraysegment will take the offset of the vector and will return the ArraySegment value corresponding to the bytes that store the vector data. I updated FlatBuffersExampleTests.cs to add tests to validate my implementation of Table.__vector_as_arraysegment. I added tests to demonstrate that the bytes for the monster's name can be extracted from the underlying byte array. I also added tests to show that Table.__vector_as_arraysegment returns a null value if the vector is not present in the FlatBuffer. I used the updated flatc.exe program to regenerate the C# source files for the MyGame example. The new Monster class includes the GetXXXBytes methods to return the byte arrays containing data for vectors. --- src/idl_gen_general.cpp | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index 88aa4dae..15dd4820 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -145,7 +145,7 @@ LanguageParameters language_parameters[] = { "", "Position", "Offset", - "using FlatBuffers;\n\n", + "using System;\nusing FlatBuffers;\n\n", { nullptr, "///", @@ -823,17 +823,29 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, code += "}\n"; } // Generate a ByteBuffer accessor for strings & vectors of scalars. - if (((field.value.type.base_type == BASE_TYPE_VECTOR && - IsScalar(field.value.type.VectorType().base_type)) || - field.value.type.base_type == BASE_TYPE_STRING) && - lang.language == IDLOptions::kJava) { - code += " public ByteBuffer "; - code += MakeCamel(field.name, lang.first_camel_upper); - code += "AsByteBuffer() { return __vector_as_bytebuffer("; - code += NumToString(field.value.offset) + ", "; - code += NumToString(field.value.type.base_type == BASE_TYPE_STRING ? 1 : - InlineSize(field.value.type.VectorType())); - code += "); }\n"; + if ((field.value.type.base_type == BASE_TYPE_VECTOR && + IsScalar(field.value.type.VectorType().base_type)) || + field.value.type.base_type == BASE_TYPE_STRING) { + switch (lang.language) { + case IDLOptions::kJava: + code += " public ByteBuffer "; + code += MakeCamel(field.name, lang.first_camel_upper); + code += "AsByteBuffer() { return __vector_as_bytebuffer("; + code += NumToString(field.value.offset) + ", "; + code += NumToString(field.value.type.base_type == BASE_TYPE_STRING ? 1 : + InlineSize(field.value.type.VectorType())); + code += "); }\n"; + break; + case IDLOptions::kCSharp: + code += " public ArraySegment? Get"; + code += MakeCamel(field.name, lang.first_camel_upper); + code += "Bytes() { return __vector_as_arraysegment("; + code += NumToString(field.value.offset); + code += "); }\n"; + break; + default: + break; + } } // generate mutators for scalar fields or vectors of scalars -- cgit v1.2.3