source: branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs @ 14927

Last change on this file since 14927 was 14927, checked in by gkronber, 5 years ago

#2520: changed all usages of StorableClass to use StorableType with an auto-generated GUID (did not add StorableType to other type definitions yet)

File size: 51.1 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.ComponentModel;
26using System.Drawing;
27using System.Drawing.Imaging;
28using System.Globalization;
29using System.IO;
30using System.Linq;
31using System.Reflection;
32using Google.ProtocolBuffers;
33
34namespace HeuristicLab.Persistence {
35  [StorableType("8c7e99f5-092f-4cef-8b72-8afec1d10236")]
36  public abstract class BoxTransformer<T> : Transformer {
37    public override bool CanTransformType(Type type) {
38      return type == typeof(T);
39    }
40
41    public override Box ToBox(object o, Mapper mapper) {
42      var box = Box.CreateBuilder();
43      box.TransformerId = mapper.GetTransformerId(this);
44      box.TypeId = mapper.GetBoxId(o.GetType());
45      Populate(box, (T)o, mapper);
46      return box.Build();
47    }
48    public override object ToObject(Box box, Mapper mapper) {
49      return Extract(box, (Type)mapper.GetObject(box.TypeId), mapper);
50    }
51    protected abstract void Populate(Box.Builder box, T value, Mapper mapper);
52    protected abstract T Extract(Box box, Type type, Mapper mapper);
53  }
54
55  #region Scalar Box Transformers
56  [StorableType("4b800c20-e93d-4186-9ccc-9e8081c028bb")]
57  public abstract class BoolBoxTransformer<T> : BoxTransformer<T> {
58    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
59      var b = BoolBox.CreateBuilder();
60      b.Value = ToBoxType(value, mapper);
61      box.SetExtension<BoolBox>(BoolBox.Bool, b.Build());
62    }
63    protected override T Extract(Box box, Type type, Mapper mapper) {
64      return ToValueType(box.GetExtension(BoolBox.Bool).Value, type, mapper);
65    }
66    protected abstract bool ToBoxType(T value, Mapper mapper);
67    protected abstract T ToValueType(bool value, Type type, Mapper mapper);
68  }
69  [StorableType("72b92810-3eee-4a35-b87a-0a9b06b58945")]
70  public abstract class IntBoxTransformer<T> : BoxTransformer<T> {
71    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
72      var b = IntBox.CreateBuilder();
73      b.Value = ToBoxType(value, mapper);
74      box.SetExtension<IntBox>(IntBox.Int, b.Build());
75    }
76    protected override T Extract(Box box, Type type, Mapper mapper) {
77      return ToValueType(box.GetExtension(IntBox.Int).Value, type, mapper);
78    }
79    protected abstract int ToBoxType(T value, Mapper mapper);
80    protected abstract T ToValueType(int value, Type type, Mapper mapper);
81  }
82  [StorableType("e7344a71-276d-4fc7-9d80-1f0a8b10dde5")]
83  public abstract class LongBoxTransformer<T> : BoxTransformer<T> {
84    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
85      var b = LongBox.CreateBuilder();
86      b.Value = ToBoxType(value, mapper);
87      box.SetExtension<LongBox>(LongBox.Long, b.Build());
88    }
89    protected override T Extract(Box box, Type type, Mapper mapper) {
90      return ToValueType(box.GetExtension(LongBox.Long).Value, type, mapper);
91    }
92    protected abstract long ToBoxType(T value, Mapper mapper);
93    protected abstract T ToValueType(long value, Type type, Mapper mapper);
94  }
95  [StorableType("76833301-5d1d-436d-86f6-6cfe1830405e")]
96  public abstract class UnsignedIntBoxTransformer<T> : BoxTransformer<T> {
97    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
98      var b = UnsignedIntBox.CreateBuilder();
99      b.Value = ToBoxType(value, mapper);
100      box.SetExtension<UnsignedIntBox>(UnsignedIntBox.UnsignedInt, b.Build());
101    }
102    protected override T Extract(Box box, Type type, Mapper mapper) {
103      return ToValueType(box.GetExtension(UnsignedIntBox.UnsignedInt).Value, type, mapper);
104    }
105    protected abstract uint ToBoxType(T value, Mapper mapper);
106    protected abstract T ToValueType(uint value, Type type, Mapper mapper);
107  }
108  [StorableType("4eb19b56-7ac7-497c-b25c-a860d3d72565")]
109  public abstract class UnsignedLongBoxTransformer<T> : BoxTransformer<T> {
110    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
111      var b = UnsignedLongBox.CreateBuilder();
112      b.Value = ToBoxType(value, mapper);
113      box.SetExtension<UnsignedLongBox>(UnsignedLongBox.UnsignedLong, b.Build());
114    }
115    protected override T Extract(Box box, Type type, Mapper mapper) {
116      return ToValueType(box.GetExtension(UnsignedLongBox.UnsignedLong).Value, type, mapper);
117    }
118    protected abstract ulong ToBoxType(T value, Mapper mapper);
119    protected abstract T ToValueType(ulong value, Type type, Mapper mapper);
120  }
121  [StorableType("1a6613b2-772f-4334-b688-8b2a8483123b")]
122  public abstract class FloatBoxTransformer<T> : BoxTransformer<T> {
123    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
124      var b = FloatBox.CreateBuilder();
125      b.Value = ToBoxType(value, mapper);
126      box.SetExtension<FloatBox>(FloatBox.Float, b.Build());
127    }
128    protected override T Extract(Box box, Type type, Mapper mapper) {
129      return ToValueType(box.GetExtension(FloatBox.Float).Value, type, mapper);
130    }
131    protected abstract float ToBoxType(T value, Mapper mapper);
132    protected abstract T ToValueType(float value, Type type, Mapper mapper);
133  }
134  [StorableType("c7fee156-6310-42a6-9f07-838cef8f405e")]
135  public abstract class DoubleBoxTransformer<T> : BoxTransformer<T> {
136    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
137      var b = DoubleBox.CreateBuilder();
138      b.Value = ToBoxType(value, mapper);
139      box.SetExtension<DoubleBox>(DoubleBox.Double, b.Build());
140    }
141    protected override T Extract(Box box, Type type, Mapper mapper) {
142      return ToValueType(box.GetExtension(DoubleBox.Double).Value, type, mapper);
143    }
144    protected abstract double ToBoxType(T value, Mapper mapper);
145    protected abstract T ToValueType(double value, Type type, Mapper mapper);
146  }
147  [StorableType("f7ea684c-8784-4939-80d4-318558a8b4a4")]
148  public abstract class StringBoxTransformer<T> : BoxTransformer<T> {
149    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
150      var b = StringBox.CreateBuilder();
151      b.Value = ToBoxType(value, mapper);
152      box.SetExtension<StringBox>(StringBox.String, b.Build());
153    }
154    protected override T Extract(Box box, Type type, Mapper mapper) {
155      return ToValueType(box.GetExtension(StringBox.String).Value, type, mapper);
156    }
157    protected abstract string ToBoxType(T value, Mapper mapper);
158    protected abstract T ToValueType(string value, Type type, Mapper mapper);
159  }
160  [StorableType("cb1680ec-3141-437d-b28d-f17595ca6815")]
161  public abstract class BytesBoxTransformer<T> : BoxTransformer<T> {
162    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
163      var b = BytesBox.CreateBuilder();
164      b.Value = ByteString.CopyFrom(ToBoxType(value, mapper));
165      box.SetExtension<BytesBox>(BytesBox.Bytes, b.Build());
166    }
167    protected override T Extract(Box box, Type type, Mapper mapper) {
168      return ToValueType(box.GetExtension(BytesBox.Bytes).Value.ToByteArray(), type, mapper);
169    }
170    protected abstract byte[] ToBoxType(T value, Mapper mapper);
171    protected abstract T ToValueType(byte[] value, Type type, Mapper mapper);
172  }
173  #endregion
174
175  #region Array Box Transformers
176  [StorableType("a6840c61-cf07-4e0a-a13c-392565e7326e")]
177  public abstract class ByteArrayBoxTransformer<T> : BoxTransformer<T> {
178    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
179      var b = ByteArrayBox.CreateBuilder();
180      b.SetValues(ToByteString(value));
181      box.SetExtension(ByteArrayBox.ByteArray, b.Build());
182    }
183    protected override T Extract(Box box, Type type, Mapper mapper) {
184      return FromByteString(box.GetExtension(ByteArrayBox.ByteArray).Values);
185    }
186    protected abstract ByteString ToByteString(T value);
187    protected abstract T FromByteString(ByteString byteString);
188  }
189  [StorableType("543de8f9-bf32-431d-af52-8dd95cfd876f")]
190  public abstract class BoolArrayBoxTransformer<T> : BoxTransformer<T> {
191    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
192      var b = BoolArrayBox.CreateBuilder();
193      b.AddRangeValues(ToBoxType(value, mapper));
194      box.SetExtension<BoolArrayBox>(BoolArrayBox.BoolArray, b.Build());
195    }
196    protected override T Extract(Box box, Type type, Mapper mapper) {
197      return ToValueType(box.GetExtension(BoolArrayBox.BoolArray).ValuesList, type, mapper);
198    }
199    protected abstract IEnumerable<bool> ToBoxType(T value, Mapper mapper);
200    protected abstract T ToValueType(IEnumerable<bool> value, Type type, Mapper mapper);
201  }
202  [StorableType("fb338e93-09c1-4e6e-8731-c5fefbb7fa82")]
203  public abstract class IntArrayBoxTransformer<T> : BoxTransformer<T> {
204    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
205      var b = IntArrayBox.CreateBuilder();
206      b.AddRangeValues(ToBoxType(value, mapper));
207      box.SetExtension<IntArrayBox>(IntArrayBox.IntArray, b.Build());
208    }
209    protected override T Extract(Box box, Type type, Mapper mapper) {
210      return ToValueType(box.GetExtension(IntArrayBox.IntArray).ValuesList, type, mapper);
211    }
212    protected abstract IEnumerable<int> ToBoxType(T value, Mapper mapper);
213    protected abstract T ToValueType(IEnumerable<int> value, Type type, Mapper mapper);
214  }
215  [StorableType("f751fbbd-ddd9-4f22-bf5e-9336d8913ab0")]
216  public abstract class LongArrayBoxTransformer<T> : BoxTransformer<T> {
217    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
218      var b = LongArrayBox.CreateBuilder();
219      b.AddRangeValues(ToBoxType(value, mapper));
220      box.SetExtension<LongArrayBox>(LongArrayBox.LongArray, b.Build());
221    }
222    protected override T Extract(Box box, Type type, Mapper mapper) {
223      return ToValueType(box.GetExtension(LongArrayBox.LongArray).ValuesList, type, mapper);
224    }
225    protected abstract IEnumerable<long> ToBoxType(T value, Mapper mapper);
226    protected abstract T ToValueType(IEnumerable<long> value, Type type, Mapper mapper);
227  }
228  [StorableType("912793f8-0136-4621-880c-5d6da0bbefa5")]
229  public abstract class UnsignedIntArrayBoxTransformer<T> : BoxTransformer<T> {
230    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
231      var b = UnsignedIntArrayBox.CreateBuilder();
232      b.AddRangeValues(ToBoxType(value, mapper));
233      box.SetExtension<UnsignedIntArrayBox>(UnsignedIntArrayBox.UnsignedIntArray, b.Build());
234    }
235    protected override T Extract(Box box, Type type, Mapper mapper) {
236      return ToValueType(box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray).ValuesList, type, mapper);
237    }
238    protected abstract IEnumerable<uint> ToBoxType(T value, Mapper mapper);
239    protected abstract T ToValueType(IEnumerable<uint> value, Type type, Mapper mapper);
240  }
241  [StorableType("e9b844a1-9f14-438f-abd8-8af0fdb73d77")]
242  public abstract class UnsignedLongArrayBoxTransformer<T> : BoxTransformer<T> {
243    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
244      var b = UnsignedLongArrayBox.CreateBuilder();
245      b.AddRangeValues(ToBoxType(value, mapper));
246      box.SetExtension<UnsignedLongArrayBox>(UnsignedLongArrayBox.UnsignedLongArray, b.Build());
247    }
248    protected override T Extract(Box box, Type type, Mapper mapper) {
249      return ToValueType(box.GetExtension(UnsignedLongArrayBox.UnsignedLongArray).ValuesList, type, mapper);
250    }
251    protected abstract IEnumerable<ulong> ToBoxType(T value, Mapper mapper);
252    protected abstract T ToValueType(IEnumerable<ulong> value, Type type, Mapper mapper);
253  }
254  [StorableType("f08a787d-d695-44ee-a9ac-e8b966f98c34")]
255  public abstract class FloatArrayBoxTransformer<T> : BoxTransformer<T> {
256    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
257      var b = FloatArrayBox.CreateBuilder();
258      b.AddRangeValues(ToBoxType(value, mapper));
259      box.SetExtension<FloatArrayBox>(FloatArrayBox.FloatArray, b.Build());
260    }
261    protected override T Extract(Box box, Type type, Mapper mapper) {
262      return ToValueType(box.GetExtension(FloatArrayBox.FloatArray).ValuesList, type, mapper);
263    }
264    protected abstract IEnumerable<float> ToBoxType(T value, Mapper mapper);
265    protected abstract T ToValueType(IEnumerable<float> value, Type type, Mapper mapper);
266  }
267  [StorableType("622efa85-484e-459e-b79d-4462ba225394")]
268  public abstract class DoubleArrayBoxTransformer<T> : BoxTransformer<T> {
269    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
270      var b = DoubleArrayBox.CreateBuilder();
271      b.AddRangeValues(ToBoxType(value, mapper));
272      box.SetExtension<DoubleArrayBox>(DoubleArrayBox.DoubleArray, b.Build());
273    }
274    protected override T Extract(Box box, Type type, Mapper mapper) {
275      return ToValueType(box.GetExtension(DoubleArrayBox.DoubleArray).ValuesList, type, mapper);
276    }
277    protected abstract IEnumerable<double> ToBoxType(T value, Mapper mapper);
278    protected abstract T ToValueType(IEnumerable<double> value, Type type, Mapper mapper);
279  }
280  [StorableType("5dfba6f6-2762-4d08-9c34-f6d10cc20913")]
281  public abstract class StringArrayBoxTransformer<T> : BoxTransformer<T> {
282    protected override void Populate(Box.Builder box, T value, Mapper mapper) {
283      var b = StringArrayBox.CreateBuilder();
284      b.AddRangeValues(ToBoxType(value, mapper));
285      box.SetExtension<StringArrayBox>(StringArrayBox.StringArray, b.Build());
286    }
287    protected override T Extract(Box box, Type type, Mapper mapper) {
288      return ToValueType(box.GetExtension(StringArrayBox.StringArray).ValuesList, type, mapper);
289    }
290    protected abstract IEnumerable<string> ToBoxType(T value, Mapper mapper);
291    protected abstract T ToValueType(IEnumerable<string> value, Type type, Mapper mapper);
292  }
293  #endregion
294
295  [Transformer("11B822C9-46A0-4B65-AE4A-D12F63DDE9F6", 50)]
296  [StorableType("7d783c07-2e05-4d89-9c06-b243c546e667")]
297  internal sealed class TypeTransformer : Transformer {
298    public override bool CanTransformType(Type type) {
299      return typeof(Type).IsAssignableFrom(type);
300    }
301    public override Box ToBox(object o, Mapper mapper) {
302      var box = Box.CreateBuilder();
303      box.TransformerId = mapper.GetTransformerId(this);
304      Populate(box, o, mapper);
305      return box.Build();
306    }
307    private void Populate(Box.Builder box, object value, Mapper mapper) {
308      var type = (Type)value;
309      var typeBox = TypeBox.CreateBuilder();
310      if (type.IsGenericType) {
311        box.TypeId = mapper.GetTypeId(type.GetGenericTypeDefinition());
312        typeBox.AddRangeGenericTypeIds(type.GetGenericArguments().Select(t => mapper.GetBoxId(t)));
313      } else if (type.IsArray) {
314        box.TypeId = mapper.GetTypeId(typeof(Array));
315        typeBox.AddGenericTypeIds(mapper.GetBoxId(type.GetElementType()));
316      } else {
317        box.TypeId = mapper.GetTypeId(type);
318      }
319
320      if (StorableTypeAttribute.IsStorableType(type))
321        typeBox.Version = StorableTypeAttribute.GetStorableTypeAttribute(type).Version;
322      box.SetExtension(TypeBox.Type, typeBox.Build());
323    }
324    public override object ToObject(Box box, Mapper mapper) {
325      return Extract(box, mapper.GetType(box.TypeId), mapper);
326    }
327    private object Extract(Box box, Type type, Mapper mapper) {
328      var b = box.GetExtension(TypeBox.Type);
329      if (type.IsGenericType) {
330        return type.MakeGenericType(b.GenericTypeIdsList.Select(id => (Type)mapper.GetObject(id)).ToArray());
331      } else if (type == typeof(Array)) {
332        return ((Type)mapper.GetObject(b.GetGenericTypeIds(0))).MakeArrayType();
333      } else {
334        return type;
335      }
336    }
337  }
338
339  #region Primitive Value Types
340  [Transformer("268617FE-3F0F-4029-8248-EDA420901FB6", 10000)]
341  [StorableType("320cba58-1aaf-492b-be3b-f59eea085ebe")]
342  internal sealed class ObjectTransformer : BoxTransformer<object> {
343    protected override void Populate(Box.Builder box, object value, Mapper mapper) { }
344    protected override object Extract(Box box, Type type, Mapper mapper) { return new object(); }
345  }
346
347  [Transformer("9FA1C6A8-517E-4623-AC1B-7E9AEF6ED13D", 200)]
348  [StorableType("be3c6d74-5c67-4a54-ab3c-f1040795d364")]
349  internal sealed class BoolTransformer : BoolBoxTransformer<bool> {
350    protected override bool ToBoxType(bool value, Mapper mapper) { return value; }
351    protected override bool ToValueType(bool value, Type type, Mapper mapper) { return value; }
352  }
353
354  [Transformer("059633E9-12CB-43EC-8544-57746536E281", 201)]
355  [StorableType("3f1e111d-3ea8-4318-884f-185ef96bdfe4")]
356  internal sealed class ByteTransformer : UnsignedIntBoxTransformer<byte> {
357    protected override uint ToBoxType(byte value, Mapper mapper) { return value; }
358    protected override byte ToValueType(uint value, Type type, Mapper mapper) { return (byte)value; }
359  }
360
361  [Transformer("5DC2F3AC-0852-4B57-A861-D29CC814A94C", 202)]
362  [StorableType("498da443-43d3-4bc4-9ee9-c41416bb5c6b")]
363  internal sealed class SByteTransformer : IntBoxTransformer<sbyte> {
364    protected override int ToBoxType(sbyte value, Mapper mapper) { return value; }
365    protected override sbyte ToValueType(int value, Type type, Mapper mapper) { return (sbyte)value; }
366  }
367
368  [Transformer("B862E642-A94A-4870-8065-06126A35A9E1", 203)]
369  [StorableType("9f9ea850-1dd5-4dc0-abb5-8e75b22a0ffe")]
370  internal sealed class ShortTransformer : IntBoxTransformer<short> {
371    protected override int ToBoxType(short value, Mapper mapper) { return value; }
372    protected override short ToValueType(int value, Type type, Mapper mapper) { return (short)value; }
373  }
374
375  [Transformer("D1D3062D-F1BB-4189-AE50-D6986E1DEB4E", 204)]
376  [StorableType("4b70fdb5-a2ed-4a08-ab17-10fb5db11ada")]
377  internal sealed class UShortTransformer : UnsignedIntBoxTransformer<ushort> {
378    protected override uint ToBoxType(ushort value, Mapper mapper) { return value; }
379    protected override ushort ToValueType(uint value, Type type, Mapper mapper) { return (ushort)value; }
380  }
381
382  [Transformer("6C444645-3062-4D15-AD01-E6E1B0692A2B", 205)]
383  [StorableType("fdf24c34-7c10-49bb-aad2-a668c1490381")]
384  internal sealed class CharTransformer : UnsignedIntBoxTransformer<char> {
385    protected override uint ToBoxType(char value, Mapper mapper) { return value; }
386    protected override char ToValueType(uint value, Type type, Mapper mapper) { return (char)value; }
387  }
388
389  [Transformer("649E73B2-EFA6-4E01-BCB4-4B29D652C9CB", 206)]
390  [StorableType("0b2bcdf7-5787-4027-acd4-46f5f7641bff")]
391  internal sealed class IntTransformer : IntBoxTransformer<int> {
392    protected override int ToBoxType(int value, Mapper mapper) { return value; }
393    protected override int ToValueType(int value, Type type, Mapper mapper) { return value; }
394  }
395
396  [Transformer("BCF25010-81A2-49BC-88CC-407D3F393D5B", 207)]
397  [StorableType("eec933ec-f705-435e-9b7e-e4fc4d90c21e")]
398  internal sealed class UIntTransformer : UnsignedIntBoxTransformer<uint> {
399    protected override uint ToBoxType(uint value, Mapper mapper) { return value; }
400    protected override uint ToValueType(uint value, Type type, Mapper mapper) { return value; }
401  }
402
403  [Transformer("B6F6ACAE-755C-47EE-B8BF-7CDECBE19C30", 208)]
404  [StorableType("b0edffdd-8a00-48e1-a7a1-808ddec3b933")]
405  internal sealed class LongTransformer : LongBoxTransformer<long> {
406    protected override long ToBoxType(long value, Mapper mapper) { return value; }
407    protected override long ToValueType(long value, Type type, Mapper mapper) { return value; }
408  }
409
410  [Transformer("82333ACA-F041-44E0-B365-27C399594BA7", 209)]
411  [StorableType("0ebd1d6b-aa2d-442b-b524-68558d77e22e")]
412  internal sealed class ULongTransformer : UnsignedLongBoxTransformer<ulong> {
413    protected override ulong ToBoxType(ulong value, Mapper mapper) { return value; }
414    protected override ulong ToValueType(ulong value, Type type, Mapper mapper) { return value; }
415  }
416
417  [Transformer("8FE91ECF-2261-4934-BECD-C38923B7A703", 210)]
418  [StorableType("d9f05b32-218f-4b8e-9ec7-ab5dcbbaf0fc")]
419  internal sealed class FloatTransformer : FloatBoxTransformer<float> {
420    protected override float ToBoxType(float value, Mapper mapper) { return value; }
421    protected override float ToValueType(float value, Type type, Mapper mapper) { return value; }
422  }
423
424  [Transformer("070D23EA-7F38-46B7-A667-219BEF914E49", 211)]
425  [StorableType("70b79b46-2713-42ac-ace6-a55ffb0624be")]
426  internal sealed class DoubleTransformer : DoubleBoxTransformer<double> {
427    protected override double ToBoxType(double value, Mapper mapper) { return value; }
428    protected override double ToValueType(double value, Type type, Mapper mapper) { return value; }
429  }
430
431  [Transformer("BA55C7A6-C91E-4351-A889-E4A7E647F16D", 212)]
432  [StorableType("4ed946fd-aac8-407b-bb9e-093048024d15")]
433  internal sealed class DateTimeTransformer : LongBoxTransformer<DateTime> {
434    protected override long ToBoxType(DateTime value, Mapper mapper) { return value.Ticks; }
435    protected override DateTime ToValueType(long value, Type type, Mapper mapper) { return new DateTime(value); }
436  }
437
438  [Transformer("0C91441B-2D97-432B-B493-D6EC483FC5AD", 213)]
439  [StorableType("bbc20151-b695-43dc-a38a-a610c8410fd2")]
440  internal sealed class TimeSpanTransformer : LongBoxTransformer<TimeSpan> {
441    protected override long ToBoxType(TimeSpan value, Mapper mapper) { return value.Ticks; }
442    protected override TimeSpan ToValueType(long value, Type type, Mapper mapper) { return new TimeSpan(value); }
443  }
444
445  [Transformer("0B540EAC-79AB-40CF-8277-D2C81135FEB6", 214)]
446  [StorableType("aac1fee2-ea34-46b4-9db7-3ae8da8927eb")]
447  internal sealed class ColorTransformers : IntBoxTransformer<Color> {
448    protected override int ToBoxType(Color value, Mapper mapper) { return value.ToArgb(); }
449    protected override Color ToValueType(int value, Type type, Mapper mapper) { return Color.FromArgb(value); }
450  }
451
452  [Transformer("2E6D4A40-B4BE-425F-8E35-2D7C00054639", 215)]
453  [StorableType("11a33d00-5edc-4c49-884a-9f2809c23b30")]
454  internal sealed class PointTransformer : IntArrayBoxTransformer<Point> {
455    protected override IEnumerable<int> ToBoxType(Point value, Mapper mapper) { return new int[] { value.X, value.Y }; }
456    protected override Point ToValueType(IEnumerable<int> value, Type type, Mapper mapper) { return new Point(value.ElementAt(0), value.ElementAt(1)); }
457  }
458
459  [Transformer("97B5CFC8-CDFA-4EB5-B4CD-5B3CFA5CD844", 216)]
460  [StorableType("993238c2-a2ed-4e0d-84ae-ad025f2f20f4")]
461  internal sealed class KeyValuePairTransformer : BoxTransformer<object> {
462    public override bool CanTransformType(Type type) {
463      return type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>));
464    }
465    protected override void Populate(Box.Builder box, object value, Mapper mapper) {
466      var b = UnsignedIntArrayBox.CreateBuilder();
467      var type = value.GetType();
468      var pair = new uint[2];
469      pair[0] = mapper.GetBoxId(type.GetProperty("Key").GetValue(value));
470      pair[1] = mapper.GetBoxId(type.GetProperty("Value").GetValue(value));
471      b.AddRangeValues(pair);
472      box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, b.Build());
473    }
474    public override void FillFromBox(object obj, Box box, Mapper mapper) {
475      var b = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
476      var key = mapper.GetObject(b.GetValues(0));
477      var val = mapper.GetObject(b.GetValues(1));
478      var type = obj.GetType();
479      //DataMemberAccessor.GenerateFieldSetter(type.GetField("key", BindingFlags.NonPublic | BindingFlags.Instance))(obj, key);
480      //DataMemberAccessor.GenerateFieldSetter(type.GetField("value", BindingFlags.NonPublic | BindingFlags.Instance))(obj, val);
481      type.GetField("key", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(obj, key);
482      type.GetField("value", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(obj, val);
483    }
484    protected override object Extract(Box box, Type type, Mapper mapper) {
485      return Activator.CreateInstance(type);
486    }
487  }
488
489  [Transformer("EBD8BF65-D97D-4FD4-BF4F-9D58A72B6249", 217)]
490  [StorableType("b7725dfa-94fb-4169-b375-6e46b7ca4810")]
491  internal sealed class DecimalTransformer : UnsignedIntBoxTransformer<decimal> {
492    protected override uint ToBoxType(decimal value, Mapper mapper) {
493      return mapper.GetStringId(FormatG30(value));
494    }
495    protected override decimal ToValueType(uint value, Type type, Mapper mapper) {
496      var converter = TypeDescriptor.GetConverter(typeof(Font));
497      return ParseG30(mapper.GetString(value));
498    }
499    private static decimal ParseG30(string s) {
500      decimal d;
501      if (decimal.TryParse(s,
502        NumberStyles.AllowDecimalPoint |
503        NumberStyles.AllowExponent |
504        NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out d))
505        return d;
506      throw new FormatException(
507        string.Format("Invalid decimal G30 number format \"{0}\" could not be parsed", s));
508    }
509    private static string FormatG30(decimal d) {
510      return d.ToString("g30", CultureInfo.InvariantCulture);
511    }
512  }
513  #endregion
514
515  #region String
516  [Transformer("E75A594C-0034-4DAB-B28E-8F84F9F6DE8D", 218)]
517  [StorableType("ba52fce8-bf0c-47ad-95dd-df5d9281942d")]
518  internal sealed class StringTransformer : UnsignedIntBoxTransformer<string> {
519    protected override uint ToBoxType(string value, Mapper mapper) { return mapper.GetStringId(value); }
520    protected override string ToValueType(uint value, Type type, Mapper mapper) { return mapper.GetString(value); }
521  }
522  #endregion
523
524  #region Enum
525  [Transformer("93FF076B-BC4B-4C39-8C40-15E004468C98", 219)]
526  [StorableType("f8a7dbb4-e1ce-4db8-905e-f3a05f5ca000")]
527  internal sealed class EnumTransformer : Transformer {
528    public override bool CanTransformType(Type type) {
529      return typeof(Enum).IsAssignableFrom(type);
530    }
531
532    public override Box ToBox(object o, Mapper mapper) {
533      var boxBuilder = Box.CreateBuilder();
534      var enumBuilder = new UnsignedIntBox.Builder();
535
536      boxBuilder.TransformerId = mapper.GetTransformerId(this);
537      boxBuilder.TypeId = mapper.GetStringId(o.GetType().AssemblyQualifiedName);
538      enumBuilder.Value = mapper.GetStringId(Enum.Format(o.GetType(), o, "G"));
539
540      boxBuilder.SetExtension(UnsignedIntBox.UnsignedInt, enumBuilder.Build());
541      return boxBuilder.Build();
542    }
543
544    public override object ToObject(Box box, Mapper mapper) {
545      uint value = box.GetExtension(UnsignedIntBox.UnsignedInt).Value;
546      return Enum.Parse(Type.GetType(mapper.GetString(box.TypeId)), mapper.GetString(value));
547    }
548  }
549  #endregion
550
551  #region Struct
552  [Transformer("89DAAFC5-726C-48D4-A4E0-2B0D27329642", 220)]
553  [StorableType("c3c4ad38-7110-439c-b73c-d33466db3db4")]
554  internal sealed class StructTransformer : Transformer {
555    public override bool CanTransformType(Type type) {
556      return type.IsValueType && !type.IsPrimitive && !type.IsEnum && type.IsSealed;
557    }
558    public override Box ToBox(object o, Mapper mapper) {
559      var box = Box.CreateBuilder();
560      box.TransformerId = mapper.GetTransformerId(this);
561      Populate(box, o, mapper);
562      return box.Build();
563    }
564    public override object ToObject(Box box, Mapper mapper) {
565      return Extract(box, (Type)mapper.GetObject(box.TypeId), mapper);
566    }
567    private void Populate(Box.Builder box, object value, Mapper mapper) {
568      var b = StorableClassBox.CreateBuilder();
569      var type = value.GetType();
570
571      var components = new Dictionary<uint, uint>();
572      foreach (var fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) {
573        var component = mapper.GetBoxId(fieldInfo.GetValue(value));
574        components.Add(mapper.GetStringId(fieldInfo.Name), component);
575      }
576
577      b.AddRangeKeyIds(components.Keys);
578      b.AddRangeValueIds(components.Values);
579
580      box.TypeId = mapper.GetBoxId(type);
581      box.SetExtension(StorableClassBox.StorableClass, b.Build());
582    }
583    private object Extract(Box box, Type type, Mapper mapper) {
584      var data = box.GetExtension(StorableClassBox.StorableClass);
585      var obj = Activator.CreateInstance(type);
586
587      var components = new Dictionary<uint, uint>();
588      for (int i = 0; i < data.KeyIdsList.Count; i++) {
589        components.Add(data.KeyIdsList[i], data.ValueIdsList[i]);
590      }
591
592      foreach (var t in components) {
593        string name = mapper.GetString(t.Key);
594        MemberInfo[] mis = type.GetMember(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
595        if (mis.Length != 1)
596          throw new Exception("ambiguous struct member name " + name);
597        MemberInfo mi = mis[0];
598        if (StorableAttribute.IsStorable(mi))
599          throw new PersistenceException("Don't use stroable attributes for structs as all fields are serialized automatically.");
600        if (mi.MemberType == MemberTypes.Field)
601          ((FieldInfo)mi).SetValue(obj, mapper.GetObject(t.Value));
602        else
603          throw new Exception("invalid struct member type " + mi.MemberType.ToString());
604      }
605
606      return obj;
607    }
608  }
609  #endregion
610
611  #region Tuple
612  [Transformer("63A19D57-EEEF-4346-9F06-B35B15EBFFA3", 221)]
613  [StorableType("ab537f25-dc21-4585-b743-0350a5cf1e54")]
614  internal sealed class TupleTransformer : Transformer {
615    private static readonly HashSet<Type> supportedTypes = new HashSet<Type> {
616      typeof(Tuple<>), typeof(Tuple<,>), typeof(Tuple<,,>), typeof(Tuple<,,,>),
617      typeof(Tuple<,,,,>), typeof(Tuple<,,,,,>), typeof(Tuple<,,,,,,>), typeof(Tuple<,,,,,,,>)
618    };
619    public override bool CanTransformType(Type type) {
620      return type.IsGenericType && supportedTypes.Contains(type.GetGenericTypeDefinition());
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    private void Populate(Box.Builder box, object value, Mapper mapper) {
629      var type = value.GetType();
630      var uIntArrayBox = UnsignedIntArrayBox.CreateBuilder();
631      for (int i = 1; i <= type.GetGenericArguments().Length; i++) {
632        string name = string.Format("Item{0}", i);
633        uIntArrayBox.AddValues(mapper.GetBoxId(type.GetProperty(name).GetValue(value)));
634      }
635      box.TypeId = mapper.GetBoxId(type);
636      box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, uIntArrayBox.Build());
637    }
638    public override object ToObject(Box box, Mapper mapper) {
639      var type = (Type)mapper.GetObject(box.TypeId);
640      var defaultValues = type.GetGenericArguments().Select(x =>
641        x.IsValueType ? Activator.CreateInstance(x) : null
642      ).ToArray();
643      return Activator.CreateInstance(type, defaultValues);
644    }
645    public override void FillFromBox(object obj, Box box, Mapper mapper) {
646      var uIntArrayBox = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
647      var elements = uIntArrayBox.ValuesList.Select(mapper.GetObject).ToArray();
648      var type = obj.GetType();
649      for (int i = 1; i <= elements.Length; i++) {
650        string name = string.Format("m_Item{0}", i);
651        DataMemberAccessor.GenerateFieldSetter(type.GetField(name, BindingFlags.NonPublic | BindingFlags.Instance))(obj, elements[i - 1]);
652      }
653    }
654  }
655  #endregion
656
657  #region Bitmap
658  [Transformer("D0ADB806-2DFD-459D-B5DA-14B5F1152534", 222)]
659  [StorableType("b13d6153-e71d-4b76-9893-81d3570403e8")]
660  internal sealed class BitmapTransformer : ByteArrayBoxTransformer<Bitmap> {
661    protected override ByteString ToByteString(Bitmap value) {
662      lock (value)
663        using (var ms = new MemoryStream()) {
664          value.Save(ms, ImageFormat.Png);
665          return ByteString.CopyFrom(ms.ToArray());
666        }
667    }
668    protected override Bitmap FromByteString(ByteString byteString) {
669      using (var ms = new MemoryStream()) {
670        ms.Write(byteString.ToArray(), 0, byteString.Length);
671        ms.Seek(0, SeekOrigin.Begin);
672        return new Bitmap(ms);
673      }
674    }
675  }
676  #endregion
677
678  #region Font
679  [Transformer("AFF27987-3301-4D70-9601-EFCA31BDA0DB", 223)]
680  [StorableType("b9f9a371-4dcb-478b-b0d4-6f87a15c02b5")]
681  internal sealed class FontTransformer : UnsignedIntArrayBoxTransformer<Font> {
682
683    protected override IEnumerable<uint> ToBoxType(Font value, Mapper mapper) {
684      yield return mapper.GetStringId(GetFontFamilyName(value.FontFamily));
685      yield return mapper.GetBoxId(value.Size);
686      yield return mapper.GetBoxId(value.Style);
687      yield return mapper.GetBoxId(value.Unit);
688      yield return mapper.GetBoxId(value.GdiCharSet);
689      yield return mapper.GetBoxId(value.GdiVerticalFont);
690    }
691    protected override Font ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) {
692      var fontData = value.ToArray();
693      return new Font(
694        GetFontFamily(mapper.GetString(fontData[0])),
695        (float)mapper.GetObject(fontData[1]),
696        (FontStyle)mapper.GetObject(fontData[2]),
697        (GraphicsUnit)mapper.GetObject(fontData[3]),
698        (byte)mapper.GetObject(fontData[4]),
699        (bool)mapper.GetObject(fontData[5])
700      );
701    }
702
703    public const string GENERIC_MONOSPACE_NAME = "_GenericMonospace";
704    public const string GENERIC_SANS_SERIF_NAME = "_GenericSansSerif";
705    public const string GENERIC_SERIF_NAME = "_GenericSerif";
706
707    public static FontFamily GetFontFamily(string name) {
708      if (name == GENERIC_MONOSPACE_NAME) return FontFamily.GenericMonospace;
709      if (name == GENERIC_SANS_SERIF_NAME) return FontFamily.GenericSansSerif;
710      if (name == GENERIC_SERIF_NAME) return FontFamily.GenericSerif;
711      return new FontFamily(name);
712    }
713
714    public static string GetFontFamilyName(FontFamily ff) {
715      if (ff.Equals(FontFamily.GenericMonospace)) return GENERIC_MONOSPACE_NAME;
716      if (ff.Equals(FontFamily.GenericSansSerif)) return GENERIC_SANS_SERIF_NAME;
717      if (ff.Equals(FontFamily.GenericSerif)) return GENERIC_SERIF_NAME;
718      return ff.Name;
719    }
720  }
721  #endregion
722
723  #region Array Types
724  [Transformer("FF89F6D1-CDE3-498E-9166-F70AC6EB01F1", 301)]
725  [StorableType("59d8363a-7e6c-4d29-9432-8274acd2145f")]
726  internal sealed class ByteArrayTransformer : ByteArrayBoxTransformer<byte[]> {
727    protected override ByteString ToByteString(byte[] value) {
728      return ByteString.CopyFrom(value);
729    }
730    protected override byte[] FromByteString(ByteString byteString) {
731      return byteString.ToArray();
732    }
733  }
734
735  [Transformer("B49B3F2D-2E97-4BAB-8705-8D29DA707C6A", 302)]
736  [StorableType("304c7463-4575-481b-8922-6a54957305c7")]
737  internal sealed class SByteArrayTransformer : ByteArrayBoxTransformer<sbyte[]> {
738    protected override ByteString ToByteString(sbyte[] value) {
739      return ByteString.CopyFrom(value.Select(b => (byte)b).ToArray());
740    }
741    protected override sbyte[] FromByteString(ByteString byteString) {
742      return byteString.Select(b => (sbyte)b).ToArray();
743    }
744  }
745
746  [Transformer("F25A73B2-6B67-4493-BD59-B836AF4455D1", 300)]
747  [StorableType("06fdb68c-ec5e-4cb5-9ad1-063fca0b9c2e")]
748  internal sealed class BoolArrayTransformer : BoolArrayBoxTransformer<bool[]> {
749    protected override IEnumerable<bool> ToBoxType(bool[] value, Mapper mapper) { return value; }
750    protected override bool[] ToValueType(IEnumerable<bool> value, Type type, Mapper mapper) { return value.ToArray(); }
751  }
752
753  [Transformer("2811FDD4-6800-4CBA-86D7-9071ED5775ED", 303)]
754  [StorableType("832d8971-51b1-4264-b7eb-07d6e836bf7b")]
755  internal sealed class ShortArrayTransformer : ByteArrayBoxTransformer<short[]> {
756    protected override ByteString ToByteString(short[] value) {
757      var res = new byte[value.Length * 2];
758      for (int i = 0; i < value.Length; i++) {
759        var bytes = BitConverter.GetBytes(value[i]);
760        Array.Copy(bytes, 0, res, i * 2, 2);
761      }
762      return ByteString.CopyFrom(res);
763    }
764    protected override short[] FromByteString(ByteString byteString) {
765      var bytes = byteString.ToArray();
766      var res = new short[bytes.Length / 2];
767      for (int i = 0; i < bytes.Length; i += 2) {
768        res[i / 2] = BitConverter.ToInt16(bytes, i);
769      }
770      return res;
771    }
772  }
773
774  [Transformer("1AAC2625-356C-40BC-8CB4-15CB3D047EB8", 304)]
775  [StorableType("c303b4cf-ffd0-47e2-9d94-07f2d558d17f")]
776  internal sealed class UShortArrayTransformer : ByteArrayBoxTransformer<ushort[]> {
777    protected override ByteString ToByteString(ushort[] value) {
778      var res = new byte[value.Length * 2];
779      for (int i = 0; i < value.Length; i++) {
780        var bytes = BitConverter.GetBytes(value[i]);
781        Array.Copy(bytes, 0, res, i * 2, 2);
782      }
783      return ByteString.CopyFrom(res);
784    }
785    protected override ushort[] FromByteString(ByteString byteString) {
786      var bytes = byteString.ToArray();
787      var res = new ushort[bytes.Length / 2];
788      for (int i = 0; i < bytes.Length; i += 2) {
789        res[i / 2] = BitConverter.ToUInt16(bytes, i);
790      }
791      return res;
792    }
793  }
794
795  [Transformer("12F19098-5D49-4C23-8897-69087F1C146D", 305)]
796  [StorableType("55f7c8b0-f2aa-4830-857d-6ce2807da138")]
797  internal sealed class CharArrayTransformer : ByteArrayBoxTransformer<char[]> {
798    protected override ByteString ToByteString(char[] value) {
799      var res = new byte[value.Length * 2];
800      for (int i = 0; i < value.Length; i++) {
801        var bytes = BitConverter.GetBytes(value[i]);
802        Array.Copy(bytes, 0, res, i * 2, 2);
803      }
804      return ByteString.CopyFrom(res);
805    }
806    protected override char[] FromByteString(ByteString byteString) {
807      var bytes = byteString.ToArray();
808      var res = new char[bytes.Length / 2];
809      for (int i = 0; i < bytes.Length; i += 2) {
810        res[i / 2] = BitConverter.ToChar(bytes, i);
811      }
812      return res;
813    }
814  }
815
816  [Transformer("5F6DC3BC-4433-4AE9-A636-4BD126F7DACD", 306)]
817  [StorableType("e7089621-5236-4d37-a592-c8a0816c59fa")]
818  internal sealed class IntArrayTransformer : IntArrayBoxTransformer<int[]> {
819    protected override IEnumerable<int> ToBoxType(int[] value, Mapper mapper) { return value; }
820    protected override int[] ToValueType(IEnumerable<int> value, Type type, Mapper mapper) { return value.ToArray(); }
821  }
822
823  [Transformer("3F10274F-D350-4C82-89EA-A5EB36D4EFCC", 307)]
824  [StorableType("0972d235-5e21-4abf-84f8-aad1bbfdff40")]
825  internal sealed class UIntArrayTransformer : UnsignedIntArrayBoxTransformer<uint[]> {
826    protected override IEnumerable<uint> ToBoxType(uint[] value, Mapper mapper) { return value; }
827    protected override uint[] ToValueType(IEnumerable<uint> value, Type type, Mapper mapper) { return value.ToArray(); }
828  }
829
830  [Transformer("E9D550E2-57F7-47F3-803D-37A619DA1A5C", 308)]
831  [StorableType("0c656d5d-11dc-4a75-895a-06701048cd70")]
832  internal sealed class LongArrayTransformer : LongArrayBoxTransformer<long[]> {
833    protected override IEnumerable<long> ToBoxType(long[] value, Mapper mapper) { return value; }
834    protected override long[] ToValueType(IEnumerable<long> value, Type type, Mapper mapper) { return value.ToArray(); }
835  }
836
837  [Transformer("C02A205B-2176-4282-AC2B-ADEF96DDBE24", 309)]
838  [StorableType("c5f87514-a6c1-400b-9a6c-8adcf980f701")]
839  internal sealed class ULongArrayTransformer : UnsignedLongArrayBoxTransformer<ulong[]> {
840    protected override IEnumerable<ulong> ToBoxType(ulong[] value, Mapper mapper) { return value; }
841    protected override ulong[] ToValueType(IEnumerable<ulong> value, Type type, Mapper mapper) { return value.ToArray(); }
842  }
843
844  [Transformer("3C4590D9-C76E-4AFB-98FD-E50D3D051648", 310)]
845  [StorableType("ecc63624-1b78-4bef-8326-74c1a43078ff")]
846  internal sealed class FloatArrayTransformer : FloatArrayBoxTransformer<float[]> {
847    protected override IEnumerable<float> ToBoxType(float[] value, Mapper mapper) { return value; }
848    protected override float[] ToValueType(IEnumerable<float> value, Type type, Mapper mapper) { return value.ToArray(); }
849  }
850
851  [Transformer("FB98C399-9323-4470-9A85-9186C2B2D5D4", 311)]
852  [StorableType("03025baa-f57a-48de-b0e2-8e6dc10740ee")]
853  internal sealed class DoubleArrayTransformer : DoubleArrayBoxTransformer<double[]> {
854    protected override IEnumerable<double> ToBoxType(double[] value, Mapper mapper) { return value; }
855    protected override double[] ToValueType(IEnumerable<double> value, Type type, Mapper mapper) { return value.ToArray(); }
856  }
857
858  [Transformer("68332513-9CF1-47FA-A093-6DDB663186EC", 312)]
859  [StorableType("791ea34d-e071-4ef2-812b-eb8e2faced45")]
860  internal sealed class StringArrayTransformer : StringArrayBoxTransformer<string[]> {
861    protected override IEnumerable<string> ToBoxType(string[] value, Mapper mapper) { return value; }
862    protected override string[] ToValueType(IEnumerable<string> value, Type type, Mapper mapper) { return value.ToArray(); }
863  }
864
865  [Transformer("B01ADF0A-ACAA-444E-9F82-0C7C2AF1F490", 400)]
866  [StorableType("56ae4f46-9fc1-4243-a07f-7b7c24e30545")]
867  internal sealed class ArrayTransformer : Transformer {
868    public override bool CanTransformType(Type type) {
869      return type.IsArray;
870    }
871    public override Box ToBox(object o, Mapper mapper) {
872      var box = Box.CreateBuilder();
873      box.TransformerId = mapper.GetTransformerId(this);
874      Populate(box, o, mapper);
875      return box.Build();
876    }
877    private void Populate(Box.Builder box, object value, Mapper mapper) {
878      var type = value.GetType();
879      var array = (Array)value;
880      var rank = array.Rank;
881
882      var uIntArrayBox = UnsignedIntArrayBox.CreateBuilder();
883      uIntArrayBox.AddValues(mapper.GetBoxId(rank));
884
885      int[] lengths = new int[rank];
886      int[] lowerBounds = new int[rank];
887      for (int i = 0; i < rank; i++) {
888        lengths[i] = array.GetLength(i);
889        lowerBounds[i] = array.GetLowerBound(i);
890      }
891
892      uIntArrayBox.AddRangeValues(lengths.Select(x => mapper.GetBoxId(x)));
893      uIntArrayBox.AddRangeValues(lowerBounds.Select(x => mapper.GetBoxId(x)));
894
895      int[] positions = (int[])lowerBounds.Clone();
896      while (positions[rank - 1] < lengths[rank - 1] + lowerBounds[rank - 1]) {
897        uIntArrayBox.AddValues(mapper.GetBoxId(array.GetValue(positions)));
898        positions[0] += 1;
899        for (int i = 0; i < rank - 1; i++) {
900          if (positions[i] >= lowerBounds[i] + lengths[i]) {
901            positions[i] = lowerBounds[i];
902            positions[i + 1] += 1;
903          } else {
904            break;
905          }
906        }
907      }
908
909      box.TypeId = mapper.GetBoxId(type);
910      box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, uIntArrayBox.Build());
911    }
912    public override object ToObject(Box box, Mapper mapper) {
913      var uIntArrayBox = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
914      var rank = (int)mapper.GetObject(uIntArrayBox.GetValues(0));
915
916      int[] lengths = new int[rank], lowerBounds = new int[rank];
917      for (int i = 0; i < rank; i++)
918        lengths[i] = (int)mapper.GetObject(uIntArrayBox.GetValues(i + 1));
919      for (int i = 0; i < rank; i++)
920        lowerBounds[i] = (int)mapper.GetObject(uIntArrayBox.GetValues(i + 1 + rank));
921
922      var type = (Type)mapper.GetObject(box.TypeId);
923      return Array.CreateInstance(type.GetElementType(), lengths, lowerBounds);
924    }
925    public override void FillFromBox(object obj, Box box, Mapper mapper) {
926      var array = (Array)obj;
927      var uIntArrayBox = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
928      var rank = (int)mapper.GetObject(uIntArrayBox.GetValues(0));
929
930      int[] lengths = new int[rank], lowerBounds = new int[rank];
931      for (int i = 0; i < rank; i++)
932        lengths[i] = (int)mapper.GetObject(uIntArrayBox.GetValues(i + 1));
933      for (int i = 0; i < rank; i++)
934        lowerBounds[i] = (int)mapper.GetObject(uIntArrayBox.GetValues(i + 1 + rank));
935
936      int[] positions = (int[])lowerBounds.Clone();
937      var e = uIntArrayBox.ValuesList.Skip(1 + 2 * rank).GetEnumerator();
938      while (e.MoveNext()) {
939        int[] currentPositions = positions;
940        array.SetValue(mapper.GetObject(e.Current), currentPositions);
941        positions[0] += 1;
942        for (int i = 0; i < rank - 1; i++) {
943          if (positions[i] >= lengths[i] + lowerBounds[i]) {
944            positions[i] = lowerBounds[i];
945            positions[i + 1] += 1;
946          } else {
947            break;
948          }
949        }
950      }
951    }
952  }
953  #endregion
954
955  [Transformer("4FA5EAAF-ECC7-4A9C-84E7-6583DA96F3B9", 500)]
956  [StorableType("b19c0fe5-6bba-4492-af91-c111edce77dc")]
957  internal sealed class EnumerableTransformer : Transformer {
958    private static readonly HashSet<Type> supportedTypes = new HashSet<Type> {
959      typeof(Stack<>), typeof(Stack),
960      typeof(Queue<>), typeof(Queue),
961      typeof(HashSet<>),
962      typeof(List<>), typeof(ArrayList)
963    };
964    public override bool CanTransformType(Type type) {
965      return type.IsGenericType && supportedTypes.Contains(type.GetGenericTypeDefinition()) || supportedTypes.Contains(type);
966    }
967    public override Box ToBox(object o, Mapper mapper) {
968      var box = Box.CreateBuilder();
969      box.TransformerId = mapper.GetTransformerId(this);
970      Populate(box, o, mapper);
971      return box.Build();
972    }
973    private void Populate(Box.Builder box, object value, Mapper mapper) {
974      var uIntArrayBox = UnsignedIntArrayBox.CreateBuilder();
975
976      var type = value.GetType();
977      var propertyInfo = type.GetProperty("Comparer");
978      if (propertyInfo != null) {
979        // TODO: where to store id for comparer box? (atm: first element in int array ...)
980        var comparer = propertyInfo.GetValue(value);
981        var comparerType = comparer.GetType();
982        if (StorableTypeAttribute.IsStorableType(comparerType))
983          uIntArrayBox.AddValues(mapper.GetBoxId(comparer));
984        else if (comparerType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).Any())
985          throw new NotSupportedException("Cannot serialize non-storable equality comparers with fields");
986        else
987          uIntArrayBox.AddValues(mapper.GetBoxId(comparerType));
988      }
989
990      foreach (var item in (IEnumerable)value)
991        uIntArrayBox.AddValues(mapper.GetBoxId(item));
992
993      box.TypeId = mapper.GetBoxId(type);
994      box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, uIntArrayBox.Build());
995    }
996    public override object ToObject(Box box, Mapper mapper) {
997      var uIntArrayBox = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
998      var type = (Type)mapper.GetObject(box.TypeId);
999      return Activator.CreateInstance(type);
1000    }
1001    public override void FillFromBox(object obj, Box box, Mapper mapper) {
1002      var uIntArrayBox = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
1003      var elements = uIntArrayBox.ValuesList.Select(mapper.GetObject);
1004      var type = obj.GetType();
1005
1006      string methodName = string.Empty;
1007      if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Stack<>) || type == typeof(Stack)) {
1008        elements = elements.Reverse();
1009        methodName = "Push";
1010      } else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Queue<>) || type == typeof(Queue)) {
1011        methodName = "Enqueue";
1012      } else {
1013        methodName = "Add";
1014        if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(HashSet<>)) {
1015          var fieldInfo = type.GetField("m_comparer", BindingFlags.NonPublic | BindingFlags.Instance);
1016          var comparerObj = mapper.GetObject(uIntArrayBox.GetValues(0));
1017          var comparer = comparerObj is Type ? Activator.CreateInstance((Type)comparerObj) : comparerObj;
1018          fieldInfo.SetValue(obj, comparer);
1019          elements = elements.Skip(1);
1020        }
1021      }
1022
1023      MethodInfo addMethod = type.GetMethod(methodName);
1024      foreach (var e in elements)
1025        addMethod.Invoke(obj, new[] { e });
1026    }
1027  }
1028
1029  [Transformer("C47A62F5-F113-4A43-A8EE-CF817EC799A2", 501)]
1030  [StorableType("ba53df84-dc97-4d8d-a493-868972ed1002")]
1031  internal sealed class DictionaryTransformer : Transformer {
1032    public override bool CanTransformType(Type type) {
1033      return type.IsGenericType && typeof(Dictionary<,>) == type.GetGenericTypeDefinition();
1034    }
1035    public override Box ToBox(object o, Mapper mapper) {
1036      var box = Box.CreateBuilder();
1037      box.TransformerId = mapper.GetTransformerId(this);
1038      Populate(box, o, mapper);
1039      return box.Build();
1040    }
1041    private void Populate(Box.Builder box, object value, Mapper mapper) {
1042      var dictionaryBox = DictionaryBox.CreateBuilder();
1043      foreach (DictionaryEntry item in (IDictionary)value) {
1044        dictionaryBox.AddKeyIds(mapper.GetBoxId(item.Key));
1045        dictionaryBox.AddValueIds(mapper.GetBoxId(item.Value));
1046      }
1047
1048      var type = value.GetType();
1049      var propertyInfo = type.GetProperty("Comparer");
1050      var comparer = propertyInfo.GetValue(value);
1051
1052      var comparerType = comparer.GetType();
1053      if (StorableTypeAttribute.IsStorableType(comparerType))
1054        dictionaryBox.SetComparerId(mapper.GetBoxId(comparer));
1055      else if (comparerType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).Any())
1056        throw new NotSupportedException("Cannot serialize non-storable equality comparers with fields");
1057      else
1058        dictionaryBox.SetComparerId(mapper.GetBoxId(comparerType));
1059
1060      box.TypeId = mapper.GetBoxId(type);
1061      box.SetExtension(DictionaryBox.Dictionary, dictionaryBox.Build());
1062    }
1063    public override object ToObject(Box box, Mapper mapper) {
1064      var dictionaryBox = box.GetExtension(DictionaryBox.Dictionary);
1065      return Activator.CreateInstance((Type)mapper.GetObject(box.TypeId), dictionaryBox.ValueIdsCount);
1066    }
1067    public override void FillFromBox(object obj, Box box, Mapper mapper) {
1068      var type = obj.GetType();
1069      var dictionaryBox = box.GetExtension(DictionaryBox.Dictionary);
1070      var comparerObj = mapper.GetObject(dictionaryBox.ComparerId);
1071      var comparer = comparerObj is Type ? Activator.CreateInstance((Type)comparerObj) : comparerObj;
1072
1073      var fieldInfo = type.GetField("comparer", BindingFlags.NonPublic | BindingFlags.Instance);
1074      fieldInfo.SetValue(obj, comparer);
1075
1076
1077      var addMethod = type.GetMethod("Add");
1078      for (int i = 0; i < dictionaryBox.KeyIdsCount; i++) {
1079        var key = mapper.GetObject(dictionaryBox.GetKeyIds(i));
1080        var value = mapper.GetObject(dictionaryBox.GetValueIds(i));
1081        addMethod.Invoke(obj, new[] { key, value });
1082      }
1083    }
1084  }
1085}
Note: See TracBrowser for help on using the repository browser.