Ignore:
Timestamp:
11/26/15 17:07:28 (7 years ago)
Author:
ascheibe
Message:

#2520 worked on type and enum transformers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs

    r13387 r13407  
    2020#endregion
    2121
    22 using Google.ProtocolBuffers;
    2322using System;
    2423using System.Collections.Generic;
    2524using System.Drawing;
    2625using System.Linq;
     26using Google.ProtocolBuffers;
    2727
    2828namespace HeuristicLab.Persistence {
     
    383383  #region Enum
    384384  [Transformer("93FF076B-BC4B-4C39-8C40-15E004468C98", 218)]
    385   internal sealed class EnumTransformer : UnsignedIntBoxTransformer<object> {
     385  internal sealed class EnumTransformer : Transformer {
    386386    public override bool CanTransformType(Type type) {
    387387      return typeof(Enum).IsAssignableFrom(type);
    388388    }
    389     protected override uint ToBoxType(object value, Mapper mapper) {
    390       return mapper.GetStringId(Enum.Format(value.GetType(), value, "G"));
    391     }
    392     protected override object ToValueType(uint value, Type type, Mapper mapper) {
    393       return Enum.Parse(type, mapper.GetString(value));
     389
     390    public override Box ToBox(object o, Mapper mapper) {
     391      var boxBuilder = Box.CreateBuilder();
     392      var enumBuilder = new UnsignedIntBox.Builder();
     393
     394      boxBuilder.TransformerId = mapper.GetTransformerId(this);
     395      boxBuilder.TypeId = mapper.GetStringId(o.GetType().AssemblyQualifiedName);
     396      enumBuilder.Value = mapper.GetStringId(Enum.Format(o.GetType(), o, "G"));
     397
     398      boxBuilder.SetExtension(UnsignedIntBox.UnsignedInt, enumBuilder.Build());
     399      return boxBuilder.Build();
     400    }
     401
     402    public override object ToObject(Box box, Mapper mapper) {
     403      uint value = box.GetExtension(UnsignedIntBox.UnsignedInt).Value;
     404      return Enum.Parse(Type.GetType(mapper.GetString(box.TypeId)), mapper.GetString(value));
    394405    }
    395406  }
     
    398409  #region Type
    399410  [Transformer("8D17FD28-383B-44E9-9BBF-B19D351C5E38", 219)]
    400   internal sealed class TypeTransformer : UnsignedIntBoxTransformer<Type> {
     411  internal sealed class TypeTransformer : Transformer {
    401412    public override bool CanTransformType(Type type) {
    402413      return typeof(Type).IsAssignableFrom(type);
    403414    }
    404     protected override uint ToBoxType(Type value, Mapper mapper) { return mapper.GetTypeId(value); }
    405     protected override Type ToValueType(uint value, Type type, Mapper mapper) { return mapper.GetType(value); }
     415    public override Box ToBox(object o, Mapper mapper) {
     416      var boxBuilder = Box.CreateBuilder();
     417
     418      boxBuilder.TransformerId = mapper.GetTransformerId(this);
     419      boxBuilder.TypeId = mapper.GetStringId(((Type)o).AssemblyQualifiedName);
     420
     421      return boxBuilder.Build();
     422    }
     423
     424    public override object ToObject(Box box, Mapper mapper) {
     425      return Type.GetType(mapper.GetString(box.TypeId));
     426    }
    406427  }
    407428  #endregion
Note: See TracChangeset for help on using the changeset viewer.