Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13326 was 13326, checked in by swagner, 8 years ago

#2520: Created plugin for new persistence implementation

File size: 4.1 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.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Persistence {
30  [Transformer("78556C88-0FEE-4602-95C7-A469B2DDB468", 100)]
31  internal sealed class StorableClassTransformer : TransformerBase {
32    public override bool CanTransformType(Type type) {
33      return StorableClassAttribute.IsStorableClass(type);
34    }
35    public override PersistenceData ToData(object o, PersistenceMapper mapper) {
36      var type = o.GetType();
37      var typeInfo = mapper.GetTypeInfo(type);
38      var attribute = typeInfo.StorableClassAttribute;
39      var data = new CompositeData(Id);
40      mapper.Cache(o, data);
41      data.TypeId = mapper.GetTypeId(type);
42      // data.GuidId = attribute.Guid;
43      // data.Version = attribute.Version;
44
45      var emptyArgs = new object[0];
46      foreach (var hook in typeInfo.BeforeSerializationHooks) {
47        hook.Invoke(o, emptyArgs);
48      }
49      var components = new Dictionary<uint, uint>();
50      foreach (var componentInfo in typeInfo.Fields) {
51        var field = (FieldInfo)componentInfo.MemberInfo;
52        var component = mapper.GetDataId(field.GetValue(o));
53        components.Add(mapper.GetStringId(componentInfo.Name), component);
54      }
55      foreach (var componentInfo in typeInfo.Properties.Where(x => x.Readable)) {
56        var property = (PropertyInfo)componentInfo.MemberInfo;
57        var component = mapper.GetDataId(property.GetValue(o, null));
58        components.Add(mapper.GetStringId(componentInfo.Name), component);
59      }
60      data.ComponentNameIds = components.Keys.ToArray();
61      data.ComponentIds = components.Values.ToArray();
62      return data;
63    }
64
65    public override object ToObject(PersistenceData data, PersistenceMapper mapper) {
66      var composite = (CompositeData)data;
67      var type = mapper.GetType(composite.TypeId);
68      var typeInfo = mapper.GetTypeInfo(type);
69      var o = mapper.CreateInstance(type);
70      mapper.Cache(data, o);
71
72      var components = new Dictionary<uint, uint>();
73      for (int i = 0; i < composite.ComponentNameIds.Length; i++) {
74        components.Add(composite.ComponentNameIds[i], composite.ComponentIds[i]);
75      }
76
77      foreach (var componentInfo in typeInfo.Fields) {
78        var field = (FieldInfo)componentInfo.MemberInfo;
79        uint componentId;
80        components.TryGetValue(mapper.GetStringId(componentInfo.Name), out componentId);
81        field.SetValue(o, componentId != 0 ? mapper.GetObject(componentId) : componentInfo.StorableAttribute.DefaultValue);
82      }
83      foreach (var componentInfo in typeInfo.Properties.Where(x => x.Writeable)) {
84        var property = (PropertyInfo)componentInfo.MemberInfo;
85        uint componentId;
86        components.TryGetValue(mapper.GetStringId(componentInfo.Name), out componentId);
87        property.SetValue(o, componentId != 0 ? mapper.GetObject(componentId) : componentInfo.StorableAttribute.DefaultValue, null);
88      }
89      var emptyArgs = new object[0];
90      foreach (var hook in typeInfo.AfterDeserializationHooks) {
91        hook.Invoke(o, emptyArgs);
92      }
93      return o;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.