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

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

#2520: worked on persistence

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Generic;
24using System.Linq;
25using System.Reflection;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Persistence {
29  [Transformer("78556C88-0FEE-4602-95C7-A469B2DDB468", 100)]
30  internal sealed class StorableClassBoxTransformer : BoxTransformer<object> {
31    public override bool CanTransformType(Type type) {
32      return StorableClassAttribute.IsStorableClass(type);
33    }
34
35    protected override void Populate(Box.Builder box, object value, Mapper mapper) {
36      var b = StorableClassBox.CreateBuilder();
37      var type = value.GetType();
38      var typeInfo = Mapper.StaticCache.GetTypeInfo(type);
39
40      var emptyArgs = new object[0];
41      foreach (var hook in typeInfo.BeforeSerializationHooks) {
42        hook.Invoke(value, emptyArgs);
43      }
44
45      var components = new Dictionary<uint, uint>();
46      foreach (var componentInfo in typeInfo.Fields) {
47        var field = (FieldInfo)componentInfo.MemberInfo;
48        var component = mapper.GetBoxId(field.GetValue(value));
49        components.Add(mapper.GetStringId(componentInfo.Name), component);
50      }
51
52      foreach (var componentInfo in typeInfo.Properties.Where(x => x.Readable)) {
53        var property = (PropertyInfo)componentInfo.MemberInfo;
54        var component = mapper.GetBoxId(property.GetValue(value, null));
55        components.Add(mapper.GetStringId(componentInfo.Name), component);
56      }
57
58      b.AddRangeKeyIds(components.Keys);
59      b.AddRangeValueIds(components.Values);
60
61      box.SetExtension(StorableClassBox.StorableClass, b.Build());
62    }
63
64    protected override object Extract(Box box, Type type, Mapper mapper) {
65      return mapper.CreateInstance(type);
66    }
67    public override void FillFromBox(object obj, Box box, Mapper mapper) {
68      var data = box.GetExtension(StorableClassBox.StorableClass);
69      var type = obj.GetType();
70      var typeInfo = Mapper.StaticCache.GetTypeInfo(type);
71
72      var components = new Dictionary<uint, uint>();
73      for (int i = 0; i < data.KeyIdsList.Count; i++) {
74        components.Add(data.KeyIdsList[i], data.ValueIdsList[i]);
75      }
76
77      foreach (var componentInfo in typeInfo.Fields) {
78        var field = (FieldInfo)componentInfo.MemberInfo;
79        uint componentId;
80        bool found = components.TryGetValue(mapper.GetStringId(componentInfo.Name), out componentId);
81        field.SetValue(obj, found ? mapper.GetObject(componentId) : componentInfo.StorableAttribute.DefaultValue);
82      }
83
84      foreach (var componentInfo in typeInfo.Properties.Where(x => x.Writeable)) {
85        var property = (PropertyInfo)componentInfo.MemberInfo;
86        uint componentId;
87        bool found = components.TryGetValue(mapper.GetStringId(componentInfo.Name), out componentId);
88        property.SetValue(obj, found ? mapper.GetObject(componentId) : componentInfo.StorableAttribute.DefaultValue, null);
89      }
90
91      var emptyArgs = new object[0];
92      foreach (var hook in typeInfo.AfterDeserializationHooks) {
93        hook.Invoke(obj, emptyArgs);
94      }
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.