[3743] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3743] | 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;
|
---|
[1454] | 23 | using System.Collections.Generic;
|
---|
[4068] | 24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[1454] | 25 | using HeuristicLab.Persistence.Interfaces;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Persistence.Core {
|
---|
| 28 |
|
---|
[3004] | 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
|
---|
[3016] | 32 | /// <c>ConfigurationService</c>.
|
---|
[3004] | 33 | /// </summary>
|
---|
[3030] | 34 | [StorableClass]
|
---|
[1454] | 35 | public class Configuration {
|
---|
| 36 |
|
---|
| 37 | [Storable]
|
---|
[1823] | 38 | private readonly Dictionary<Type, IPrimitiveSerializer> primitiveSerializers;
|
---|
[1564] | 39 |
|
---|
[1454] | 40 | [Storable]
|
---|
[1823] | 41 | private readonly List<ICompositeSerializer> compositeSerializers;
|
---|
| 42 | private readonly Dictionary<Type, ICompositeSerializer> compositeSerializerCache;
|
---|
[1454] | 43 |
|
---|
[3016] | 44 | /// <summary>
|
---|
| 45 | /// Gets the format.
|
---|
| 46 | /// </summary>
|
---|
| 47 | /// <value>The format.</value>
|
---|
[1566] | 48 | [Storable]
|
---|
[1564] | 49 | public IFormat Format { get; private set; }
|
---|
[3030] | 50 |
|
---|
| 51 | [StorableConstructor]
|
---|
[4806] | 52 | protected Configuration(bool isDeserializing) {
|
---|
[1823] | 53 | compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>();
|
---|
[3030] | 54 | if (isDeserializing)
|
---|
| 55 | return;
|
---|
| 56 | primitiveSerializers = new Dictionary<Type, IPrimitiveSerializer>();
|
---|
| 57 | compositeSerializers = new List<ICompositeSerializer>();
|
---|
[1454] | 58 | }
|
---|
| 59 |
|
---|
[3016] | 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>
|
---|
[1823] | 66 | public Configuration(IFormat format,
|
---|
| 67 | IEnumerable<IPrimitiveSerializer> primitiveSerializers,
|
---|
[3030] | 68 | IEnumerable<ICompositeSerializer> compositeSerializers)
|
---|
| 69 | : this(false) {
|
---|
[1564] | 70 | this.Format = format;
|
---|
[3030] | 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,
|
---|
[4068] | 78 | format.Name, format.SerialDataType.FullName));
|
---|
[1823] | 79 | this.primitiveSerializers.Add(primitiveSerializer.SourceType, primitiveSerializer);
|
---|
[4068] | 80 | }
|
---|
[1454] | 81 | }
|
---|
| 82 |
|
---|
[3016] | 83 | /// <summary>
|
---|
| 84 | /// Gets the primitive serializers.
|
---|
| 85 | /// </summary>
|
---|
| 86 | /// <value>The primitive serializers.</value>
|
---|
[1823] | 87 | public IEnumerable<IPrimitiveSerializer> PrimitiveSerializers {
|
---|
| 88 | get { return primitiveSerializers.Values; }
|
---|
[1454] | 89 | }
|
---|
| 90 |
|
---|
[3016] | 91 | /// <summary>
|
---|
| 92 | /// Gets the composite serializers.
|
---|
| 93 | /// </summary>
|
---|
| 94 | /// <value>An enumerable of composite serializers.</value>
|
---|
[1823] | 95 | public IEnumerable<ICompositeSerializer> CompositeSerializers {
|
---|
| 96 | get { return compositeSerializers; }
|
---|
[1454] | 97 | }
|
---|
| 98 |
|
---|
[3016] | 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>
|
---|
[1823] | 104 | public IPrimitiveSerializer GetPrimitiveSerializer(Type type) {
|
---|
| 105 | IPrimitiveSerializer primitiveSerializer;
|
---|
| 106 | primitiveSerializers.TryGetValue(type, out primitiveSerializer);
|
---|
| 107 | return primitiveSerializer;
|
---|
[1454] | 108 | }
|
---|
| 109 |
|
---|
[3016] | 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>
|
---|
[1823] | 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);
|
---|
[1454] | 121 | return d;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
[1823] | 124 | compositeSerializerCache.Add(type, null);
|
---|
[1454] | 125 | return null;
|
---|
[1566] | 126 | }
|
---|
[3030] | 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 |
|
---|
[1566] | 144 | }
|
---|
| 145 |
|
---|
[1454] | 146 | } |
---|