Changeset 1564 for trunk/sources/HeuristicLab.Persistence/3.3/Core
- Timestamp:
- 04/16/09 12:40:41 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3/Core
- Files:
-
- 2 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Core/Configuration.cs
r1542 r1564 9 9 [Storable] 10 10 private readonly Dictionary<Type, IFormatter> formatters; 11 11 12 [Storable] 12 13 private readonly List<IDecomposer> decomposers; 13 14 private readonly Dictionary<Type, IDecomposer> decomposerCache; 14 [Storable] 15 public IFormat Format { get; set; } 15 16 [Storable] 17 public IFormat Format { get; private set; } 16 18 17 19 private Configuration() { … … 19 21 } 20 22 21 public Configuration(Dictionary<Type, IFormatter> formatters, IEnumerable<IDecomposer> decomposers) { 23 public Configuration(IFormat format, Dictionary<Type, IFormatter> formatters, IEnumerable<IDecomposer> decomposers) { 24 this.Format = format; 22 25 this.formatters = new Dictionary<Type, IFormatter>(); 23 26 foreach ( var pair in formatters ) { 24 if (Format == null) { 25 Format = pair.Value.Format; 26 } else if (pair.Value.Format != Format ) { 27 if (pair.Value.SerialDataType != format.SerialDataType ) { 27 28 throw new ArgumentException("All formatters must have the same IFormat."); 28 29 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/ConfigurationService.cs
r1556 r1564 15 15 private static ConfigurationService instance; 16 16 private readonly Dictionary<IFormat, Configuration> customConfigurations; 17 public Dictionary< IFormat, List<IFormatter>> Formatters { get; private set; }17 public Dictionary<Type, List<IFormatter>> Formatters { get; private set; } 18 18 public List<IDecomposer> Decomposers { get; private set; } 19 public List<IFormat> Formats { get; private set; } 19 20 20 21 public static ConfigurationService Instance { … … 27 28 28 29 private ConfigurationService() { 29 Formatters = new Dictionary< IFormat, List<IFormatter>>();30 Formatters = new Dictionary<Type, List<IFormatter>>(); 30 31 Decomposers = new List<IDecomposer>(); 31 customConfigurations = new Dictionary<IFormat, Configuration>(); 32 customConfigurations = new Dictionary<IFormat, Configuration>(); 33 Formats = new List<IFormat>(); 32 34 Reset(); 33 35 LoadSettings(); … … 59 61 Serializer serializer = new Serializer( 60 62 customConfigurations, 61 GetDefaultConfig( XmlFormat.Instance),63 GetDefaultConfig(new XmlFormat()), 62 64 "CustomConfigurations"); 63 65 XmlGenerator generator = new XmlGenerator(); … … 103 105 try { 104 106 IFormatter formatter = (IFormatter) Activator.CreateInstance(t, true); 105 if ( ! Formatters.ContainsKey(formatter. Format) ) {106 Formatters.Add(formatter. Format, new List<IFormatter>());107 if ( ! Formatters.ContainsKey(formatter.SerialDataType) ) { 108 Formatters.Add(formatter.SerialDataType, new List<IFormatter>()); 107 109 } 108 Formatters[formatter.Format].Add(formatter); 109 Logger.Debug("discovered formatter " + t.VersionInvariantName()); 110 Formatters[formatter.SerialDataType].Add(formatter); 111 Logger.Debug(String.Format("discovered formatter {0} ({1} -> {2})", 112 t.VersionInvariantName(), 113 formatter.SourceType.VersionInvariantName(), 114 formatter.SerialDataType.VersionInvariantName())); 110 115 } catch (MissingMethodException e) { 111 116 Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e); … … 124 129 } 125 130 } 131 if (t.GetInterface(typeof(IFormat).FullName) != null) { 132 try { 133 IFormat format = (IFormat)Activator.CreateInstance(t, true); 134 Formats.Add(format); 135 Logger.Debug(String.Format("discovered format {0} ({2}) with serial data {1}.", 136 format.Name, 137 format.SerialDataType, 138 t.VersionInvariantName())); 139 } catch (MissingMethodException e) { 140 Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e); 141 } catch (ArgumentException e) { 142 Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e); 143 } 144 } 126 145 } 127 146 } … … 129 148 public Configuration GetDefaultConfig(IFormat format) { 130 149 Dictionary<Type, IFormatter> formatterConfig = new Dictionary<Type, IFormatter>(); 131 foreach ( IFormatter f in Formatters[format ] ) {132 if ( ! formatterConfig.ContainsKey(f. Type) )133 formatterConfig.Add(f. Type, f);150 foreach ( IFormatter f in Formatters[format.SerialDataType] ) { 151 if ( ! formatterConfig.ContainsKey(f.SourceType) ) 152 formatterConfig.Add(f.SourceType, f); 134 153 } 135 return new Configuration(format terConfig, Decomposers);154 return new Configuration(format, formatterConfig, Decomposers); 136 155 } 137 156 … … 142 161 } 143 162 144 public void DefineConfiguration( IFormat format, Configuration configuration) {145 customConfigurations[ format] = configuration;163 public void DefineConfiguration(Configuration configuration) { 164 customConfigurations[configuration.Format] = configuration; 146 165 SaveSettings(); 147 166 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/FormatBase.cs
r1556 r1564 3 3 namespace HeuristicLab.Persistence.Interfaces { 4 4 5 public abstract class FormatBase : IFormat { 6 public abstract string Name { get; } 5 public abstract class FormatBase<SerialDataFormat> : IFormat<SerialDataFormat> where SerialDataFormat : ISerialData { 6 7 public abstract string Name { get; } 8 9 public Type SerialDataType { get { return typeof(SerialDataFormat); } } 10 11 public bool Equals(FormatBase<SerialDataFormat> f) { 12 if (f == null) 13 return false; 14 return f.Name == this.Name; 15 } 16 7 17 public override bool Equals(object obj) { 8 if (obj as IFormat == null) 9 return false; 10 return this.Equals((FormatBase)obj); 18 FormatBase<SerialDataFormat> f = obj as FormatBase<SerialDataFormat>; 19 return Equals(f); 11 20 } 12 public bool Equals(FormatBase f) { 13 return Name.Equals(f.Name); 14 } 15 public override int GetHashCode() { 16 return Name.GetHashCode(); 17 } 21 18 22 } 23 19 24 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs
r1556 r1564 69 69 IFormatter formatter = configuration.GetFormatter(value.GetType()); 70 70 if (formatter != null) 71 return PrimitiveEnumerator(accessor.Name, typeId, formatter. DoFormat(value), id);71 return PrimitiveEnumerator(accessor.Name, typeId, formatter.Format(value), id); 72 72 IDecomposer decomposer = configuration.GetDecomposer(value.GetType()); 73 73 if (decomposer != null) … … 88 88 89 89 private IEnumerator<ISerializationToken> PrimitiveEnumerator(string name, 90 int typeId, objectserializedValue, int? id) {90 int typeId, ISerialData serializedValue, int? id) { 91 91 yield return new PrimitiveToken(name, typeId, id, serializedValue); 92 92 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tokens/PrimitiveToken.cs
r1556 r1564 6 6 public readonly int TypeId; 7 7 public readonly int? Id; 8 public readonly objectSerialData;9 public PrimitiveToken(string name, int typeId, int? id, objectserialData)8 public readonly ISerialData SerialData; 9 public PrimitiveToken(string name, int typeId, int? id, ISerialData serialData) 10 10 : base(name) { 11 11 TypeId = typeId;
Note: See TracChangeset
for help on using the changeset viewer.