CompiledSerializer.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #if FEAT_COMPILER && !(FX11 || FEAT_IKVM)
  2. using System;
  3. using ProtoBuf.Meta;
  4. namespace ProtoBuf.Serializers
  5. {
  6. sealed class CompiledSerializer : IProtoTypeSerializer
  7. {
  8. bool IProtoTypeSerializer.HasCallbacks(TypeModel.CallbackType callbackType)
  9. {
  10. return head.HasCallbacks(callbackType); // these routes only used when bits of the model not compiled
  11. }
  12. bool IProtoTypeSerializer.CanCreateInstance()
  13. {
  14. return head.CanCreateInstance();
  15. }
  16. object IProtoTypeSerializer.CreateInstance(ProtoReader source)
  17. {
  18. return head.CreateInstance(source);
  19. }
  20. public void Callback(object value, TypeModel.CallbackType callbackType, SerializationContext context)
  21. {
  22. head.Callback(value, callbackType, context); // these routes only used when bits of the model not compiled
  23. }
  24. public static CompiledSerializer Wrap(IProtoTypeSerializer head, TypeModel model)
  25. {
  26. CompiledSerializer result = head as CompiledSerializer;
  27. if (result == null)
  28. {
  29. result = new CompiledSerializer(head, model);
  30. Helpers.DebugAssert(((IProtoTypeSerializer)result).ExpectedType == head.ExpectedType);
  31. }
  32. return result;
  33. }
  34. private readonly IProtoTypeSerializer head;
  35. private readonly Compiler.ProtoSerializer serializer;
  36. private readonly Compiler.ProtoDeserializer deserializer;
  37. private CompiledSerializer(IProtoTypeSerializer head, TypeModel model)
  38. {
  39. this.head = head;
  40. serializer = Compiler.CompilerContext.BuildSerializer(head, model);
  41. deserializer = Compiler.CompilerContext.BuildDeserializer(head, model);
  42. }
  43. bool IProtoSerializer.RequiresOldValue { get { return head.RequiresOldValue; } }
  44. bool IProtoSerializer.ReturnsValue { get { return head.ReturnsValue; } }
  45. Type IProtoSerializer.ExpectedType { get { return head.ExpectedType; } }
  46. void IProtoSerializer.Write(object value, ProtoWriter dest)
  47. {
  48. serializer(value, dest);
  49. }
  50. object IProtoSerializer.Read(object value, ProtoReader source)
  51. {
  52. return deserializer(value, source);
  53. }
  54. void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  55. {
  56. head.EmitWrite(ctx, valueFrom);
  57. }
  58. void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  59. {
  60. head.EmitRead(ctx, valueFrom);
  61. }
  62. void IProtoTypeSerializer.EmitCallback(Compiler.CompilerContext ctx, Compiler.Local valueFrom, TypeModel.CallbackType callbackType)
  63. {
  64. head.EmitCallback(ctx, valueFrom, callbackType);
  65. }
  66. void IProtoTypeSerializer.EmitCreateInstance(Compiler.CompilerContext ctx)
  67. {
  68. head.EmitCreateInstance(ctx);
  69. }
  70. }
  71. }
  72. #endif