Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/24/15 15:22:21 (7 years ago)
Author:
swagner
Message:

#2520: Worked on new persistence implementation

File:
1 moved

Legend:

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

    r13326 r13358  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2222using System;
    2323using System.Drawing;
    24 using HeuristicLab.Persistence.Data;
    2524
    2625namespace HeuristicLab.Persistence {
    27   internal abstract class ValueTransformer<TSource, TTarget> : TransformerBase {
     26  public abstract class BoxTransformer<T> : Transformer {
    2827    public override bool CanTransformType(Type type) {
    29       return typeof(TSource) == type;
    30     }
    31     public override PersistenceData ToData(object o, PersistenceMapper mapper) {
    32       return GetTarget((TSource)o, mapper);
    33     }
    34 
    35     public override object ToObject(PersistenceData data, PersistenceMapper mapper) {
    36       return GetSource((ValueData<TTarget>)data, mapper);
    37     }
    38 
    39     protected abstract ValueData<TTarget> GetTarget(TSource source, PersistenceMapper mapper);
    40     protected abstract TSource GetSource(ValueData<TTarget> target, PersistenceMapper mapper);
    41   }
    42 
    43   internal abstract class ValueTypeTransformer<T> : ValueTransformer<T, T> {
    44     protected override ValueData<T> GetTarget(T source, PersistenceMapper mapper) {
    45       var data = new ValueData<T>(Id);
    46       data.TypeId = mapper.GetTypeId(source.GetType());
    47       data.Value = source;
    48       return data;
    49     }
    50     protected override T GetSource(ValueData<T> target, PersistenceMapper mapper) {
    51       return target.Value;
    52     }
    53   }
     28      return type == typeof(T);
     29    }
     30
     31    public override Box ToBox(object o, Mapper mapper) {
     32      var box = Box.CreateBuilder();
     33      box.TransformerId = mapper.GetTransformerId(this);
     34      box.TypeId = mapper.GetTypeId(o.GetType());
     35      Populate(box, (T)o, mapper);
     36      return box.Build();
     37    }
     38    public override object ToObject(Box box, Mapper mapper) {
     39      return Extract(box, mapper.GetType(box.TypeId), mapper);
     40    }
     41    protected abstract void Populate(Box.Builder box, T value, Mapper mapper);
     42    protected abstract T Extract(Box box, Type type, Mapper mapper);
     43  }
     44
     45  #region Scalar Value Boxes
     46  public abstract class BoolBoxTransformer<T> : BoxTransformer<T> {
     47    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     48      var b = BoolBox.CreateBuilder();
     49      b.Value = ToBoxType(value, mapper);
     50      box.SetExtension<BoolBox>(BoolBox.Bool, b.Build());
     51    }
     52    protected override T Extract(Box box, Type type, Mapper mapper) {
     53      return ToValueType(box.GetExtension(BoolBox.Bool).Value, type, mapper);
     54    }
     55    protected abstract bool ToBoxType(T value, Mapper mapper);
     56    protected abstract T ToValueType(bool value, Type type, Mapper mapper);
     57  }
     58  public abstract class IntBoxTransformer<T> : BoxTransformer<T> {
     59    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     60      var b = IntBox.CreateBuilder();
     61      b.Value = ToBoxType(value, mapper);
     62      box.SetExtension<IntBox>(IntBox.Int, b.Build());
     63    }
     64    protected override T Extract(Box box, Type type, Mapper mapper) {
     65      return ToValueType(box.GetExtension(IntBox.Int).Value, type, mapper);
     66    }
     67    protected abstract int ToBoxType(T value, Mapper mapper);
     68    protected abstract T ToValueType(int value, Type type, Mapper mapper);
     69  }
     70  public abstract class UnsignedIntBoxTransformer<T> : BoxTransformer<T> {
     71    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     72      var b = UnsignedIntBox.CreateBuilder();
     73      b.Value = ToBoxType(value, mapper);
     74      box.SetExtension<UnsignedIntBox>(UnsignedIntBox.UnsignedInt, b.Build());
     75    }
     76    protected override T Extract(Box box, Type type, Mapper mapper) {
     77      return ToValueType(box.GetExtension(UnsignedIntBox.UnsignedInt).Value, type, mapper);
     78    }
     79    protected abstract uint ToBoxType(T value, Mapper mapper);
     80    protected abstract T ToValueType(uint value, Type type, Mapper mapper);
     81  }
     82  public abstract class LongBoxTransformer<T> : BoxTransformer<T> {
     83    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     84      var b = LongBox.CreateBuilder();
     85      b.Value = ToBoxType(value, mapper);
     86      box.SetExtension<LongBox>(LongBox.Long, b.Build());
     87    }
     88    protected override T Extract(Box box, Type type, Mapper mapper) {
     89      return ToValueType(box.GetExtension(LongBox.Long).Value, type, mapper);
     90    }
     91    protected abstract long ToBoxType(T value, Mapper mapper);
     92    protected abstract T ToValueType(long value, Type type, Mapper mapper);
     93  }
     94  public abstract class UnsignedLongBoxTransformer<T> : BoxTransformer<T> {
     95    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     96      var b = UnsignedLongBox.CreateBuilder();
     97      b.Value = ToBoxType(value, mapper);
     98      box.SetExtension<UnsignedLongBox>(UnsignedLongBox.UnsignedLong, b.Build());
     99    }
     100    protected override T Extract(Box box, Type type, Mapper mapper) {
     101      return ToValueType(box.GetExtension(UnsignedLongBox.UnsignedLong).Value, type, mapper);
     102    }
     103    protected abstract ulong ToBoxType(T value, Mapper mapper);
     104    protected abstract T ToValueType(ulong value, Type type, Mapper mapper);
     105  }
     106  public abstract class FloatBoxTransformer<T> : BoxTransformer<T> {
     107    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     108      var b = FloatBox.CreateBuilder();
     109      b.Value = ToBoxType(value, mapper);
     110      box.SetExtension<FloatBox>(FloatBox.Float, b.Build());
     111    }
     112    protected override T Extract(Box box, Type type, Mapper mapper) {
     113      return ToValueType(box.GetExtension(FloatBox.Float).Value, type, mapper);
     114    }
     115    protected abstract float ToBoxType(T value, Mapper mapper);
     116    protected abstract T ToValueType(float value, Type type, Mapper mapper);
     117  }
     118  public abstract class DoubleBoxTransformer<T> : BoxTransformer<T> {
     119    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     120      var b = DoubleBox.CreateBuilder();
     121      b.Value = ToBoxType(value, mapper);
     122      box.SetExtension<DoubleBox>(DoubleBox.Double, b.Build());
     123    }
     124    protected override T Extract(Box box, Type type, Mapper mapper) {
     125      return ToValueType(box.GetExtension(DoubleBox.Double).Value, type, mapper);
     126    }
     127    protected abstract double ToBoxType(T value, Mapper mapper);
     128    protected abstract T ToValueType(double value, Type type, Mapper mapper);
     129  }
     130  public abstract class StringBoxTransformer<T> : BoxTransformer<T> {
     131    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
     132      var b = StringBox.CreateBuilder();
     133      b.Value = ToBoxType(value, mapper);
     134      box.SetExtension<StringBox>(StringBox.String, b.Build());
     135    }
     136    protected override T Extract(Box box, Type type, Mapper mapper) {
     137      return ToValueType(box.GetExtension(StringBox.String).Value, type, mapper);
     138    }
     139    protected abstract string ToBoxType(T value, Mapper mapper);
     140    protected abstract T ToValueType(string value, Type type, Mapper mapper);
     141  }
     142  #endregion
    54143
    55144  #region Primitive Value Types
    56145  [Transformer("9FA1C6A8-517E-4623-AC1B-7E9AEF6ED13D", 200)]
    57   internal sealed class BoolTransformer : ValueTypeTransformer<bool> { }
     146  internal sealed class BoolTransformer : BoolBoxTransformer<bool> {
     147    protected override bool ToBoxType(bool value, Mapper mapper) { return value; }
     148    protected override bool ToValueType(bool value, Type type, Mapper mapper) { return value; }
     149  }
    58150
    59151  [Transformer("059633E9-12CB-43EC-8544-57746536E281", 201)]
    60   internal sealed class ByteTransformer : ValueTypeTransformer<byte> { }
     152  internal sealed class ByteTransformer : UnsignedIntBoxTransformer<byte> {
     153    protected override uint ToBoxType(byte value, Mapper mapper) { return value; }
     154    protected override byte ToValueType(uint value, Type type, Mapper mapper) { return (byte)value; }
     155  }
    61156
    62157  [Transformer("5DC2F3AC-0852-4B57-A861-D29CC814A94C", 202)]
    63   internal sealed class SByteTransformer : ValueTypeTransformer<sbyte> { }
     158  internal sealed class SByteTransformer : IntBoxTransformer<sbyte> {
     159    protected override int ToBoxType(sbyte value, Mapper mapper) { return value; }
     160    protected override sbyte ToValueType(int value, Type type, Mapper mapper) { return (sbyte)value; }
     161  }
    64162
    65163  [Transformer("B862E642-A94A-4870-8065-06126A35A9E1", 203)]
    66   internal sealed class ShortTransformer : ValueTypeTransformer<short> { }
     164  internal sealed class ShortTransformer : IntBoxTransformer<short> {
     165    protected override int ToBoxType(short value, Mapper mapper) { return value; }
     166    protected override short ToValueType(int value, Type type, Mapper mapper) { return (short)value; }
     167  }
    67168
    68169  [Transformer("D1D3062D-F1BB-4189-AE50-D6986E1DEB4E", 204)]
    69   internal sealed class UShortTransformer : ValueTypeTransformer<ushort> { }
     170  internal sealed class UShortTransformer : UnsignedIntBoxTransformer<ushort> {
     171    protected override uint ToBoxType(ushort value, Mapper mapper) { return value; }
     172    protected override ushort ToValueType(uint value, Type type, Mapper mapper) { return (ushort)value; }
     173  }
    70174
    71175  [Transformer("6C444645-3062-4D15-AD01-E6E1B0692A2B", 205)]
    72   internal sealed class CharTransformer : ValueTypeTransformer<char> { }
     176  internal sealed class CharTransformer : UnsignedIntBoxTransformer<char> {
     177    protected override uint ToBoxType(char value, Mapper mapper) { return value; }
     178    protected override char ToValueType(uint value, Type type, Mapper mapper) { return (char)value; }
     179  }
    73180
    74181  [Transformer("649E73B2-EFA6-4E01-BCB4-4B29D652C9CB", 206)]
    75   internal sealed class IntTransformer : ValueTypeTransformer<int> { }
     182  internal sealed class IntTransformer : IntBoxTransformer<int> {
     183    protected override int ToBoxType(int value, Mapper mapper) { return value; }
     184    protected override int ToValueType(int value, Type type, Mapper mapper) { return value; }
     185  }
    76186
    77187  [Transformer("BCF25010-81A2-49BC-88CC-407D3F393D5B", 207)]
    78   internal sealed class UIntTransformer : ValueTypeTransformer<uint> { }
     188  internal sealed class UIntTransformer : UnsignedIntBoxTransformer<uint> {
     189    protected override uint ToBoxType(uint value, Mapper mapper) { return value; }
     190    protected override uint ToValueType(uint value, Type type, Mapper mapper) { return value; }
     191  }
    79192
    80193  [Transformer("B6F6ACAE-755C-47EE-B8BF-7CDECBE19C30", 208)]
    81   internal sealed class LongTransformer : ValueTypeTransformer<long> { }
     194  internal sealed class LongTransformer : LongBoxTransformer<long> {
     195    protected override long ToBoxType(long value, Mapper mapper) { return value; }
     196    protected override long ToValueType(long value, Type type, Mapper mapper) { return value; }
     197  }
    82198
    83199  [Transformer("82333ACA-F041-44E0-B365-27C399594BA7", 209)]
    84   internal sealed class ULongTransformer : ValueTypeTransformer<ulong> { }
     200  internal sealed class ULongTransformer : UnsignedLongBoxTransformer<ulong> {
     201    protected override ulong ToBoxType(ulong value, Mapper mapper) { return value; }
     202    protected override ulong ToValueType(ulong value, Type type, Mapper mapper) { return value; }
     203  }
    85204
    86205  [Transformer("8FE91ECF-2261-4934-BECD-C38923B7A703", 210)]
    87   internal sealed class FloatTransformer : ValueTypeTransformer<float> { }
     206  internal sealed class FloatTransformer : FloatBoxTransformer<float> {
     207    protected override float ToBoxType(float value, Mapper mapper) { return value; }
     208    protected override float ToValueType(float value, Type type, Mapper mapper) { return value; }
     209  }
    88210
    89211  [Transformer("070D23EA-7F38-46B7-A667-219BEF914E49", 211)]
    90   internal sealed class DoubleTransformer : ValueTypeTransformer<double> { }
    91 
    92   [Transformer("80368686-0531-4D52-B550-E772B8842E6D", 212)]
    93   internal sealed class DecimalTransformer : ValueTypeTransformer<decimal> { }
     212  internal sealed class DoubleTransformer : DoubleBoxTransformer<double> {
     213    protected override double ToBoxType(double value, Mapper mapper) { return value; }
     214    protected override double ToValueType(double value, Type type, Mapper mapper) { return value; }
     215  }
    94216
    95217  [Transformer("BA55C7A6-C91E-4351-A889-E4A7E647F16D", 213)]
    96   internal sealed class DateTimeTransformer : ValueTypeTransformer<DateTime> { }
     218  internal sealed class DateTimeTransformer : LongBoxTransformer<DateTime> {
     219    protected override long ToBoxType(DateTime value, Mapper mapper) { return value.Ticks; }
     220    protected override DateTime ToValueType(long value, Type type, Mapper mapper) { return new DateTime(value); }
     221  }
    97222
    98223  [Transformer("0C91441B-2D97-432B-B493-D6EC483FC5AD", 214)]
    99   internal sealed class TimeSpanTransformer : ValueTypeTransformer<TimeSpan> { }
     224  internal sealed class TimeSpanTransformer : LongBoxTransformer<TimeSpan> {
     225    protected override long ToBoxType(TimeSpan value, Mapper mapper) { return value.Ticks; }
     226    protected override TimeSpan ToValueType(long value, Type type, Mapper mapper) { return new TimeSpan(value); }
     227  }
    100228  #endregion
    101229
    102230  #region String
    103231  [Transformer("E75A594C-0034-4DAB-B28E-8F84F9F6DE8D", 215)]
    104   internal sealed class StringTransformer : ValueTransformer<string, uint> {
    105     protected override ValueData<uint> GetTarget(string source, PersistenceMapper mapper) {
    106       var data = new ValueData<uint>(Id);
    107       mapper.Cache(source, data);
    108       data.TypeId = mapper.GetTypeId(source.GetType());
    109       data.Value = mapper.GetStringId(source);
    110       return data;
    111     }
    112     protected override string GetSource(ValueData<uint> target, PersistenceMapper mapper) {
    113       var source = mapper.GetString(target.Value);
    114       mapper.Cache(target, source);
    115       return source;
    116     }
     232  internal sealed class StringTransformer : UnsignedIntBoxTransformer<string> {
     233    protected override uint ToBoxType(string value, Mapper mapper) { return mapper.GetStringId(value); }
     234    protected override string ToValueType(uint value, Type type, Mapper mapper) { return mapper.GetString(value); }
    117235  }
    118236  #endregion
     
    120238  #region Enum
    121239  [Transformer("93FF076B-BC4B-4C39-8C40-15E004468C98", 216)]
    122   internal sealed class EnumTransformer : ValueTransformer<object, uint> {
     240  internal sealed class EnumTransformer : UnsignedIntBoxTransformer<object> {
    123241    public override bool CanTransformType(Type type) {
    124242      return typeof(Enum).IsAssignableFrom(type);
    125243    }
    126     protected override ValueData<uint> GetTarget(object source, PersistenceMapper mapper) {
    127       var type = source.GetType();
    128       var data = new ValueData<uint>(Id);
    129       data.TypeId = mapper.GetTypeId(type);
    130       data.Value = mapper.GetStringId(Enum.Format(type, source, "G"));
    131       return data;
    132     }
    133     protected override object GetSource(ValueData<uint> target, PersistenceMapper mapper) {
    134       return Enum.Parse(mapper.GetType(target.TypeId), mapper.GetString(target.Value));
     244    protected override uint ToBoxType(object value, Mapper mapper) {
     245      return mapper.GetStringId(Enum.Format(value.GetType(), value, "G"));
     246    }
     247    protected override object ToValueType(uint value, Type type, Mapper mapper) {
     248      return Enum.Parse(type, mapper.GetString(value));
    135249    }
    136250  }
     
    139253  #region Color
    140254  [Transformer("0B540EAC-79AB-40CF-8277-D2C81135FEB6", 217)]
    141   internal sealed class ColorTransformer : ValueTransformer<Color, int> {
    142     protected override ValueData<int> GetTarget(Color source, PersistenceMapper mapper) {
    143       var data = new ValueData<int>(Id);
    144       data.TypeId = mapper.GetTypeId(source.GetType());
    145       data.Value = source.ToArgb();
    146       return data;
    147     }
    148     protected override Color GetSource(ValueData<int> target, PersistenceMapper mapper) {
    149       return Color.FromArgb(target.Value);
    150     }
     255  internal sealed class ColorTransformers : IntBoxTransformer<Color> {
     256    protected override int ToBoxType(Color value, Mapper mapper) { return value.ToArgb(); }
     257    protected override Color ToValueType(int value, Type type, Mapper mapper) { return Color.FromArgb(value); }
    151258  }
    152259  #endregion
     
    154261  #region Type
    155262  [Transformer("8D17FD28-383B-44E9-9BBF-B19D351C5E38", 218)]
    156   internal sealed class TypeTransformer : ValueTransformer<Type, ushort> {
     263  internal sealed class TypeTransformer : UnsignedIntBoxTransformer<Type> {
    157264    public override bool CanTransformType(Type type) {
    158265      return typeof(Type).IsAssignableFrom(type);
    159266    }
    160     protected override ValueData<ushort> GetTarget(Type source, PersistenceMapper mapper) {
    161       var data = new ValueData<ushort>(Id);
    162       mapper.Cache(source, data);
    163       data.Value = mapper.GetTypeId(source);
    164       return data;
    165     }
    166     protected override Type GetSource(ValueData<ushort> target, PersistenceMapper mapper) {
    167       var type = mapper.GetType(target.Value);
    168       mapper.Cache(target, type);
    169       return type;
    170     }
     267    protected override uint ToBoxType(Type value, Mapper mapper) { return mapper.GetTypeId(value); }
     268    protected override Type ToValueType(uint value, Type type, Mapper mapper) { return mapper.GetType(value); }
    171269  }
    172270  #endregion
Note: See TracChangeset for help on using the changeset viewer.