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

Last change on this file since 14537 was 14537, checked in by jkarder, 5 years ago

#2520: worked on persistence

File size: 35.8 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.Collections;
24using System.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using Google.ProtocolBuffers;
28
29namespace HeuristicLab.Persistence {
30  public abstract class BoxTransformer<T> : Transformer {
31    public override bool CanTransformType(Type type) {
32      return type == typeof(T);
33    }
34
35    public override Box ToBox(object o, Mapper mapper) {
36      var box = Box.CreateBuilder();
37      box.TransformerId = mapper.GetTransformerId(this);
38      box.TypeId = mapper.GetTypeId(o.GetType());
39      Populate(box, (T)o, mapper);
40      return box.Build();
41    }
42    public override object ToObject(Box box, Mapper mapper) {
43      return Extract(box, mapper.GetType(box.TypeId), mapper);
44    }
45    protected abstract void Populate(Box.Builder box, T value, Mapper mapper);
46    protected abstract T Extract(Box box, Type type, Mapper mapper);
47  }
48
49  #region Scalar Box Transformers
50  public abstract class BoolBoxTransformer<T> : BoxTransformer<T> {
51    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
52      var b = BoolBox.CreateBuilder();
53      b.Value = ToBoxType(value, mapper);
54      box.SetExtension<BoolBox>(BoolBox.Bool, b.Build());
55    }
56    protected override T Extract(Box box, Type type, Mapper mapper) {
57      return ToValueType(box.GetExtension(BoolBox.Bool).Value, type, mapper);
58    }
59    protected abstract bool ToBoxType(T value, Mapper mapper);
60    protected abstract T ToValueType(bool value, Type type, Mapper mapper);
61  }
62  public abstract class IntBoxTransformer<T> : BoxTransformer<T> {
63    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
64      var b = IntBox.CreateBuilder();
65      b.Value = ToBoxType(value, mapper);
66      box.SetExtension<IntBox>(IntBox.Int, b.Build());
67    }
68    protected override T Extract(Box box, Type type, Mapper mapper) {
69      return ToValueType(box.GetExtension(IntBox.Int).Value, type, mapper);
70    }
71    protected abstract int ToBoxType(T value, Mapper mapper);
72    protected abstract T ToValueType(int value, Type type, Mapper mapper);
73  }
74  public abstract class LongBoxTransformer<T> : BoxTransformer<T> {
75    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
76      var b = LongBox.CreateBuilder();
77      b.Value = ToBoxType(value, mapper);
78      box.SetExtension<LongBox>(LongBox.Long, b.Build());
79    }
80    protected override T Extract(Box box, Type type, Mapper mapper) {
81      return ToValueType(box.GetExtension(LongBox.Long).Value, type, mapper);
82    }
83    protected abstract long ToBoxType(T value, Mapper mapper);
84    protected abstract T ToValueType(long value, Type type, Mapper mapper);
85  }
86  public abstract class UnsignedIntBoxTransformer<T> : BoxTransformer<T> {
87    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
88      var b = UnsignedIntBox.CreateBuilder();
89      b.Value = ToBoxType(value, mapper);
90      box.SetExtension<UnsignedIntBox>(UnsignedIntBox.UnsignedInt, b.Build());
91    }
92    protected override T Extract(Box box, Type type, Mapper mapper) {
93      return ToValueType(box.GetExtension(UnsignedIntBox.UnsignedInt).Value, type, mapper);
94    }
95    protected abstract uint ToBoxType(T value, Mapper mapper);
96    protected abstract T ToValueType(uint value, Type type, Mapper mapper);
97  }
98  public abstract class UnsignedLongBoxTransformer<T> : BoxTransformer<T> {
99    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
100      var b = UnsignedLongBox.CreateBuilder();
101      b.Value = ToBoxType(value, mapper);
102      box.SetExtension<UnsignedLongBox>(UnsignedLongBox.UnsignedLong, b.Build());
103    }
104    protected override T Extract(Box box, Type type, Mapper mapper) {
105      return ToValueType(box.GetExtension(UnsignedLongBox.UnsignedLong).Value, type, mapper);
106    }
107    protected abstract ulong ToBoxType(T value, Mapper mapper);
108    protected abstract T ToValueType(ulong value, Type type, Mapper mapper);
109  }
110  public abstract class FloatBoxTransformer<T> : BoxTransformer<T> {
111    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
112      var b = FloatBox.CreateBuilder();
113      b.Value = ToBoxType(value, mapper);
114      box.SetExtension<FloatBox>(FloatBox.Float, b.Build());
115    }
116    protected override T Extract(Box box, Type type, Mapper mapper) {
117      return ToValueType(box.GetExtension(FloatBox.Float).Value, type, mapper);
118    }
119    protected abstract float ToBoxType(T value, Mapper mapper);
120    protected abstract T ToValueType(float value, Type type, Mapper mapper);
121  }
122  public abstract class DoubleBoxTransformer<T> : BoxTransformer<T> {
123    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
124      var b = DoubleBox.CreateBuilder();
125      b.Value = ToBoxType(value, mapper);
126      box.SetExtension<DoubleBox>(DoubleBox.Double, b.Build());
127    }
128    protected override T Extract(Box box, Type type, Mapper mapper) {
129      return ToValueType(box.GetExtension(DoubleBox.Double).Value, type, mapper);
130    }
131    protected abstract double ToBoxType(T value, Mapper mapper);
132    protected abstract T ToValueType(double value, Type type, Mapper mapper);
133  }
134  public abstract class StringBoxTransformer<T> : BoxTransformer<T> {
135    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
136      var b = StringBox.CreateBuilder();
137      b.Value = ToBoxType(value, mapper);
138      box.SetExtension<StringBox>(StringBox.String, b.Build());
139    }
140    protected override T Extract(Box box, Type type, Mapper mapper) {
141      return ToValueType(box.GetExtension(StringBox.String).Value, type, mapper);
142    }
143    protected abstract string ToBoxType(T value, Mapper mapper);
144    protected abstract T ToValueType(string value, Type type, Mapper mapper);
145  }
146  public abstract class BytesBoxTransformer<T> : BoxTransformer<T> {
147    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
148      var b = BytesBox.CreateBuilder();
149      b.Value = ByteString.CopyFrom(ToBoxType(value, mapper));
150      box.SetExtension<BytesBox>(BytesBox.Bytes, b.Build());
151    }
152    protected override T Extract(Box box, Type type, Mapper mapper) {
153      return ToValueType(box.GetExtension(BytesBox.Bytes).Value.ToByteArray(), type, mapper);
154    }
155    protected abstract byte[] ToBoxType(T value, Mapper mapper);
156    protected abstract T ToValueType(byte[] value, Type type, Mapper mapper);
157  }
158  #endregion
159
160  #region Array Box Transformers
161  public abstract class ByteArrayBoxTransformer<T> : BoxTransformer<T> {
162    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
163      var b = ByteArrayBox.CreateBuilder();
164      b.SetValues(ToByteString(value));
165      box.SetExtension(ByteArrayBox.ByteArray, b.Build());
166    }
167    protected override T Extract(Box box, Type type, Mapper mapper) {
168      return FromByteString(box.GetExtension(ByteArrayBox.ByteArray).Values);
169    }
170    protected abstract ByteString ToByteString(T value);
171    protected abstract T FromByteString(ByteString byteString);
172  }
173  public abstract class BoolArrayBoxTransformer<T> : BoxTransformer<T> {
174    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
175      var b = BoolArrayBox.CreateBuilder();
176      b.AddRangeValues(ToBoxType(value, mapper));
177      box.SetExtension<BoolArrayBox>(BoolArrayBox.BoolArray, b.Build());
178    }
179    protected override T Extract(Box box, Type type, Mapper mapper) {
180      return ToValueType(box.GetExtension(BoolArrayBox.BoolArray).ValuesList, type, mapper);
181    }
182    protected abstract IEnumerable<bool> ToBoxType(T value, Mapper mapper);
183    protected abstract T ToValueType(IEnumerable<bool> value, Type type, Mapper mapper);
184  }
185  public abstract class IntArrayBoxTransformer<T> : BoxTransformer<T> {
186    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
187      var b = IntArrayBox.CreateBuilder();
188      b.AddRangeValues(ToBoxType(value, mapper));
189      box.SetExtension<IntArrayBox>(IntArrayBox.IntArray, b.Build());
190    }
191    protected override T Extract(Box box, Type type, Mapper mapper) {
192      return ToValueType(box.GetExtension(IntArrayBox.IntArray).ValuesList, type, mapper);
193    }
194    protected abstract IEnumerable<int> ToBoxType(T value, Mapper mapper);
195    protected abstract T ToValueType(IEnumerable<int> value, Type type, Mapper mapper);
196  }
197  public abstract class LongArrayBoxTransformer<T> : BoxTransformer<T> {
198    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
199      var b = LongArrayBox.CreateBuilder();
200      b.AddRangeValues(ToBoxType(value, mapper));
201      box.SetExtension<LongArrayBox>(LongArrayBox.LongArray, b.Build());
202    }
203    protected override T Extract(Box box, Type type, Mapper mapper) {
204      return ToValueType(box.GetExtension(LongArrayBox.LongArray).ValuesList, type, mapper);
205    }
206    protected abstract IEnumerable<long> ToBoxType(T value, Mapper mapper);
207    protected abstract T ToValueType(IEnumerable<long> value, Type type, Mapper mapper);
208  }
209  public abstract class UnsignedIntArrayBoxTransformer<T> : BoxTransformer<T> {
210    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
211      var b = UnsignedIntArrayBox.CreateBuilder();
212      b.AddRangeValues(ToBoxType(value, mapper));
213      box.SetExtension<UnsignedIntArrayBox>(UnsignedIntArrayBox.UnsignedIntArray, b.Build());
214    }
215    protected override T Extract(Box box, Type type, Mapper mapper) {
216      return ToValueType(box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray).ValuesList, type, mapper);
217    }
218    protected abstract IEnumerable<uint> ToBoxType(T value, Mapper mapper);
219    protected abstract T ToValueType(IEnumerable<uint> value, Type type, Mapper mapper);
220  }
221  public abstract class UnsignedLongArrayBoxTransformer<T> : BoxTransformer<T> {
222    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
223      var b = UnsignedLongArrayBox.CreateBuilder();
224      b.AddRangeValues(ToBoxType(value, mapper));
225      box.SetExtension<UnsignedLongArrayBox>(UnsignedLongArrayBox.UnsignedLongArray, b.Build());
226    }
227    protected override T Extract(Box box, Type type, Mapper mapper) {
228      return ToValueType(box.GetExtension(UnsignedLongArrayBox.UnsignedLongArray).ValuesList, type, mapper);
229    }
230    protected abstract IEnumerable<ulong> ToBoxType(T value, Mapper mapper);
231    protected abstract T ToValueType(IEnumerable<ulong> value, Type type, Mapper mapper);
232  }
233  public abstract class FloatArrayBoxTransformer<T> : BoxTransformer<T> {
234    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
235      var b = FloatArrayBox.CreateBuilder();
236      b.AddRangeValues(ToBoxType(value, mapper));
237      box.SetExtension<FloatArrayBox>(FloatArrayBox.FloatArray, b.Build());
238    }
239    protected override T Extract(Box box, Type type, Mapper mapper) {
240      return ToValueType(box.GetExtension(FloatArrayBox.FloatArray).ValuesList, type, mapper);
241    }
242    protected abstract IEnumerable<float> ToBoxType(T value, Mapper mapper);
243    protected abstract T ToValueType(IEnumerable<float> value, Type type, Mapper mapper);
244  }
245  public abstract class DoubleArrayBoxTransformer<T> : BoxTransformer<T> {
246    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
247      var b = DoubleArrayBox.CreateBuilder();
248      b.AddRangeValues(ToBoxType(value, mapper));
249      box.SetExtension<DoubleArrayBox>(DoubleArrayBox.DoubleArray, b.Build());
250    }
251    protected override T Extract(Box box, Type type, Mapper mapper) {
252      return ToValueType(box.GetExtension(DoubleArrayBox.DoubleArray).ValuesList, type, mapper);
253    }
254    protected abstract IEnumerable<double> ToBoxType(T value, Mapper mapper);
255    protected abstract T ToValueType(IEnumerable<double> value, Type type, Mapper mapper);
256  }
257  public abstract class StringArrayBoxTransformer<T> : BoxTransformer<T> {
258    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
259      var b = StringArrayBox.CreateBuilder();
260      b.AddRangeValues(ToBoxType(value, mapper));
261      box.SetExtension<StringArrayBox>(StringArrayBox.StringArray, b.Build());
262    }
263    protected override T Extract(Box box, Type type, Mapper mapper) {
264      return ToValueType(box.GetExtension(StringArrayBox.StringArray).ValuesList, type, mapper);
265    }
266    protected abstract IEnumerable<string> ToBoxType(T value, Mapper mapper);
267    protected abstract T ToValueType(IEnumerable<string> value, Type type, Mapper mapper);
268  }
269  #endregion
270
271  #region Primitive Value Types
272  [Transformer("9FA1C6A8-517E-4623-AC1B-7E9AEF6ED13D", 200)]
273  internal sealed class BoolTransformer : BoolBoxTransformer<bool> {
274    protected override bool ToBoxType(bool value, Mapper mapper) { return value; }
275    protected override bool ToValueType(bool value, Type type, Mapper mapper) { return value; }
276  }
277
278  [Transformer("059633E9-12CB-43EC-8544-57746536E281", 201)]
279  internal sealed class ByteTransformer : UnsignedIntBoxTransformer<byte> {
280    protected override uint ToBoxType(byte value, Mapper mapper) { return value; }
281    protected override byte ToValueType(uint value, Type type, Mapper mapper) { return (byte)value; }
282  }
283
284  [Transformer("5DC2F3AC-0852-4B57-A861-D29CC814A94C", 202)]
285  internal sealed class SByteTransformer : IntBoxTransformer<sbyte> {
286    protected override int ToBoxType(sbyte value, Mapper mapper) { return value; }
287    protected override sbyte ToValueType(int value, Type type, Mapper mapper) { return (sbyte)value; }
288  }
289
290  [Transformer("B862E642-A94A-4870-8065-06126A35A9E1", 203)]
291  internal sealed class ShortTransformer : IntBoxTransformer<short> {
292    protected override int ToBoxType(short value, Mapper mapper) { return value; }
293    protected override short ToValueType(int value, Type type, Mapper mapper) { return (short)value; }
294  }
295
296  [Transformer("D1D3062D-F1BB-4189-AE50-D6986E1DEB4E", 204)]
297  internal sealed class UShortTransformer : UnsignedIntBoxTransformer<ushort> {
298    protected override uint ToBoxType(ushort value, Mapper mapper) { return value; }
299    protected override ushort ToValueType(uint value, Type type, Mapper mapper) { return (ushort)value; }
300  }
301
302  [Transformer("6C444645-3062-4D15-AD01-E6E1B0692A2B", 205)]
303  internal sealed class CharTransformer : UnsignedIntBoxTransformer<char> {
304    protected override uint ToBoxType(char value, Mapper mapper) { return value; }
305    protected override char ToValueType(uint value, Type type, Mapper mapper) { return (char)value; }
306  }
307
308  [Transformer("649E73B2-EFA6-4E01-BCB4-4B29D652C9CB", 206)]
309  internal sealed class IntTransformer : IntBoxTransformer<int> {
310    protected override int ToBoxType(int value, Mapper mapper) { return value; }
311    protected override int ToValueType(int value, Type type, Mapper mapper) { return value; }
312  }
313
314  [Transformer("BCF25010-81A2-49BC-88CC-407D3F393D5B", 207)]
315  internal sealed class UIntTransformer : UnsignedIntBoxTransformer<uint> {
316    protected override uint ToBoxType(uint value, Mapper mapper) { return value; }
317    protected override uint ToValueType(uint value, Type type, Mapper mapper) { return value; }
318  }
319
320  [Transformer("B6F6ACAE-755C-47EE-B8BF-7CDECBE19C30", 208)]
321  internal sealed class LongTransformer : LongBoxTransformer<long> {
322    protected override long ToBoxType(long value, Mapper mapper) { return value; }
323    protected override long ToValueType(long value, Type type, Mapper mapper) { return value; }
324  }
325
326  [Transformer("82333ACA-F041-44E0-B365-27C399594BA7", 209)]
327  internal sealed class ULongTransformer : UnsignedLongBoxTransformer<ulong> {
328    protected override ulong ToBoxType(ulong value, Mapper mapper) { return value; }
329    protected override ulong ToValueType(ulong value, Type type, Mapper mapper) { return value; }
330  }
331
332  [Transformer("8FE91ECF-2261-4934-BECD-C38923B7A703", 210)]
333  internal sealed class FloatTransformer : FloatBoxTransformer<float> {
334    protected override float ToBoxType(float value, Mapper mapper) { return value; }
335    protected override float ToValueType(float value, Type type, Mapper mapper) { return value; }
336  }
337
338  [Transformer("070D23EA-7F38-46B7-A667-219BEF914E49", 211)]
339  internal sealed class DoubleTransformer : DoubleBoxTransformer<double> {
340    protected override double ToBoxType(double value, Mapper mapper) { return value; }
341    protected override double ToValueType(double value, Type type, Mapper mapper) { return value; }
342  }
343
344  [Transformer("BA55C7A6-C91E-4351-A889-E4A7E647F16D", 212)]
345  internal sealed class DateTimeTransformer : LongBoxTransformer<DateTime> {
346    protected override long ToBoxType(DateTime value, Mapper mapper) { return value.Ticks; }
347    protected override DateTime ToValueType(long value, Type type, Mapper mapper) { return new DateTime(value); }
348  }
349
350  [Transformer("0C91441B-2D97-432B-B493-D6EC483FC5AD", 213)]
351  internal sealed class TimeSpanTransformer : LongBoxTransformer<TimeSpan> {
352    protected override long ToBoxType(TimeSpan value, Mapper mapper) { return value.Ticks; }
353    protected override TimeSpan ToValueType(long value, Type type, Mapper mapper) { return new TimeSpan(value); }
354  }
355
356  [Transformer("0B540EAC-79AB-40CF-8277-D2C81135FEB6", 214)]
357  internal sealed class ColorTransformers : IntBoxTransformer<Color> {
358    protected override int ToBoxType(Color value, Mapper mapper) { return value.ToArgb(); }
359    protected override Color ToValueType(int value, Type type, Mapper mapper) { return Color.FromArgb(value); }
360  }
361
362  [Transformer("2E6D4A40-B4BE-425F-8E35-2D7C00054639", 215)]
363  internal sealed class PointTransformer : IntArrayBoxTransformer<Point> {
364    protected override IEnumerable<int> ToBoxType(Point value, Mapper mapper) { return new int[] { value.X, value.Y }; }
365    protected override Point ToValueType(IEnumerable<int> value, Type type, Mapper mapper) { return new Point(value.ElementAt(0), value.ElementAt(1)); }
366  }
367
368  [Transformer("97B5CFC8-CDFA-4EB5-B4CD-5B3CFA5CD844", 216)]
369  internal sealed class KeyValuePairTransformer : UnsignedIntArrayBoxTransformer<object> {
370    public override bool CanTransformType(Type type) {
371      return type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>));
372    }
373    protected override IEnumerable<uint> ToBoxType(object value, Mapper mapper) {
374      var type = value.GetType();
375      var pair = new uint[2];
376      pair[0] = mapper.GetBoxId(type.GetProperty("Key").GetValue(value));
377      pair[1] = mapper.GetBoxId(type.GetProperty("Value").GetValue(value));
378      return pair;
379    }
380    protected override object ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) {
381      var key = mapper.GetObject(value.ElementAt(0));
382      var val = mapper.GetObject(value.ElementAt(1));
383      return Activator.CreateInstance(type, key, val);
384    }
385  }
386  #endregion
387
388  #region String
389  [Transformer("E75A594C-0034-4DAB-B28E-8F84F9F6DE8D", 217)]
390  internal sealed class StringTransformer : UnsignedIntBoxTransformer<string> {
391    protected override uint ToBoxType(string value, Mapper mapper) { return mapper.GetStringId(value); }
392    protected override string ToValueType(uint value, Type type, Mapper mapper) { return mapper.GetString(value); }
393  }
394  #endregion
395
396  #region Enum
397  [Transformer("93FF076B-BC4B-4C39-8C40-15E004468C98", 218)]
398  internal sealed class EnumTransformer : Transformer {
399    public override bool CanTransformType(Type type) {
400      return typeof(Enum).IsAssignableFrom(type);
401    }
402
403    public override Box ToBox(object o, Mapper mapper) {
404      var boxBuilder = Box.CreateBuilder();
405      var enumBuilder = new UnsignedIntBox.Builder();
406
407      boxBuilder.TransformerId = mapper.GetTransformerId(this);
408      boxBuilder.TypeId = mapper.GetStringId(o.GetType().AssemblyQualifiedName);
409      enumBuilder.Value = mapper.GetStringId(Enum.Format(o.GetType(), o, "G"));
410
411      boxBuilder.SetExtension(UnsignedIntBox.UnsignedInt, enumBuilder.Build());
412      return boxBuilder.Build();
413    }
414
415    public override object ToObject(Box box, Mapper mapper) {
416      uint value = box.GetExtension(UnsignedIntBox.UnsignedInt).Value;
417      return Enum.Parse(Type.GetType(mapper.GetString(box.TypeId)), mapper.GetString(value));
418    }
419  }
420  #endregion
421
422  //   #region Type
423  //   [Transformer("8D17FD28-383B-44E9-9BBF-B19D351C5E38", 219)]
424  //   internal sealed class TypeTransformer : Transformer {
425  //     public override bool CanTransformType(Type type) {
426  //       return typeof(Type).IsAssignableFrom(type);
427  //     }
428  //     public override Box ToBox(object o, Mapper mapper) {
429  //       var boxBuilder = Box.CreateBuilder();
430  //
431  //       boxBuilder.TransformerId = mapper.GetTransformerId(this);
432  //       boxBuilder.TypeId = mapper.GetStringId(((Type)o).AssemblyQualifiedName);
433  //
434  //       return boxBuilder.Build();
435  //     }
436  //
437  //     public override object ToObject(Box box, Mapper mapper) {
438  //       return Type.GetType(mapper.GetString(box.TypeId));
439  //     }
440  //   }
441  //   #endregion
442
443  #region Array Types
444  [Transformer("FF89F6D1-CDE3-498E-9166-F70AC6EB01F1", 301)]
445  internal sealed class ByteArrayTransformer : ByteArrayBoxTransformer<byte[]> {
446    protected override ByteString ToByteString(byte[] value) {
447      return ByteString.CopyFrom(value);
448    }
449    protected override byte[] FromByteString(ByteString byteString) {
450      return byteString.ToArray();
451    }
452  }
453
454  [Transformer("B49B3F2D-2E97-4BAB-8705-8D29DA707C6A", 302)]
455  internal sealed class SByteArrayTransformer : ByteArrayBoxTransformer<sbyte[]> {
456    protected override ByteString ToByteString(sbyte[] value) {
457      return ByteString.CopyFrom(value.Select(b => (byte)b).ToArray());
458    }
459    protected override sbyte[] FromByteString(ByteString byteString) {
460      return byteString.Select(b => (sbyte)b).ToArray();
461    }
462  }
463
464  [Transformer("F25A73B2-6B67-4493-BD59-B836AF4455D1", 300)]
465  internal sealed class BoolArrayTransformer : BoolArrayBoxTransformer<bool[]> {
466    protected override IEnumerable<bool> ToBoxType(bool[] value, Mapper mapper) { return value; }
467    protected override bool[] ToValueType(IEnumerable<bool> value, Type type, Mapper mapper) { return value.ToArray(); }
468  }
469
470  [Transformer("2811FDD4-6800-4CBA-86D7-9071ED5775ED", 303)]
471  internal sealed class ShortArrayTransformer : ByteArrayBoxTransformer<short[]> {
472    protected override ByteString ToByteString(short[] value) {
473      var res = new byte[value.Length * 2];
474      for (int i = 0; i < value.Length; i++) {
475        var bytes = BitConverter.GetBytes(value[i]);
476        Array.Copy(bytes, 0, res, i * 2, 2);
477      }
478      return ByteString.CopyFrom(res);
479    }
480    protected override short[] FromByteString(ByteString byteString) {
481      var bytes = byteString.ToArray();
482      var res = new short[bytes.Length / 2];
483      for (int i = 0; i < bytes.Length; i += 2) {
484        res[i / 2] = BitConverter.ToInt16(bytes, i);
485      }
486      return res;
487    }
488  }
489
490  [Transformer("1AAC2625-356C-40BC-8CB4-15CB3D047EB8", 304)]
491  internal sealed class UShortArrayTransformer : ByteArrayBoxTransformer<ushort[]> {
492    protected override ByteString ToByteString(ushort[] value) {
493      var res = new byte[value.Length * 2];
494      for (int i = 0; i < value.Length; i++) {
495        var bytes = BitConverter.GetBytes(value[i]);
496        Array.Copy(bytes, 0, res, i * 2, 2);
497      }
498      return ByteString.CopyFrom(res);
499    }
500    protected override ushort[] FromByteString(ByteString byteString) {
501      var bytes = byteString.ToArray();
502      var res = new ushort[bytes.Length / 2];
503      for (int i = 0; i < bytes.Length; i += 2) {
504        res[i / 2] = BitConverter.ToUInt16(bytes, i);
505      }
506      return res;
507    }
508  }
509
510  [Transformer("12F19098-5D49-4C23-8897-69087F1C146D", 305)]
511  internal sealed class CharArrayTransformer : ByteArrayBoxTransformer<char[]> {
512    protected override ByteString ToByteString(char[] value) {
513      var res = new byte[value.Length * 2];
514      for (int i = 0; i < value.Length; i++) {
515        var bytes = BitConverter.GetBytes(value[i]);
516        Array.Copy(bytes, 0, res, i * 2, 2);
517      }
518      return ByteString.CopyFrom(res);
519    }
520    protected override char[] FromByteString(ByteString byteString) {
521      var bytes = byteString.ToArray();
522      var res = new char[bytes.Length / 2];
523      for (int i = 0; i < bytes.Length; i += 2) {
524        res[i / 2] = BitConverter.ToChar(bytes, i);
525      }
526      return res;
527    }
528  }
529
530  [Transformer("5F6DC3BC-4433-4AE9-A636-4BD126F7DACD", 306)]
531  internal sealed class IntArrayTransformer : IntArrayBoxTransformer<int[]> {
532    protected override IEnumerable<int> ToBoxType(int[] value, Mapper mapper) { return value; }
533    protected override int[] ToValueType(IEnumerable<int> value, Type type, Mapper mapper) { return value.ToArray(); }
534  }
535
536  [Transformer("3F10274F-D350-4C82-89EA-A5EB36D4EFCC", 307)]
537  internal sealed class UIntArrayTransformer : UnsignedIntArrayBoxTransformer<uint[]> {
538    protected override IEnumerable<uint> ToBoxType(uint[] value, Mapper mapper) { return value; }
539    protected override uint[] ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) { return value.ToArray(); }
540  }
541
542  [Transformer("E9D550E2-57F7-47F3-803D-37A619DA1A5C", 308)]
543  internal sealed class LongArrayTransformer : LongArrayBoxTransformer<long[]> {
544    protected override IEnumerable<long> ToBoxType(long[] value, Mapper mapper) { return value; }
545    protected override long[] ToValueType(IEnumerable<long> value, Type type, Mapper mapper) { return value.ToArray(); }
546  }
547
548  [Transformer("C02A205B-2176-4282-AC2B-ADEF96DDBE24", 309)]
549  internal sealed class ULongArrayTransformer : UnsignedLongArrayBoxTransformer<ulong[]> {
550    protected override IEnumerable<ulong> ToBoxType(ulong[] value, Mapper mapper) { return value; }
551    protected override ulong[] ToValueType(IEnumerable<ulong> value, Type type, Mapper mapper) { return value.ToArray(); }
552  }
553
554  [Transformer("3C4590D9-C76E-4AFB-98FD-E50D3D051648", 310)]
555  internal sealed class FloatArrayTransformer : FloatArrayBoxTransformer<float[]> {
556    protected override IEnumerable<float> ToBoxType(float[] value, Mapper mapper) { return value; }
557    protected override float[] ToValueType(IEnumerable<float> value, Type type, Mapper mapper) { return value.ToArray(); }
558  }
559
560  [Transformer("FB98C399-9323-4470-9A85-9186C2B2D5D4", 311)]
561  internal sealed class DoubleArrayTransformer : DoubleArrayBoxTransformer<double[]> {
562    protected override IEnumerable<double> ToBoxType(double[] value, Mapper mapper) { return value; }
563    protected override double[] ToValueType(IEnumerable<double> value, Type type, Mapper mapper) { return value.ToArray(); }
564  }
565
566  [Transformer("68332513-9CF1-47FA-A093-6DDB663186EC", 312)]
567  internal sealed class StringArrayTransformer : StringArrayBoxTransformer<string[]> {
568    protected override IEnumerable<string> ToBoxType(string[] value, Mapper mapper) { return value; }
569    protected override string[] ToValueType(IEnumerable<string> value, Type type, Mapper mapper) { return value.ToArray(); }
570  }
571
572  //[Transformer("C83F0B5A-68D8-4271-81F9-FF259FC6F126", 400)]
573  //internal sealed class GenericArrayTransformer : UnsignedIntArrayBoxTransformer<object> {
574  //  public override bool CanTransformType(Type type) {
575  //    return type.IsArray;
576  //  }
577  //  protected override IEnumerable<uint> ToBoxType(object value, Mapper mapper) {
578  //    var arr = (object[])value;
579  //    return arr.Select(x => mapper.GetBoxId(x));
580  //  }
581  //  protected override object ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) {
582  //    return value.Select(x => mapper.GetObject(x)).ToArray();
583  //  }
584  //}
585
586  //   internal sealed class QueueTransformer : Transformer {
587  //     public override bool CanTransformType(Type type) {
588  //       return type == typeof(System.Collections.Queue) || type == typeof(Queue<>);
589  //     }
590  //     public override Box ToBox(object o, Mapper mapper) {
591  //       var box = Box.CreateBuilder();
592  //       box.TransformerId = mapper.GetTransformerId(this);
593  //       Populate(box, o, mapper);
594  //       return box.Build();
595  //     }
596  //     public override object ToObject(Box box, Mapper mapper) {
597  //       return Extract(box, mapper.GetType(box.TypeId), mapper);
598  //     }
599  //     private void Populate(Box.Builder box, object value, Mapper mapper) {
600  //       var elements = (System.Collections.IEnumerable)value;
601  //       if (value.GetType().IsGenericType) {
602  //         var elementType = value.GetType().GetGenericTypeDefinition().GetGenericArguments().First();
603  //         box.TypeId = mapper.GetTypeId(elementType);
604  //       }
605  //
606  //       var b = UnsignedIntArrayBox.CreateBuilder();
607  //       foreach (var elem in elements) {
608  //         b.AddValues(mapper.GetBoxId(elem));
609  //       }
610  //
611  //       box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, b.Build());
612  //     }
613  //     private object Extract(Box box, Type type, Mapper mapper) {
614  //     }
615  //   }
616
617  [Transformer("B01ADF0A-ACAA-444E-9F82-0C7C2AF1F490", 2000)]
618  internal sealed class ArrayTransformer : Transformer {
619    public override bool CanTransformType(Type type) {
620      return type.IsArray;
621    }
622    public override Box ToBox(object o, Mapper mapper) {
623      var box = Box.CreateBuilder();
624      box.TransformerId = mapper.GetTransformerId(this);
625      Populate(box, o, mapper);
626      return box.Build();
627    }
628    public override object ToObject(Box box, Mapper mapper) {
629      return Extract(box, (Type)mapper.GetObject(box.TypeId), mapper);
630    }
631    private void Populate(Box.Builder box, object value, Mapper mapper) {
632      var array = (object[])value;
633      var elementType = value.GetType().GetElementType();
634
635      var b = UnsignedIntArrayBox.CreateBuilder();
636      b.AddRangeValues(array.Select(x => mapper.GetBoxId(x)));
637
638      box.TypeId = mapper.GetBoxId(elementType);
639      box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, b.Build());
640    }
641    private object Extract(Box box, Type type, Mapper mapper) {
642      var b = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
643
644      var array = (object[])Array.CreateInstance((Type)mapper.GetObject(box.TypeId), b.ValuesCount);
645
646      for (int i = 0; i < array.Length; i++) {
647        array[i] = mapper.GetObject(b.GetValues(i));
648      }
649
650      return array;
651    }
652  }
653
654  [Transformer("11B822C9-46A0-4B65-AE4A-D12F63DDE9F6", 1)]
655  internal sealed class TypeTransformer : Transformer {
656    public override bool CanTransformType(Type type) {
657      return typeof(Type).IsAssignableFrom(type);
658    }
659
660    public override Box ToBox(object o, Mapper mapper) {
661      var box = Box.CreateBuilder();
662      box.TransformerId = mapper.GetTransformerId(this);
663      Populate(box, o, mapper);
664      return box.Build();
665    }
666
667    private void Populate(Box.Builder box, object o, Mapper mapper) {
668      var type = (Type)o;
669
670      var b = TypeBox.CreateBuilder();
671      if (type.IsGenericType) {
672        box.TypeId = mapper.GetTypeId(type.GetGenericTypeDefinition());
673        b.AddRangeGenericTypeIds(type.GetGenericArguments().Select(t => mapper.GetBoxId(t)));
674      } else if (type.IsArray) {
675        box.TypeId = mapper.GetTypeId(typeof(Array));
676        b.AddGenericTypeIds(mapper.GetBoxId(type.GetElementType()));
677      } else {
678        box.TypeId = mapper.GetTypeId(type);
679      }
680      box.SetExtension(TypeBox.Type, b.Build());
681    }
682
683    public override object ToObject(Box box, Mapper mapper) {
684      return Extract(box, mapper.GetType(box.TypeId), mapper);
685    }
686
687    private object Extract(Box box, Type type, Mapper mapper) {
688      var b = box.GetExtension(TypeBox.Type);
689      if (b.GenericTypeIdsCount > 0) {
690        if (typeof(Array).IsAssignableFrom(type)) {
691          return ((Type)mapper.GetObject(b.GetGenericTypeIds(0))).MakeArrayType();
692        } else {
693          return type.MakeGenericType(b.GenericTypeIdsList.Select(id => (Type)mapper.GetObject(id)).ToArray());
694        }
695      } else {
696        return mapper.GetType(box.TypeId);
697      }
698    }
699  }
700
701  [Transformer("4FA5EAAF-ECC7-4A9C-84E7-6583DA96F3B9", 1000)]
702  internal sealed class EnumerableTransformer : Transformer {
703    public override bool CanTransformType(Type type) {
704      return (type.IsGenericType && typeof(Stack<>) == type.GetGenericTypeDefinition()) ||
705             type == typeof(System.Collections.Stack);
706    }
707    public override Box ToBox(object o, Mapper mapper) {
708      var box = Box.CreateBuilder();
709      box.TransformerId = mapper.GetTransformerId(this);
710      Populate(box, o, mapper);
711      return box.Build();
712    }
713    public override object ToObject(Box box, Mapper mapper) {
714      return Extract(box, (Type)mapper.GetObject(box.TypeId), mapper);
715    }
716    private void Populate(Box.Builder box, object value, Mapper mapper) {
717      var enumerableBox = UnsignedIntArrayBox.CreateBuilder();
718      box.TypeId = mapper.GetBoxId(value.GetType());
719      enumerableBox.AddRangeValues(((IEnumerable)value).OfType<object>().Select(mapper.GetBoxId));
720
721      box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, enumerableBox.Build());
722    }
723    private object Extract(Box box, Type type, Mapper mapper) {
724      var enumerableBox = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
725      if ((type.IsGenericType && typeof(Stack<>) == type.GetGenericTypeDefinition())) {
726        var elemType = type.GenericTypeArguments[0];
727        var stack = Activator.CreateInstance(type);
728        var addMeth = type.GetMethod("Push");
729        foreach (var e in enumerableBox.ValuesList.Select(mapper.GetObject).Reverse())
730          addMeth.Invoke(stack, new[] { e });
731        return stack;
732      } else if (typeof(Stack).IsAssignableFrom(type)) {
733        var l = new System.Collections.ArrayList();
734        foreach (var e in enumerableBox.ValuesList.Select(mapper.GetObject)) {
735          l.Add(e);
736        }
737        l.Reverse();
738        return Activator.CreateInstance(type, l);
739      } else {
740        throw new NotSupportedException();
741      }
742    }
743  }
744
745  [Transformer("C47A62F5-F113-4A43-A8EE-CF817EC799A2", 500)]
746  internal sealed class DictionaryTransformer : Transformer {
747    public override bool CanTransformType(Type type) {
748      return type.IsGenericType && typeof(IDictionary<,>).IsAssignableFrom(type);
749    }
750    public override Box ToBox(object o, Mapper mapper) {
751      var box = Box.CreateBuilder();
752      box.TransformerId = mapper.GetTransformerId(this);
753      Populate(box, o, mapper);
754      return box.Build();
755    }
756    public override object ToObject(Box box, Mapper mapper) {
757      return Extract(box, mapper.GetType(box.TypeId), mapper);
758    }
759    private void Populate(Box.Builder box, object value, Mapper mapper) {
760      var dictionaryBox = DictionaryBox.CreateBuilder();
761      foreach (DictionaryEntry item in (IDictionary)value) {
762        dictionaryBox.AddKeyIds(mapper.GetBoxId(item.Key));
763        dictionaryBox.AddValueIds(mapper.GetBoxId(item.Value));
764      }
765      // TODO: comparer
766    }
767    private object Extract(Box box, Type type, Mapper mapper) {
768      throw new NotImplementedException();
769    }
770  }
771  #endregion
772}
Note: See TracBrowser for help on using the repository browser.