mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-30 18:10:02 +00:00
Refactored the Java and C# code generators into one.
Also made the C# implementation support unsigned types, and made it more like the Java version. Bug: 17359988 Change-Id: If5305c08cd5c97f35426639516ce05e53bbec36c Tested: on Linux and Windows.
This commit is contained in:
@@ -25,6 +25,7 @@ namespace FlatBuffers
|
||||
public class ByteBuffer
|
||||
{
|
||||
private readonly byte[] _buffer;
|
||||
private int _pos; // Must track start of the buffer.
|
||||
|
||||
public int Length { get { return _buffer.Length; } }
|
||||
|
||||
@@ -33,8 +34,11 @@ namespace FlatBuffers
|
||||
public ByteBuffer(byte[] buffer)
|
||||
{
|
||||
_buffer = buffer;
|
||||
_pos = 0;
|
||||
}
|
||||
|
||||
public int position() { return _pos; }
|
||||
|
||||
protected void WriteLittleEndian(int offset, byte[] data)
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
@@ -42,6 +46,7 @@ namespace FlatBuffers
|
||||
data = data.Reverse().ToArray();
|
||||
}
|
||||
Buffer.BlockCopy(data, 0, _buffer, offset, data.Length);
|
||||
_pos = offset;
|
||||
}
|
||||
|
||||
protected byte[] ReadLittleEndian(int offset, int count)
|
||||
@@ -62,10 +67,18 @@ namespace FlatBuffers
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
public void PutSbyte(int offset, sbyte value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(sbyte));
|
||||
_buffer[offset] = (byte)value;
|
||||
_pos = offset;
|
||||
}
|
||||
|
||||
public void PutByte(int offset, byte value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(byte));
|
||||
_buffer[offset] = value;
|
||||
_pos = offset;
|
||||
}
|
||||
|
||||
public void PutShort(int offset, short value)
|
||||
@@ -74,18 +87,36 @@ namespace FlatBuffers
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public void PutUshort(int offset, ushort value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(ushort));
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public void PutInt(int offset, int value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(int));
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public void PutUint(int offset, uint value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(uint));
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public void PutLong(int offset, long value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(long));
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public void PutUlong(int offset, ulong value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(ulong));
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public void PutFloat(int offset, float value)
|
||||
{
|
||||
AssertOffsetAndLength(offset, sizeof(float));
|
||||
@@ -98,6 +129,12 @@ namespace FlatBuffers
|
||||
WriteLittleEndian(offset, BitConverter.GetBytes(value));
|
||||
}
|
||||
|
||||
public sbyte GetSbyte(int index)
|
||||
{
|
||||
AssertOffsetAndLength(index, sizeof(sbyte));
|
||||
return (sbyte)_buffer[index];
|
||||
}
|
||||
|
||||
public byte Get(int index)
|
||||
{
|
||||
AssertOffsetAndLength(index, sizeof(byte));
|
||||
@@ -111,6 +148,13 @@ namespace FlatBuffers
|
||||
return value;
|
||||
}
|
||||
|
||||
public ushort GetUshort(int index)
|
||||
{
|
||||
var tmp = ReadLittleEndian(index, sizeof(ushort));
|
||||
var value = BitConverter.ToUInt16(tmp, 0);
|
||||
return value;
|
||||
}
|
||||
|
||||
public int GetInt(int index)
|
||||
{
|
||||
var tmp = ReadLittleEndian(index, sizeof(int));
|
||||
@@ -118,6 +162,13 @@ namespace FlatBuffers
|
||||
return value;
|
||||
}
|
||||
|
||||
public uint GetUint(int index)
|
||||
{
|
||||
var tmp = ReadLittleEndian(index, sizeof(uint));
|
||||
var value = BitConverter.ToUInt32(tmp, 0);
|
||||
return value;
|
||||
}
|
||||
|
||||
public long GetLong(int index)
|
||||
{
|
||||
var tmp = ReadLittleEndian(index, sizeof(long));
|
||||
@@ -125,6 +176,13 @@ namespace FlatBuffers
|
||||
return value;
|
||||
}
|
||||
|
||||
public ulong GetUlong(int index)
|
||||
{
|
||||
var tmp = ReadLittleEndian(index, sizeof(ulong));
|
||||
var value = BitConverter.ToUInt64(tmp, 0);
|
||||
return value;
|
||||
}
|
||||
|
||||
public float GetFloat(int index)
|
||||
{
|
||||
var tmp = ReadLittleEndian(index, sizeof(float));
|
||||
|
||||
@@ -50,7 +50,7 @@ namespace FlatBuffers
|
||||
}
|
||||
|
||||
|
||||
public int Offset { get { return _bb.Length - _space; } }
|
||||
public int Offset() { return _bb.Length - _space; }
|
||||
|
||||
public void Pad(int size)
|
||||
{
|
||||
@@ -105,6 +105,11 @@ namespace FlatBuffers
|
||||
Pad(alignSize);
|
||||
}
|
||||
|
||||
public void PutSbyte(sbyte x)
|
||||
{
|
||||
_bb.PutSbyte(_space -= sizeof(sbyte), x);
|
||||
}
|
||||
|
||||
public void PutByte(byte x)
|
||||
{
|
||||
_bb.PutByte(_space -= sizeof(byte), x);
|
||||
@@ -115,16 +120,31 @@ namespace FlatBuffers
|
||||
_bb.PutShort(_space -= sizeof(short), x);
|
||||
}
|
||||
|
||||
public void PutInt32(int x)
|
||||
public void PutUshort(ushort x)
|
||||
{
|
||||
_bb.PutUshort(_space -= sizeof(ushort), x);
|
||||
}
|
||||
|
||||
public void PutInt(int x)
|
||||
{
|
||||
_bb.PutInt(_space -= sizeof(int), x);
|
||||
}
|
||||
|
||||
public void PutInt64(long x)
|
||||
public void PutUint(uint x)
|
||||
{
|
||||
_bb.PutUint(_space -= sizeof(uint), x);
|
||||
}
|
||||
|
||||
public void PutLong(long x)
|
||||
{
|
||||
_bb.PutLong(_space -= sizeof(long), x);
|
||||
}
|
||||
|
||||
public void PutUlong(ulong x)
|
||||
{
|
||||
_bb.PutUlong(_space -= sizeof(ulong), x);
|
||||
}
|
||||
|
||||
public void PutFloat(float x)
|
||||
{
|
||||
_bb.PutFloat(_space -= sizeof(float), x);
|
||||
@@ -137,10 +157,14 @@ namespace FlatBuffers
|
||||
|
||||
// Adds a scalar to the buffer, properly aligned, and the buffer grown
|
||||
// if needed.
|
||||
public void AddSbyte(sbyte x) { Prep(sizeof(sbyte), 0); PutSbyte(x); }
|
||||
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 AddUshort(ushort x) { Prep(sizeof(ushort), 0); PutUshort(x); }
|
||||
public void AddInt(int x) { Prep(sizeof(int), 0); PutInt(x); }
|
||||
public void AddUint(uint x) { Prep(sizeof(uint), 0); PutUint(x); }
|
||||
public void AddLong(long x) { Prep(sizeof(long), 0); PutLong(x); }
|
||||
public void AddULong(ulong x) { Prep(sizeof(ulong), 0); PutUlong(x); }
|
||||
public void AddFloat(float x) { Prep(sizeof(float), 0); PutFloat(x); }
|
||||
public void AddDouble(double x) { Prep(sizeof(double), 0);
|
||||
PutDouble(x); }
|
||||
@@ -151,11 +175,11 @@ namespace FlatBuffers
|
||||
public void AddOffset(int off)
|
||||
{
|
||||
Prep(sizeof(int), 0); // Ensure alignment is already done.
|
||||
if (off > Offset)
|
||||
if (off > Offset())
|
||||
throw new ArgumentException();
|
||||
|
||||
off = Offset - off + sizeof(int);
|
||||
PutInt32(off);
|
||||
off = Offset() - off + sizeof(int);
|
||||
PutInt(off);
|
||||
}
|
||||
|
||||
public void StartVector(int elemSize, int count, int alignment)
|
||||
@@ -168,8 +192,8 @@ namespace FlatBuffers
|
||||
|
||||
public int EndVector()
|
||||
{
|
||||
PutInt32(_vectorNumElems);
|
||||
return Offset;
|
||||
PutInt(_vectorNumElems);
|
||||
return Offset();
|
||||
}
|
||||
|
||||
public void Nested(int obj)
|
||||
@@ -177,7 +201,7 @@ namespace FlatBuffers
|
||||
// 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)
|
||||
if (obj != Offset())
|
||||
throw new Exception(
|
||||
"FlatBuffers: struct must be serialized inline.");
|
||||
}
|
||||
@@ -195,7 +219,7 @@ namespace FlatBuffers
|
||||
{
|
||||
NotNested();
|
||||
_vtable = new int[numfields];
|
||||
_objectStart = Offset;
|
||||
_objectStart = Offset();
|
||||
}
|
||||
|
||||
|
||||
@@ -203,14 +227,18 @@ namespace FlatBuffers
|
||||
// buffer.
|
||||
public void Slot(int voffset)
|
||||
{
|
||||
_vtable[voffset] = Offset;
|
||||
_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 AddSbyte(int o, sbyte x, sbyte d) { if (x != d) { AddSbyte(x); Slot(o); } }
|
||||
public void AddByte(int o, byte x, byte 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 AddUshort(int o, ushort x, ushort d) { if (x != d) { AddUshort(x); Slot(o); } }
|
||||
public void AddInt(int o, int x, int d) { if (x != d) { AddInt(x); Slot(o); } }
|
||||
public void AddUint(int o, uint x, uint d) { if (x != d) { AddUint(x); Slot(o); } }
|
||||
public void AddLong(int o, long x, long d) { if (x != d) { AddLong(x); Slot(o); } }
|
||||
public void AddULong(int o, ulong x, ulong d) { if (x != d) { AddULong(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); } }
|
||||
@@ -245,7 +273,7 @@ namespace FlatBuffers
|
||||
"Flatbuffers: calling endObject without a startObject");
|
||||
|
||||
AddInt((int)0);
|
||||
var vtableloc = Offset;
|
||||
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.
|
||||
@@ -298,9 +326,9 @@ namespace FlatBuffers
|
||||
|
||||
_vtables = newvtables;
|
||||
};
|
||||
_vtables[_numVtables++] = Offset;
|
||||
_vtables[_numVtables++] = Offset();
|
||||
// Point table to current vtable.
|
||||
_bb.PutInt(_bb.Length - vtableloc, Offset - vtableloc);
|
||||
_bb.PutInt(_bb.Length - vtableloc, Offset() - vtableloc);
|
||||
}
|
||||
|
||||
_vtable = null;
|
||||
@@ -313,17 +341,13 @@ namespace FlatBuffers
|
||||
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; } }
|
||||
|
||||
public ByteBuffer DataBuffer() { return _bb; }
|
||||
|
||||
// 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,
|
||||
Buffer.BlockCopy(_bb.Data, _bb.position(), newArray, 0,
|
||||
_bb.Data.Length);
|
||||
return newArray;
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
<?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>
|
||||
@@ -74,14 +74,14 @@ namespace FlatBuffers
|
||||
return t;
|
||||
}
|
||||
|
||||
protected static bool __has_identifier(ByteBuffer bb, int offset, string ident)
|
||||
protected static bool __has_identifier(ByteBuffer bb, 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;
|
||||
if (ident[i] != (char)bb.Get(bb.position() + sizeof(int) + i)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user