mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-03 20:34:13 +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));
|
||||
|
||||
Reference in New Issue
Block a user