SingleSerializer.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #if !NO_RUNTIME
  2. using System;
  3. using ProtoBuf.Meta;
  4. #if FEAT_IKVM
  5. using Type = IKVM.Reflection.Type;
  6. using IKVM.Reflection;
  7. #else
  8. #endif
  9. namespace ProtoBuf.Serializers
  10. {
  11. sealed class SingleSerializer : IProtoSerializer
  12. {
  13. #if FEAT_IKVM
  14. readonly Type expectedType;
  15. #else
  16. static readonly Type expectedType = typeof(float);
  17. #endif
  18. public Type ExpectedType { get { return expectedType; } }
  19. public SingleSerializer(TypeModel model)
  20. {
  21. #if FEAT_IKVM
  22. expectedType = model.MapType(typeof(float));
  23. #endif
  24. }
  25. bool IProtoSerializer.RequiresOldValue { get { return false; } }
  26. bool IProtoSerializer.ReturnsValue { get { return true; } }
  27. #if !FEAT_IKVM
  28. public object Read(object value, ProtoReader source)
  29. {
  30. Helpers.DebugAssert(value == null); // since replaces
  31. return source.ReadSingle();
  32. }
  33. public void Write(object value, ProtoWriter dest)
  34. {
  35. ProtoWriter.WriteSingle((float)value, dest);
  36. }
  37. #endif
  38. #if FEAT_COMPILER
  39. void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  40. {
  41. ctx.EmitBasicWrite("WriteSingle", valueFrom);
  42. }
  43. void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  44. {
  45. ctx.EmitBasicRead("ReadSingle", ExpectedType);
  46. }
  47. #endif
  48. }
  49. }
  50. #endif