Jon Skeet | ee835a3 | 2015-06-30 17:22:26 +0100 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | // Protocol Buffers - Google's data interchange format |
| 3 | // Copyright 2015 Google Inc. All rights reserved. |
| 4 | // https://developers.google.com/protocol-buffers/ |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | #endregion |
| 32 | |
| 33 | using System; |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 34 | using System.Collections.Generic; |
| 35 | |
| 36 | namespace Google.Protobuf |
| 37 | { |
| 38 | /// <summary> |
| 39 | /// Factory methods for <see cref="FieldCodec{T}"/>. |
| 40 | /// </summary> |
| 41 | public static class FieldCodec |
| 42 | { |
Jon Skeet | f34d37a | 2015-06-30 13:16:20 +0100 | [diff] [blame] | 43 | // TODO: Avoid the "dual hit" of lambda expressions: create open delegates instead. (At least test...) |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 44 | public static FieldCodec<string> ForString(uint tag) |
| 45 | { |
| 46 | return new FieldCodec<string>(input => input.ReadString(), (output, value) => output.WriteString(value), CodedOutputStream.ComputeStringSize, tag); |
| 47 | } |
| 48 | |
| 49 | public static FieldCodec<ByteString> ForBytes(uint tag) |
| 50 | { |
| 51 | return new FieldCodec<ByteString>(input => input.ReadBytes(), (output, value) => output.WriteBytes(value), CodedOutputStream.ComputeBytesSize, tag); |
| 52 | } |
| 53 | |
| 54 | public static FieldCodec<bool> ForBool(uint tag) |
| 55 | { |
| 56 | return new FieldCodec<bool>(input => input.ReadBool(), (output, value) => output.WriteBool(value), CodedOutputStream.ComputeBoolSize, tag); |
| 57 | } |
| 58 | |
| 59 | public static FieldCodec<int> ForInt32(uint tag) |
| 60 | { |
| 61 | return new FieldCodec<int>(input => input.ReadInt32(), (output, value) => output.WriteInt32(value), CodedOutputStream.ComputeInt32Size, tag); |
| 62 | } |
| 63 | |
| 64 | public static FieldCodec<int> ForSInt32(uint tag) |
| 65 | { |
| 66 | return new FieldCodec<int>(input => input.ReadSInt32(), (output, value) => output.WriteSInt32(value), CodedOutputStream.ComputeSInt32Size, tag); |
| 67 | } |
| 68 | |
Jon Skeet | c128331 | 2015-06-26 10:32:23 +0100 | [diff] [blame] | 69 | public static FieldCodec<uint> ForFixed32(uint tag) |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 70 | { |
| 71 | return new FieldCodec<uint>(input => input.ReadFixed32(), (output, value) => output.WriteFixed32(value), CodedOutputStream.ComputeFixed32Size, tag); |
| 72 | } |
| 73 | |
Jon Skeet | c128331 | 2015-06-26 10:32:23 +0100 | [diff] [blame] | 74 | public static FieldCodec<int> ForSFixed32(uint tag) |
| 75 | { |
| 76 | return new FieldCodec<int>(input => input.ReadSFixed32(), (output, value) => output.WriteSFixed32(value), CodedOutputStream.ComputeSFixed32Size, tag); |
| 77 | } |
| 78 | |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 79 | public static FieldCodec<uint> ForUInt32(uint tag) |
| 80 | { |
| 81 | return new FieldCodec<uint>(input => input.ReadUInt32(), (output, value) => output.WriteUInt32(value), CodedOutputStream.ComputeUInt32Size, tag); |
| 82 | } |
| 83 | |
| 84 | public static FieldCodec<long> ForInt64(uint tag) |
| 85 | { |
| 86 | return new FieldCodec<long>(input => input.ReadInt64(), (output, value) => output.WriteInt64(value), CodedOutputStream.ComputeInt64Size, tag); |
| 87 | } |
| 88 | |
| 89 | public static FieldCodec<long> ForSInt64(uint tag) |
| 90 | { |
| 91 | return new FieldCodec<long>(input => input.ReadSInt64(), (output, value) => output.WriteSInt64(value), CodedOutputStream.ComputeSInt64Size, tag); |
| 92 | } |
| 93 | |
Jon Skeet | c128331 | 2015-06-26 10:32:23 +0100 | [diff] [blame] | 94 | public static FieldCodec<ulong> ForFixed64(uint tag) |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 95 | { |
| 96 | return new FieldCodec<ulong>(input => input.ReadFixed64(), (output, value) => output.WriteFixed64(value), CodedOutputStream.ComputeFixed64Size, tag); |
| 97 | } |
| 98 | |
Jon Skeet | c128331 | 2015-06-26 10:32:23 +0100 | [diff] [blame] | 99 | public static FieldCodec<long> ForSFixed64(uint tag) |
| 100 | { |
| 101 | return new FieldCodec<long>(input => input.ReadSFixed64(), (output, value) => output.WriteSFixed64(value), CodedOutputStream.ComputeSFixed64Size, tag); |
| 102 | } |
| 103 | |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 104 | public static FieldCodec<ulong> ForUInt64(uint tag) |
| 105 | { |
| 106 | return new FieldCodec<ulong>(input => input.ReadUInt64(), (output, value) => output.WriteUInt64(value), CodedOutputStream.ComputeUInt64Size, tag); |
| 107 | } |
| 108 | |
| 109 | public static FieldCodec<float> ForFloat(uint tag) |
| 110 | { |
| 111 | return new FieldCodec<float>(input => input.ReadFloat(), (output, value) => output.WriteFloat(value), CodedOutputStream.ComputeFloatSize, tag); |
| 112 | } |
| 113 | |
| 114 | public static FieldCodec<double> ForDouble(uint tag) |
| 115 | { |
Jon Skeet | 286edc0 | 2015-06-26 20:11:34 +0100 | [diff] [blame] | 116 | return new FieldCodec<double>(input => input.ReadDouble(), (output, value) => output.WriteDouble(value), CodedOutputStream.ComputeDoubleSize, tag); |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Enums are tricky. We can probably use expression trees to build these delegates automatically, |
Jon Skeet | f34d37a | 2015-06-30 13:16:20 +0100 | [diff] [blame] | 120 | // but it's easy to generate the code for it. |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 121 | public static FieldCodec<T> ForEnum<T>(uint tag, Func<T, int> toInt32, Func<int, T> fromInt32) |
| 122 | { |
| 123 | return new FieldCodec<T>(input => fromInt32( |
| 124 | input.ReadEnum()), |
| 125 | (output, value) => output.WriteEnum(toInt32(value)), |
| 126 | value => CodedOutputStream.ComputeEnumSize(toInt32(value)), tag); |
| 127 | } |
| 128 | |
| 129 | public static FieldCodec<T> ForMessage<T>(uint tag, MessageParser<T> parser) where T : IMessage<T> |
| 130 | { |
| 131 | return new FieldCodec<T>(input => { T message = parser.CreateTemplate(); input.ReadMessage(message); return message; }, |
| 132 | (output, value) => output.WriteMessage(value), message => CodedOutputStream.ComputeMessageSize(message), tag); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /// <summary> |
| 137 | /// An encode/decode pair for a single field. This effectively encapsulates |
| 138 | /// all the information needed to read or write the field value from/to a coded |
| 139 | /// stream. |
| 140 | /// </summary> |
| 141 | /// <remarks> |
| 142 | /// This never writes default values to the stream, and is not currently designed |
| 143 | /// to play well with packed arrays. |
| 144 | /// </remarks> |
| 145 | public sealed class FieldCodec<T> |
| 146 | { |
| 147 | private static readonly Func<T, bool> IsDefault; |
| 148 | private static readonly T Default; |
| 149 | |
| 150 | static FieldCodec() |
| 151 | { |
| 152 | if (typeof(T) == typeof(string)) |
| 153 | { |
| 154 | Default = (T)(object)""; |
| 155 | IsDefault = CreateDefaultValueCheck<string>(x => x.Length == 0); |
| 156 | } |
| 157 | else if (typeof(T) == typeof(ByteString)) |
| 158 | { |
| 159 | Default = (T)(object)ByteString.Empty; |
| 160 | IsDefault = CreateDefaultValueCheck<ByteString>(x => x.Length == 0); |
| 161 | } |
| 162 | else if (!typeof(T).IsValueType) |
| 163 | { |
| 164 | // Default default |
| 165 | IsDefault = CreateDefaultValueCheck<T>(x => x == null); |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | // Default default |
| 170 | IsDefault = CreateDefaultValueCheck<T>(x => EqualityComparer<T>.Default.Equals(x, default(T))); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | private static Func<T, bool> CreateDefaultValueCheck<TTmp>(Func<TTmp, bool> check) |
| 175 | { |
| 176 | return (Func<T, bool>)(object)check; |
| 177 | } |
| 178 | |
| 179 | private readonly Func<CodedInputStream, T> reader; |
| 180 | private readonly Action<CodedOutputStream, T> writer; |
Jon Skeet | f2a27cc | 2015-06-26 17:37:14 +0100 | [diff] [blame] | 181 | private readonly Func<T, int> sizeCalculator; |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 182 | private readonly uint tag; |
| 183 | private readonly int tagSize; |
Jon Skeet | f2a27cc | 2015-06-26 17:37:14 +0100 | [diff] [blame] | 184 | private readonly int fixedSize; |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 185 | |
| 186 | internal FieldCodec( |
| 187 | Func<CodedInputStream, T> reader, |
| 188 | Action<CodedOutputStream, T> writer, |
Jon Skeet | f2a27cc | 2015-06-26 17:37:14 +0100 | [diff] [blame] | 189 | Func<T, int> sizeCalculator, |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 190 | uint tag) |
| 191 | { |
| 192 | this.reader = reader; |
| 193 | this.writer = writer; |
Jon Skeet | f2a27cc | 2015-06-26 17:37:14 +0100 | [diff] [blame] | 194 | this.sizeCalculator = sizeCalculator; |
| 195 | this.fixedSize = 0; |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 196 | this.tag = tag; |
| 197 | tagSize = CodedOutputStream.ComputeRawVarint32Size(tag); |
| 198 | } |
| 199 | |
Jon Skeet | f2a27cc | 2015-06-26 17:37:14 +0100 | [diff] [blame] | 200 | internal FieldCodec( |
| 201 | Func<CodedInputStream, T> reader, |
| 202 | Action<CodedOutputStream, T> writer, |
| 203 | int fixedSize, |
| 204 | uint tag) |
| 205 | { |
| 206 | this.reader = reader; |
| 207 | this.writer = writer; |
| 208 | this.sizeCalculator = _ => fixedSize; |
| 209 | this.fixedSize = fixedSize; |
| 210 | this.tag = tag; |
| 211 | tagSize = CodedOutputStream.ComputeRawVarint32Size(tag); |
| 212 | } |
| 213 | |
| 214 | /// <summary> |
| 215 | /// Returns the size calculator for just a value. |
| 216 | /// </summary> |
| 217 | internal Func<T, int> ValueSizeCalculator { get { return sizeCalculator; } } |
| 218 | |
| 219 | /// <summary> |
| 220 | /// Returns a delegate to write a value (unconditionally) to a coded output stream. |
| 221 | /// </summary> |
| 222 | internal Action<CodedOutputStream, T> ValueWriter { get { return writer; } } |
| 223 | |
| 224 | /// <summary> |
| 225 | /// Returns a delegate to read a value from a coded input stream. It is assumed that |
| 226 | /// the stream is already positioned on the appropriate tag. |
| 227 | /// </summary> |
| 228 | internal Func<CodedInputStream, T> ValueReader { get { return reader; } } |
| 229 | |
| 230 | /// <summary> |
| 231 | /// Returns the fixed size for an entry, or 0 if sizes vary. |
| 232 | /// </summary> |
| 233 | internal int FixedSize { get { return fixedSize; } } |
| 234 | |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 235 | public uint Tag { get { return tag; } } |
| 236 | |
| 237 | public T DefaultValue { get { return Default; } } |
| 238 | |
Jon Skeet | f2a27cc | 2015-06-26 17:37:14 +0100 | [diff] [blame] | 239 | /// <summary> |
| 240 | /// Write a tag and the given value, *if* the value is not the default. |
| 241 | /// </summary> |
| 242 | public void WriteTagAndValue(CodedOutputStream output, T value) |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 243 | { |
| 244 | if (!IsDefault(value)) |
| 245 | { |
| 246 | output.WriteTag(tag); |
| 247 | writer(output, value); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | public T Read(CodedInputStream input) |
| 252 | { |
| 253 | return reader(input); |
| 254 | } |
| 255 | |
Jon Skeet | f2a27cc | 2015-06-26 17:37:14 +0100 | [diff] [blame] | 256 | /// <summary> |
| 257 | /// Calculates the size required to write the given value, with a tag, |
| 258 | /// if the value is not the default. |
| 259 | /// </summary> |
| 260 | public int CalculateSizeWithTag(T value) |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 261 | { |
Jon Skeet | f2a27cc | 2015-06-26 17:37:14 +0100 | [diff] [blame] | 262 | return IsDefault(value) ? 0 : sizeCalculator(value) + tagSize; |
Jon Skeet | 0d684d3 | 2015-06-24 17:21:55 +0100 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | } |