diff options
Diffstat (limited to 'net/FlatBuffers/FlatBufferBuilder.cs')
-rw-r--r-- | net/FlatBuffers/FlatBufferBuilder.cs | 44 |
1 files changed, 37 insertions, 7 deletions
diff --git a/net/FlatBuffers/FlatBufferBuilder.cs b/net/FlatBuffers/FlatBufferBuilder.cs index 93f72be3..27c16b36 100644 --- a/net/FlatBuffers/FlatBufferBuilder.cs +++ b/net/FlatBuffers/FlatBufferBuilder.cs @@ -16,6 +16,7 @@ using System; +using System.Collections.Generic; using System.Text; /// @file @@ -47,6 +48,9 @@ namespace FlatBuffers // For the current vector being built. private int _vectorNumElems = 0; + // For CreateSharedString + private Dictionary<string, StringOffset> _sharedStringMap = null; + /// <summary> /// Create a FlatBufferBuilder with a given initial size. /// </summary> @@ -191,7 +195,7 @@ namespace FlatBuffers } /// <summary> - /// Puts an array of type T into this builder at the + /// Puts an array of type T into this builder at the /// current offset /// </summary> /// <typeparam name="T">The type of the input data </typeparam> @@ -204,7 +208,7 @@ namespace FlatBuffers #if ENABLE_SPAN_T /// <summary> - /// Puts a span of type T into this builder at the + /// Puts a span of type T into this builder at the /// current offset /// </summary> /// <typeparam name="T">The type of the input data </typeparam> @@ -407,7 +411,7 @@ namespace FlatBuffers "FlatBuffers: object serialization must not be nested."); } - public void StartObject(int numfields) + public void StartTable(int numfields) { if (numfields < 0) throw new ArgumentOutOfRangeException("Flatbuffers: invalid numfields"); @@ -536,9 +540,9 @@ namespace FlatBuffers /// </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> + /// 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); } } + public void AddOffset(int o, int x, int d) { if (x != d) { AddOffset(x); Slot(o); } } /// @endcond /// <summary> @@ -579,6 +583,32 @@ namespace FlatBuffers } #endif + /// <summary> + /// Store a string in the buffer, which can contain any binary data. + /// If a string with this exact contents has already been serialized before, + /// instead simply returns the offset of the existing string. + /// </summary> + /// <param name="s">The string to encode.</param> + /// <returns> + /// The offset in the buffer where the encoded string starts. + /// </returns> + public StringOffset CreateSharedString(string s) + { + if (_sharedStringMap == null) + { + _sharedStringMap = new Dictionary<string, StringOffset>(); + } + + if (_sharedStringMap.ContainsKey(s)) + { + return _sharedStringMap[s]; + } + + var stringOffset = CreateString(s); + _sharedStringMap.Add(s, stringOffset); + return stringOffset; + } + /// @cond FLATBUFFERS_INTERNAL // Structs are stored inline, so nothing additional is being added. // `d` is always 0. @@ -591,11 +621,11 @@ namespace FlatBuffers } } - public int EndObject() + public int EndTable() { if (_vtableSize < 0) throw new InvalidOperationException( - "Flatbuffers: calling endObject without a startObject"); + "Flatbuffers: calling EndTable without a StartTable"); AddInt((int)0); var vtableloc = Offset; |