Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/16/09 12:40:41 (15 years ago)
Author:
epitzer
Message:

Stronger typing for formatters with the help of generics. Separate format and serial data type. (#548)

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  
    99    [Storable]
    1010    private readonly Dictionary<Type, IFormatter> formatters;
     11
    1112    [Storable]
    1213    private readonly List<IDecomposer> decomposers;
    1314    private readonly Dictionary<Type, IDecomposer> decomposerCache;
    14     [Storable]
    15     public IFormat Format { get; set; }
     15
     16    [Storable]   
     17    public IFormat Format { get; private set; }
    1618
    1719    private Configuration() {
     
    1921    }
    2022
    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;
    2225      this.formatters = new Dictionary<Type, IFormatter>();
    2326      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 ) {
    2728          throw new ArgumentException("All formatters must have the same IFormat.");
    2829        }
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/ConfigurationService.cs

    r1556 r1564  
    1515    private static ConfigurationService instance;
    1616    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; }
    1818    public List<IDecomposer> Decomposers { get; private set; }
     19    public List<IFormat> Formats { get; private set; }
    1920   
    2021    public static ConfigurationService Instance {
     
    2728
    2829    private ConfigurationService() {
    29       Formatters = new Dictionary<IFormat, List<IFormatter>>();
     30      Formatters = new Dictionary<Type, List<IFormatter>>();
    3031      Decomposers = new List<IDecomposer>();
    31       customConfigurations = new Dictionary<IFormat, Configuration>();     
     32      customConfigurations = new Dictionary<IFormat, Configuration>();
     33      Formats = new List<IFormat>();
    3234      Reset();
    3335      LoadSettings();
     
    5961      Serializer serializer = new Serializer(
    6062        customConfigurations,
    61         GetDefaultConfig(XmlFormat.Instance),
     63        GetDefaultConfig(new XmlFormat()),
    6264        "CustomConfigurations");
    6365      XmlGenerator generator = new XmlGenerator();
     
    103105          try {
    104106            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>());
    107109            }
    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()));
    110115          } catch (MissingMethodException e) {
    111116            Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e);           
     
    124129          }
    125130        }
     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        }
    126145      }
    127146    }
     
    129148    public Configuration GetDefaultConfig(IFormat format) {
    130149      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);
    134153      }
    135       return new Configuration(formatterConfig, Decomposers);
     154      return new Configuration(format, formatterConfig, Decomposers);
    136155    }
    137156
     
    142161    }
    143162
    144     public void DefineConfiguration(IFormat format, Configuration configuration) {
    145       customConfigurations[format] = configuration;
     163    public void DefineConfiguration(Configuration configuration) {     
     164      customConfigurations[configuration.Format] = configuration;
    146165      SaveSettings();
    147166    }
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/FormatBase.cs

    r1556 r1564  
    33namespace HeuristicLab.Persistence.Interfaces {
    44
    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
    717    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);
    1120    }
    12     public bool Equals(FormatBase f) {
    13       return Name.Equals(f.Name);
    14     }
    15     public override int GetHashCode() {
    16       return Name.GetHashCode();
    17     }
     21   
    1822  }
     23
    1924}
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs

    r1556 r1564  
    6969      IFormatter formatter = configuration.GetFormatter(value.GetType());
    7070      if (formatter != null)
    71         return PrimitiveEnumerator(accessor.Name, typeId, formatter.DoFormat(value), id);
     71        return PrimitiveEnumerator(accessor.Name, typeId, formatter.Format(value), id);
    7272      IDecomposer decomposer = configuration.GetDecomposer(value.GetType());
    7373      if (decomposer != null)
     
    8888
    8989    private IEnumerator<ISerializationToken> PrimitiveEnumerator(string name,
    90         int typeId, object serializedValue, int? id) {
     90        int typeId, ISerialData serializedValue, int? id) {
    9191      yield return new PrimitiveToken(name, typeId, id, serializedValue);
    9292    }
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/Tokens/PrimitiveToken.cs

    r1556 r1564  
    66    public readonly int TypeId;
    77    public readonly int? Id;
    8     public readonly object SerialData;
    9     public PrimitiveToken(string name, int typeId, int? id, object serialData)
     8    public readonly ISerialData SerialData;
     9    public PrimitiveToken(string name, int typeId, int? id, ISerialData serialData)
    1010      : base(name) {
    1111      TypeId = typeId;
Note: See TracChangeset for help on using the changeset viewer.