123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- #region Copyright notice and license
- #endregion
- using System;
- using System.Collections.Generic;
- namespace Google.Protobuf
- {
-
-
-
- public static class FieldCodec
- {
-
-
-
-
-
-
- public static FieldCodec<string> ForString(uint tag)
- {
- return new FieldCodec<string>(input => input.ReadString(), (output, value) => output.WriteString(value), CodedOutputStream.ComputeStringSize, tag);
- }
-
-
-
-
-
- public static FieldCodec<ByteString> ForBytes(uint tag)
- {
- return new FieldCodec<ByteString>(input => input.ReadBytes(), (output, value) => output.WriteBytes(value), CodedOutputStream.ComputeBytesSize, tag);
- }
-
-
-
-
-
- public static FieldCodec<bool> ForBool(uint tag)
- {
- return new FieldCodec<bool>(input => input.ReadBool(), (output, value) => output.WriteBool(value), CodedOutputStream.ComputeBoolSize, tag);
- }
-
-
-
-
-
- public static FieldCodec<int> ForInt32(uint tag)
- {
- return new FieldCodec<int>(input => input.ReadInt32(), (output, value) => output.WriteInt32(value), CodedOutputStream.ComputeInt32Size, tag);
- }
-
-
-
-
-
- public static FieldCodec<int> ForSInt32(uint tag)
- {
- return new FieldCodec<int>(input => input.ReadSInt32(), (output, value) => output.WriteSInt32(value), CodedOutputStream.ComputeSInt32Size, tag);
- }
-
-
-
-
-
- public static FieldCodec<uint> ForFixed32(uint tag)
- {
- return new FieldCodec<uint>(input => input.ReadFixed32(), (output, value) => output.WriteFixed32(value), 4, tag);
- }
-
-
-
-
-
- public static FieldCodec<int> ForSFixed32(uint tag)
- {
- return new FieldCodec<int>(input => input.ReadSFixed32(), (output, value) => output.WriteSFixed32(value), 4, tag);
- }
-
-
-
-
-
- public static FieldCodec<uint> ForUInt32(uint tag)
- {
- return new FieldCodec<uint>(input => input.ReadUInt32(), (output, value) => output.WriteUInt32(value), CodedOutputStream.ComputeUInt32Size, tag);
- }
-
-
-
-
-
- public static FieldCodec<long> ForInt64(uint tag)
- {
- return new FieldCodec<long>(input => input.ReadInt64(), (output, value) => output.WriteInt64(value), CodedOutputStream.ComputeInt64Size, tag);
- }
-
-
-
-
-
- public static FieldCodec<long> ForSInt64(uint tag)
- {
- return new FieldCodec<long>(input => input.ReadSInt64(), (output, value) => output.WriteSInt64(value), CodedOutputStream.ComputeSInt64Size, tag);
- }
-
-
-
-
-
- public static FieldCodec<ulong> ForFixed64(uint tag)
- {
- return new FieldCodec<ulong>(input => input.ReadFixed64(), (output, value) => output.WriteFixed64(value), 8, tag);
- }
-
-
-
-
-
- public static FieldCodec<long> ForSFixed64(uint tag)
- {
- return new FieldCodec<long>(input => input.ReadSFixed64(), (output, value) => output.WriteSFixed64(value), 8, tag);
- }
-
-
-
-
-
- public static FieldCodec<ulong> ForUInt64(uint tag)
- {
- return new FieldCodec<ulong>(input => input.ReadUInt64(), (output, value) => output.WriteUInt64(value), CodedOutputStream.ComputeUInt64Size, tag);
- }
-
-
-
-
-
- public static FieldCodec<float> ForFloat(uint tag)
- {
- return new FieldCodec<float>(input => input.ReadFloat(), (output, value) => output.WriteFloat(value), CodedOutputStream.ComputeFloatSize, tag);
- }
-
-
-
-
-
- public static FieldCodec<double> ForDouble(uint tag)
- {
- return new FieldCodec<double>(input => input.ReadDouble(), (output, value) => output.WriteDouble(value), CodedOutputStream.ComputeDoubleSize, tag);
- }
-
-
-
-
-
-
-
-
-
- public static FieldCodec<T> ForEnum<T>(uint tag, Func<T, int> toInt32, Func<int, T> fromInt32)
- {
- return new FieldCodec<T>(input => fromInt32(
- input.ReadEnum()),
- (output, value) => output.WriteEnum(toInt32(value)),
- value => CodedOutputStream.ComputeEnumSize(toInt32(value)), tag);
- }
-
-
-
-
-
-
- public static FieldCodec<T> ForMessage<T>(uint tag, MessageParser<T> parser) where T : IMessage
- {
- return new FieldCodec<T>(input => { T message = parser.CreateTemplate(); input.ReadMessage(message); return message; },
- (output, value) => output.WriteMessage(value), message => CodedOutputStream.ComputeMessageSize(message), tag);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public sealed class FieldCodec<T>
- {
- private static readonly T DefaultDefault;
-
- private static readonly bool TypeSupportsPacking = default(T) != null;
- static FieldCodec()
- {
- if (typeof(T) == typeof(string))
- {
- DefaultDefault = (T)(object)"";
- }
- else if (typeof(T) == typeof(ByteString))
- {
- DefaultDefault = (T)(object)ByteString.Empty;
- }
-
- }
- internal static bool IsPackedRepeatedField(uint tag)
- {
- return TypeSupportsPacking && WireFormat.GetTagWireType(tag) == WireFormat.WireType.LengthDelimited;
- }
- internal readonly bool PackedRepeatedField;
-
-
-
- internal readonly Action<CodedOutputStream, T> ValueWriter;
-
-
-
- internal readonly Func<T, int> ValueSizeCalculator;
-
-
-
-
- internal readonly Func<CodedInputStream, T> ValueReader;
-
-
-
- internal readonly int FixedSize;
-
-
-
-
-
-
- internal readonly uint Tag;
-
-
-
-
-
-
-
-
- internal readonly T DefaultValue;
- private readonly int tagSize;
-
- internal FieldCodec(
- Func<CodedInputStream, T> reader,
- Action<CodedOutputStream, T> writer,
- int fixedSize,
- uint tag) : this(reader, writer, _ => fixedSize, tag)
- {
- FixedSize = fixedSize;
- }
- internal FieldCodec(
- Func<CodedInputStream, T> reader,
- Action<CodedOutputStream, T> writer,
- Func<T, int> sizeCalculator,
- uint tag) : this(reader, writer, sizeCalculator, tag, DefaultDefault)
- {
- }
- internal FieldCodec(
- Func<CodedInputStream, T> reader,
- Action<CodedOutputStream, T> writer,
- Func<T, int> sizeCalculator,
- uint tag,
- T defaultValue)
- {
- ValueReader = reader;
- ValueWriter = writer;
- ValueSizeCalculator = sizeCalculator;
- FixedSize = 0;
- Tag = tag;
- DefaultValue = defaultValue;
- tagSize = CodedOutputStream.ComputeRawVarint32Size(tag);
-
- PackedRepeatedField = IsPackedRepeatedField(tag);
- }
-
-
-
- public void WriteTagAndValue(CodedOutputStream output, T value)
- {
- if (!IsDefault(value))
- {
- output.WriteTag(Tag);
- ValueWriter(output, value);
- }
- }
-
-
-
-
-
- public T Read(CodedInputStream input)
- {
- return ValueReader(input);
- }
-
-
-
-
- public int CalculateSizeWithTag(T value)
- {
- return IsDefault(value) ? 0 : ValueSizeCalculator(value) + tagSize;
- }
- private bool IsDefault(T value)
- {
- return EqualityComparer<T>.Default.Equals(value, DefaultValue);
- }
- }
- }
|