Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs @ 6222

Last change on this file since 6222 was 6222, checked in by epitzer, 13 years ago

use static cache of storable information (#1530)

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