Merge pull request #3734 from evolutional/cs-forcedefaults

Implemented ForceDefaults option on C# FlatBufferBuilder.
This commit is contained in:
Wouter van Oortmerssen 2016-01-20 13:46:49 -08:00
commit db0727e943
4 changed files with 366 additions and 16 deletions

View File

@ -1644,9 +1644,6 @@ One way to solve this is to call `ForceDefaults` on a FlatBufferBuilder to
force all fields you set to actually be written. This, of course, increases the force all fields you set to actually be written. This, of course, increases the
size of the buffer somewhat, but this may be acceptable for a mutable buffer. size of the buffer somewhat, but this may be acceptable for a mutable buffer.
<div class="language-csharp">
**Note: `ForceDefaults` is not yet implemented in C#.**
</div>
## JSON with FlatBuffers ## JSON with FlatBuffers

View File

@ -77,6 +77,15 @@ namespace FlatBuffers
_vectorNumElems = 0; _vectorNumElems = 0;
} }
/// <summary>
/// Gets and sets a Boolean to disable the optimization when serializing
/// default values to a Table.
///
/// In order to save space, fields that are set to their default value
/// don't get serialized into the buffer.
/// </summary>
public bool ForceDefaults { get; set; }
/// @cond FLATBUFFERS_INTERNAL /// @cond FLATBUFFERS_INTERNAL
public int Offset { get { return _bb.Length - _space; } } public int Offset { get { return _bb.Length - _space; } }
@ -332,19 +341,113 @@ namespace FlatBuffers
_vtable[voffset] = Offset; _vtable[voffset] = Offset;
} }
// Add a scalar to a table at `o` into its vtable, with value `x` and default `d` /// <summary>
public void AddBool(int o, bool x, bool d) { if (x != d) { AddBool(x); Slot(o); } } /// Adds a Boolean to the Table at index `o` in its vtable using the value `x` and default `d`
public void AddSbyte(int o, sbyte x, sbyte d) { if (x != d) { AddSbyte(x); Slot(o); } } /// </summary>
public void AddByte(int o, byte x, byte d) { if (x != d) { AddByte(x); Slot(o); } } /// <param name="o">The index into the vtable</param>
public void AddShort(int o, short x, int d) { if (x != d) { AddShort(x); Slot(o); } } /// <param name="x">The value to put into the buffer. If the value is equal to the default
public void AddUshort(int o, ushort x, ushort d) { if (x != d) { AddUshort(x); Slot(o); } } /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
public void AddInt(int o, int x, int d) { if (x != d) { AddInt(x); Slot(o); } } /// <param name="d">The default value to compare the value against</param>
public void AddUint(int o, uint x, uint d) { if (x != d) { AddUint(x); Slot(o); } } public void AddBool(int o, bool x, bool d) { if (ForceDefaults || x != d) { AddBool(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); } } /// <summary>
public void AddFloat(int o, float x, double d) { if (x != d) { AddFloat(x); Slot(o); } } /// Adds a SByte to the Table at index `o` in its vtable using the value `x` and default `d`
public void AddDouble(int o, double x, double d) { if (x != d) { AddDouble(x); Slot(o); } } /// </summary>
public void AddOffset(int o, int x, int d) { if (x != d) { AddOffset(x); Slot(o); } } /// <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); } }
/// <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); } }
/// @endcond /// @endcond
/// <summary> /// <summary>

View File

@ -0,0 +1,249 @@
/*
* Copyright 2016 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.Test
{
[FlatBuffersTestClass]
public class FlatBufferBuilderTests
{
private FlatBufferBuilder CreateBuffer(bool forceDefaults = true)
{
var fbb = new FlatBufferBuilder(16) {ForceDefaults = forceDefaults};
fbb.StartObject(1);
return fbb;
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WithForceDefaults_WhenAddBool_AndDefaultValue_OffsetIncreasesBySize()
{
var fbb = CreateBuffer();
var storedOffset = fbb.Offset;
fbb.AddBool(0, false, false);
var endOffset = fbb.Offset;
Assert.AreEqual(sizeof(bool), endOffset-storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WithForceDefaults_WhenAddSByte_AndDefaultValue_OffsetIncreasesBySize()
{
var fbb = CreateBuffer();
var storedOffset = fbb.Offset;
fbb.AddSbyte(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(sizeof(sbyte), endOffset - storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WithForceDefaults_WhenAddByte_AndDefaultValue_OffsetIncreasesBySize()
{
var fbb = CreateBuffer();
var storedOffset = fbb.Offset;
fbb.AddByte(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(sizeof(byte), endOffset - storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WithForceDefaults_WhenAddShort_AndDefaultValue_OffsetIncreasesBySize()
{
var fbb = CreateBuffer();
var storedOffset = fbb.Offset;
fbb.AddShort(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(sizeof(short), endOffset - storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WithForceDefaults_WhenAddUShort_AndDefaultValue_OffsetIncreasesBySize()
{
var fbb = CreateBuffer();
var storedOffset = fbb.Offset;
fbb.AddUshort(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(sizeof(ushort), endOffset - storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WithForceDefaults_WhenAddInt_AndDefaultValue_OffsetIncreasesBySize()
{
var fbb = CreateBuffer();
var storedOffset = fbb.Offset;
fbb.AddInt(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(sizeof(int), endOffset - storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WithForceDefaults_WhenAddUInt_AndDefaultValue_OffsetIncreasesBySize()
{
var fbb = CreateBuffer();
var storedOffset = fbb.Offset;
fbb.AddUint(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(sizeof(uint), endOffset - storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WithForceDefaults_WhenAddLong_AndDefaultValue_OffsetIncreasesBySize()
{
var fbb = CreateBuffer();
var storedOffset = fbb.Offset;
fbb.AddLong(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(sizeof(long), endOffset - storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WithForceDefaults_WhenAddULong_AndDefaultValue_OffsetIncreasesBySize()
{
var fbb = CreateBuffer();
var storedOffset = fbb.Offset;
fbb.AddUlong(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(sizeof(ulong), endOffset - storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WithForceDefaults_WhenAddFloat_AndDefaultValue_OffsetIncreasesBySize()
{
var fbb = CreateBuffer();
var storedOffset = fbb.Offset;
fbb.AddFloat(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(sizeof(float), endOffset - storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WithForceDefaults_WhenAddDouble_AndDefaultValue_OffsetIncreasesBySize()
{
var fbb = CreateBuffer();
var storedOffset = fbb.Offset;
fbb.AddDouble(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(sizeof(double), endOffset - storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WhenAddBool_AndDefaultValue_OffsetIsUnchanged()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
fbb.AddBool(0, false, false);
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WhenAddSByte_AndDefaultValue_OffsetIsUnchanged()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
fbb.AddSbyte(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WhenAddByte_AndDefaultValue_OffsetIsUnchanged()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
fbb.AddByte(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WhenAddShort_AndDefaultValue_OffsetIsUnchanged()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
fbb.AddShort(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WhenAddUShort_AndDefaultValue_OffsetIsUnchanged()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
fbb.AddUshort(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WhenAddInt_AndDefaultValue_OffsetIsUnchanged()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
fbb.AddInt(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WhenAddUInt_AndDefaultValue_OffsetIsUnchanged()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
fbb.AddUint(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WhenAddLong_AndDefaultValue_OffsetIsUnchanged()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
fbb.AddLong(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WhenAddULong_AndDefaultValue_OffsetIsUnchanged()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
fbb.AddUlong(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WhenAddFloat_AndDefaultValue_OffsetIsUnchanged()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
fbb.AddFloat(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_WhenAddDouble_AndDefaultValue_OffsetIsUnchanged()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
fbb.AddDouble(0, 0, 0);
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset);
}
}
}

View File

@ -91,6 +91,7 @@
</Compile> </Compile>
<Compile Include="Assert.cs" /> <Compile Include="Assert.cs" />
<Compile Include="ByteBufferTests.cs" /> <Compile Include="ByteBufferTests.cs" />
<Compile Include="FlatBufferBuilderTests.cs" />
<Compile Include="FlatBuffersFuzzTests.cs" /> <Compile Include="FlatBuffersFuzzTests.cs" />
<Compile Include="FlatBuffersTestClassAttribute.cs" /> <Compile Include="FlatBuffersTestClassAttribute.cs" />
<Compile Include="FlatBuffersTestMethodAttribute.cs" /> <Compile Include="FlatBuffersTestMethodAttribute.cs" />