diff options
author | evolutional <oli@evolutional.co.uk> | 2014-09-09 11:46:13 -0700 |
---|---|---|
committer | Wouter van Oortmerssen <wvo@google.com> | 2014-09-15 16:13:27 -0700 |
commit | 9a1f7be6fd318ddd9545926b5925cf0a10a083e4 (patch) | |
tree | 620496cdd74515cb55491d3dfe405b77947c6782 /net | |
parent | 3f85183c88852c9332089b6f1c192f51986fb7bd (diff) | |
download | flatbuffers-9a1f7be6fd318ddd9545926b5925cf0a10a083e4.tar.gz flatbuffers-9a1f7be6fd318ddd9545926b5925cf0a10a083e4.tar.bz2 flatbuffers-9a1f7be6fd318ddd9545926b5925cf0a10a083e4.zip |
Initial commit of .NET port of FlatBuffers
Include C# codegen in flatc and .NET FlatBuffer access via the
FlatBufferBuilder class
Tested: on Windows.
Change-Id: If5228a8df60a10e0751b245c6c64530264ea2d8a
Diffstat (limited to 'net')
-rw-r--r-- | net/FlatBuffers/ByteBuffer.cs | 142 | ||||
-rw-r--r-- | net/FlatBuffers/FlatBufferBuilder.cs | 351 | ||||
-rw-r--r-- | net/FlatBuffers/FlatBufferConstants.cs | 12 | ||||
-rw-r--r-- | net/FlatBuffers/FlatBuffers.1.0.0.nuspec | 12 | ||||
-rw-r--r-- | net/FlatBuffers/FlatBuffers.csproj | 55 | ||||
-rw-r--r-- | net/FlatBuffers/Properties/AssemblyInfo.cs | 36 | ||||
-rw-r--r-- | net/FlatBuffers/Struct.cs | 27 | ||||
-rw-r--r-- | net/FlatBuffers/Table.cs | 92 |
8 files changed, 727 insertions, 0 deletions
diff --git a/net/FlatBuffers/ByteBuffer.cs b/net/FlatBuffers/ByteBuffer.cs new file mode 100644 index 00000000..a1c29ab4 --- /dev/null +++ b/net/FlatBuffers/ByteBuffer.cs @@ -0,0 +1,142 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Linq; + +namespace FlatBuffers +{ + /// <summary> + /// Class to mimick Java's ByteBuffer which is used heavily in Flatbuffers + /// </summary> + public class ByteBuffer + { + private readonly byte[] _buffer; + + public int Length { get { return _buffer.Length; } } + + public byte[] Data { get { return _buffer; } } + + public ByteBuffer(byte[] buffer) + { + _buffer = buffer; + } + + protected void WriteLittleEndian(int offset, byte[] data) + { + if (!BitConverter.IsLittleEndian) + { + data = data.Reverse().ToArray(); + } + Buffer.BlockCopy(data, 0, _buffer, offset, data.Length); + } + + protected byte[] ReadLittleEndian(int offset, int count) + { + AssertOffsetAndLength(offset, count); + var tmp = new byte[count]; + Buffer.BlockCopy(_buffer, offset, tmp, 0, count); + return (BitConverter.IsLittleEndian) + ? tmp + : tmp.Reverse().ToArray(); + } + + private void AssertOffsetAndLength(int offset, int length) + { + if (offset < 0 || + offset >= _buffer.Length || + offset + length > _buffer.Length) + throw new ArgumentOutOfRangeException(); + } + + public void PutByte(int offset, byte value) + { + AssertOffsetAndLength(offset, sizeof(byte)); + _buffer[offset] = value; + } + + public void PutShort(int offset, short value) + { + AssertOffsetAndLength(offset, sizeof(short)); + WriteLittleEndian(offset, BitConverter.GetBytes(value)); + } + + public void PutInt(int offset, int value) + { + AssertOffsetAndLength(offset, sizeof(int)); + WriteLittleEndian(offset, BitConverter.GetBytes(value)); + } + + public void PutLong(int offset, long value) + { + AssertOffsetAndLength(offset, sizeof(long)); + WriteLittleEndian(offset, BitConverter.GetBytes(value)); + } + + public void PutFloat(int offset, float value) + { + AssertOffsetAndLength(offset, sizeof(float)); + WriteLittleEndian(offset, BitConverter.GetBytes(value)); + } + + public void PutDouble(int offset, double value) + { + AssertOffsetAndLength(offset, sizeof(double)); + WriteLittleEndian(offset, BitConverter.GetBytes(value)); + } + + public byte Get(int index) + { + AssertOffsetAndLength(index, sizeof(byte)); + return _buffer[index]; + } + + public short GetShort(int index) + { + var tmp = ReadLittleEndian(index, sizeof(short)); + var value = BitConverter.ToInt16(tmp, 0); + return value; + } + + public int GetInt(int index) + { + var tmp = ReadLittleEndian(index, sizeof(int)); + var value = BitConverter.ToInt32(tmp, 0); + return value; + } + + public long GetLong(int index) + { + var tmp = ReadLittleEndian(index, sizeof(long)); + var value = BitConverter.ToInt64(tmp, 0); + return value; + } + + public float GetFloat(int index) + { + var tmp = ReadLittleEndian(index, sizeof(float)); + var value = BitConverter.ToSingle(tmp, 0); + return value; + } + + public double GetDouble(int index) + { + var tmp = ReadLittleEndian(index, sizeof(double)); + var value = BitConverter.ToDouble(tmp, 0); + return value; + } + } +} diff --git a/net/FlatBuffers/FlatBufferBuilder.cs b/net/FlatBuffers/FlatBufferBuilder.cs new file mode 100644 index 00000000..f48e7a47 --- /dev/null +++ b/net/FlatBuffers/FlatBufferBuilder.cs @@ -0,0 +1,351 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Text; + +namespace FlatBuffers +{ + /// <summary> + /// Responsible for building up and accessing a flatbuffer formatted byte + /// array (via ByteBuffer) + /// </summary> + public class FlatBufferBuilder + { + private int _space; + private ByteBuffer _bb; + private int _minAlign = 1; + + // The vtable for the current table, null otherwise. + private int[] _vtable; + // Starting offset of the current struct/table. + private int _objectStart; + // List of offsets of all vtables. + private int[] _vtables = new int[16]; + // Number of entries in `vtables` in use. + private int _numVtables = 0; + // For the current vector being built. + private int _vectorNumElems = 0; + + public FlatBufferBuilder(int initialSize) + { + if (initialSize <= 0) + throw new ArgumentOutOfRangeException("initialSize", + initialSize, "Must be greater than zero"); + _space = initialSize; + _bb = new ByteBuffer(new byte[initialSize]); + } + + + public int Offset { get { return _bb.Length - _space; } } + + public void Pad(int size) + { + for (var i = 0; i < size; i++) + { + _bb.PutByte(--_space, 0); + } + } + + // Doubles the size of the ByteBuffer, and copies the old data towards + // the end of the new buffer (since we build the buffer backwards). + void GrowBuffer() + { + var oldBuf = _bb.Data; + var oldBufSize = oldBuf.Length; + if ((oldBufSize & 0xC0000000) != 0) + throw new Exception( + "FlatBuffers: cannot grow buffer beyond 2 gigabytes."); + + var newBufSize = oldBufSize << 1; + var newBuf = new byte[newBufSize]; + + Buffer.BlockCopy(oldBuf, 0, newBuf, newBufSize - oldBufSize, + oldBufSize); + + _bb = new ByteBuffer(newBuf); + } + + // Prepare to write an element of `size` after `additional_bytes` + // have been written, e.g. if you write a string, you need to align + // such the int length field is aligned to SIZEOF_INT, and the string + // data follows it directly. + // If all you need to do is align, `additional_bytes` will be 0. + public void Prep(int size, int additionalBytes) + { + // Track the biggest thing we've ever aligned to. + if (size > _minAlign) + _minAlign = size; + // Find the amount of alignment needed such that `size` is properly + // aligned after `additional_bytes` + var alignSize = + ((~((int)_bb.Length - _space + additionalBytes)) + 1) & + (size - 1); + // Reallocate the buffer if needed. + while (_space < alignSize + size + additionalBytes) + { + var oldBufSize = (int)_bb.Length; + GrowBuffer(); + _space += (int)_bb.Length - oldBufSize; + + } + Pad(alignSize); + } + + public void PutByte(byte x) + { + _bb.PutByte(_space -= sizeof(byte), x); + } + + public void PutShort(short x) + { + _bb.PutShort(_space -= sizeof(short), x); + } + + public void PutInt32(int x) + { + _bb.PutInt(_space -= sizeof(int), x); + } + + public void PutInt64(long x) + { + _bb.PutLong(_space -= sizeof(long), x); + } + + public void PutFloat(float x) + { + _bb.PutFloat(_space -= sizeof(float), x); + } + + public void PutDouble(double x) + { + _bb.PutDouble(_space -= sizeof(double), x); + } + + // Adds a scalar to the buffer, properly aligned, and the buffer grown + // if needed. + public void AddByte(byte x) { Prep(sizeof(byte), 0); PutByte(x); } + public void AddShort(short x) { Prep(sizeof(short), 0); PutShort(x); } + public void AddInt(int x) { Prep(sizeof(int), 0); PutInt32(x); } + public void AddLong(long x) { Prep(sizeof(long), 0); PutInt64(x); } + public void AddFloat(float x) { Prep(sizeof(float), 0); PutFloat(x); } + public void AddDouble(double x) { Prep(sizeof(double), 0); + PutDouble(x); } + + + + // Adds on offset, relative to where it will be written. + public void AddOffset(int off) + { + Prep(sizeof(int), 0); // Ensure alignment is already done. + if (off > Offset) + throw new ArgumentException(); + + off = Offset - off + sizeof(int); + PutInt32(off); + } + + public void StartVector(int elemSize, int count, int alignment) + { + NotNested(); + _vectorNumElems = count; + Prep(sizeof(int), elemSize * count); + Prep(alignment, elemSize * count); // Just in case alignment > int. + } + + public int EndVector() + { + PutInt32(_vectorNumElems); + return Offset; + } + + public void Nested(int obj) + { + // Structs are always stored inline, so need to be created right + // where they are used. You'll get this assert if you created it + // elsewhere. + if (obj != Offset) + throw new Exception( + "FlatBuffers: struct must be serialized inline."); + } + + public void NotNested() + { + // You should not be creating any other objects or strings/vectors + // while an object is being constructed + if (_vtable != null) + throw new Exception( + "FlatBuffers: object serialization must not be nested."); + } + + public void StartObject(int numfields) + { + NotNested(); + _vtable = new int[numfields]; + _objectStart = Offset; + } + + + // Set the current vtable at `voffset` to the current location in the + // buffer. + public void Slot(int voffset) + { + _vtable[voffset] = Offset; + } + + // Add a scalar to a table at `o` into its vtable, with value `x` and default `d` + public void AddByte(int o, byte x, int d) { if (x != d) { AddByte(x); Slot(o); } } + public void AddShort(int o, short x, int d) { if (x != d) { AddShort(x); Slot(o); } } + public void AddInt(int o, int x, int d) { if (x != d) { AddInt(x); Slot(o); } } + public void AddLong(int o, long x, long d) { if (x != d) { AddLong(x); Slot(o); } } + public void AddFloat(int o, float x, double d) { if (x != d) { AddFloat(x); Slot(o); } } + public void AddDouble(int o, double x, double d) { if (x != d) { AddDouble(x); Slot(o); } } + public void AddOffset(int o, int x, int d) { if (x != d) { AddOffset(x); Slot(o); } } + + public int CreateString(string s) + { + NotNested(); + byte[] utf8 = Encoding.UTF8.GetBytes(s); + AddByte((byte)0); + StartVector(1, utf8.Length, 1); + Buffer.BlockCopy(utf8, 0, _bb.Data, _space -= utf8.Length, + utf8.Length); + return EndVector(); + } + + // Structs are stored inline, so nothing additional is being added. + // `d` is always 0. + public void AddStruct(int voffset, int x, int d) + { + if (x != d) + { + Nested(x); + Slot(voffset); + } + } + + public int EndObject() + { + + if (_vtable == null) + throw new InvalidOperationException( + "Flatbuffers: calling endObject without a startObject"); + + AddInt((int)0); + var vtableloc = Offset; + // Write out the current vtable. + for (int i = _vtable.Length - 1; i >= 0 ; i--) { + // Offset relative to the start of the table. + short off = (short)(_vtable[i] != 0 + ? vtableloc - _vtable[i] + : 0); + AddShort(off); + } + + const int standardFields = 2; // The fields below: + AddShort((short)(vtableloc - _objectStart)); + AddShort((short)((_vtable.Length + standardFields) * + sizeof(short))); + + // Search for an existing vtable that matches the current one. + int existingVtable = 0; + + for (int i = 0; i < _numVtables; i++) { + int vt1 = _bb.Length - _vtables[i]; + int vt2 = _space; + short len = _bb.GetShort(vt1); + if (len == _bb.GetShort(vt2)) { + for (int j = sizeof(short); j < len; j += sizeof(short)) { + if (_bb.GetShort(vt1 + j) != _bb.GetShort(vt2 + j)) { + goto endLoop; + } + } + existingVtable = _vtables[i]; + break; + } + + endLoop: { } + } + + if (existingVtable != 0) { + // Found a match: + // Remove the current vtable. + _space = _bb.Length - vtableloc; + // Point table to existing vtable. + _bb.PutInt(_space, existingVtable - vtableloc); + } else { + // No match: + // Add the location of the current vtable to the list of + // vtables. + if (_numVtables == _vtables.Length) + { + // Arrays.CopyOf(vtables num_vtables * 2); + var newvtables = new int[ _numVtables * 2]; + Array.Copy(_vtables, newvtables, _vtables.Length); + + _vtables = newvtables; + }; + _vtables[_numVtables++] = Offset; + // Point table to current vtable. + _bb.PutInt(_bb.Length - vtableloc, Offset - vtableloc); + } + + _vtable = null; + return vtableloc; + } + + public void Finish(int rootTable) + { + Prep(_minAlign, sizeof(int)); + AddOffset(rootTable); + } + + public ByteBuffer Data { get { return _bb; }} + + // The FlatBuffer data doesn't start at offset 0 in the ByteBuffer: + public int DataStart { get { return _space; } } + + + // Utility function for copying a byte array that starts at 0. + public byte[] SizedByteArray() + { + var newArray = new byte[_bb.Data.Length]; + Buffer.BlockCopy(_bb.Data, DataStart, newArray, 0, + _bb.Data.Length); + return newArray; + } + + public void Finish(int rootTable, string fileIdentifier) + { + Prep(_minAlign, sizeof(int) + + FlatBufferConstants.FileIdentifierLength); + if (fileIdentifier.Length != + FlatBufferConstants.FileIdentifierLength) + throw new ArgumentException( + "FlatBuffers: file identifier must be length " + + FlatBufferConstants.FileIdentifierLength, + "fileIdentifier"); + for (int i = FlatBufferConstants.FileIdentifierLength - 1; i >= 0; + i--) + { + AddByte((byte)fileIdentifier[i]); + } + AddOffset(rootTable); + } + + + } +} diff --git a/net/FlatBuffers/FlatBufferConstants.cs b/net/FlatBuffers/FlatBufferConstants.cs new file mode 100644 index 00000000..5c3706e1 --- /dev/null +++ b/net/FlatBuffers/FlatBufferConstants.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace FlatBuffers +{ + public static class FlatBufferConstants + { + public const int FileIdentifierLength = 4; + } +} diff --git a/net/FlatBuffers/FlatBuffers.1.0.0.nuspec b/net/FlatBuffers/FlatBuffers.1.0.0.nuspec new file mode 100644 index 00000000..93ddf938 --- /dev/null +++ b/net/FlatBuffers/FlatBuffers.1.0.0.nuspec @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="utf-8"?> +<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> + <metadata> + <id>FlatBuffers</id> + <version>1.0.0-alpha00003</version> + <authors>Google Inc</authors> + <description>A .NET port of Google Inc's FlatBuffers project.</description> + <language>en-US</language> + <projectUrl>https://github.com/evolutional/flatbuffers</projectUrl> + <licenseUrl>http://www.apache.org/licenses/LICENSE-2.0</licenseUrl> + </metadata> +</package>
\ No newline at end of file diff --git a/net/FlatBuffers/FlatBuffers.csproj b/net/FlatBuffers/FlatBuffers.csproj new file mode 100644 index 00000000..a973e698 --- /dev/null +++ b/net/FlatBuffers/FlatBuffers.csproj @@ -0,0 +1,55 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{28C00774-1E73-4A75-AD8F-844CD21A064D}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>FlatBuffers</RootNamespace> + <AssemblyName>FlatBuffers</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + <Reference Include="System.Core" /> + </ItemGroup> + <ItemGroup> + <Compile Include="ByteBuffer.cs" /> + <Compile Include="FlatBufferBuilder.cs" /> + <Compile Include="FlatBufferConstants.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="Struct.cs" /> + <Compile Include="Table.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="FlatBuffers.1.0.0.nuspec" /> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project>
\ No newline at end of file diff --git a/net/FlatBuffers/Properties/AssemblyInfo.cs b/net/FlatBuffers/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..3d4ea15c --- /dev/null +++ b/net/FlatBuffers/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("FlatBuffers")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("FlatBuffers")] +[assembly: AssemblyCopyright("Copyright © 2014 Google Inc")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("91c32e64-ef20-47df-9c9f-cec9207bc6df")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/net/FlatBuffers/Struct.cs b/net/FlatBuffers/Struct.cs new file mode 100644 index 00000000..4cd28012 --- /dev/null +++ b/net/FlatBuffers/Struct.cs @@ -0,0 +1,27 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace FlatBuffers +{ + /// <summary> + /// All structs in the generated code derive from this class, and add their own accessors. + /// </summary> + public abstract class Struct + { + protected int bb_pos; + protected ByteBuffer bb; + } +}
\ No newline at end of file diff --git a/net/FlatBuffers/Table.cs b/net/FlatBuffers/Table.cs new file mode 100644 index 00000000..f09cb05e --- /dev/null +++ b/net/FlatBuffers/Table.cs @@ -0,0 +1,92 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Text; + +namespace FlatBuffers +{ + /// <summary> + /// All tables in the generated code derive from this class, and add their own accessors. + /// </summary> + public abstract class Table + { + protected int bb_pos; + protected ByteBuffer bb; + + // Look up a field in the vtable, return an offset into the object, or 0 if the field is not + // present. + protected int __offset(int vtableOffset) + { + int vtable = bb_pos - bb.GetInt(bb_pos); + return vtableOffset < bb.GetShort(vtable) ? bb.GetShort(vtable + vtableOffset) : 0; + } + + // Retrieve the relative offset stored at "offset" + protected int __indirect(int offset) + { + return offset + bb.GetInt(offset); + } + + // Create a .NET String from UTF-8 data stored inside the flatbuffer. + protected string __string(int offset) + { + offset += bb.GetInt(offset); + var len = bb.GetInt(offset); + var startPos = offset + sizeof(int); + return Encoding.UTF8.GetString(bb.Data, startPos , len); + } + + // Get the length of a vector whose offset is stored at "offset" in this object. + protected int __vector_len(int offset) + { + offset += bb_pos; + offset += bb.GetInt(offset); + return bb.GetInt(offset); + } + + // Get the start of data of a vector whose offset is stored at "offset" in this object. + protected int __vector(int offset) + { + offset += bb_pos; + return offset + bb.GetInt(offset) + sizeof(int); // data starts after the length + } + + // Initialize any Table-derived type to point to the union at the given offset. + protected Table __union(Table t, int offset) + { + offset += bb_pos; + t.bb_pos = offset + bb.GetInt(offset); + t.bb = bb; + return t; + } + + protected static bool __has_identifier(ByteBuffer bb, int offset, string ident) + { + if (ident.Length != FlatBufferConstants.FileIdentifierLength) + throw new ArgumentException("FlatBuffers: file identifier must be length " + FlatBufferConstants.FileIdentifierLength, "ident"); + + for (var i = 0; i < FlatBufferConstants.FileIdentifierLength; i++) + { + if (ident[i] != (char)bb.Get(offset + sizeof(int) + i)) return false; + } + + return true; + } + + + } +} |