Free cookie consent management tool by TermsFeed Policy Generator

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

#2520 worked on type and enum transformers

Location:
branches/PersistenceOverhaul
Files:
3 edited

Legend:

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

    r13387 r13407  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Drawing;
    2425using System.Linq;
    25 using HeuristicLab.PluginInfrastructure;
    2626using Google.ProtocolBuffers;
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    28 using System.Drawing;
     28using HeuristicLab.PluginInfrastructure;
    2929
    3030namespace HeuristicLab.Persistence {
     
    6868      RegisterType(new Guid("E84C326A-7E14-4F28-AEFF-BC16CC671655"), typeof(KeyValuePair<,>));
    6969      RegisterType(new Guid("F0280B55-25E8-4981-B309-D675D081402A"), typeof(string));
    70       // Enum?
    71       RegisterType(new Guid("859B7E34-3A07-41A3-AE93-79DCE5A1DB2C"), typeof(Type));
     70
    7271      RegisterType(new Guid("D15AD28B-203A-460E-815C-F7230C4B1F75"), typeof(bool[]));
    7372      RegisterType(new Guid("EE318DC4-580D-4DB1-9AAD-988B0E50A3DB"), typeof(byte[]));
  • 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
  • branches/PersistenceOverhaul/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCasesPersistenceNew.cs

    r13386 r13407  
    154154  }
    155155
    156   public enum TestEnum { va1, va2, va3, va8 } ;
     156  public enum TestEnum { va1, va2, va3, va8 };
    157157
    158158  [StorableClass("26BA37F6-926D-4665-A10A-1F39E1CF6468")]
     
    314314      Assert.AreEqual(test, result);
    315315    }
     316
     317    [TestMethod]
     318    [TestCategory("Persistence")]
     319    [TestProperty("Time", "short")]
     320    public void TestEnumSimple() {
     321      SimpleEnum se = SimpleEnum.two;
     322      ComplexEnum ce = ComplexEnum.three;
     323      ProtoBufSerializer serializer = new ProtoBufSerializer();
     324      serializer.Serialize(se, tempFile);
     325      object o = serializer.Deserialize(tempFile);
     326      SimpleEnum result = (SimpleEnum)o;
     327      Assert.AreEqual(se, result);
     328    }
     329
     330    [TestMethod]
     331    [TestCategory("Persistence")]
     332    [TestProperty("Time", "short")]
     333    public void TestEnumComplex() {
     334      ComplexEnum ce = ComplexEnum.three;
     335      ProtoBufSerializer serializer = new ProtoBufSerializer();
     336      serializer.Serialize(ce, tempFile);
     337      object o = serializer.Deserialize(tempFile);
     338      ComplexEnum result = (ComplexEnum)o;
     339      Assert.AreEqual(ce, result);
     340    }
     341
     342    [TestMethod]
     343    [TestCategory("Persistence")]
     344    [TestProperty("Time", "short")]
     345    public void TestType() {
     346      Type test = typeof(HeuristicLab.Algorithms.GeneticAlgorithm.GeneticAlgorithm);
     347      ProtoBufSerializer serializer = new ProtoBufSerializer();
     348      serializer.Serialize(test, tempFile);
     349      object o = serializer.Deserialize(tempFile);
     350      Type result = (Type)o;
     351      Assert.AreEqual(test, result);
     352    }
     353
    316354    #endregion
    317355
Note: See TracChangeset for help on using the changeset viewer.