FieldCodec.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. using Google.Protobuf.WellKnownTypes;
  33. using System;
  34. using System.Collections.Generic;
  35. namespace Google.Protobuf
  36. {
  37. /// <summary>
  38. /// Factory methods for <see cref="FieldCodec{T}"/>.
  39. /// </summary>
  40. public static class FieldCodec
  41. {
  42. // TODO: Avoid the "dual hit" of lambda expressions: create open delegates instead. (At least test...)
  43. /// <summary>
  44. /// Retrieves a codec suitable for a string field with the given tag.
  45. /// </summary>
  46. /// <param name="tag">The tag.</param>
  47. /// <returns>A codec for the given tag.</returns>
  48. public static FieldCodec<string> ForString(uint tag)
  49. {
  50. return new FieldCodec<string>(input => input.ReadString(), (output, value) => output.WriteString(value), CodedOutputStream.ComputeStringSize, tag);
  51. }
  52. /// <summary>
  53. /// Retrieves a codec suitable for a bytes field with the given tag.
  54. /// </summary>
  55. /// <param name="tag">The tag.</param>
  56. /// <returns>A codec for the given tag.</returns>
  57. public static FieldCodec<ByteString> ForBytes(uint tag)
  58. {
  59. return new FieldCodec<ByteString>(input => input.ReadBytes(), (output, value) => output.WriteBytes(value), CodedOutputStream.ComputeBytesSize, tag);
  60. }
  61. /// <summary>
  62. /// Retrieves a codec suitable for a bool field with the given tag.
  63. /// </summary>
  64. /// <param name="tag">The tag.</param>
  65. /// <returns>A codec for the given tag.</returns>
  66. public static FieldCodec<bool> ForBool(uint tag)
  67. {
  68. return new FieldCodec<bool>(input => input.ReadBool(), (output, value) => output.WriteBool(value), CodedOutputStream.ComputeBoolSize, tag);
  69. }
  70. /// <summary>
  71. /// Retrieves a codec suitable for an int32 field with the given tag.
  72. /// </summary>
  73. /// <param name="tag">The tag.</param>
  74. /// <returns>A codec for the given tag.</returns>
  75. public static FieldCodec<int> ForInt32(uint tag)
  76. {
  77. return new FieldCodec<int>(input => input.ReadInt32(), (output, value) => output.WriteInt32(value), CodedOutputStream.ComputeInt32Size, tag);
  78. }
  79. /// <summary>
  80. /// Retrieves a codec suitable for an sint32 field with the given tag.
  81. /// </summary>
  82. /// <param name="tag">The tag.</param>
  83. /// <returns>A codec for the given tag.</returns>
  84. public static FieldCodec<int> ForSInt32(uint tag)
  85. {
  86. return new FieldCodec<int>(input => input.ReadSInt32(), (output, value) => output.WriteSInt32(value), CodedOutputStream.ComputeSInt32Size, tag);
  87. }
  88. /// <summary>
  89. /// Retrieves a codec suitable for a fixed32 field with the given tag.
  90. /// </summary>
  91. /// <param name="tag">The tag.</param>
  92. /// <returns>A codec for the given tag.</returns>
  93. public static FieldCodec<uint> ForFixed32(uint tag)
  94. {
  95. return new FieldCodec<uint>(input => input.ReadFixed32(), (output, value) => output.WriteFixed32(value), 4, tag);
  96. }
  97. /// <summary>
  98. /// Retrieves a codec suitable for an sfixed32 field with the given tag.
  99. /// </summary>
  100. /// <param name="tag">The tag.</param>
  101. /// <returns>A codec for the given tag.</returns>
  102. public static FieldCodec<int> ForSFixed32(uint tag)
  103. {
  104. return new FieldCodec<int>(input => input.ReadSFixed32(), (output, value) => output.WriteSFixed32(value), 4, tag);
  105. }
  106. /// <summary>
  107. /// Retrieves a codec suitable for a uint32 field with the given tag.
  108. /// </summary>
  109. /// <param name="tag">The tag.</param>
  110. /// <returns>A codec for the given tag.</returns>
  111. public static FieldCodec<uint> ForUInt32(uint tag)
  112. {
  113. return new FieldCodec<uint>(input => input.ReadUInt32(), (output, value) => output.WriteUInt32(value), CodedOutputStream.ComputeUInt32Size, tag);
  114. }
  115. /// <summary>
  116. /// Retrieves a codec suitable for an int64 field with the given tag.
  117. /// </summary>
  118. /// <param name="tag">The tag.</param>
  119. /// <returns>A codec for the given tag.</returns>
  120. public static FieldCodec<long> ForInt64(uint tag)
  121. {
  122. return new FieldCodec<long>(input => input.ReadInt64(), (output, value) => output.WriteInt64(value), CodedOutputStream.ComputeInt64Size, tag);
  123. }
  124. /// <summary>
  125. /// Retrieves a codec suitable for an sint64 field with the given tag.
  126. /// </summary>
  127. /// <param name="tag">The tag.</param>
  128. /// <returns>A codec for the given tag.</returns>
  129. public static FieldCodec<long> ForSInt64(uint tag)
  130. {
  131. return new FieldCodec<long>(input => input.ReadSInt64(), (output, value) => output.WriteSInt64(value), CodedOutputStream.ComputeSInt64Size, tag);
  132. }
  133. /// <summary>
  134. /// Retrieves a codec suitable for a fixed64 field with the given tag.
  135. /// </summary>
  136. /// <param name="tag">The tag.</param>
  137. /// <returns>A codec for the given tag.</returns>
  138. public static FieldCodec<ulong> ForFixed64(uint tag)
  139. {
  140. return new FieldCodec<ulong>(input => input.ReadFixed64(), (output, value) => output.WriteFixed64(value), 8, tag);
  141. }
  142. /// <summary>
  143. /// Retrieves a codec suitable for an sfixed64 field with the given tag.
  144. /// </summary>
  145. /// <param name="tag">The tag.</param>
  146. /// <returns>A codec for the given tag.</returns>
  147. public static FieldCodec<long> ForSFixed64(uint tag)
  148. {
  149. return new FieldCodec<long>(input => input.ReadSFixed64(), (output, value) => output.WriteSFixed64(value), 8, tag);
  150. }
  151. /// <summary>
  152. /// Retrieves a codec suitable for a uint64 field with the given tag.
  153. /// </summary>
  154. /// <param name="tag">The tag.</param>
  155. /// <returns>A codec for the given tag.</returns>
  156. public static FieldCodec<ulong> ForUInt64(uint tag)
  157. {
  158. return new FieldCodec<ulong>(input => input.ReadUInt64(), (output, value) => output.WriteUInt64(value), CodedOutputStream.ComputeUInt64Size, tag);
  159. }
  160. /// <summary>
  161. /// Retrieves a codec suitable for a float field with the given tag.
  162. /// </summary>
  163. /// <param name="tag">The tag.</param>
  164. /// <returns>A codec for the given tag.</returns>
  165. public static FieldCodec<float> ForFloat(uint tag)
  166. {
  167. return new FieldCodec<float>(input => input.ReadFloat(), (output, value) => output.WriteFloat(value), CodedOutputStream.ComputeFloatSize, tag);
  168. }
  169. /// <summary>
  170. /// Retrieves a codec suitable for a double field with the given tag.
  171. /// </summary>
  172. /// <param name="tag">The tag.</param>
  173. /// <returns>A codec for the given tag.</returns>
  174. public static FieldCodec<double> ForDouble(uint tag)
  175. {
  176. return new FieldCodec<double>(input => input.ReadDouble(), (output, value) => output.WriteDouble(value), CodedOutputStream.ComputeDoubleSize, tag);
  177. }
  178. // Enums are tricky. We can probably use expression trees to build these delegates automatically,
  179. // but it's easy to generate the code for it.
  180. /// <summary>
  181. /// Retrieves a codec suitable for an enum field with the given tag.
  182. /// </summary>
  183. /// <param name="tag">The tag.</param>
  184. /// <param name="toInt32">A conversion function from <see cref="Int32"/> to the enum type.</param>
  185. /// <param name="fromInt32">A conversion function from the enum type to <see cref="Int32"/>.</param>
  186. /// <returns>A codec for the given tag.</returns>
  187. public static FieldCodec<T> ForEnum<T>(uint tag, Func<T, int> toInt32, Func<int, T> fromInt32)
  188. {
  189. return new FieldCodec<T>(input => fromInt32(
  190. input.ReadEnum()),
  191. (output, value) => output.WriteEnum(toInt32(value)),
  192. value => CodedOutputStream.ComputeEnumSize(toInt32(value)), tag);
  193. }
  194. /// <summary>
  195. /// Retrieves a codec suitable for a message field with the given tag.
  196. /// </summary>
  197. /// <param name="tag">The tag.</param>
  198. /// <param name="parser">A parser to use for the message type.</param>
  199. /// <returns>A codec for the given tag.</returns>
  200. public static FieldCodec<T> ForMessage<T>(uint tag, MessageParser<T> parser) where T : IMessage<T>
  201. {
  202. return new FieldCodec<T>(input => { T message = parser.CreateTemplate(); input.ReadMessage(message); return message; },
  203. (output, value) => output.WriteMessage(value), message => CodedOutputStream.ComputeMessageSize(message), tag);
  204. }
  205. /// <summary>
  206. /// Creates a codec for a wrapper type of a class - which must be string or ByteString.
  207. /// </summary>
  208. public static FieldCodec<T> ForClassWrapper<T>(uint tag) where T : class
  209. {
  210. var nestedCodec = WrapperCodecs.GetCodec<T>();
  211. return new FieldCodec<T>(
  212. input => WrapperCodecs.Read<T>(input, nestedCodec),
  213. (output, value) => WrapperCodecs.Write<T>(output, value, nestedCodec),
  214. value => WrapperCodecs.CalculateSize<T>(value, nestedCodec),
  215. tag,
  216. null); // Default value for the wrapper
  217. }
  218. /// <summary>
  219. /// Creates a codec for a wrapper type of a struct - which must be Int32, Int64, UInt32, UInt64,
  220. /// Bool, Single or Double.
  221. /// </summary>
  222. public static FieldCodec<T?> ForStructWrapper<T>(uint tag) where T : struct
  223. {
  224. var nestedCodec = WrapperCodecs.GetCodec<T>();
  225. return new FieldCodec<T?>(
  226. input => WrapperCodecs.Read<T>(input, nestedCodec),
  227. (output, value) => WrapperCodecs.Write<T>(output, value.Value, nestedCodec),
  228. value => value == null ? 0 : WrapperCodecs.CalculateSize<T>(value.Value, nestedCodec),
  229. tag,
  230. null); // Default value for the wrapper
  231. }
  232. /// <summary>
  233. /// Helper code to create codecs for wrapper types.
  234. /// </summary>
  235. /// <remarks>
  236. /// Somewhat ugly with all the static methods, but the conversions involved to/from nullable types make it
  237. /// slightly tricky to improve. So long as we keep the public API (ForClassWrapper, ForStructWrapper) in place,
  238. /// we can refactor later if we come up with something cleaner.
  239. /// </remarks>
  240. private static class WrapperCodecs
  241. {
  242. private static readonly Dictionary<System.Type, object> Codecs = new Dictionary<System.Type, object>
  243. {
  244. { typeof(bool), ForBool(WireFormat.MakeTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.Varint)) },
  245. { typeof(int), ForInt32(WireFormat.MakeTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.Varint)) },
  246. { typeof(long), ForInt64(WireFormat.MakeTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.Varint)) },
  247. { typeof(uint), ForUInt32(WireFormat.MakeTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.Varint)) },
  248. { typeof(ulong), ForUInt64(WireFormat.MakeTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.Varint)) },
  249. { typeof(float), ForFloat(WireFormat.MakeTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.Fixed32)) },
  250. { typeof(double), ForDouble(WireFormat.MakeTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.Fixed64)) },
  251. { typeof(string), ForString(WireFormat.MakeTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.LengthDelimited)) },
  252. { typeof(ByteString), ForBytes(WireFormat.MakeTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.LengthDelimited)) }
  253. };
  254. /// <summary>
  255. /// Returns a field codec which effectively wraps a value of type T in a message.
  256. ///
  257. /// </summary>
  258. internal static FieldCodec<T> GetCodec<T>()
  259. {
  260. object value;
  261. if (!Codecs.TryGetValue(typeof(T), out value))
  262. {
  263. throw new InvalidOperationException("Invalid type argument requested for wrapper codec: " + typeof(T));
  264. }
  265. return (FieldCodec<T>) value;
  266. }
  267. internal static T Read<T>(CodedInputStream input, FieldCodec<T> codec)
  268. {
  269. int length = input.ReadLength();
  270. int oldLimit = input.PushLimit(length);
  271. uint tag;
  272. T value = codec.DefaultValue;
  273. while ((tag = input.ReadTag()) != 0)
  274. {
  275. if (tag == codec.Tag)
  276. {
  277. value = codec.Read(input);
  278. }
  279. else
  280. {
  281. input.SkipLastField();
  282. }
  283. }
  284. input.CheckReadEndOfStreamTag();
  285. input.PopLimit(oldLimit);
  286. return value;
  287. }
  288. internal static void Write<T>(CodedOutputStream output, T value, FieldCodec<T> codec)
  289. {
  290. output.WriteLength(codec.CalculateSizeWithTag(value));
  291. codec.WriteTagAndValue(output, value);
  292. }
  293. internal static int CalculateSize<T>(T value, FieldCodec<T> codec)
  294. {
  295. int fieldLength = codec.CalculateSizeWithTag(value);
  296. return CodedOutputStream.ComputeLengthSize(fieldLength) + fieldLength;
  297. }
  298. }
  299. }
  300. /// <summary>
  301. /// An encode/decode pair for a single field. This effectively encapsulates
  302. /// all the information needed to read or write the field value from/to a coded
  303. /// stream.
  304. /// </summary>
  305. /// <remarks>
  306. /// This never writes default values to the stream, and is not currently designed
  307. /// to play well with packed arrays.
  308. /// </remarks>
  309. public sealed class FieldCodec<T>
  310. {
  311. private static readonly T DefaultDefault;
  312. static FieldCodec()
  313. {
  314. if (typeof(T) == typeof(string))
  315. {
  316. DefaultDefault = (T)(object)"";
  317. }
  318. else if (typeof(T) == typeof(ByteString))
  319. {
  320. DefaultDefault = (T)(object)ByteString.Empty;
  321. }
  322. // Otherwise it's the default value of the CLR type
  323. }
  324. private static Func<T, bool> CreateDefaultValueCheck<TTmp>(Func<TTmp, bool> check)
  325. {
  326. return (Func<T, bool>)(object)check;
  327. }
  328. private readonly Func<CodedInputStream, T> reader;
  329. private readonly Action<CodedOutputStream, T> writer;
  330. private readonly Func<T, int> sizeCalculator;
  331. private readonly uint tag;
  332. private readonly int tagSize;
  333. private readonly int fixedSize;
  334. // Default value for this codec. Usually the same for every instance of the same type, but
  335. // for string/ByteString wrapper fields the codec's default value is null, whereas for
  336. // other string/ByteString fields it's "" or ByteString.Empty.
  337. private readonly T defaultValue;
  338. internal FieldCodec(
  339. Func<CodedInputStream, T> reader,
  340. Action<CodedOutputStream, T> writer,
  341. Func<T, int> sizeCalculator,
  342. uint tag) : this(reader, writer, sizeCalculator, tag, DefaultDefault)
  343. {
  344. }
  345. internal FieldCodec(
  346. Func<CodedInputStream, T> reader,
  347. Action<CodedOutputStream, T> writer,
  348. Func<T, int> sizeCalculator,
  349. uint tag,
  350. T defaultValue)
  351. {
  352. this.reader = reader;
  353. this.writer = writer;
  354. this.sizeCalculator = sizeCalculator;
  355. this.fixedSize = 0;
  356. this.tag = tag;
  357. this.defaultValue = defaultValue;
  358. tagSize = CodedOutputStream.ComputeRawVarint32Size(tag);
  359. }
  360. internal FieldCodec(
  361. Func<CodedInputStream, T> reader,
  362. Action<CodedOutputStream, T> writer,
  363. int fixedSize,
  364. uint tag)
  365. {
  366. this.reader = reader;
  367. this.writer = writer;
  368. this.sizeCalculator = _ => fixedSize;
  369. this.fixedSize = fixedSize;
  370. this.tag = tag;
  371. tagSize = CodedOutputStream.ComputeRawVarint32Size(tag);
  372. }
  373. /// <summary>
  374. /// Returns the size calculator for just a value.
  375. /// </summary>
  376. internal Func<T, int> ValueSizeCalculator { get { return sizeCalculator; } }
  377. /// <summary>
  378. /// Returns a delegate to write a value (unconditionally) to a coded output stream.
  379. /// </summary>
  380. internal Action<CodedOutputStream, T> ValueWriter { get { return writer; } }
  381. /// <summary>
  382. /// Returns a delegate to read a value from a coded input stream. It is assumed that
  383. /// the stream is already positioned on the appropriate tag.
  384. /// </summary>
  385. internal Func<CodedInputStream, T> ValueReader { get { return reader; } }
  386. /// <summary>
  387. /// Returns the fixed size for an entry, or 0 if sizes vary.
  388. /// </summary>
  389. internal int FixedSize { get { return fixedSize; } }
  390. /// <summary>
  391. /// Gets the tag of the codec.
  392. /// </summary>
  393. /// <value>
  394. /// The tag of the codec.
  395. /// </value>
  396. public uint Tag { get { return tag; } }
  397. /// <summary>
  398. /// Gets the default value of the codec's type.
  399. /// </summary>
  400. /// <value>
  401. /// The default value of the codec's type.
  402. /// </value>
  403. public T DefaultValue { get { return defaultValue; } }
  404. /// <summary>
  405. /// Write a tag and the given value, *if* the value is not the default.
  406. /// </summary>
  407. public void WriteTagAndValue(CodedOutputStream output, T value)
  408. {
  409. if (!IsDefault(value))
  410. {
  411. output.WriteTag(tag);
  412. writer(output, value);
  413. }
  414. }
  415. /// <summary>
  416. /// Reads a value of the codec type from the given <see cref="CodedInputStream"/>.
  417. /// </summary>
  418. /// <param name="input">The input stream to read from.</param>
  419. /// <returns>The value read from the stream.</returns>
  420. public T Read(CodedInputStream input)
  421. {
  422. return reader(input);
  423. }
  424. /// <summary>
  425. /// Calculates the size required to write the given value, with a tag,
  426. /// if the value is not the default.
  427. /// </summary>
  428. public int CalculateSizeWithTag(T value)
  429. {
  430. return IsDefault(value) ? 0 : sizeCalculator(value) + tagSize;
  431. }
  432. private bool IsDefault(T value)
  433. {
  434. return EqualityComparer<T>.Default.Equals(value, defaultValue);
  435. }
  436. }
  437. }