aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/ProtocolBuffers/FieldCodec.cs
diff options
context:
space:
mode:
authorJon Skeet <jonskeet@google.com>2015-06-26 17:37:14 +0100
committerJon Skeet <jonskeet@google.com>2015-06-30 13:20:30 +0100
commitf2a27cc2c71b4dae3ff230574a73c1de88dd61b7 (patch)
tree58cdbbbd9262732c9a104171f2563f0f2da85acb /csharp/src/ProtocolBuffers/FieldCodec.cs
parent241e17ba78b71a7ecccb289914ecaeab203b2373 (diff)
downloadprotobuf-f2a27cc2c71b4dae3ff230574a73c1de88dd61b7.tar.gz
protobuf-f2a27cc2c71b4dae3ff230574a73c1de88dd61b7.tar.bz2
protobuf-f2a27cc2c71b4dae3ff230574a73c1de88dd61b7.zip
First pass (not yet compiling) at removing all the array handling code from Coded*Stream.
Prod code works, but some tests are broken. Obviously those need fixing, then more tests, and review benchmarks.
Diffstat (limited to 'csharp/src/ProtocolBuffers/FieldCodec.cs')
-rw-r--r--csharp/src/ProtocolBuffers/FieldCodec.cs56
1 files changed, 50 insertions, 6 deletions
diff --git a/csharp/src/ProtocolBuffers/FieldCodec.cs b/csharp/src/ProtocolBuffers/FieldCodec.cs
index 4cab9cc4..d3fc2f71 100644
--- a/csharp/src/ProtocolBuffers/FieldCodec.cs
+++ b/csharp/src/ProtocolBuffers/FieldCodec.cs
@@ -145,28 +145,68 @@ namespace Google.Protobuf
private readonly Func<CodedInputStream, T> reader;
private readonly Action<CodedOutputStream, T> writer;
- private readonly Func<T, int> sizeComputer;
+ private readonly Func<T, int> sizeCalculator;
private readonly uint tag;
private readonly int tagSize;
+ private readonly int fixedSize;
internal FieldCodec(
Func<CodedInputStream, T> reader,
Action<CodedOutputStream, T> writer,
- Func<T, int> sizeComputer,
+ Func<T, int> sizeCalculator,
uint tag)
{
this.reader = reader;
this.writer = writer;
- this.sizeComputer = sizeComputer;
+ this.sizeCalculator = sizeCalculator;
+ this.fixedSize = 0;
this.tag = tag;
tagSize = CodedOutputStream.ComputeRawVarint32Size(tag);
}
+ internal FieldCodec(
+ Func<CodedInputStream, T> reader,
+ Action<CodedOutputStream, T> writer,
+ int fixedSize,
+ uint tag)
+ {
+ this.reader = reader;
+ this.writer = writer;
+ this.sizeCalculator = _ => fixedSize;
+ this.fixedSize = fixedSize;
+ this.tag = tag;
+ tagSize = CodedOutputStream.ComputeRawVarint32Size(tag);
+ }
+
+ /// <summary>
+ /// Returns the size calculator for just a value.
+ /// </summary>
+ internal Func<T, int> ValueSizeCalculator { get { return sizeCalculator; } }
+
+ /// <summary>
+ /// Returns a delegate to write a value (unconditionally) to a coded output stream.
+ /// </summary>
+ internal Action<CodedOutputStream, T> ValueWriter { get { return writer; } }
+
+ /// <summary>
+ /// Returns a delegate to read a value from a coded input stream. It is assumed that
+ /// the stream is already positioned on the appropriate tag.
+ /// </summary>
+ internal Func<CodedInputStream, T> ValueReader { get { return reader; } }
+
+ /// <summary>
+ /// Returns the fixed size for an entry, or 0 if sizes vary.
+ /// </summary>
+ internal int FixedSize { get { return fixedSize; } }
+
public uint Tag { get { return tag; } }
public T DefaultValue { get { return Default; } }
- public void Write(CodedOutputStream output, T value)
+ /// <summary>
+ /// Write a tag and the given value, *if* the value is not the default.
+ /// </summary>
+ public void WriteTagAndValue(CodedOutputStream output, T value)
{
if (!IsDefault(value))
{
@@ -180,9 +220,13 @@ namespace Google.Protobuf
return reader(input);
}
- public int CalculateSize(T value)
+ /// <summary>
+ /// Calculates the size required to write the given value, with a tag,
+ /// if the value is not the default.
+ /// </summary>
+ public int CalculateSizeWithTag(T value)
{
- return IsDefault(value) ? 0 : sizeComputer(value) + CodedOutputStream.ComputeRawVarint32Size(tag);
+ return IsDefault(value) ? 0 : sizeCalculator(value) + tagSize;
}
}
}