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 // Only non-nullable value types support packing. This is the simplest w
ay of detecting that. | 350 private static readonly bool TypeSupportsPacking = typeof(T).IsValueType
() && Nullable.GetUnderlyingType(typeof(T)) == null; |
351 private static readonly bool TypeSupportsPacking = default(T) != null; | |
352 | 351 |
353 static FieldCodec() | 352 static FieldCodec() |
354 { | 353 { |
355 if (typeof(T) == typeof(string)) | 354 if (typeof(T) == typeof(string)) |
356 { | 355 { |
357 DefaultDefault = (T)(object)""; | 356 DefaultDefault = (T)(object)""; |
358 } | 357 } |
359 else if (typeof(T) == typeof(ByteString)) | 358 else if (typeof(T) == typeof(ByteString)) |
360 { | 359 { |
361 DefaultDefault = (T)(object)ByteString.Empty; | 360 DefaultDefault = (T)(object)ByteString.Empty; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 | 464 |
466 /// <summary> | 465 /// <summary> |
467 /// Calculates the size required to write the given value, with a tag, | 466 /// Calculates the size required to write the given value, with a tag, |
468 /// if the value is not the default. | 467 /// if the value is not the default. |
469 /// </summary> | 468 /// </summary> |
470 public int CalculateSizeWithTag(T value) => IsDefault(value) ? 0 : Value
SizeCalculator(value) + tagSize; | 469 public int CalculateSizeWithTag(T value) => IsDefault(value) ? 0 : Value
SizeCalculator(value) + tagSize; |
471 | 470 |
472 private bool IsDefault(T value) => EqualityComparer<T>.Default.Equals(va
lue, DefaultValue); | 471 private bool IsDefault(T value) => EqualityComparer<T>.Default.Equals(va
lue, DefaultValue); |
473 } | 472 } |
474 } | 473 } |
OLD | NEW |