1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 System.Text;
|
---|
26 | using HeuristicLab.Persistence.Auxiliary;
|
---|
27 | using HeuristicLab.Persistence.Core;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Persistence.Interfaces;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
32 |
|
---|
33 | [StorableClass]
|
---|
34 | internal sealed class NumberEnumerable2StringSerializer : ICompositeSerializer {
|
---|
35 |
|
---|
36 | [StorableConstructor]
|
---|
37 | private NumberEnumerable2StringSerializer(bool deserializing) { }
|
---|
38 | public NumberEnumerable2StringSerializer() { }
|
---|
39 |
|
---|
40 | public int Priority {
|
---|
41 | get { return 200; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | private static readonly Number2StringSerializer numberConverter =
|
---|
45 | new Number2StringSerializer();
|
---|
46 |
|
---|
47 | private static readonly Dictionary<Type, Type> interfaceCache = new Dictionary<Type, Type>();
|
---|
48 |
|
---|
49 | public Type GetGenericEnumerableInterface(Type type) {
|
---|
50 | if (interfaceCache.ContainsKey(type))
|
---|
51 | return interfaceCache[type];
|
---|
52 | foreach (Type iface in type.GetInterfaces()) {
|
---|
53 | if (iface.IsGenericType &&
|
---|
54 | iface.GetGenericTypeDefinition() == typeof(IEnumerable<>) &&
|
---|
55 | numberConverter.CanSerialize(iface.GetGenericArguments()[0])) {
|
---|
56 | interfaceCache.Add(type, iface);
|
---|
57 | return iface;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | interfaceCache.Add(type, null);
|
---|
61 | return null;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public bool ImplementsGenericEnumerable(Type type) {
|
---|
65 | return GetGenericEnumerableInterface(type) != null;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public bool HasAddMethod(Type type) {
|
---|
69 | return
|
---|
70 | type.GetMethod("Add") != null &&
|
---|
71 | type.GetMethod("Add").GetParameters().Length == 1 &&
|
---|
72 | type.GetConstructor(
|
---|
73 | BindingFlags.Public |
|
---|
74 | BindingFlags.NonPublic |
|
---|
75 | BindingFlags.Instance,
|
---|
76 | null, Type.EmptyTypes, null) != null;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public bool CanSerialize(Type type) {
|
---|
80 | return
|
---|
81 | ReflectionTools.HasDefaultConstructor(type) &&
|
---|
82 | ImplementsGenericEnumerable(type) &&
|
---|
83 | HasAddMethod(type);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public string JustifyRejection(Type type) {
|
---|
87 | if (!ReflectionTools.HasDefaultConstructor(type))
|
---|
88 | return "no default constructor";
|
---|
89 | if (!ImplementsGenericEnumerable(type))
|
---|
90 | return "IEnumerable<> not implemented";
|
---|
91 | return "no Add method with one parameter";
|
---|
92 | }
|
---|
93 |
|
---|
94 | public IEnumerable<Tag> CreateMetaInfo(object o) {
|
---|
95 | return new Tag[] { };
|
---|
96 | }
|
---|
97 |
|
---|
98 | private static object[] emptyArgs = new object[0];
|
---|
99 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
100 | Type type = obj.GetType();
|
---|
101 | Type enumerable = GetGenericEnumerableInterface(type);
|
---|
102 | InterfaceMapping iMap = obj.GetType().GetInterfaceMap(enumerable);
|
---|
103 | MethodInfo getEnumeratorMethod =
|
---|
104 | iMap.TargetMethods[
|
---|
105 | Array.IndexOf(
|
---|
106 | iMap.InterfaceMethods,
|
---|
107 | enumerable.GetMethod("GetEnumerator"))];
|
---|
108 | object genericEnumerator = getEnumeratorMethod.Invoke(obj, emptyArgs);
|
---|
109 | MethodInfo moveNextMethod = genericEnumerator.GetType().GetMethod("MoveNext");
|
---|
110 | PropertyInfo currentProperty = genericEnumerator.GetType().GetProperty("Current");
|
---|
111 | StringBuilder sb = new StringBuilder();
|
---|
112 | while ((bool)moveNextMethod.Invoke(genericEnumerator, emptyArgs))
|
---|
113 | sb.Append(
|
---|
114 | numberConverter.Format(
|
---|
115 | currentProperty.GetValue(genericEnumerator, null))).Append(';');
|
---|
116 | yield return new Tag("compact enumerable", sb.ToString());
|
---|
117 | }
|
---|
118 |
|
---|
119 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
120 | return Activator.CreateInstance(type, true);
|
---|
121 | }
|
---|
122 |
|
---|
123 | public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
|
---|
124 | Type enumerable = GetGenericEnumerableInterface(type);
|
---|
125 | Type elementType = enumerable.GetGenericArguments()[0];
|
---|
126 | MethodInfo addMethod = type.GetMethod("Add");
|
---|
127 | try {
|
---|
128 | var tagEnumerator = tags.GetEnumerator();
|
---|
129 | tagEnumerator.MoveNext();
|
---|
130 | string[] stringValues = ((string)tagEnumerator.Current.Value)
|
---|
131 | .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
|
---|
132 | foreach (var value in stringValues) {
|
---|
133 | addMethod.Invoke(instance, new[] { numberConverter.Parse(value, elementType) });
|
---|
134 | }
|
---|
135 | }
|
---|
136 | catch (InvalidOperationException e) {
|
---|
137 | throw new PersistenceException("Insufficient element data to reconstruct number enumerable", e);
|
---|
138 | }
|
---|
139 | catch (InvalidCastException e) {
|
---|
140 | throw new PersistenceException("Invalid element data during reconstruction of number enumerable", e);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | } |
---|