summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOli Wilkinson <Oli Wilkinson>2016-01-20 17:15:29 +0000
committerOli Wilkinson <Oli Wilkinson>2016-01-20 17:25:58 +0000
commitfff4590faf2a0dd57e741bb6ee4efbc7e7667a36 (patch)
treec872f218f4de3f11c0b468091bff04a3723cf097
parent41395d981d3d560f99dd9619c97bc52790ab6eb9 (diff)
downloadflatbuffers-fff4590faf2a0dd57e741bb6ee4efbc7e7667a36.tar.gz
flatbuffers-fff4590faf2a0dd57e741bb6ee4efbc7e7667a36.tar.bz2
flatbuffers-fff4590faf2a0dd57e741bb6ee4efbc7e7667a36.zip
Implemented ForceDefaults option on C# FlatBufferBuilder.
Tested on: Windows, Unit Tests
-rw-r--r--docs/source/Tutorial.md3
-rw-r--r--net/FlatBuffers/FlatBufferBuilder.cs129
-rw-r--r--tests/FlatBuffers.Test/FlatBufferBuilderTests.cs249
-rw-r--r--tests/FlatBuffers.Test/FlatBuffers.Test.csproj1
4 files changed, 366 insertions, 16 deletions
diff --git a/docs/source/Tutorial.md b/docs/source/Tutorial.md
index fcc208a3..74941be8 100644
--- a/docs/source/Tutorial.md
+++ b/docs/source/Tutorial.md
@@ -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
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
diff --git a/net/FlatBuffers/FlatBufferBuilder.cs b/net/FlatBuffers/FlatBufferBuilder.cs
index 1f9f9d5f..c320ea8c 100644
--- a/net/FlatBuffers/FlatBufferBuilder.cs
+++ b/net/FlatBuffers/FlatBufferBuilder.cs
@@ -77,6 +77,15 @@ namespace FlatBuffers
_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
public int Offset { get { return _bb.Length - _space; } }
@@ -332,19 +341,113 @@ namespace FlatBuffers
_vtable[voffset] = Offset;
}
- // Add a scalar to a table at `o` into its vtable, with value `x` and default `d`
- public void AddBool(int o, bool x, bool d) { if (x != d) { AddBool(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); } }
+ /// <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); } }
+
+ /// <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); } }
+
+ /// <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
/// <summary>
diff --git a/tests/FlatBuffers.Test/FlatBufferBuilderTests.cs b/tests/FlatBuffers.Test/FlatBufferBuilderTests.cs
new file mode 100644
index 00000000..529c813b
--- /dev/null
+++ b/tests/FlatBuffers.Test/FlatBufferBuilderTests.cs
@@ -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);
+ }
+ }
+}
diff --git a/tests/FlatBuffers.Test/FlatBuffers.Test.csproj b/tests/FlatBuffers.Test/FlatBuffers.Test.csproj
index 13b1faae..4055fa6a 100644
--- a/tests/FlatBuffers.Test/FlatBuffers.Test.csproj
+++ b/tests/FlatBuffers.Test/FlatBuffers.Test.csproj
@@ -91,6 +91,7 @@
</Compile>
<Compile Include="Assert.cs" />
<Compile Include="ByteBufferTests.cs" />
+ <Compile Include="FlatBufferBuilderTests.cs" />
<Compile Include="FlatBuffersFuzzTests.cs" />
<Compile Include="FlatBuffersTestClassAttribute.cs" />
<Compile Include="FlatBuffersTestMethodAttribute.cs" />