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

Last change on this file since 14713 was 14713, checked in by gkronber, 2 years ago

#2520 work in progress to support loading/saving of all samples

File size: 4.3 KB
RevLine 
[13326]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)]
[14537]30  internal sealed class StorableClassBoxTransformer : BoxTransformer<object> {
[13326]31    public override bool CanTransformType(Type type) {
[14713]32      return StorableTypeAttribute.IsStorableType(type) && !type.IsValueType && !type.IsEnum || // don't transform structs or enums
[14711]33        type.BaseType != null && CanTransformType(type.BaseType);
[13326]34    }
35
[14537]36    protected override void Populate(Box.Builder box, object value, Mapper mapper) {
37      var b = StorableClassBox.CreateBuilder();
38      var type = value.GetType();
39      var typeInfo = Mapper.StaticCache.GetTypeInfo(type);
40
[13326]41      var emptyArgs = new object[0];
42      foreach (var hook in typeInfo.BeforeSerializationHooks) {
[14537]43        hook.Invoke(value, emptyArgs);
[13326]44      }
[14537]45
[13326]46      var components = new Dictionary<uint, uint>();
47      foreach (var componentInfo in typeInfo.Fields) {
48        var field = (FieldInfo)componentInfo.MemberInfo;
[14537]49        var component = mapper.GetBoxId(field.GetValue(value));
[13326]50        components.Add(mapper.GetStringId(componentInfo.Name), component);
51      }
[14537]52
[13326]53      foreach (var componentInfo in typeInfo.Properties.Where(x => x.Readable)) {
54        var property = (PropertyInfo)componentInfo.MemberInfo;
[14537]55        var component = mapper.GetBoxId(property.GetValue(value, null));
[13326]56        components.Add(mapper.GetStringId(componentInfo.Name), component);
57      }
[14537]58
59      b.AddRangeKeyIds(components.Keys);
60      b.AddRangeValueIds(components.Values);
61
62      box.SetExtension(StorableClassBox.StorableClass, b.Build());
[13326]63    }
64
[14537]65    protected override object Extract(Box box, Type type, Mapper mapper) {
[14549]66      return mapper.CreateInstance(type);
67    }
68    public override void FillFromBox(object obj, Box box, Mapper mapper) {
[14537]69      var data = box.GetExtension(StorableClassBox.StorableClass);
[14549]70      var type = obj.GetType();
[14537]71      var typeInfo = Mapper.StaticCache.GetTypeInfo(type);
[13326]72
73      var components = new Dictionary<uint, uint>();
[14537]74      for (int i = 0; i < data.KeyIdsList.Count; i++) {
75        components.Add(data.KeyIdsList[i], data.ValueIdsList[i]);
[13326]76      }
77
78      foreach (var componentInfo in typeInfo.Fields) {
79        var field = (FieldInfo)componentInfo.MemberInfo;
80        uint componentId;
[14537]81        bool found = components.TryGetValue(mapper.GetStringId(componentInfo.Name), out componentId);
[14594]82        if (found)
83          field.SetValue(obj, mapper.GetObject(componentId));
84        else if (componentInfo.StorableAttribute.DefaultValue != null)
85          field.SetValue(obj, componentInfo.StorableAttribute.DefaultValue);
[13326]86      }
[14537]87
[13326]88      foreach (var componentInfo in typeInfo.Properties.Where(x => x.Writeable)) {
89        var property = (PropertyInfo)componentInfo.MemberInfo;
90        uint componentId;
[14537]91        bool found = components.TryGetValue(mapper.GetStringId(componentInfo.Name), out componentId);
[14594]92        if (found)
93          property.SetValue(obj, mapper.GetObject(componentId), null);
94        else if (componentInfo.StorableAttribute.DefaultValue != null)
95          property.SetValue(obj, componentInfo.StorableAttribute.DefaultValue, null);
[13326]96      }
[14537]97
[13326]98      var emptyArgs = new object[0];
99      foreach (var hook in typeInfo.AfterDeserializationHooks) {
[14549]100        hook.Invoke(obj, emptyArgs);
[13326]101      }
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.