2015-07-28 21:33:45 +00:00
|
|
|
/*
|
2014-09-09 18:46:13 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-12-04 15:49:03 +00:00
|
|
|
|
2014-09-09 18:46:13 +00:00
|
|
|
using System;
|
|
|
|
using System.Text;
|
|
|
|
|
2015-12-04 04:30:54 +00:00
|
|
|
/// @file
|
|
|
|
/// @addtogroup flatbuffers_csharp_api
|
|
|
|
/// @{
|
2015-12-04 15:49:03 +00:00
|
|
|
|
2014-09-09 18:46:13 +00:00
|
|
|
namespace FlatBuffers
|
|
|
|
{
|
|
|
|
/// <summary>
|
2015-12-04 04:30:54 +00:00
|
|
|
/// Responsible for building up and accessing a FlatBuffer formatted byte
|
|
|
|
/// array (via ByteBuffer).
|
2014-09-09 18:46:13 +00:00
|
|
|
/// </summary>
|
|
|
|
public class FlatBufferBuilder
|
|
|
|
{
|
|
|
|
private int _space;
|
|
|
|
private ByteBuffer _bb;
|
|
|
|
private int _minAlign = 1;
|
|
|
|
|
2015-12-04 15:49:03 +00:00
|
|
|
// The vtable for the current table (if _vtableSize >= 0)
|
|
|
|
private int[] _vtable = new int[16];
|
|
|
|
// The size of the vtable. -1 indicates no vtable
|
|
|
|
private int _vtableSize = -1;
|
2014-09-09 18:46:13 +00:00
|
|
|
// 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;
|
|
|
|
|
2015-12-04 04:30:54 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Create a FlatBufferBuilder with a given initial size.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="initialSize">
|
|
|
|
/// The initial size to use for the internal buffer.
|
|
|
|
/// </param>
|
2014-09-09 18:46:13 +00:00
|
|
|
public FlatBufferBuilder(int initialSize)
|
|
|
|
{
|
|
|
|
if (initialSize <= 0)
|
|
|
|
throw new ArgumentOutOfRangeException("initialSize",
|
|
|
|
initialSize, "Must be greater than zero");
|
|
|
|
_space = initialSize;
|
2018-03-12 18:30:46 +00:00
|
|
|
_bb = new ByteBuffer(initialSize);
|
2014-09-09 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 04:30:54 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Reset the FlatBufferBuilder by purging all data that it holds.
|
|
|
|
/// </summary>
|
2015-07-28 21:33:45 +00:00
|
|
|
public void Clear()
|
|
|
|
{
|
|
|
|
_space = _bb.Length;
|
|
|
|
_bb.Reset();
|
|
|
|
_minAlign = 1;
|
2015-12-04 15:49:03 +00:00
|
|
|
while (_vtableSize > 0) _vtable[--_vtableSize] = 0;
|
|
|
|
_vtableSize = -1;
|
2015-07-28 21:33:45 +00:00
|
|
|
_objectStart = 0;
|
|
|
|
_numVtables = 0;
|
|
|
|
_vectorNumElems = 0;
|
|
|
|
}
|
2014-09-09 18:46:13 +00:00
|
|
|
|
2016-01-20 17:15:29 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets and sets a Boolean to disable the optimization when serializing
|
|
|
|
/// default values to a Table.
|
2016-05-23 19:24:44 +00:00
|
|
|
///
|
2016-01-20 17:15:29 +00:00
|
|
|
/// In order to save space, fields that are set to their default value
|
2016-05-23 19:24:44 +00:00
|
|
|
/// don't get serialized into the buffer.
|
2016-01-20 17:15:29 +00:00
|
|
|
/// </summary>
|
|
|
|
public bool ForceDefaults { get; set; }
|
|
|
|
|
2015-12-04 04:30:54 +00:00
|
|
|
/// @cond FLATBUFFERS_INTERNAL
|
|
|
|
|
2015-05-06 14:33:50 +00:00
|
|
|
public int Offset { get { return _bb.Length - _space; } }
|
2014-09-09 18:46:13 +00:00
|
|
|
|
|
|
|
public void Pad(int size)
|
|
|
|
{
|
2015-12-12 16:39:57 +00:00
|
|
|
_bb.PutByte(_space -= size, 0, size);
|
2014-09-09 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
{
|
2018-03-12 18:30:46 +00:00
|
|
|
_bb.GrowFront(_bb.Length << 1);
|
2014-09-09 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
}
|
2015-12-12 16:39:57 +00:00
|
|
|
if (alignSize > 0)
|
|
|
|
Pad(alignSize);
|
2014-09-09 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-17 01:48:51 +00:00
|
|
|
public void PutBool(bool x)
|
|
|
|
{
|
|
|
|
_bb.PutByte(_space -= sizeof(byte), (byte)(x ? 1 : 0));
|
|
|
|
}
|
|
|
|
|
2014-09-17 00:37:17 +00:00
|
|
|
public void PutSbyte(sbyte x)
|
|
|
|
{
|
|
|
|
_bb.PutSbyte(_space -= sizeof(sbyte), x);
|
|
|
|
}
|
|
|
|
|
2014-09-09 18:46:13 +00:00
|
|
|
public void PutByte(byte x)
|
|
|
|
{
|
|
|
|
_bb.PutByte(_space -= sizeof(byte), x);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void PutShort(short x)
|
|
|
|
{
|
|
|
|
_bb.PutShort(_space -= sizeof(short), x);
|
|
|
|
}
|
|
|
|
|
2014-09-17 00:37:17 +00:00
|
|
|
public void PutUshort(ushort x)
|
|
|
|
{
|
|
|
|
_bb.PutUshort(_space -= sizeof(ushort), x);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void PutInt(int x)
|
2014-09-09 18:46:13 +00:00
|
|
|
{
|
|
|
|
_bb.PutInt(_space -= sizeof(int), x);
|
|
|
|
}
|
|
|
|
|
2014-09-17 00:37:17 +00:00
|
|
|
public void PutUint(uint x)
|
|
|
|
{
|
|
|
|
_bb.PutUint(_space -= sizeof(uint), x);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void PutLong(long x)
|
2014-09-09 18:46:13 +00:00
|
|
|
{
|
|
|
|
_bb.PutLong(_space -= sizeof(long), x);
|
|
|
|
}
|
|
|
|
|
2014-09-17 00:37:17 +00:00
|
|
|
public void PutUlong(ulong x)
|
|
|
|
{
|
|
|
|
_bb.PutUlong(_space -= sizeof(ulong), x);
|
|
|
|
}
|
|
|
|
|
2014-09-09 18:46:13 +00:00
|
|
|
public void PutFloat(float x)
|
|
|
|
{
|
|
|
|
_bb.PutFloat(_space -= sizeof(float), x);
|
|
|
|
}
|
|
|
|
|
2018-08-20 23:31:44 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Puts an array of type T into this builder at the
|
|
|
|
/// current offset
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The type of the input data </typeparam>
|
|
|
|
/// <param name="x">The array to copy data from</param>
|
|
|
|
public void Put<T>(T[] x)
|
|
|
|
where T : struct
|
|
|
|
{
|
|
|
|
_space = _bb.Put(_space, x);
|
|
|
|
}
|
|
|
|
|
2014-09-09 18:46:13 +00:00
|
|
|
public void PutDouble(double x)
|
|
|
|
{
|
|
|
|
_bb.PutDouble(_space -= sizeof(double), x);
|
|
|
|
}
|
2015-12-04 04:30:54 +00:00
|
|
|
/// @endcond
|
2014-09-09 18:46:13 +00:00
|
|
|
|
2015-12-04 04:30:54 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Add a `bool` to the buffer (aligns the data and grows if necessary).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The `bool` to add to the buffer.</param>
|
2015-01-17 01:48:51 +00:00
|
|
|
public void AddBool(bool x) { Prep(sizeof(byte), 0); PutBool(x); }
|
2015-12-04 04:30:54 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Add a `sbyte` to the buffer (aligns the data and grows if necessary).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The `sbyte` to add to the buffer.</param>
|
2014-09-17 00:37:17 +00:00
|
|
|
public void AddSbyte(sbyte x) { Prep(sizeof(sbyte), 0); PutSbyte(x); }
|
2015-12-04 04:30:54 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Add a `byte` to the buffer (aligns the data and grows if necessary).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The `byte` to add to the buffer.</param>
|
2014-09-09 18:46:13 +00:00
|
|
|
public void AddByte(byte x) { Prep(sizeof(byte), 0); PutByte(x); }
|
2015-12-04 04:30:54 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Add a `short` to the buffer (aligns the data and grows if necessary).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The `short` to add to the buffer.</param>
|
2014-09-09 18:46:13 +00:00
|
|
|
public void AddShort(short x) { Prep(sizeof(short), 0); PutShort(x); }
|
2015-12-04 04:30:54 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Add an `ushort` to the buffer (aligns the data and grows if necessary).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The `ushort` to add to the buffer.</param>
|
2014-09-17 00:37:17 +00:00
|
|
|
public void AddUshort(ushort x) { Prep(sizeof(ushort), 0); PutUshort(x); }
|
2015-12-04 04:30:54 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Add an `int` to the buffer (aligns the data and grows if necessary).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The `int` to add to the buffer.</param>
|
2014-09-17 00:37:17 +00:00
|
|
|
public void AddInt(int x) { Prep(sizeof(int), 0); PutInt(x); }
|
2015-12-04 04:30:54 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Add an `uint` to the buffer (aligns the data and grows if necessary).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The `uint` to add to the buffer.</param>
|
2014-09-17 00:37:17 +00:00
|
|
|
public void AddUint(uint x) { Prep(sizeof(uint), 0); PutUint(x); }
|
2015-12-04 04:30:54 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Add a `long` to the buffer (aligns the data and grows if necessary).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The `long` to add to the buffer.</param>
|
2014-09-17 00:37:17 +00:00
|
|
|
public void AddLong(long x) { Prep(sizeof(long), 0); PutLong(x); }
|
2015-12-04 04:30:54 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Add an `ulong` to the buffer (aligns the data and grows if necessary).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The `ulong` to add to the buffer.</param>
|
2015-03-13 20:05:28 +00:00
|
|
|
public void AddUlong(ulong x) { Prep(sizeof(ulong), 0); PutUlong(x); }
|
2015-12-04 04:30:54 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Add a `float` to the buffer (aligns the data and grows if necessary).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The `float` to add to the buffer.</param>
|
2014-09-09 18:46:13 +00:00
|
|
|
public void AddFloat(float x) { Prep(sizeof(float), 0); PutFloat(x); }
|
2015-12-04 04:30:54 +00:00
|
|
|
|
2018-08-20 23:31:44 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Add an array of type T to the buffer (aligns the data and grows if necessary).
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The type of the input data</typeparam>
|
|
|
|
/// <param name="x">The array to copy data from</param>
|
|
|
|
public void Add<T>(T[] x)
|
|
|
|
where T : struct
|
|
|
|
{
|
|
|
|
if (x == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("Cannot add a null array");
|
|
|
|
}
|
|
|
|
|
|
|
|
if( x.Length == 0)
|
|
|
|
{
|
|
|
|
// don't do anything if the array is empty
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!ByteBuffer.IsSupportedType<T>())
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Cannot add this Type array to the builder");
|
|
|
|
}
|
|
|
|
|
|
|
|
int size = ByteBuffer.SizeOf<T>();
|
|
|
|
// Need to prep on size (for data alignment) and then we pass the
|
|
|
|
// rest of the length (minus 1) as additional bytes
|
|
|
|
Prep(size, size * (x.Length - 1));
|
|
|
|
Put(x);
|
|
|
|
}
|
|
|
|
|
2015-12-04 04:30:54 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Add a `double` to the buffer (aligns the data and grows if necessary).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The `double` to add to the buffer.</param>
|
2014-09-09 18:46:13 +00:00
|
|
|
public void AddDouble(double x) { Prep(sizeof(double), 0);
|
|
|
|
PutDouble(x); }
|
|
|
|
|
2015-12-04 04:30:54 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Adds an offset, relative to where it will be written.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="off">The offset to add to the buffer.</param>
|
2014-09-09 18:46:13 +00:00
|
|
|
public void AddOffset(int off)
|
|
|
|
{
|
|
|
|
Prep(sizeof(int), 0); // Ensure alignment is already done.
|
2015-05-06 14:33:50 +00:00
|
|
|
if (off > Offset)
|
2014-09-09 18:46:13 +00:00
|
|
|
throw new ArgumentException();
|
|
|
|
|
2015-05-06 14:33:50 +00:00
|
|
|
off = Offset - off + sizeof(int);
|
2014-09-17 00:37:17 +00:00
|
|
|
PutInt(off);
|
2014-09-09 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 04:30:54 +00:00
|
|
|
/// @cond FLATBUFFERS_INTERNAL
|
2014-09-09 18:46:13 +00:00
|
|
|
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.
|
|
|
|
}
|
2015-12-04 04:30:54 +00:00
|
|
|
/// @endcond
|
2014-09-09 18:46:13 +00:00
|
|
|
|
2015-12-04 04:30:54 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Writes data necessary to finish a vector construction.
|
|
|
|
/// </summary>
|
2015-07-28 21:33:45 +00:00
|
|
|
public VectorOffset EndVector()
|
2014-09-09 18:46:13 +00:00
|
|
|
{
|
2014-09-17 00:37:17 +00:00
|
|
|
PutInt(_vectorNumElems);
|
2015-07-28 21:33:45 +00:00
|
|
|
return new VectorOffset(Offset);
|
2014-09-09 18:46:13 +00:00
|
|
|
}
|
2016-08-13 01:01:38 +00:00
|
|
|
|
2016-08-03 10:29:50 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a vector of tables.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="offsets">Offsets of the tables.</param>
|
2016-08-13 01:01:38 +00:00
|
|
|
public VectorOffset CreateVectorOfTables<T>(Offset<T>[] offsets) where T : struct
|
2016-08-03 10:29:50 +00:00
|
|
|
{
|
|
|
|
NotNested();
|
|
|
|
StartVector(sizeof(int), offsets.Length, sizeof(int));
|
|
|
|
for (int i = offsets.Length - 1; i >= 0; i--) AddOffset(offsets[i].Value);
|
|
|
|
return EndVector();
|
|
|
|
}
|
2014-09-09 18:46:13 +00:00
|
|
|
|
2015-12-04 04:30:54 +00:00
|
|
|
/// @cond FLATBUFFERS_INTENRAL
|
2014-09-09 18:46:13 +00:00
|
|
|
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.
|
2015-05-06 14:33:50 +00:00
|
|
|
if (obj != Offset)
|
2014-09-09 18:46:13 +00:00
|
|
|
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
|
2015-12-04 15:49:03 +00:00
|
|
|
if (_vtableSize >= 0)
|
2014-09-09 18:46:13 +00:00
|
|
|
throw new Exception(
|
|
|
|
"FlatBuffers: object serialization must not be nested.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void StartObject(int numfields)
|
|
|
|
{
|
2015-12-04 15:49:03 +00:00
|
|
|
if (numfields < 0)
|
|
|
|
throw new ArgumentOutOfRangeException("Flatbuffers: invalid numfields");
|
|
|
|
|
2014-09-09 18:46:13 +00:00
|
|
|
NotNested();
|
2015-12-04 15:49:03 +00:00
|
|
|
|
|
|
|
if (_vtable.Length < numfields)
|
|
|
|
_vtable = new int[numfields];
|
|
|
|
|
|
|
|
_vtableSize = numfields;
|
2015-05-06 14:33:50 +00:00
|
|
|
_objectStart = Offset;
|
2014-09-09 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set the current vtable at `voffset` to the current location in the
|
|
|
|
// buffer.
|
|
|
|
public void Slot(int voffset)
|
|
|
|
{
|
2015-12-04 15:49:03 +00:00
|
|
|
if (voffset >= _vtableSize)
|
|
|
|
throw new IndexOutOfRangeException("Flatbuffers: invalid voffset");
|
|
|
|
|
2015-05-06 14:33:50 +00:00
|
|
|
_vtable[voffset] = Offset;
|
2014-09-09 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 17:15:29 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Adds a Boolean to the Table at index `o` in its vtable using the value `x` and default `d`
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="o">The index into the vtable</param>
|
|
|
|
/// <param name="x">The value to put into the buffer. If the value is equal to the default
|
|
|
|
/// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
|
|
|
|
/// <param name="d">The default value to compare the value against</param>
|
|
|
|
public void AddBool(int o, bool x, bool d) { if (ForceDefaults || x != d) { AddBool(x); Slot(o); } }
|
2016-05-23 19:24:44 +00:00
|
|
|
|
2016-01-20 17:15:29 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Adds a SByte to the Table at index `o` in its vtable using the value `x` and default `d`
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="o">The index into the vtable</param>
|
|
|
|
/// <param name="x">The value to put into the buffer. If the value is equal to the default
|
|
|
|
/// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
|
|
|
|
/// <param name="d">The default value to compare the value against</param>
|
|
|
|
public void AddSbyte(int o, sbyte x, sbyte d) { if (ForceDefaults || x != d) { AddSbyte(x); Slot(o); } }
|
2016-05-23 19:24:44 +00:00
|
|
|
|
2016-01-20 17:15:29 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Adds a Byte to the Table at index `o` in its vtable using the value `x` and default `d`
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="o">The index into the vtable</param>
|
|
|
|
/// <param name="x">The value to put into the buffer. If the value is equal to the default
|
|
|
|
/// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
|
|
|
|
/// <param name="d">The default value to compare the value against</param>
|
|
|
|
public void AddByte(int o, byte x, byte d) { if (ForceDefaults || x != d) { AddByte(x); Slot(o); } }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a Int16 to the Table at index `o` in its vtable using the value `x` and default `d`
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="o">The index into the vtable</param>
|
|
|
|
/// <param name="x">The value to put into the buffer. If the value is equal to the default
|
|
|
|
/// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
|
|
|
|
/// <param name="d">The default value to compare the value against</param>
|
|
|
|
public void AddShort(int o, short x, int d) { if (ForceDefaults || x != d) { AddShort(x); Slot(o); } }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a UInt16 to the Table at index `o` in its vtable using the value `x` and default `d`
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="o">The index into the vtable</param>
|
|
|
|
/// <param name="x">The value to put into the buffer. If the value is equal to the default
|
|
|
|
/// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
|
|
|
|
/// <param name="d">The default value to compare the value against</param>
|
|
|
|
public void AddUshort(int o, ushort x, ushort d) { if (ForceDefaults || x != d) { AddUshort(x); Slot(o); } }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds an Int32 to the Table at index `o` in its vtable using the value `x` and default `d`
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="o">The index into the vtable</param>
|
|
|
|
/// <param name="x">The value to put into the buffer. If the value is equal to the default
|
|
|
|
/// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
|
|
|
|
/// <param name="d">The default value to compare the value against</param>
|
|
|
|
public void AddInt(int o, int x, int d) { if (ForceDefaults || x != d) { AddInt(x); Slot(o); } }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a UInt32 to the Table at index `o` in its vtable using the value `x` and default `d`
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="o">The index into the vtable</param>
|
|
|
|
/// <param name="x">The value to put into the buffer. If the value is equal to the default
|
|
|
|
/// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
|
|
|
|
/// <param name="d">The default value to compare the value against</param>
|
|
|
|
public void AddUint(int o, uint x, uint d) { if (ForceDefaults || x != d) { AddUint(x); Slot(o); } }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds an Int64 to the Table at index `o` in its vtable using the value `x` and default `d`
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="o">The index into the vtable</param>
|
|
|
|
/// <param name="x">The value to put into the buffer. If the value is equal to the default
|
|
|
|
/// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
|
|
|
|
/// <param name="d">The default value to compare the value against</param>
|
|
|
|
public void AddLong(int o, long x, long d) { if (ForceDefaults || x != d) { AddLong(x); Slot(o); } }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a UInt64 to the Table at index `o` in its vtable using the value `x` and default `d`
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="o">The index into the vtable</param>
|
|
|
|
/// <param name="x">The value to put into the buffer. If the value is equal to the default
|
|
|
|
/// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
|
|
|
|
/// <param name="d">The default value to compare the value against</param>
|
|
|
|
public void AddUlong(int o, ulong x, ulong d) { if (ForceDefaults || x != d) { AddUlong(x); Slot(o); } }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a Single to the Table at index `o` in its vtable using the value `x` and default `d`
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="o">The index into the vtable</param>
|
|
|
|
/// <param name="x">The value to put into the buffer. If the value is equal to the default
|
|
|
|
/// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
|
|
|
|
/// <param name="d">The default value to compare the value against</param>
|
|
|
|
public void AddFloat(int o, float x, double d) { if (ForceDefaults || x != d) { AddFloat(x); Slot(o); } }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a Double to the Table at index `o` in its vtable using the value `x` and default `d`
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="o">The index into the vtable</param>
|
|
|
|
/// <param name="x">The value to put into the buffer. If the value is equal to the default
|
|
|
|
/// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
|
|
|
|
/// <param name="d">The default value to compare the value against</param>
|
|
|
|
public void AddDouble(int o, double x, double d) { if (ForceDefaults || x != d) { AddDouble(x); Slot(o); } }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a buffer offset to the Table at index `o` in its vtable using the value `x` and default `d`
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="o">The index into the vtable</param>
|
|
|
|
/// <param name="x">The value to put into the buffer. If the value is equal to the default
|
|
|
|
/// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
|
|
|
|
/// <param name="d">The default value to compare the value against</param>
|
|
|
|
public void AddOffset(int o, int x, int d) { if (ForceDefaults || x != d) { AddOffset(x); Slot(o); } }
|
2015-12-04 04:30:54 +00:00
|
|
|
/// @endcond
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Encode the string `s` in the buffer using UTF-8.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="s">The string to encode.</param>
|
|
|
|
/// <returns>
|
|
|
|
/// The offset in the buffer where the encoded string starts.
|
|
|
|
/// </returns>
|
2015-07-28 21:33:45 +00:00
|
|
|
public StringOffset CreateString(string s)
|
2014-09-09 18:46:13 +00:00
|
|
|
{
|
2015-12-04 04:30:54 +00:00
|
|
|
NotNested();
|
2015-12-11 19:57:59 +00:00
|
|
|
AddByte(0);
|
|
|
|
var utf8StringLen = Encoding.UTF8.GetByteCount(s);
|
|
|
|
StartVector(1, utf8StringLen, 1);
|
2018-03-12 18:30:46 +00:00
|
|
|
_bb.PutStringUTF8(_space -= utf8StringLen, s);
|
2015-07-28 21:33:45 +00:00
|
|
|
return new StringOffset(EndVector().Value);
|
2014-09-09 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 04:30:54 +00:00
|
|
|
/// @cond FLATBUFFERS_INTERNAL
|
2014-09-09 18:46:13 +00:00
|
|
|
// 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()
|
|
|
|
{
|
2015-12-04 15:49:03 +00:00
|
|
|
if (_vtableSize < 0)
|
2014-09-09 18:46:13 +00:00
|
|
|
throw new InvalidOperationException(
|
|
|
|
"Flatbuffers: calling endObject without a startObject");
|
|
|
|
|
|
|
|
AddInt((int)0);
|
2015-05-06 14:33:50 +00:00
|
|
|
var vtableloc = Offset;
|
2014-09-09 18:46:13 +00:00
|
|
|
// Write out the current vtable.
|
2017-08-21 20:44:23 +00:00
|
|
|
int i = _vtableSize - 1;
|
|
|
|
// Trim trailing zeroes.
|
|
|
|
for (; i >= 0 && _vtable[i] == 0; i--) {}
|
|
|
|
int trimmedSize = i + 1;
|
|
|
|
for (; i >= 0 ; i--) {
|
2014-09-09 18:46:13 +00:00
|
|
|
// Offset relative to the start of the table.
|
|
|
|
short off = (short)(_vtable[i] != 0
|
|
|
|
? vtableloc - _vtable[i]
|
|
|
|
: 0);
|
|
|
|
AddShort(off);
|
2015-12-04 15:49:03 +00:00
|
|
|
|
|
|
|
// clear out written entry
|
|
|
|
_vtable[i] = 0;
|
2014-09-09 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const int standardFields = 2; // The fields below:
|
|
|
|
AddShort((short)(vtableloc - _objectStart));
|
2017-08-21 20:44:23 +00:00
|
|
|
AddShort((short)((trimmedSize + standardFields) *
|
2014-09-09 18:46:13 +00:00
|
|
|
sizeof(short)));
|
|
|
|
|
|
|
|
// Search for an existing vtable that matches the current one.
|
|
|
|
int existingVtable = 0;
|
2017-08-21 20:44:23 +00:00
|
|
|
for (i = 0; i < _numVtables; i++) {
|
2014-09-09 18:46:13 +00:00
|
|
|
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;
|
|
|
|
};
|
2015-05-06 14:33:50 +00:00
|
|
|
_vtables[_numVtables++] = Offset;
|
2014-09-09 18:46:13 +00:00
|
|
|
// Point table to current vtable.
|
2015-05-06 14:33:50 +00:00
|
|
|
_bb.PutInt(_bb.Length - vtableloc, Offset - vtableloc);
|
2014-09-09 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 15:49:03 +00:00
|
|
|
_vtableSize = -1;
|
2014-09-09 18:46:13 +00:00
|
|
|
return vtableloc;
|
|
|
|
}
|
|
|
|
|
2014-09-19 23:51:36 +00:00
|
|
|
// This checks a required field has been set in a given table that has
|
|
|
|
// just been constructed.
|
|
|
|
public void Required(int table, int field)
|
|
|
|
{
|
|
|
|
int table_start = _bb.Length - table;
|
|
|
|
int vtable_start = table_start - _bb.GetInt(table_start);
|
|
|
|
bool ok = _bb.GetShort(vtable_start + field) != 0;
|
|
|
|
// If this fails, the caller will show what field needs to be set.
|
|
|
|
if (!ok)
|
|
|
|
throw new InvalidOperationException("FlatBuffers: field " + field +
|
|
|
|
" must be set");
|
|
|
|
}
|
2015-12-04 04:30:54 +00:00
|
|
|
/// @endcond
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Finalize a buffer, pointing to the given `root_table`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="rootTable">
|
|
|
|
/// An offset to be added to the buffer.
|
|
|
|
/// </param>
|
2018-03-12 18:30:46 +00:00
|
|
|
/// <param name="sizePrefix">
|
|
|
|
/// Whether to prefix the size to the buffer.
|
|
|
|
/// </param>
|
|
|
|
protected void Finish(int rootTable, bool sizePrefix)
|
2014-09-09 18:46:13 +00:00
|
|
|
{
|
2018-03-12 18:30:46 +00:00
|
|
|
Prep(_minAlign, sizeof(int) + (sizePrefix ? sizeof(int) : 0));
|
2014-09-09 18:46:13 +00:00
|
|
|
AddOffset(rootTable);
|
2018-03-12 18:30:46 +00:00
|
|
|
if (sizePrefix) {
|
|
|
|
AddInt(_bb.Length - _space);
|
|
|
|
}
|
2015-08-07 07:51:15 +00:00
|
|
|
_bb.Position = _space;
|
2014-09-09 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 18:30:46 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Finalize a buffer, pointing to the given `root_table`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="rootTable">
|
|
|
|
/// An offset to be added to the buffer.
|
|
|
|
/// </param>
|
|
|
|
public void Finish(int rootTable)
|
|
|
|
{
|
|
|
|
Finish(rootTable, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Finalize a buffer, pointing to the given `root_table`, with the size prefixed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="rootTable">
|
|
|
|
/// An offset to be added to the buffer.
|
|
|
|
/// </param>
|
|
|
|
public void FinishSizePrefixed(int rootTable)
|
|
|
|
{
|
|
|
|
Finish(rootTable, true);
|
|
|
|
}
|
|
|
|
|
2015-12-04 04:30:54 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get the ByteBuffer representing the FlatBuffer.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// This is typically only called after you call `Finish()`.
|
2016-08-11 00:53:40 +00:00
|
|
|
/// The actual data starts at the ByteBuffer's current position,
|
|
|
|
/// not necessarily at `0`.
|
2015-12-04 04:30:54 +00:00
|
|
|
/// </remarks>
|
|
|
|
/// <returns>
|
|
|
|
/// Returns the ByteBuffer for this FlatBuffer.
|
|
|
|
/// </returns>
|
2015-05-06 14:33:50 +00:00
|
|
|
public ByteBuffer DataBuffer { get { return _bb; } }
|
2014-09-09 18:46:13 +00:00
|
|
|
|
2015-12-04 04:30:54 +00:00
|
|
|
/// <summary>
|
|
|
|
/// A utility function to copy and return the ByteBuffer data as a
|
|
|
|
/// `byte[]`.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>
|
|
|
|
/// A full copy of the FlatBuffer data.
|
|
|
|
/// </returns>
|
2014-09-09 18:46:13 +00:00
|
|
|
public byte[] SizedByteArray()
|
|
|
|
{
|
2018-03-12 18:30:46 +00:00
|
|
|
return _bb.ToSizedArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Finalize a buffer, pointing to the given `rootTable`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="rootTable">
|
|
|
|
/// An offset to be added to the buffer.
|
|
|
|
/// </param>
|
|
|
|
/// <param name="fileIdentifier">
|
|
|
|
/// A FlatBuffer file identifier to be added to the buffer before
|
|
|
|
/// `root_table`.
|
|
|
|
/// </param>
|
|
|
|
/// <param name="sizePrefix">
|
|
|
|
/// Whether to prefix the size to the buffer.
|
|
|
|
/// </param>
|
|
|
|
protected void Finish(int rootTable, string fileIdentifier, bool sizePrefix)
|
|
|
|
{
|
|
|
|
Prep(_minAlign, sizeof(int) + (sizePrefix ? sizeof(int) : 0) +
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
Finish(rootTable, sizePrefix);
|
2014-09-09 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 18:30:46 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Finalize a buffer, pointing to the given `rootTable`.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="rootTable">
|
|
|
|
/// An offset to be added to the buffer.
|
|
|
|
/// </param>
|
|
|
|
/// <param name="fileIdentifier">
|
|
|
|
/// A FlatBuffer file identifier to be added to the buffer before
|
|
|
|
/// `root_table`.
|
|
|
|
/// </param>
|
|
|
|
public void Finish(int rootTable, string fileIdentifier)
|
|
|
|
{
|
|
|
|
Finish(rootTable, fileIdentifier, false);
|
|
|
|
}
|
2014-09-09 18:46:13 +00:00
|
|
|
|
2018-03-12 18:30:46 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Finalize a buffer, pointing to the given `rootTable`, with the size prefixed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="rootTable">
|
|
|
|
/// An offset to be added to the buffer.
|
|
|
|
/// </param>
|
|
|
|
/// <param name="fileIdentifier">
|
|
|
|
/// A FlatBuffer file identifier to be added to the buffer before
|
|
|
|
/// `root_table`.
|
|
|
|
/// </param>
|
|
|
|
public void FinishSizePrefixed(int rootTable, string fileIdentifier)
|
|
|
|
{
|
|
|
|
Finish(rootTable, fileIdentifier, true);
|
|
|
|
}
|
2014-09-09 18:46:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-04 04:30:54 +00:00
|
|
|
|
|
|
|
/// @}
|