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