OLD | NEW |
1 #region Copyright notice and license | 1 #region Copyright notice and license |
2 // Protocol Buffers - Google's data interchange format | 2 // Protocol Buffers - Google's data interchange format |
3 // Copyright 2015 Google Inc. All rights reserved. | 3 // Copyright 2015 Google Inc. All rights reserved. |
4 // https://developers.google.com/protocol-buffers/ | 4 // https://developers.google.com/protocol-buffers/ |
5 // | 5 // |
6 // Redistribution and use in source and binary forms, with or without | 6 // Redistribution and use in source and binary forms, with or without |
7 // modification, are permitted provided that the following conditions are | 7 // modification, are permitted provided that the following conditions are |
8 // met: | 8 // met: |
9 // | 9 // |
10 // * Redistributions of source code must retain the above copyright | 10 // * Redistributions of source code must retain the above copyright |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 /// API is very limited - just what the generated code needs to call directl
y. | 340 /// API is very limited - just what the generated code needs to call directl
y. |
341 /// </para> | 341 /// </para> |
342 /// </summary> | 342 /// </summary> |
343 /// <remarks> | 343 /// <remarks> |
344 /// This never writes default values to the stream, and does not address "pa
ckedness" | 344 /// This never writes default values to the stream, and does not address "pa
ckedness" |
345 /// in repeated fields itself, other than to know whether or not the field *
should* be packed. | 345 /// in repeated fields itself, other than to know whether or not the field *
should* be packed. |
346 /// </remarks> | 346 /// </remarks> |
347 public sealed class FieldCodec<T> | 347 public sealed class FieldCodec<T> |
348 { | 348 { |
349 private static readonly T DefaultDefault; | 349 private static readonly T DefaultDefault; |
350 private static readonly bool TypeSupportsPacking = typeof(T).IsValueType
() && Nullable.GetUnderlyingType(typeof(T)) == null; | 350 // Only non-nullable value types support packing. This is the simplest w
ay of detecting that. |
| 351 private static readonly bool TypeSupportsPacking = default(T) != null; |
351 | 352 |
352 static FieldCodec() | 353 static FieldCodec() |
353 { | 354 { |
354 if (typeof(T) == typeof(string)) | 355 if (typeof(T) == typeof(string)) |
355 { | 356 { |
356 DefaultDefault = (T)(object)""; | 357 DefaultDefault = (T)(object)""; |
357 } | 358 } |
358 else if (typeof(T) == typeof(ByteString)) | 359 else if (typeof(T) == typeof(ByteString)) |
359 { | 360 { |
360 DefaultDefault = (T)(object)ByteString.Empty; | 361 DefaultDefault = (T)(object)ByteString.Empty; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 | 465 |
465 /// <summary> | 466 /// <summary> |
466 /// Calculates the size required to write the given value, with a tag, | 467 /// Calculates the size required to write the given value, with a tag, |
467 /// if the value is not the default. | 468 /// if the value is not the default. |
468 /// </summary> | 469 /// </summary> |
469 public int CalculateSizeWithTag(T value) => IsDefault(value) ? 0 : Value
SizeCalculator(value) + tagSize; | 470 public int CalculateSizeWithTag(T value) => IsDefault(value) ? 0 : Value
SizeCalculator(value) + tagSize; |
470 | 471 |
471 private bool IsDefault(T value) => EqualityComparer<T>.Default.Equals(va
lue, DefaultValue); | 472 private bool IsDefault(T value) => EqualityComparer<T>.Default.Equals(va
lue, DefaultValue); |
472 } | 473 } |
473 } | 474 } |
OLD | NEW |