diff options
author | Michael Collins <michael.collins@neudesic.com> | 2015-10-04 19:57:39 -0700 |
---|---|---|
committer | Michael Collins <michael.collins@neudesic.com> | 2015-12-04 11:44:43 -0700 |
commit | e083e466b85c1ae512193cee74e0dbd60be5ab87 (patch) | |
tree | c2c0af7577419dfe1ce526ff309b974cdf4be8a2 /net/FlatBuffers/Table.cs | |
parent | fe2f8d32aa7ffb23baf5989a208477189cc98f99 (diff) | |
download | flatbuffers-e083e466b85c1ae512193cee74e0dbd60be5ab87.tar.gz flatbuffers-e083e466b85c1ae512193cee74e0dbd60be5ab87.tar.bz2 flatbuffers-e083e466b85c1ae512193cee74e0dbd60be5ab87.zip |
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<byte> 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<byte> 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.
Diffstat (limited to 'net/FlatBuffers/Table.cs')
-rw-r--r-- | net/FlatBuffers/Table.cs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/net/FlatBuffers/Table.cs b/net/FlatBuffers/Table.cs index 09b00285..bd5e3641 100644 --- a/net/FlatBuffers/Table.cs +++ b/net/FlatBuffers/Table.cs @@ -67,6 +67,21 @@ namespace FlatBuffers return offset + bb.GetInt(offset) + sizeof(int); // data starts after the length } + // Get the data of a vector whoses offset is stored at "offset" in this object as an + // ArraySegment<byte>. If the vector is not present in the ByteBuffer, + // then a null value will be returned. + protected ArraySegment<byte>? __vector_as_arraysegment(int offset) { + var o = this.__offset(offset); + if (0 == o) + { + return null; + } + + var pos = this.__vector(o); + var len = this.__vector_len(o); + return new ArraySegment<byte>(this.bb.Data, pos, len); + } + // Initialize any Table-derived type to point to the union at the given offset. protected TTable __union<TTable>(TTable t, int offset) where TTable : Table { |