Changeset 1566 for trunk/sources/HeuristicLab.Persistence/3.3/Core
- Timestamp:
- 04/16/09 12:58:14 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3/Core
- Files:
-
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Core/Configuration.cs
r1564 r1566 14 14 private readonly Dictionary<Type, IDecomposer> decomposerCache; 15 15 16 [Storable] 16 [Storable] 17 17 public IFormat Format { get; private set; } 18 18 … … 24 24 this.Format = format; 25 25 this.formatters = new Dictionary<Type, IFormatter>(); 26 foreach ( var pair in formatters) {27 if (pair.Value.SerialDataType != format.SerialDataType 26 foreach (var pair in formatters) { 27 if (pair.Value.SerialDataType != format.SerialDataType) { 28 28 throw new ArgumentException("All formatters must have the same IFormat."); 29 29 } … … 31 31 } 32 32 this.decomposers = new List<IDecomposer>(decomposers); 33 decomposerCache = new Dictionary<Type, IDecomposer>(); 33 decomposerCache = new Dictionary<Type, IDecomposer>(); 34 34 } 35 35 … … 42 42 } 43 43 44 public IFormatter GetFormatter(Type type) { 44 public IFormatter GetFormatter(Type type) { 45 45 IFormatter formatter; 46 46 formatters.TryGetValue(type, out formatter); … … 50 50 public IDecomposer GetDecomposer(Type type) { 51 51 if (decomposerCache.ContainsKey(type)) 52 return decomposerCache[type]; 52 return decomposerCache[type]; 53 53 foreach (IDecomposer d in decomposers) { 54 54 if (d.CanDecompose(type)) { … … 59 59 decomposerCache.Add(type, null); 60 60 return null; 61 } 62 } 63 61 } 62 } 63 64 64 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/ConfigurationService.cs
r1564 r1566 10 10 11 11 namespace HeuristicLab.Persistence.Core { 12 12 13 13 public class ConfigurationService { 14 14 … … 18 18 public List<IDecomposer> Decomposers { get; private set; } 19 19 public List<IFormat> Formats { get; private set; } 20 20 21 21 public static ConfigurationService Instance { 22 22 get { … … 54 54 } 55 55 } catch (Exception e) { 56 Logger.Warn("Could not load settings.", e); 56 Logger.Warn("Could not load settings.", e); 57 57 } 58 58 } 59 59 60 public void SaveSettings() { 60 public void SaveSettings() { 61 61 Serializer serializer = new Serializer( 62 62 customConfigurations, … … 78 78 } 79 79 80 public void Reset() { 80 public void Reset() { 81 81 customConfigurations.Clear(); 82 82 Formatters.Clear(); … … 85 85 DiscoverFrom(defaultAssembly); 86 86 foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) 87 if ( a != defaultAssembly)87 if (a != defaultAssembly) 88 88 DiscoverFrom(a); 89 89 SortDecomposers(); … … 102 102 protected void DiscoverFrom(Assembly a) { 103 103 foreach (Type t in a.GetTypes()) { 104 if (t.GetInterface(typeof 104 if (t.GetInterface(typeof(IFormatter).FullName) != null) { 105 105 try { 106 IFormatter formatter = (IFormatter) 107 if ( ! Formatters.ContainsKey(formatter.SerialDataType)) {106 IFormatter formatter = (IFormatter)Activator.CreateInstance(t, true); 107 if (!Formatters.ContainsKey(formatter.SerialDataType)) { 108 108 Formatters.Add(formatter.SerialDataType, new List<IFormatter>()); 109 109 } … … 114 114 formatter.SerialDataType.VersionInvariantName())); 115 115 } catch (MissingMethodException e) { 116 Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e); 116 Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e); 117 117 } catch (ArgumentException e) { 118 118 Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e); 119 119 } 120 120 } 121 if (t.GetInterface(typeof 121 if (t.GetInterface(typeof(IDecomposer).FullName) != null) { 122 122 try { 123 Decomposers.Add((IDecomposer) 123 Decomposers.Add((IDecomposer)Activator.CreateInstance(t, true)); 124 124 Logger.Debug("discovered decomposer " + t.VersionInvariantName()); 125 125 } catch (MissingMethodException e) { 126 Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e); 126 Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e); 127 127 } catch (ArgumentException e) { 128 128 Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e); … … 136 136 format.Name, 137 137 format.SerialDataType, 138 t.VersionInvariantName())); 138 t.VersionInvariantName())); 139 139 } catch (MissingMethodException e) { 140 Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e); 140 Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e); 141 141 } catch (ArgumentException e) { 142 142 Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e); 143 } 143 } 144 144 } 145 145 } … … 148 148 public Configuration GetDefaultConfig(IFormat format) { 149 149 Dictionary<Type, IFormatter> formatterConfig = new Dictionary<Type, IFormatter>(); 150 foreach ( IFormatter f in Formatters[format.SerialDataType]) {151 if ( ! formatterConfig.ContainsKey(f.SourceType))150 foreach (IFormatter f in Formatters[format.SerialDataType]) { 151 if (!formatterConfig.ContainsKey(f.SourceType)) 152 152 formatterConfig.Add(f.SourceType, f); 153 153 } … … 161 161 } 162 162 163 public void DefineConfiguration(Configuration configuration) { 163 public void DefineConfiguration(Configuration configuration) { 164 164 customConfigurations[configuration.Format] = configuration; 165 165 SaveSettings(); … … 167 167 168 168 } 169 169 170 170 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/DataMemberAccessor.cs
r1542 r1566 11 11 public readonly Getter Get; 12 12 public readonly Setter Set; 13 public readonly string Name; 13 public readonly string Name; 14 14 public readonly object DefaultValue; 15 15 … … 21 21 FieldInfo fieldInfo = (FieldInfo)memberInfo; 22 22 Get = () => fieldInfo.GetValue(obj); 23 Set = value => fieldInfo.SetValue(obj, value); 23 Set = value => fieldInfo.SetValue(obj, value); 24 24 } else if (memberInfo.MemberType == MemberTypes.Property) { 25 25 PropertyInfo propertyInfo = (PropertyInfo)memberInfo; … … 29 29 } 30 30 Get = () => propertyInfo.GetValue(obj, null); 31 Set = value => propertyInfo.SetValue(obj, value, null); 31 Set = value => propertyInfo.SetValue(obj, value, null); 32 32 } else { 33 33 throw new NotSupportedException( … … 41 41 string name, object defaultValue, 42 42 Getter getter, Setter setter) { 43 Name = name; 43 Name = name; 44 44 DefaultValue = defaultValue; 45 45 Get = getter; … … 48 48 49 49 public DataMemberAccessor(object o) { 50 Name = null; 50 Name = null; 51 51 DefaultValue = null; 52 52 Get = () => o; … … 55 55 56 56 public DataMemberAccessor(object o, string name) { 57 Name = name; 57 Name = name; 58 58 DefaultValue = null; 59 59 Get = () => o; -
trunk/sources/HeuristicLab.Persistence/3.3/Core/DeSerializer.cs
r1556 r1566 4 4 using HeuristicLab.Persistence.Core.Tokens; 5 5 6 namespace HeuristicLab.Persistence.Core { 6 namespace HeuristicLab.Persistence.Core { 7 7 8 public class ParentReference { }8 public class ParentReference { } 9 9 10 10 class Midwife { 11 12 public int? Id { get; private set; } 11 12 public int? Id { get; private set; } 13 13 public bool MetaMode { get; set; } 14 14 public object Obj { get; private set; } … … 22 22 this.Obj = value; 23 23 } 24 25 public Midwife(Type type, IDecomposer decomposer, int? id) { 24 25 public Midwife(Type type, IDecomposer decomposer, int? id) { 26 26 this.type = type; 27 this.decomposer = decomposer; 27 this.decomposer = decomposer; 28 28 this.Id = id; 29 MetaMode = false; 29 MetaMode = false; 30 30 metaInfo = new List<Tag>(); 31 customValues = new List<Tag>(); 31 customValues = new List<Tag>(); 32 32 } 33 33 … … 35 35 if (Obj != null) 36 36 throw new ApplicationException("object already instantiated"); 37 Obj = decomposer.CreateInstance(type, metaInfo); 37 Obj = decomposer.CreateInstance(type, metaInfo); 38 38 } 39 39 … … 49 49 decomposer.Populate(Obj, customValues, type); 50 50 } 51 } 51 } 52 52 53 53 public class Deserializer { 54 54 55 55 private readonly Dictionary<int, object> id2obj; 56 56 private readonly Dictionary<Type, object> serializerMapping; 57 private readonly Stack<Midwife> parentStack; 57 private readonly Stack<Midwife> parentStack; 58 58 private readonly Dictionary<int, Type> typeIds; 59 59 … … 79 79 } 80 80 81 public object Deserialize(IEnumerable<ISerializationToken> tokens) { 81 public object Deserialize(IEnumerable<ISerializationToken> tokens) { 82 82 foreach (ISerializationToken token in tokens) { 83 83 Type t = token.GetType(); 84 if ( t == typeof(BeginToken)) {84 if (t == typeof(BeginToken)) { 85 85 CompositeStartHandler((BeginToken)token); 86 } else if ( t == typeof(EndToken)) {87 CompositeEndHandler((EndToken) 88 } else if ( t == typeof(PrimitiveToken)) {89 PrimitiveHandler((PrimitiveToken) 90 } else if ( t == typeof(ReferenceToken)) {91 ReferenceHandler((ReferenceToken) 86 } else if (t == typeof(EndToken)) { 87 CompositeEndHandler((EndToken)token); 88 } else if (t == typeof(PrimitiveToken)) { 89 PrimitiveHandler((PrimitiveToken)token); 90 } else if (t == typeof(ReferenceToken)) { 91 ReferenceHandler((ReferenceToken)token); 92 92 } else if (t == typeof(NullReferenceToken)) { 93 93 NullHandler((NullReferenceToken)token); … … 106 106 Type type = typeIds[(int)token.TypeId]; 107 107 IDecomposer decomposer = null; 108 if ( serializerMapping.ContainsKey(type))108 if (serializerMapping.ContainsKey(type)) 109 109 decomposer = serializerMapping[type] as IDecomposer; 110 110 if (decomposer == null) … … 124 124 private void PrimitiveHandler(PrimitiveToken token) { 125 125 Type type = typeIds[(int)token.TypeId]; 126 object value = ((IFormatter) 127 if ( token.Id != null )126 object value = ((IFormatter)serializerMapping[type]).Parse(token.SerialData); 127 if (token.Id != null) 128 128 id2obj[(int)token.Id] = value; 129 129 SetValue(token.Name, value); … … 144 144 145 145 private void MetaInfoEnd(MetaInfoEndToken token) { 146 Midwife m = parentStack.Peek(); 146 Midwife m = parentStack.Peek(); 147 147 m.MetaMode = false; 148 148 CreateInstance(m); … … 152 152 m.CreateInstance(); 153 153 if (m.Id != null) 154 id2obj.Add((int)m.Id, m.Obj); 154 id2obj.Add((int)m.Id, m.Obj); 155 155 } 156 156 157 157 private void SetValue(string name, object value) { 158 if (parentStack.Count == 0) { 158 if (parentStack.Count == 0) { 159 159 parentStack.Push(new Midwife(value)); 160 160 } else { … … 162 162 if (m.MetaMode == false && m.Obj == null) 163 163 CreateInstance(m); 164 m.AddValue(name, value); 164 m.AddValue(name, value); 165 165 } 166 166 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/EmptyStorableClassAttribute.cs
r1555 r1566 5 5 namespace HeuristicLab.Persistence.Core { 6 6 7 7 8 8 [AttributeUsage( 9 9 AttributeTargets.Class, 10 AllowMultiple =false,11 Inherited =false)]10 AllowMultiple = false, 11 Inherited = false)] 12 12 public class EmptyStorableClassAttribute : Attribute { 13 13 14 14 private static readonly Dictionary<Type, bool> emptyTypeInfo = new Dictionary<Type, bool>(); 15 public static bool IsEmptyStorable(Type type) { 15 public static bool IsEmptyStorable(Type type) { 16 16 if (emptyTypeInfo.ContainsKey(type)) 17 17 return emptyTypeInfo[type]; … … 24 24 } 25 25 int nFields = 0; 26 foreach ( MemberInfo memberInfo in type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) {26 foreach (MemberInfo memberInfo in type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { 27 27 if (memberInfo.MemberType == MemberTypes.Field || 28 28 memberInfo.MemberType == MemberTypes.Property) … … 36 36 return false; 37 37 } 38 } 38 } 39 39 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/FormatBase.cs
r1564 r1566 16 16 17 17 public override bool Equals(object obj) { 18 FormatBase<SerialDataFormat> f = obj as FormatBase<SerialDataFormat>; 18 FormatBase<SerialDataFormat> f = obj as FormatBase<SerialDataFormat>; 19 19 return Equals(f); 20 20 } 21 21 22 22 } 23 23 -
trunk/sources/HeuristicLab.Persistence/3.3/Core/FormatterBase.cs
r1564 r1566 2 2 3 3 namespace HeuristicLab.Persistence.Interfaces { 4 4 5 5 public abstract class FormatterBase<Source, SerialData> : IFormatter<Source, SerialData> where SerialData : ISerialData { 6 6 … … 14 14 15 15 ISerialData IFormatter.Format(object o) { 16 return Format((Source)o); 16 return Format((Source)o); 17 17 } 18 18 … … 22 22 23 23 } 24 24 25 25 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/GeneratorBase.cs
r1564 r1566 11 11 12 12 namespace HeuristicLab.Persistence.Core { 13 13 14 14 public abstract class GeneratorBase<T> { 15 15 public T Format(ISerializationToken token) { … … 38 38 protected abstract T Format(MetaInfoBeginToken metaInfoBeginToken); 39 39 protected abstract T Format(MetaInfoEndToken metaInfoEndToken); 40 } 40 } 41 41 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs
r1564 r1566 16 16 17 17 public List<TypeMapping> TypeCache { 18 get { 18 get { 19 19 List<TypeMapping> result = new List<TypeMapping>(); 20 20 foreach (var pair in typeCache) { … … 30 30 result.Add(new TypeMapping(pair.Value, pair.Key.VersionInvariantName(), serializer)); 31 31 } 32 return result; 32 return result; 33 33 } 34 34 } 35 35 36 public Serializer(object obj, Configuration configuration) : 36 public Serializer(object obj, Configuration configuration) : 37 37 this(obj, configuration, "ROOT") { } 38 38 … … 40 40 this.obj = obj; 41 41 this.rootName = rootName; 42 this.configuration = configuration; 43 obj2id = new Dictionary<object, int> { {new object(), 0}};42 this.configuration = configuration; 43 obj2id = new Dictionary<object, int> { { new object(), 0 } }; 44 44 typeCache = new Dictionary<Type, int>(); 45 45 } … … 52 52 return Serialize(new DataMemberAccessor(rootName, null, () => obj, null)); 53 53 } 54 54 55 55 private IEnumerator<ISerializationToken> Serialize(DataMemberAccessor accessor) { 56 56 object value = accessor.Get(); … … 58 58 return NullReferenceEnumerator(accessor.Name); 59 59 if (obj2id.ContainsKey(value)) 60 return ReferenceEnumerator(accessor.Name, obj2id[value]); 61 if ( !typeCache.ContainsKey(value.GetType()))60 return ReferenceEnumerator(accessor.Name, obj2id[value]); 61 if (!typeCache.ContainsKey(value.GetType())) 62 62 typeCache.Add(value.GetType(), typeCache.Count); 63 63 int typeId = typeCache[value.GetType()]; 64 64 int? id = null; 65 if ( !value.GetType().IsValueType) {65 if (!value.GetType().IsValueType) { 66 66 id = obj2id.Count; 67 67 obj2id.Add(value, (int)id); … … 70 70 if (formatter != null) 71 71 return PrimitiveEnumerator(accessor.Name, typeId, formatter.Format(value), id); 72 IDecomposer decomposer = configuration.GetDecomposer(value.GetType()); 72 IDecomposer decomposer = configuration.GetDecomposer(value.GetType()); 73 73 if (decomposer != null) 74 74 return CompositeEnumerator(accessor.Name, decomposer.Decompose(value), id, typeId, decomposer.CreateMetaInfo(value)); … … 76 76 String.Format( 77 77 "No suitable method for serializing values of type \"{0}\" found.", 78 value.GetType().VersionInvariantName())); 78 value.GetType().VersionInvariantName())); 79 79 } 80 80 … … 94 94 private IEnumerator<ISerializationToken> CompositeEnumerator( 95 95 string name, IEnumerable<Tag> tags, int? id, int typeId, IEnumerable<Tag> metaInfo) { 96 yield return new BeginToken(name, typeId, id); 96 yield return new BeginToken(name, typeId, id); 97 97 bool first = true; 98 98 foreach (var tag in metaInfo) { … … 114 114 yield return it.Current; 115 115 } 116 yield return new EndToken(name, typeId, id); 116 yield return new EndToken(name, typeId, id); 117 117 } 118 118 -
trunk/sources/HeuristicLab.Persistence/3.3/Core/StorableAttribute.cs
r1555 r1566 7 7 [AttributeUsage( 8 8 AttributeTargets.Field | AttributeTargets.Property, 9 AllowMultiple =false,10 Inherited =false)]9 AllowMultiple = false, 10 Inherited = false)] 11 11 public class StorableAttribute : Attribute { 12 12 … … 19 19 BindingFlags.NonPublic | 20 20 BindingFlags.DeclaredOnly; 21 21 22 22 public static IEnumerable<KeyValuePair<StorableAttribute, MemberInfo>> GetStorableMembers(Type type) { 23 return GetStorableMembers(type, true); 23 return GetStorableMembers(type, true); 24 24 } 25 25 … … 27 27 GetStorableMembers(Type type, bool inherited) { 28 28 if (type.BaseType != null) 29 foreach ( var pair in GetStorableMembers(type.BaseType))29 foreach (var pair in GetStorableMembers(type.BaseType)) 30 30 yield return pair; 31 31 foreach (MemberInfo memberInfo in type.GetMembers(instanceMembers)) { 32 foreach (object attribute in memberInfo.GetCustomAttributes(false)) { 32 foreach (object attribute in memberInfo.GetCustomAttributes(false)) { 33 33 StorableAttribute storableAttribute = 34 34 attribute as StorableAttribute; 35 35 if (storableAttribute != null) { 36 yield return new KeyValuePair<StorableAttribute, MemberInfo>(storableAttribute, memberInfo); 36 yield return new KeyValuePair<StorableAttribute, MemberInfo>(storableAttribute, memberInfo); 37 37 } 38 38 } 39 } 39 } 40 40 } 41 41 … … 46 46 storableAccessors.Add(pair.Value.Name, 47 47 new DataMemberAccessor(pair.Value, pair.Key, obj)); 48 } 48 } 49 49 return storableAccessors; 50 } 50 } 51 51 } 52 52 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tag.cs
r1553 r1566 5 5 public class Tag { 6 6 public string Name { get; private set; } 7 public object Value { get; set; } 7 public object Value { get; set; } 8 8 9 9 public Tag(string name, object value) { -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tokens/MetaInfoEndToken.cs
r1556 r1566 4 4 5 5 public class MetaInfoEndToken : ISerializationToken { } 6 6 7 7 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tokens/NulLReferenceToken.cs
r1556 r1566 2 2 3 3 namespace HeuristicLab.Persistence.Core.Tokens { 4 4 5 5 public class NullReferenceToken : SerializationTokenBase { 6 6 public NullReferenceToken(string name) : base(name) { } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tokens/PrimitiveToken.cs
r1564 r1566 2 2 3 3 namespace HeuristicLab.Persistence.Core.Tokens { 4 5 public class PrimitiveToken : SerializationTokenBase { 4 5 public class PrimitiveToken : SerializationTokenBase { 6 6 public readonly int TypeId; 7 7 public readonly int? Id; -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tokens/ReferenceToken.cs
r1556 r1566 2 2 3 3 namespace HeuristicLab.Persistence.Core.Tokens { 4 5 public class ReferenceToken : SerializationTokenBase { 4 5 public class ReferenceToken : SerializationTokenBase { 6 6 public readonly int Id; 7 7 public ReferenceToken(string name, int id) -
trunk/sources/HeuristicLab.Persistence/3.3/Core/TypeExtensions.cs
r1555 r1566 3 3 4 4 namespace HeuristicLab.Persistence.Core { 5 5 6 6 public static class TypeExtensions { 7 7 … … 12 12 } 13 13 14 } 15 14 } 15 16 16 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/TypeMapping.cs
r1542 r1566 6 6 public readonly int Id; 7 7 public readonly string TypeName; 8 public readonly string Serializer; 8 public readonly string Serializer; 9 9 public TypeMapping(int id, string typeName, string serializer) { 10 10 Id = id; … … 16 16 {"id", Id}, 17 17 {"typeName", TypeName}, 18 {"serializer", Serializer}}; 18 {"serializer", Serializer}}; 19 19 } 20 } 20 } 21 21 22 22 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/TypeStringBuilder.cs
r1555 r1566 7 7 8 8 internal static void BuildDeclaringTypeChain(Type type, StringBuilder sb) { 9 if ( type.DeclaringType != null) {9 if (type.DeclaringType != null) { 10 10 BuildDeclaringTypeChain(type.DeclaringType, sb); 11 11 sb.Append(type.DeclaringType.Name).Append('+'); … … 20 20 sb.Append("["); 21 21 Type[] args = type.GetGenericArguments(); 22 for ( int i = 0; i<args.Length; i++) {22 for (int i = 0; i < args.Length; i++) { 23 23 sb.Append("["); 24 24 BuildVersionInvariantName(args[i], sb); … … 28 28 sb.Remove(sb.Length - 1, 1); 29 29 sb.Append("]"); 30 } 30 } 31 31 sb.Append(", ").Append(type.Assembly.GetName().Name); 32 32 } 33 33 34 34 } 35 35 36 36 }
Note: See TracChangeset
for help on using the changeset viewer.