Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Persistence/3.3/Core/Configuration.cs @ 14895

Last change on this file since 14895 was 14711, checked in by gkronber, 8 years ago

#2520

  • renamed StorableClass -> StorableType
  • changed persistence to use GUIDs instead of type names
File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25using HeuristicLab.Persistence.Interfaces;
26
27namespace HeuristicLab.Persistence.Core {
28
29  /// <summary>
30  /// Defines the set of primitive and composite serializers that are to be used
31  /// for a certain seraial format. The configuration can be obtained from the
32  /// <c>ConfigurationService</c>.
33  /// </summary>
34  [StorableType("DBAD470C-22B9-4C7A-A45E-21026FA7E765")]
35  public class Configuration {
36
37    [Storable]
38    private readonly Dictionary<Type, IPrimitiveSerializer> primitiveSerializers;
39
40    [Storable]
41    private readonly List<ICompositeSerializer> compositeSerializers;
42    private readonly Dictionary<Type, ICompositeSerializer> compositeSerializerCache;
43
44    /// <summary>
45    /// Gets the format.
46    /// </summary>
47    /// <value>The format.</value>
48    [Storable]
49    public IFormat Format { get; private set; }
50
51    [StorableConstructor]
52    protected Configuration(bool isDeserializing) {
53      compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>();
54      if (isDeserializing)
55        return;
56      primitiveSerializers = new Dictionary<Type, IPrimitiveSerializer>();
57      compositeSerializers = new List<ICompositeSerializer>();
58    }
59
60    /// <summary>
61    /// Initializes a new instance of the <see cref="Configuration"/> class.
62    /// </summary>
63    /// <param name="format">The format.</param>
64    /// <param name="primitiveSerializers">The primitive serializers.</param>
65    /// <param name="compositeSerializers">The composite serializers.</param>
66    public Configuration(IFormat format,
67        IEnumerable<IPrimitiveSerializer> primitiveSerializers,
68        IEnumerable<ICompositeSerializer> compositeSerializers)
69      : this(false) {
70      this.Format = format;
71      this.compositeSerializers.AddRange(compositeSerializers);
72      foreach (var primitiveSerializer in primitiveSerializers) {
73        if (primitiveSerializer.SerialDataType != format.SerialDataType)
74          throw new ArgumentException(string.Format(
75            "primitive serializer's ({0}) serialized data type ({1}) " + Environment.NewLine +
76            "is not compatible with selected format's ({2}) seriali data type ({3})",
77            primitiveSerializers.GetType().FullName, primitiveSerializer.SerialDataType.FullName,
78            format.Name, format.SerialDataType.FullName));
79        this.primitiveSerializers.Add(primitiveSerializer.SourceType, primitiveSerializer);
80      }
81    }
82
83    /// <summary>
84    /// Gets the primitive serializers.
85    /// </summary>
86    /// <value>The primitive serializers.</value>
87    public IEnumerable<IPrimitiveSerializer> PrimitiveSerializers {
88      get { return primitiveSerializers.Values; }
89    }
90
91    /// <summary>
92    /// Gets the composite serializers.
93    /// </summary>
94    /// <value>An enumerable of composite serializers.</value>
95    public IEnumerable<ICompositeSerializer> CompositeSerializers {
96      get { return compositeSerializers; }
97    }
98
99    /// <summary>
100    /// Gets the primitive serializer.
101    /// </summary>
102    /// <param name="type">The type.</param>
103    /// <returns>The appropriate primitive serializer for the type.</returns>
104    public IPrimitiveSerializer GetPrimitiveSerializer(Type type) {
105      IPrimitiveSerializer primitiveSerializer;
106      primitiveSerializers.TryGetValue(type, out primitiveSerializer);
107      return primitiveSerializer;
108    }
109
110    /// <summary>
111    /// Gets the composite serializer for a given type.
112    /// </summary>
113    /// <param name="type">The type.</param>
114    /// <returns>The first matching composite serializer for the type.</returns>
115    public ICompositeSerializer GetCompositeSerializer(Type type) {
116      if (compositeSerializerCache.ContainsKey(type))
117        return compositeSerializerCache[type];
118      foreach (ICompositeSerializer d in compositeSerializers) {
119        if (d.CanSerialize(type)) {
120          compositeSerializerCache.Add(type, d);
121          return d;
122        }
123      }
124      compositeSerializerCache.Add(type, null);
125      return null;
126    }
127
128    /// <summary>
129    /// Copies this configuration and re-instantiates all serializers.
130    /// </summary>
131    /// <returns>A new <see cref="Configuration"/></returns>
132    public Configuration Copy() {
133      var config = new Configuration(false);
134      config.Format = Format;
135      foreach (var ps in primitiveSerializers)
136        config.primitiveSerializers.Add(
137          ps.Key,
138          (IPrimitiveSerializer)Activator.CreateInstance(ps.Value.GetType()));
139      foreach (var cs in compositeSerializers)
140        config.compositeSerializers.Add((ICompositeSerializer)Activator.CreateInstance(cs.GetType()));
141      return config;
142    }
143
144  }
145
146}
Note: See TracBrowser for help on using the repository browser.