[1349] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Reflection;
|
---|
[1357] | 4 | using HeuristicLab.Persistence.Interfaces;
|
---|
[1349] | 5 |
|
---|
[1357] | 6 | namespace HeuristicLab.Persistence {
|
---|
[1349] | 7 |
|
---|
| 8 | public class PersistenceConfiguration {
|
---|
| 9 |
|
---|
| 10 | private readonly List<IFormatter> formatters;
|
---|
| 11 | private readonly List<IDecomposer> decomposers;
|
---|
| 12 | private readonly Dictionary<IFormat, Dictionary<Type, IFormatter>> formatterConfig;
|
---|
| 13 | private readonly Dictionary<Type, IDecomposer> decomposerCache;
|
---|
| 14 |
|
---|
| 15 | private static PersistenceConfiguration instance;
|
---|
| 16 | public static PersistenceConfiguration Instance {
|
---|
| 17 | get {
|
---|
| 18 | if (instance == null) {
|
---|
| 19 | instance = new PersistenceConfiguration();
|
---|
| 20 | instance.DefaultConfig();
|
---|
| 21 | }
|
---|
| 22 | return instance;
|
---|
| 23 | }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | public PersistenceConfiguration() {
|
---|
| 27 | formatters = new List<IFormatter>();
|
---|
| 28 | decomposers = new List<IDecomposer>();
|
---|
| 29 | formatterConfig = new Dictionary<IFormat, Dictionary<Type, IFormatter>>();
|
---|
| 30 | decomposerCache = new Dictionary<Type, IDecomposer>();
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | public void DiscoverFrom(Assembly a) {
|
---|
| 34 | foreach ( Type t in a.GetTypes() ) {
|
---|
| 35 | if (t.GetInterface(typeof(IFormatter).FullName) != null) {
|
---|
| 36 | try {
|
---|
| 37 | formatters.Add((IFormatter) Activator.CreateInstance(t, true));
|
---|
| 38 | } catch (MissingMethodException) {
|
---|
| 39 | Console.WriteLine("WARNING: Could not instantiate {0}", t.FullName);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | if (t.GetInterface(typeof(IDecomposer).FullName) != null) {
|
---|
| 43 | try {
|
---|
| 44 | decomposers.Add((IDecomposer) Activator.CreateInstance(t, true));
|
---|
| 45 | } catch (MissingMethodException) {
|
---|
| 46 | Console.WriteLine("WARNING: Could not instantiate {0}", t.FullName);
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public void DiscoverAll() {
|
---|
| 53 | DiscoverFrom(Assembly.GetAssembly(this.GetType()));
|
---|
| 54 | foreach ( Assembly a in AppDomain.CurrentDomain.GetAssemblies() )
|
---|
| 55 | DiscoverFrom(a);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public void DefaultConfig() {
|
---|
| 59 | DiscoverAll();
|
---|
| 60 | foreach ( IFormatter f in formatters ) {
|
---|
| 61 | if ( ! formatterConfig.ContainsKey(f.Format) )
|
---|
| 62 | formatterConfig.Add(f.Format, new Dictionary<Type, IFormatter>());
|
---|
| 63 | if (!formatterConfig[f.Format].ContainsKey(f.Type))
|
---|
| 64 | formatterConfig[f.Format][f.Type] = f;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public IFormatter GetFormatter(IFormat format, Type type) {
|
---|
| 69 | Dictionary<Type, IFormatter> typeMap;
|
---|
| 70 | formatterConfig.TryGetValue(format, out typeMap);
|
---|
| 71 | if ( typeMap == null )
|
---|
| 72 | return null;
|
---|
| 73 | IFormatter formatter;
|
---|
| 74 | typeMap.TryGetValue(type, out formatter);
|
---|
| 75 | return formatter;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public IDecomposer GetDecomposer(Type type) {
|
---|
| 79 | IDecomposer decomposer;
|
---|
| 80 | decomposerCache.TryGetValue(type, out decomposer);
|
---|
| 81 | if (decomposer != null)
|
---|
| 82 | return decomposer;
|
---|
| 83 | foreach ( IDecomposer d in decomposers ) {
|
---|
| 84 | if (d.CanSerialize(type)) {
|
---|
| 85 | decomposerCache.Add(type, d);
|
---|
| 86 | return d;
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | return null;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | } |
---|