1 | #region License Information |
---|
2 | /* HeuristicLab |
---|
3 | * Copyright (C) 2002-2011 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.Linq; |
---|
25 | using System.Reflection; |
---|
26 | using System.Text; |
---|
27 | using HeuristicLab.Persistence.Core; |
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable.Descriptors; |
---|
29 | using HeuristicLab.Persistence.Interfaces; |
---|
30 | |
---|
31 | namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable { |
---|
32 | |
---|
33 | /// <summary> |
---|
34 | /// Intended for serialization of all custom classes. Classes should have the |
---|
35 | /// <c>[StorableClass]</c> attribute set. The default mode is to serialize |
---|
36 | /// members with the <c>[Storable]</c> attribute set. Alternatively the |
---|
37 | /// storable mode can be set to <c>AllFields</c>, <c>AllProperties</c> |
---|
38 | /// or <c>AllFieldsAndAllProperties</c>. |
---|
39 | /// </summary> |
---|
40 | [StorableClass] |
---|
41 | public sealed class StorableSerializer : ICompositeSerializer { |
---|
42 | |
---|
43 | private StorableClassAnalyzer analyzer; |
---|
44 | |
---|
45 | public StorableSerializer() { |
---|
46 | analyzer = new StorableClassAnalyzer(); |
---|
47 | } |
---|
48 | |
---|
49 | [StorableConstructor] |
---|
50 | private StorableSerializer(bool deserializing) : this() { } |
---|
51 | |
---|
52 | #region ICompositeSerializer implementation |
---|
53 | |
---|
54 | /// <summary> |
---|
55 | /// Priority 200, one of the first default composite serializers to try. |
---|
56 | /// </summary> |
---|
57 | /// <value></value> |
---|
58 | public int Priority { |
---|
59 | get { return 200; } |
---|
60 | } |
---|
61 | |
---|
62 | /// <summary> |
---|
63 | /// Determines for every type whether the composite serializer is applicable. |
---|
64 | /// </summary> |
---|
65 | /// <param name="type">The type.</param> |
---|
66 | /// <returns> |
---|
67 | /// <c>true</c> if this instance can serialize the specified type; otherwise, <c>false</c>. |
---|
68 | /// </returns> |
---|
69 | public bool CanSerialize(Type type) { |
---|
70 | TypeDescriptor desc = analyzer[type]; |
---|
71 | if (desc.IsInvalid) |
---|
72 | return false; |
---|
73 | if (desc.HasDefaultConstructor) |
---|
74 | return true; |
---|
75 | else if (desc.IsStorableClass) |
---|
76 | if (desc.HasDefaultConstructor || desc.HasStorableConstructor) |
---|
77 | return true; |
---|
78 | else |
---|
79 | throw new PersistenceException("[Storable] type has no default constructor and no [StorableConstructor]"); |
---|
80 | return false; |
---|
81 | } |
---|
82 | |
---|
83 | /// <summary> |
---|
84 | /// Give a reason if possibly why the given type cannot be serialized by this |
---|
85 | /// ICompositeSerializer. |
---|
86 | /// </summary> |
---|
87 | /// <param name="type">The type.</param> |
---|
88 | /// <returns> |
---|
89 | /// A string justifying why type cannot be serialized. |
---|
90 | /// </returns> |
---|
91 | public string JustifyRejection(Type type) { |
---|
92 | StringBuilder sb = new StringBuilder(); |
---|
93 | TypeDescriptor desc = analyzer[type]; |
---|
94 | if (desc.IsInvalid) |
---|
95 | sb.Append("class is not marked [StorableClass] but has mutable members."); |
---|
96 | if (!desc.IsStorableClass) |
---|
97 | sb.Append("class is not marked [StorableClass]"); |
---|
98 | if (!desc.HasDefaultConstructor) |
---|
99 | sb.Append("class has no default constructor"); |
---|
100 | if (!desc.HasStorableConstructor) |
---|
101 | sb.Append("class has no [StorableConstructor]"); |
---|
102 | return sb.ToString(); |
---|
103 | } |
---|
104 | |
---|
105 | /// <summary> |
---|
106 | /// Creates the meta info. |
---|
107 | /// </summary> |
---|
108 | /// <param name="o">The object.</param> |
---|
109 | /// <returns>A list of storable components.</returns> |
---|
110 | public IEnumerable<Tag> CreateMetaInfo(object o) { |
---|
111 | return new Tag[] { }; |
---|
112 | } |
---|
113 | |
---|
114 | /// <summary> |
---|
115 | /// Decompose an object into <see cref="Tag"/>s, the tag name can be null, |
---|
116 | /// the order in which elements are generated is guaranteed to be |
---|
117 | /// the same as they will be supplied to the Populate method. |
---|
118 | /// </summary> |
---|
119 | /// <param name="obj">An object.</param> |
---|
120 | /// <returns>An enumerable of <see cref="Tag"/>s.</returns> |
---|
121 | public IEnumerable<Tag> Decompose(object obj) { |
---|
122 | foreach (var kvp in analyzer[obj.GetType()].Decompose(obj)) { |
---|
123 | yield return new Tag(kvp.Key, kvp.Value); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | /// <summary> |
---|
128 | /// Create an instance of the object using the provided meta information. |
---|
129 | /// </summary> |
---|
130 | /// <param name="type">A type.</param> |
---|
131 | /// <param name="metaInfo">The meta information.</param> |
---|
132 | /// <returns>A fresh instance of the provided type.</returns> |
---|
133 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) { |
---|
134 | try { |
---|
135 | return analyzer[type].CreateInstance(); |
---|
136 | } catch (TargetInvocationException x) { |
---|
137 | throw new PersistenceException( |
---|
138 | "Could not instantiate storable object: Encountered exception during constructor call", |
---|
139 | x.InnerException); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | /// <summary> |
---|
144 | /// Populates the specified instance. |
---|
145 | /// </summary> |
---|
146 | /// <param name="instance">The instance.</param> |
---|
147 | /// <param name="objects">The objects.</param> |
---|
148 | /// <param name="type">The type.</param> |
---|
149 | public void Populate(object instance, IEnumerable<Tag> objects, Type type) { |
---|
150 | analyzer[type].Populate(instance, objects.ToDictionary(kvp => kvp.Name, kvp => kvp.Value)); |
---|
151 | } |
---|
152 | |
---|
153 | #endregion |
---|
154 | |
---|
155 | } |
---|
156 | |
---|
157 | } |
---|