1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Reflection;
|
---|
25 | using HeuristicLab.Persistence.Core;
|
---|
26 | using HEAL.Attic;
|
---|
27 | using HeuristicLab.Persistence.Interfaces;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
30 |
|
---|
31 | [StorableType("85C63E3B-C604-4267-8008-6B89521BA312")]
|
---|
32 | internal sealed class StructSerializer : ICompositeSerializer {
|
---|
33 |
|
---|
34 | [StorableConstructor]
|
---|
35 | private StructSerializer(StorableConstructorFlag _) { }
|
---|
36 | public StructSerializer() { }
|
---|
37 |
|
---|
38 | public int Priority {
|
---|
39 | get { return 50; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public bool CanSerialize(Type type) {
|
---|
43 | return type.IsValueType && !type.IsPrimitive && !type.IsEnum && type.IsSealed;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public string JustifyRejection(Type type) {
|
---|
47 | if (!type.IsValueType)
|
---|
48 | return "not a value type";
|
---|
49 | if (type.IsPrimitive)
|
---|
50 | return "type is primitive (int, float, ...)";
|
---|
51 | if (type.IsEnum)
|
---|
52 | return "type is enum";
|
---|
53 | return "type is not sealed";
|
---|
54 | }
|
---|
55 |
|
---|
56 | public IEnumerable<Tag> CreateMetaInfo(object obj) {
|
---|
57 | return null;
|
---|
58 | }
|
---|
59 |
|
---|
60 | private readonly BindingFlags AllInstanceMembers =
|
---|
61 | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
---|
62 |
|
---|
63 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
64 | Type t = obj.GetType();
|
---|
65 | foreach (MemberInfo mi in t.GetMembers(AllInstanceMembers)) {
|
---|
66 | if (mi.MemberType == MemberTypes.Field) {
|
---|
67 | string name = mi.Name.Replace("<", "<").Replace(">", ">");
|
---|
68 | yield return new Tag(name, ((FieldInfo)mi).GetValue(obj));
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
74 | return Activator.CreateInstance(type, true);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
|
---|
78 | foreach (Tag t in tags) {
|
---|
79 | string name = t.Name.Replace("<", "<").Replace(">", ">");
|
---|
80 | MemberInfo[] mis = type.GetMember(name, AllInstanceMembers);
|
---|
81 | if (mis.Length != 1)
|
---|
82 | throw new PersistenceException("ambiguous struct member name " + name);
|
---|
83 | MemberInfo mi = mis[0];
|
---|
84 | if (mi.MemberType == MemberTypes.Field)
|
---|
85 | ((FieldInfo)mi).SetValue(instance, t.Value);
|
---|
86 | else
|
---|
87 | throw new PersistenceException("invalid struct member type " + mi.MemberType.ToString());
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | }
|
---|
92 | }
|
---|