Quasar/Server/Core/ProtoBuf/Serializers/SingleSerializer.cs

70 lines
1.5 KiB
C#

#if !NO_RUNTIME
using System;
using ProtoBuf.Meta;
#if FEAT_IKVM
using Type = IKVM.Reflection.Type;
using IKVM.Reflection;
#else
using System.Reflection;
#endif
namespace ProtoBuf.Serializers
{
internal sealed class SingleSerializer : IProtoSerializer
{
#if FEAT_IKVM
readonly Type expectedType;
#else
private static readonly Type expectedType = typeof (float);
#endif
public Type ExpectedType
{
get { return expectedType; }
}
public SingleSerializer(TypeModel model)
{
#if FEAT_IKVM
expectedType = model.MapType(typeof(float));
#endif
}
bool IProtoSerializer.RequiresOldValue
{
get { return false; }
}
bool IProtoSerializer.ReturnsValue
{
get { return true; }
}
#if !FEAT_IKVM
public object Read(object value, ProtoReader source)
{
Helpers.DebugAssert(value == null); // since replaces
return source.ReadSingle();
}
public void Write(object value, ProtoWriter dest)
{
ProtoWriter.WriteSingle((float) value, dest);
}
#endif
#if FEAT_COMPILER
void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
{
ctx.EmitBasicWrite("WriteSingle", valueFrom);
}
void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
{
ctx.EmitBasicRead("ReadSingle", ExpectedType);
}
#endif
}
}
#endif