source: branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs @ 13358

Last change on this file since 13358 was 13358, checked in by swagner, 7 years ago

#2520: Worked on new persistence implementation

File size: 12.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Drawing;
24
25namespace HeuristicLab.Persistence {
26  public abstract class BoxTransformer<T> : Transformer {
27    public override bool CanTransformType(Type type) {
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
143
144  #region Primitive Value Types
145  [Transformer("9FA1C6A8-517E-4623-AC1B-7E9AEF6ED13D", 200)]
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  }
150
151  [Transformer("059633E9-12CB-43EC-8544-57746536E281", 201)]
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  }
156
157  [Transformer("5DC2F3AC-0852-4B57-A861-D29CC814A94C", 202)]
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  }
162
163  [Transformer("B862E642-A94A-4870-8065-06126A35A9E1", 203)]
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  }
168
169  [Transformer("D1D3062D-F1BB-4189-AE50-D6986E1DEB4E", 204)]
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  }
174
175  [Transformer("6C444645-3062-4D15-AD01-E6E1B0692A2B", 205)]
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  }
180
181  [Transformer("649E73B2-EFA6-4E01-BCB4-4B29D652C9CB", 206)]
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  }
186
187  [Transformer("BCF25010-81A2-49BC-88CC-407D3F393D5B", 207)]
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  }
192
193  [Transformer("B6F6ACAE-755C-47EE-B8BF-7CDECBE19C30", 208)]
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  }
198
199  [Transformer("82333ACA-F041-44E0-B365-27C399594BA7", 209)]
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  }
204
205  [Transformer("8FE91ECF-2261-4934-BECD-C38923B7A703", 210)]
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  }
210
211  [Transformer("070D23EA-7F38-46B7-A667-219BEF914E49", 211)]
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  }
216
217  [Transformer("BA55C7A6-C91E-4351-A889-E4A7E647F16D", 213)]
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  }
222
223  [Transformer("0C91441B-2D97-432B-B493-D6EC483FC5AD", 214)]
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  }
228  #endregion
229
230  #region String
231  [Transformer("E75A594C-0034-4DAB-B28E-8F84F9F6DE8D", 215)]
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); }
235  }
236  #endregion
237
238  #region Enum
239  [Transformer("93FF076B-BC4B-4C39-8C40-15E004468C98", 216)]
240  internal sealed class EnumTransformer : UnsignedIntBoxTransformer<object> {
241    public override bool CanTransformType(Type type) {
242      return typeof(Enum).IsAssignableFrom(type);
243    }
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));
249    }   }
250  #endregion
251
252  #region Color
253  [Transformer("0B540EAC-79AB-40CF-8277-D2C81135FEB6", 217)]
254  internal sealed class ColorTransformers : IntBoxTransformer<Color> {
255    protected override int ToBoxType(Color value, Mapper mapper) { return value.ToArgb(); }
256    protected override Color ToValueType(int value, Type type, Mapper mapper) { return Color.FromArgb(value); }
257  }
258  #endregion
259
260  #region Type
261  [Transformer("8D17FD28-383B-44E9-9BBF-B19D351C5E38", 218)]
262  internal sealed class TypeTransformer : UnsignedIntBoxTransformer<Type> {
263    public override bool CanTransformType(Type type) {
264      return typeof(Type).IsAssignableFrom(type);
265    }
266    protected override uint ToBoxType(Type value, Mapper mapper) { return mapper.GetTypeId(value); }
267    protected override Type ToValueType(uint value, Type type, Mapper mapper) { return mapper.GetType(value); }
268  }
269  #endregion
270}
Note: See TracBrowser for help on using the repository browser.