Changeset 1419
- Timestamp:
- 03/25/09 17:16:32 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence/Persistence
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/Core/DataMemberAccessor.cs
r1360 r1419 12 12 public readonly Getter Get; 13 13 public readonly Setter Set; 14 public readonly string Name; 15 public readonly Type Type; 14 public readonly string Name; 16 15 public readonly object DefaultValue; 17 16 … … 23 22 FieldInfo fieldInfo = (FieldInfo)memberInfo; 24 23 Get = () => fieldInfo.GetValue(obj); 25 Set = value => fieldInfo.SetValue(obj, value); 26 Type = fieldInfo.FieldType; 24 Set = value => fieldInfo.SetValue(obj, value); 27 25 } else if (memberInfo.MemberType == MemberTypes.Property) { 28 26 PropertyInfo propertyInfo = (PropertyInfo)memberInfo; … … 32 30 } 33 31 Get = () => propertyInfo.GetValue(obj, null); 34 Set = value => propertyInfo.SetValue(obj, value, null); 35 Type = propertyInfo.PropertyType; 32 Set = value => propertyInfo.SetValue(obj, value, null); 36 33 } else { 37 34 throw new NotSupportedException( … … 43 40 44 41 public DataMemberAccessor( 45 string name, Type type,object defaultValue,42 string name, object defaultValue, 46 43 Getter getter, Setter setter) { 47 Name = name; 48 Type = type; 44 Name = name; 49 45 DefaultValue = defaultValue; 50 46 Get = getter; … … 53 49 54 50 public DataMemberAccessor(object o) { 55 Name = null; 56 Type = o.GetType(); 51 Name = null; 57 52 DefaultValue = null; 58 53 Get = () => o; … … 60 55 } 61 56 57 public DataMemberAccessor(object o, string name) { 58 Name = name; 59 DefaultValue = null; 60 Get = () => o; 61 Set = null; 62 } 63 64 62 65 public override string ToString() { 63 return String.Format("DataMember({0}, { 1}, {2}, {3}, {4})",66 return String.Format("DataMember({0}, {2}, {3}, {4})", 64 67 Name, 65 Type == null ? "<null>" : Type.FullName,66 68 DefaultValue ?? "<null>", 67 69 Get.Method, Set.Method); -
branches/New Persistence Exploration/Persistence/Persistence/Core/DeSerializer.cs
r1362 r1419 3 3 using HeuristicLab.Persistence.Interfaces; 4 4 5 namespace HeuristicLab.Persistence.Core { 6 5 namespace HeuristicLab.Persistence.Core { 7 6 8 7 struct ParentReference { } … … 19 18 20 19 public object Obj { get; private set; } 21 public readonly List<object> customValues;20 public List<Tag> customValues; 22 21 23 22 public CustomComposite(object obj) { 24 23 Obj = obj; 25 customValues = new List< object>();24 customValues = new List<Tag>(); 26 25 } 27 26 28 public void AddValue( object value) {29 customValues.Add( value);27 public void AddValue(string name, object value) { 28 customValues.Add(new Tag(name, value)); 30 29 } 31 30 32 public Setter GetSetter(string name) { 31 public Setter GetSetter(string name) { 33 32 int index = customValues.Count - 1; 34 return value => customValues[index] = value; 33 Tag t = customValues[index]; 34 return value => t.Value = value; 35 35 } 36 36 } … … 122 122 if ( serializerMapping.ContainsKey(type) ) 123 123 decomposer = serializerMapping[type] as IDecomposer; 124 if (decomposer != null) { 125 instance = new ParentReference(); 124 if (decomposer != null) { 125 instance = decomposer.CreateInstance(type); 126 if (instance == null) 127 instance = new ParentReference(); 126 128 parentStack.Push(new CustomComposite(instance)); 127 129 } else { … … 143 145 if (decomposer != null) { 144 146 CustomComposite customComposite = (CustomComposite)parentStack.Pop(); 145 object deserializedObject = 146 decomposer. Compose(customComposite.customValues, type);147 object deserializedObject = 148 decomposer.Populate(customComposite.Obj, customComposite.customValues, type); 147 149 if ( end.Id != null ) 148 150 id2obj[(int)end.Id] = deserializedObject; … … 188 190 ((StorableComposite)accessibleObject).SetValue(name, value); 189 191 } else if (accessibleObject is CustomComposite) { 190 ((CustomComposite)accessibleObject).AddValue( value);192 ((CustomComposite)accessibleObject).AddValue(name, value); 191 193 } 192 194 } -
branches/New Persistence Exploration/Persistence/Persistence/Core/Serializer.cs
r1404 r1419 67 67 public IEnumerator<ISerializationToken> GetEnumerator() { 68 68 DataMemberAccessor rootAccessor = new DataMemberAccessor( 69 rootName, obj.GetType(),null, () => obj, null);69 rootName, null, () => obj, null); 70 70 IEnumerator<ISerializationToken> iterator = Serialize(rootAccessor); 71 71 while (iterator.MoveNext()) … … 110 110 111 111 private IEnumerator<ISerializationToken> CompositeEnumerator(string name, 112 IEnumerable values, int? id, int typeId) {112 IEnumerable<Tag> tags, int? id, int typeId) { 113 113 yield return new BeginToken(name, typeId, id); 114 foreach (object o in values) { 115 IEnumerator<ISerializationToken> iterator = Serialize(new DataMemberAccessor(o)); 114 foreach (var tag in tags) { 115 IEnumerator<ISerializationToken> iterator = Serialize( 116 new DataMemberAccessor(tag.Value, tag.Name)); 116 117 while (iterator.MoveNext()) 117 118 yield return iterator.Current; … … 132 133 } 133 134 } 134 if (nSubComponents == 0 && ! EmptyStorableClassAttribute.IsEmpyStorable(value )) {135 if (nSubComponents == 0 && ! EmptyStorableClassAttribute.IsEmpyStorable(value.GetType())) { 135 136 throw new ApplicationException( 136 137 String.Format( -
branches/New Persistence Exploration/Persistence/Persistence/Core/StorableAttribute.cs
r1404 r1419 12 12 public class EmptyStorableClassAttribute : Attribute { 13 13 private static readonly Dictionary<Type, bool> emptyTypeInfo = new Dictionary<Type, bool>(); 14 public static bool IsEmpyStorable(object o) { 15 Type type = o.GetType(); 14 public static bool IsEmpyStorable(Type type) { 16 15 if (emptyTypeInfo.ContainsKey(type)) 17 16 return emptyTypeInfo[type]; 18 foreach (var attribute in o.GetType().GetCustomAttributes(false)) {17 foreach (var attribute in type.GetCustomAttributes(false)) { 19 18 EmptyStorableClassAttribute empty = attribute as EmptyStorableClassAttribute; 20 19 if (empty != null) { -
branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/ArrayDecomposer.cs
r1405 r1419 3 3 using HeuristicLab.Persistence.Core; 4 4 using HeuristicLab.Persistence.Interfaces; 5 using System.Collections.Generic; 5 6 6 7 namespace HeuristicLab.Persistence.Default.Decomposers { … … 13 14 } 14 15 15 public IEnumerable DeCompose(object array) {16 public IEnumerable<Tag> DeCompose(object array) { 16 17 Array a = (Array)array; 17 yield return a.Rank;18 yield return new Tag("rank", a.Rank); 18 19 for (int i = 0; i < a.Rank; i++) { 19 yield return a.GetLength(i);20 yield return new Tag("length_" + i, a.GetLength(i)); 20 21 } 21 22 foreach (object o in (Array)array) { 22 yield return o;23 yield return new Tag(null, o); 23 24 } 24 25 } 25 26 26 public object Compose(IEnumerable elements, Type t) { 27 IEnumerator e = elements.GetEnumerator(); 27 public object CreateInstance(Type t) { 28 return null; 29 } 30 31 public object Populate(object instance, IEnumerable<Tag> elements, Type t) { 32 IEnumerator<Tag> e = elements.GetEnumerator(); 28 33 e.MoveNext(); 29 int rank = (int)e.Current ;34 int rank = (int)e.Current.Value; 30 35 int[] lengths = new int[rank]; 31 36 for (int i = 0; i < rank; i++) { 32 37 e.MoveNext(); 33 lengths[i] = (int)e.Current ;38 lengths[i] = (int)e.Current.Value; 34 39 } 35 40 Array a = Array.CreateInstance(t.GetElementType(), lengths); 36 41 int[] positions = new int[rank]; 37 42 while (e.MoveNext()) { 38 a.SetValue(e.Current , positions);43 a.SetValue(e.Current.Value, positions); 39 44 positions[0] += 1; 40 45 for (int i = 0; i < rank-1; i++) { -
branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/DictionaryDecomposer.cs
r1405 r1419 3 3 using HeuristicLab.Persistence.Core; 4 4 using HeuristicLab.Persistence.Interfaces; 5 using System.Collections.Generic; 5 6 6 7 namespace HeuristicLab.Persistence.Default.Decomposers { … … 13 14 } 14 15 15 public IEnumerable DeCompose(object o) {16 public IEnumerable<Tag> DeCompose(object o) { 16 17 IDictionary dict = (IDictionary)o; 17 18 foreach ( DictionaryEntry entry in dict) { 18 yield return entry.Key;19 yield return entry.Value;19 yield return new Tag("key", entry.Key); 20 yield return new Tag("value", entry.Value); 20 21 } 21 22 } 22 23 23 public object Compose(IEnumerable o, Type t) { 24 IDictionary dict = (IDictionary)Activator.CreateInstance(t, true); 25 IEnumerator iter = o.GetEnumerator(); 24 public object CreateInstance(Type t) { 25 return Activator.CreateInstance(t, true); 26 } 27 28 public object Populate(object instance, IEnumerable<Tag> o, Type t) { 29 IDictionary dict = (IDictionary)instance; 30 IEnumerator<Tag> iter = o.GetEnumerator(); 26 31 while (iter.MoveNext()) { 27 object key = iter.Current ;32 object key = iter.Current.Value; 28 33 iter.MoveNext(); 29 object value = iter.Current ;34 object value = iter.Current.Value; 30 35 dict.Add(key, value); 31 36 } -
branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/EnumerableDecomposer.cs
r1405 r1419 4 4 using HeuristicLab.Persistence.Core; 5 5 using HeuristicLab.Persistence.Interfaces; 6 using System.Collections.Generic; 6 7 7 8 namespace HeuristicLab.Persistence.Default.Decomposers { … … 20 21 BindingFlags.Instance, 21 22 null, Type.EmptyTypes, null) != null; 22 }23 24 public IEnumerable DeCompose(object obj) {25 return (IEnumerable)obj;26 23 } 27 24 28 public object Compose(IEnumerable objects, Type type) { 29 object instance = Activator.CreateInstance(type, true); 25 public IEnumerable<Tag> DeCompose(object obj) { 26 foreach (object o in (IEnumerable)obj) { 27 yield return new Tag(null, o); 28 } 29 } 30 31 public object CreateInstance(Type type) { 32 return Activator.CreateInstance(type, true); 33 } 34 35 public object Populate(object instance, IEnumerable<Tag> objects, Type type) { 30 36 MethodInfo addMethod = type.GetMethod("Add"); 31 foreach ( object oin objects) {32 addMethod.Invoke(instance, new[] { o});37 foreach (var pair in objects) { 38 addMethod.Invoke(instance, new[] {pair.Value}); 33 39 } 34 40 return instance; -
branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/KeyValuePairDecomposer.cs
r1405 r1419 1 1 using System; 2 using System.Linq; 2 3 using System.Collections; 3 4 using System.Collections.Generic; … … 16 17 } 17 18 18 public IEnumerable DeCompose(object o) {19 public IEnumerable<Tag> DeCompose(object o) { 19 20 Type t = o.GetType(); 20 yield return t.GetProperty("Key").GetValue(o, null);21 yield return t.GetProperty("Value").GetValue(o, null);21 yield return new Tag("key", t.GetProperty("Key").GetValue(o, null)); 22 yield return new Tag("value", t.GetProperty("Value").GetValue(o, null)); 22 23 } 23 24 24 public object Compose(IEnumerable o, Type t) { 25 return Activator.CreateInstance(t, 26 new List<object>((IEnumerable<object>)o).ToArray()); 25 public object CreateInstance(Type type) { 26 return null; 27 } 28 29 public object Populate(object instance, IEnumerable<Tag> o, Type t) { 30 IEnumerator<Tag> iter = o.GetEnumerator(); 31 iter.MoveNext(); 32 object key = iter.Current.Value; 33 iter.MoveNext(); 34 object value = iter.Current.Value; 35 return Activator.CreateInstance(t, new object[] { key, value }); 27 36 } 28 37 } -
branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/TypeDecomposer.cs
r1408 r1419 3 3 using HeuristicLab.Persistence.Core; 4 4 using HeuristicLab.Persistence.Interfaces; 5 using System.Collections.Generic; 5 6 6 7 namespace HeuristicLab.Persistence.Default.Decomposers { … … 13 14 } 14 15 15 public IEnumerable DeCompose(object obj) {16 public IEnumerable<Tag> DeCompose(object obj) { 16 17 Type t = (Type) obj; 17 yield return t.AssemblyQualifiedName;18 yield return new Tag("AssemblyQualifiedName", t.AssemblyQualifiedName); 18 19 } 19 20 20 public object Compose(IEnumerable objects, Type type) { 21 foreach ( string typeName in objects ) { 22 return Type.GetType(typeName); 21 public object CreateInstance(Type type) { 22 return null; 23 } 24 25 public object Populate(object instance, IEnumerable<Tag> objects, Type type) { 26 foreach ( var typeName in objects ) { 27 return Type.GetType((string)typeName.Value); 23 28 } 24 29 return null; -
branches/New Persistence Exploration/Persistence/Persistence/HeuristicLab.Persistence.csproj
r1406 r1419 61 61 <Compile Include="Default\Decomposers\KeyValuePairDecomposer.cs" /> 62 62 <Compile Include="Default\Decomposers\DictionaryDecomposer.cs" /> 63 <Compile Include="Default\Decomposers\StorableDecomposer.cs" /> 63 64 <Compile Include="Default\Decomposers\TypeDecomposer.cs" /> 64 65 <Compile Include="Default\Xml\Compact\IntArray2XmlFormatters.cs" /> -
branches/New Persistence Exploration/Persistence/Persistence/Interfaces/IDecomposer.cs
r1360 r1419 1 1 using System; 2 2 using System.Collections; 3 using System.Collections.Generic; 3 4 4 5 namespace HeuristicLab.Persistence.Interfaces { 5 6 6 public interface IDecomposer { 7 bool CanDecompose(Type type); 8 IEnumerable DeCompose(object obj); 9 object Compose(IEnumerable objects, Type type); 7 public struct Tag { 8 public string Name; 9 public object Value; 10 public Tag(string name, object value) { 11 Name = name; 12 Value = value; 13 } 14 public Tag(object value) { 15 Name = null; 16 Value = value; 17 } 10 18 } 11 19 20 public interface IDecomposer { 21 22 /// <summary> 23 /// Determines for every type whether the decomposer is applicable. 24 /// </summary> 25 bool CanDecompose(Type type); 26 27 /// <summary> 28 /// Decompose an object into KeyValuePairs, the Key can be null, 29 /// the order in which elements are generated is guaranteed to be 30 /// the same as they are supplied in the Compose method. 31 /// </summary> 32 IEnumerable<Tag> DeCompose(object obj); 33 34 /// <summary> 35 /// Create an instance of the object if possible. May return null 36 /// in which case the Populate method must create the instance. 37 /// </summary> 38 /// <param name="type"></param> 39 /// <returns></returns> 40 object CreateInstance(Type type); 41 42 /// <summary> 43 /// Compose an object from the KeyValuePairs previously generated 44 /// in DeCompose. The order in which the values are supplied is 45 /// the same as they where generated. Keys might be null. 46 /// </summary> 47 object Populate(object instance, IEnumerable<Tag> tags, Type type); 48 } 49 12 50 }
Note: See TracChangeset
for help on using the changeset viewer.