IProtoOutputT.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. namespace ProtoBuf
  3. {
  4. /// <summary>
  5. /// Represents the ability to serialize values to an output of type <typeparamref name="TOutput"/>
  6. /// </summary>
  7. public interface IProtoOutput<TOutput>
  8. {
  9. /// <summary>
  10. /// Serialize the provided value
  11. /// </summary>
  12. void Serialize<T>(TOutput destination, T value, object userState = null);
  13. }
  14. /// <summary>
  15. /// Represents the ability to serialize values to an output of type <typeparamref name="TOutput"/>
  16. /// with pre-computation of the length
  17. /// </summary>
  18. public interface IMeasuredProtoOutput<TOutput> : IProtoOutput<TOutput>
  19. {
  20. /// <summary>
  21. /// Measure the length of a value in advance of serialization
  22. /// </summary>
  23. MeasureState<T> Measure<T>(T value, object userState = null);
  24. /// <summary>
  25. /// Serialize the previously measured value
  26. /// </summary>
  27. void Serialize<T>(MeasureState<T> measured, TOutput destination);
  28. }
  29. /// <summary>
  30. /// Represents the outcome of computing the length of an object; since this may have required computing lengths
  31. /// for multiple objects, some metadata is retained so that a subsequent serialize operation using
  32. /// this instance can re-use the previously calculated lengths. If the object state changes between the
  33. /// measure and serialize operations, the behavior is undefined.
  34. /// </summary>
  35. public struct MeasureState<T> : IDisposable
  36. // note: 2.4.* does not actually implement this API;
  37. // it only advertises it for 3.* capability/feature-testing, i.e.
  38. // callers can check whether a model implements
  39. // IMeasuredProtoOutput<Foo>, and *work from that*
  40. {
  41. /// <summary>
  42. /// Releases all resources associated with this value
  43. /// </summary>
  44. public void Dispose() => throw new NotImplementedException();
  45. /// <summary>
  46. /// Gets the calculated length of this serialize operation, in bytes
  47. /// </summary>
  48. public long Length => throw new NotImplementedException();
  49. }
  50. }