Changeset 1553 for trunk/sources/HeuristicLab.Persistence
- Timestamp:
- 04/14/09 13:23:08 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3
- Files:
-
- 2 added
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Core/DeSerializer.cs
r1542 r1553 8 8 public class ParentReference {} 9 9 10 class CompositeObject { 10 class Midwife { 11 12 public int? Id { get; private set; } 13 public bool MetaMode { get; set; } 14 public object Obj { get; private set; } 11 15 12 public object Obj { get; private set; } 13 public List<Tag> customValues; 16 private List<Tag> metaInfo; 17 private List<Tag> customValues; 18 private Type type; 19 private IDecomposer decomposer; 14 20 15 public CompositeObject(object obj) { 16 Obj = obj; 17 customValues = new List<Tag>(); 21 public Midwife(object value) { 22 this.Obj = value; 23 } 24 25 public Midwife(Type type, IDecomposer decomposer, int? id) { 26 this.type = type; 27 this.decomposer = decomposer; 28 this.Id = id; 29 MetaMode = false; 30 metaInfo = new List<Tag>(); 31 customValues = new List<Tag>(); 18 32 } 19 33 20 public void AddValue(string name, object value, List<Thunk> finalFixes) { 21 Tag t = new Tag(name, value) {globalFinalFixes = finalFixes}; 22 customValues.Add(t); 34 public void CreateInstance() { 35 if (Obj != null) 36 throw new ApplicationException("object already instantiated"); 37 Obj = decomposer.CreateInstance(type, metaInfo); 23 38 } 24 39 25 public Setter GetSetterForLastAddedValue(string name) { 26 Tag t = customValues[customValues.Count - 1]; 27 return value => t.Value = value; 40 public void AddValue(string name, object value) { 41 if (MetaMode) { 42 metaInfo.Add(new Tag(name, value)); 43 } else { 44 customValues.Add(new Tag(name, value)); 45 } 28 46 } 29 }30 47 31 public delegate void Thunk(); 48 public void Populate() { 49 decomposer.Populate(Obj, customValues, type); 50 } 51 } 32 52 33 53 public class Deserializer { 34 54 35 55 private readonly Dictionary<int, object> id2obj; 36 private readonly Dictionary<Type, object> serializerMapping; 37 private readonly Stack<CompositeObject> parentStack; 38 private readonly Dictionary<int, Type> typeIds; 39 private List<Thunk> finalFixes; 56 private readonly Dictionary<Type, object> serializerMapping; 57 private readonly Stack<Midwife> parentStack; 58 private readonly Dictionary<int, Type> typeIds; 40 59 41 60 public Deserializer( 42 IEnumerable<TypeMapping> typeCache) { 61 IEnumerable<TypeMapping> typeCache) { 43 62 id2obj = new Dictionary<int, object>(); 44 parentStack = new Stack< CompositeObject>();63 parentStack = new Stack<Midwife>(); 45 64 typeIds = new Dictionary<int, Type>(); 46 65 serializerMapping = CreateSerializers(typeCache); … … 60 79 } 61 80 62 public object Deserialize(IEnumerable<ISerializationToken> tokens) { 63 finalFixes = new List<Thunk>(); 81 public object Deserialize(IEnumerable<ISerializationToken> tokens) { 64 82 foreach (ISerializationToken token in tokens) { 65 83 Type t = token.GetType(); … … 74 92 } else if (t == typeof(NullReferenceToken)) { 75 93 NullHandler((NullReferenceToken)token); 94 } else if (t == typeof(MetaInfoBeginToken)) { 95 MetaInfoBegin((MetaInfoBeginToken)token); 96 } else if (t == typeof(MetaInfoEndToken)) { 97 MetaInfoEnd((MetaInfoEndToken)token); 76 98 } else { 77 99 throw new ApplicationException("invalid token type"); 78 100 } 79 101 } 80 foreach (Thunk fix in finalFixes) {81 fix();82 }83 102 return parentStack.Pop().Obj; 84 103 } 85 104 86 private void CompositeStartHandler(BeginToken token) { 105 private void CompositeStartHandler(BeginToken token) { 87 106 Type type = typeIds[(int)token.TypeId]; 88 107 IDecomposer decomposer = null; 89 108 if ( serializerMapping.ContainsKey(type) ) 90 decomposer = serializerMapping[type] as IDecomposer; 109 decomposer = serializerMapping[type] as IDecomposer; 91 110 if (decomposer == null) 92 111 throw new ApplicationException(String.Format( 93 112 "No suitable method for deserialization of type \"{0}\" found.", 94 113 type.VersionInvariantName())); 95 object instance = 96 decomposer.CreateInstance(type) ?? 97 new ParentReference(); 98 parentStack.Push(new CompositeObject(instance)); 99 if ( token.Id != null ) 100 id2obj.Add((int)token.Id, instance); 114 parentStack.Push(new Midwife(type, decomposer, token.Id)); 101 115 } 102 116 103 private void CompositeEndHandler(EndToken token) { 117 private void CompositeEndHandler(EndToken token) { 104 118 Type type = typeIds[(int)token.TypeId]; 105 IDecomposer decomposer = null; 106 if (serializerMapping.ContainsKey(type)) 107 decomposer = serializerMapping[type] as IDecomposer; 108 if (decomposer == null) 109 throw new ApplicationException(String.Format( 110 "No suitable method for deserialization of type \"{0}\" found.", 111 type.VersionInvariantName())); 112 CompositeObject customComposite = parentStack.Pop(); 113 object deserializedObject = 114 decomposer.Populate(customComposite.Obj, customComposite.customValues, type); 115 if ( token.Id != null ) 116 id2obj[(int)token.Id] = deserializedObject; 117 SetValue(token.Name, deserializedObject); 119 Midwife midwife = parentStack.Pop(); 120 midwife.Populate(); 121 SetValue(token.Name, midwife.Obj); 118 122 } 119 123 120 private void PrimitiveHandler(PrimitiveToken token) { 124 private void PrimitiveHandler(PrimitiveToken token) { 121 125 Type type = typeIds[(int)token.TypeId]; 122 126 object value = ((IFormatter) serializerMapping[type]).Parse(token.SerialData); … … 126 130 } 127 131 128 private void ReferenceHandler(ReferenceToken token) { 132 private void ReferenceHandler(ReferenceToken token) { 129 133 object referredObject = id2obj[token.Id]; 130 SetValue(token.Name, referredObject); 131 if (referredObject is ParentReference) { 132 Setter set = parentStack.Peek().GetSetterForLastAddedValue(token.Name); 133 finalFixes.Add(() => set(id2obj[token.Id])); 134 } 134 SetValue(token.Name, referredObject); 135 135 } 136 136 137 private void NullHandler(NullReferenceToken token) { 137 private void NullHandler(NullReferenceToken token) { 138 138 SetValue(token.Name, null); 139 } 139 } 140 141 private void MetaInfoBegin(MetaInfoBeginToken token) { 142 parentStack.Peek().MetaMode = true; 143 } 144 145 private void MetaInfoEnd(MetaInfoEndToken token) { 146 Midwife m = parentStack.Peek(); 147 m.MetaMode = false; 148 CreateInstance(m); 149 } 150 151 private void CreateInstance(Midwife m) { 152 m.CreateInstance(); 153 if (m.Id != null) 154 id2obj.Add((int)m.Id, m.Obj); 155 } 140 156 141 157 private void SetValue(string name, object value) { 142 158 if (parentStack.Count == 0) { 143 parentStack.Push(new CompositeObject(value)); 144 } else { 145 parentStack.Peek().AddValue(name, value, finalFixes); 159 parentStack.Push(new Midwife(value)); 160 } else { 161 Midwife m = parentStack.Peek(); 162 if (m.MetaMode == false && m.Obj == null) 163 CreateInstance(m); 164 m.AddValue(name, value); 146 165 } 147 166 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs
r1542 r1553 72 72 IDecomposer decomposer = configuration.GetDecomposer(value.GetType()); 73 73 if (decomposer != null) 74 return CompositeEnumerator(accessor.Name, decomposer.Decompose(value), id, typeId );74 return CompositeEnumerator(accessor.Name, decomposer.Decompose(value), id, typeId, decomposer.CreateMetaInfo(value)); 75 75 throw new ApplicationException( 76 76 String.Format( … … 92 92 } 93 93 94 private IEnumerator<ISerializationToken> CompositeEnumerator( string name,95 IEnumerable<Tag> tags, int? id, int typeId) {94 private IEnumerator<ISerializationToken> CompositeEnumerator( 95 string name, IEnumerable<Tag> tags, int? id, int typeId, IEnumerable<Tag> metaInfo) { 96 96 yield return new BeginToken(name, typeId, id); 97 foreach (var tag in tags) { 98 IEnumerator<ISerializationToken> iterator = Serialize( 99 new DataMemberAccessor(tag.Value, tag.Name)); 100 while (iterator.MoveNext()) 101 yield return iterator.Current; 97 bool first = true; 98 foreach (var tag in metaInfo) { 99 IEnumerator<ISerializationToken> metaIt = Serialize(new DataMemberAccessor(tag.Value, tag.Name)); 100 while (metaIt.MoveNext()) { 101 if (first) { 102 yield return new MetaInfoBeginToken(); 103 first = false; 104 } 105 yield return metaIt.Current; 102 106 } 107 } 108 if (!first) { 109 yield return new MetaInfoEndToken(); 110 } 111 foreach (var tag in tags) { 112 IEnumerator<ISerializationToken> it = Serialize(new DataMemberAccessor(tag.Value, tag.Name)); 113 while (it.MoveNext()) 114 yield return it.Current; 115 } 103 116 yield return new EndToken(name, typeId, id); 104 117 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tag.cs
r1542 r1553 4 4 5 5 public class Tag { 6 internal List<Thunk> globalFinalFixes; // reference to final fixes of Deserializer7 6 public string Name { get; private set; } 8 7 public object Value { get; set; } … … 16 15 Value = value; 17 16 } 18 public void SafeSet(Setter setter) { 19 if ( Value as ParentReference != null) 20 globalFinalFixes.Add(() => setter(Value)); 21 else 22 setter(Value); 23 } 24 } 17 } 25 18 26 19 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/ArrayDecomposer.cs
r1542 r1553 16 16 } 17 17 18 public IEnumerable<Tag> CreateMetaInfo(object obj) { 19 Array a = (Array)obj; 20 yield return new Tag("rank", a.Rank); 21 for (int i = 0; i < a.Rank; i++) { 22 yield return new Tag("length_" + i, a.GetLength(i)); 23 } 24 for (int i = 0; i < a.Rank; i++) { 25 yield return new Tag("lowerBound_" + i, a.GetLowerBound(i)); 26 } 27 } 28 18 29 public IEnumerable<Tag> Decompose(object array) { 19 30 Array a = (Array)array; 20 yield return new Tag("rank", a.Rank);21 31 int[] lengths = new int[a.Rank]; 22 32 int[] lowerBounds = new int[a.Rank]; 23 33 for (int i = 0; i < a.Rank; i++) { 24 34 lengths[i] = a.GetLength(i); 25 yield return new Tag("length_" + i, a.GetLength(i));26 35 } 27 36 for (int i = 0; i < a.Rank; i++) { 28 37 lowerBounds[i] = a.GetLowerBound(i); 29 yield return new Tag("lowerBound_" + i, a.GetLowerBound(i));30 38 } 31 39 int[] positions = (int[])lowerBounds.Clone(); … … 44 52 } 45 53 46 public object CreateInstance(Type t) { 47 return null; 48 } 49 50 public object Populate(object instance, IEnumerable<Tag> elements, Type t) { 51 IEnumerator<Tag> e = elements.GetEnumerator(); 54 public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) { 55 IEnumerator<Tag> e = metaInfo.GetEnumerator(); 52 56 e.MoveNext(); 53 57 int rank = (int)e.Current.Value; … … 61 65 e.MoveNext(); 62 66 lowerBounds[i] = (int)e.Current.Value; 67 } 68 return Array.CreateInstance(t.GetElementType(), lengths, lowerBounds); 69 } 70 71 public void Populate(object instance, IEnumerable<Tag> elements, Type t) { 72 Array a = (Array)instance; 73 int[] lengths = new int[a.Rank]; 74 int[] lowerBounds = new int[a.Rank]; 75 for (int i = 0; i < a.Rank; i++) { 76 lengths[i] = a.GetLength(i); 77 } 78 for (int i = 0; i < a.Rank; i++) { 79 lowerBounds[i] = a.GetLowerBound(i); 63 80 } 64 Array a = Array.CreateInstance(t.GetElementType(), lengths, lowerBounds);65 81 int[] positions = (int[])lowerBounds.Clone(); 82 IEnumerator<Tag> e = elements.GetEnumerator(); 66 83 while (e.MoveNext()) { 67 84 int[] currentPositions = positions; 68 e.Current.SafeSet(value => a.SetValue(value, currentPositions));85 a.SetValue(e.Current.Value, currentPositions); 69 86 positions[0] += 1; 70 for (int i = 0; i < rank-1; i++) {87 for (int i = 0; i < a.Rank-1; i++) { 71 88 if (positions[i] >= lengths[i]+lowerBounds[i]) { 72 89 positions[i] = lowerBounds[i]; … … 77 94 } 78 95 } 79 return a;80 96 } 81 97 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/DictionaryDecomposer.cs
r1542 r1553 5 5 using System.Collections.Generic; 6 6 7 namespace HeuristicLab.Persistence.Default.Decomposers { 8 9 class DictionaryAdder { 10 11 bool keyIsSet, valueIsSet; 12 object key; 13 object value; 14 readonly IDictionary dict; 15 16 public DictionaryAdder(IDictionary dict) { 17 this.dict = dict; 18 keyIsSet = false; 19 valueIsSet = false; 20 } 21 22 public void SetKey(object v) { 23 key = v; 24 keyIsSet = true; 25 Check(); 26 } 27 28 public void SetValue(object v) { 29 value = v; 30 valueIsSet = true; 31 Check(); 32 } 33 34 private void Check() { 35 if ( keyIsSet && valueIsSet ) 36 dict.Add(key, value); 37 } 38 39 } 7 namespace HeuristicLab.Persistence.Default.Decomposers { 40 8 41 9 public class DictionaryDecomposer : IDecomposer { … … 50 18 } 51 19 20 public IEnumerable<Tag> CreateMetaInfo(object o) { 21 return new Tag[] { }; 22 } 23 52 24 public IEnumerable<Tag> Decompose(object o) { 53 25 IDictionary dict = (IDictionary)o; … … 58 30 } 59 31 60 public object CreateInstance(Type t ) {32 public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) { 61 33 return Activator.CreateInstance(t, true); 62 34 } 63 35 64 public objectPopulate(object instance, IEnumerable<Tag> o, Type t) {36 public void Populate(object instance, IEnumerable<Tag> o, Type t) { 65 37 IDictionary dict = (IDictionary)instance; 66 38 IEnumerator<Tag> iter = o.GetEnumerator(); … … 69 41 iter.MoveNext(); 70 42 Tag value = iter.Current; 71 DictionaryAdder da = new DictionaryAdder(dict); 72 key.SafeSet(da.SetKey); 73 value.SafeSet(da.SetValue); 74 } 75 return dict; 43 dict.Add(key.Value, value.Value); 44 } 76 45 } 77 46 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/EnumDecomposer.cs
r1542 r1553 16 16 } 17 17 18 public IEnumerable<Tag> Decompose(object obj) {18 public IEnumerable<Tag> CreateMetaInfo(object obj) { 19 19 yield return new Tag(Enum.GetName(obj.GetType(), obj)); 20 20 } 21 21 22 public object CreateInstance(Type t) {23 return n ull;22 public IEnumerable<Tag> Decompose(object obj) { 23 return new Tag[] { }; 24 24 } 25 26 public object Populate(object instance, IEnumerable<Tag> elements, Type t) {27 IEnumerator<Tag> it = elements.GetEnumerator();25 26 public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) { 27 IEnumerator<Tag> it = metaInfo.GetEnumerator(); 28 28 it.MoveNext(); 29 29 return Enum.Parse(t, (string)it.Current.Value); 30 30 } 31 31 32 public void Populate(object instance, IEnumerable<Tag> elements, Type t) { 33 } 32 34 } 33 34 35 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/EnumerableDecomposer.cs
r1542 r1553 6 6 using System.Collections.Generic; 7 7 8 namespace HeuristicLab.Persistence.Default.Decomposers { 9 10 public class EnumerableCache { 11 readonly List<object> values; 12 int nSet; 13 int count; 14 readonly object enumerable; 15 readonly MethodInfo addMethod; 16 17 public EnumerableCache(object enumerable, MethodInfo addMethod) { 18 values = new List<object>(); 19 this.enumerable = enumerable; 20 this.addMethod = addMethod; 21 count = -1; 22 } 23 24 public Setter GetNextSetter() { 25 int index = values.Count; 26 values.Add(new object()); 27 return v => Set(index, v); 28 } 29 30 private void Set(int index, object value) { 31 values[index] = value; 32 nSet += 1; 33 if (count >= 0 && nSet >= count) 34 Fill(); 35 } 36 37 public void Terminate() { 38 count = values.Count; 39 if (nSet >= count) 40 Fill(); 41 } 42 43 private void Fill() { 44 foreach ( object v in values ) { 45 addMethod.Invoke(enumerable, new[] {v}); 46 } 47 } 48 49 } 8 namespace HeuristicLab.Persistence.Default.Decomposers { 50 9 51 10 public class EnumerableDecomposer : IDecomposer { … … 68 27 } 69 28 29 public IEnumerable<Tag> CreateMetaInfo(object o) { 30 return new Tag[] { }; 31 } 32 70 33 public IEnumerable<Tag> Decompose(object obj) { 71 34 foreach (object o in (IEnumerable)obj) { … … 74 37 } 75 38 76 public object CreateInstance(Type type ) {39 public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) { 77 40 return Activator.CreateInstance(type, true); 78 41 } 79 42 80 public objectPopulate(object instance, IEnumerable<Tag> tags, Type type) {43 public void Populate(object instance, IEnumerable<Tag> tags, Type type) { 81 44 MethodInfo addMethod = type.GetMethod("Add"); 82 EnumerableCache cache = new EnumerableCache(instance, addMethod); 83 foreach (var tag in tags) { 84 tag.SafeSet(cache.GetNextSetter()); 85 } 86 cache.Terminate(); 87 return instance; 45 foreach (var tag in tags) 46 addMethod.Invoke(instance, new[] { tag.Value }); 88 47 } 89 90 } 91 48 } 92 49 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/KeyValuePairDecomposer.cs
r1542 r1553 21 21 } 22 22 23 public IEnumerable<Tag> Decompose(object o) { 23 public IEnumerable<Tag> CreateMetaInfo(object o) { 24 return new Tag[] { }; 25 } 26 27 public IEnumerable<Tag> Decompose(object o) { 24 28 Type t = o.GetType(); 25 29 yield return new Tag("key", t.GetProperty("Key").GetValue(o, null)); … … 27 31 } 28 32 29 public object CreateInstance(Type type ) {33 public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) { 30 34 return Activator.CreateInstance(type, true); 31 35 } 32 36 33 public objectPopulate(object instance, IEnumerable<Tag> o, Type t) {37 public void Populate(object instance, IEnumerable<Tag> o, Type t) { 34 38 IEnumerator<Tag> iter = o.GetEnumerator(); 39 iter.MoveNext(); 40 t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) 41 .Single(fi => fi.Name == "key").SetValue(instance, iter.Current.Value); 35 42 iter.MoveNext(); 36 FieldInfo keyFieldInfo = 37 t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) 38 .Single(fi => fi.Name == "key"); 39 FieldInfo valueFieldInfo = 40 t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) 41 .Single(fi => fi.Name == "value"); 42 iter.Current.SafeSet(value => keyFieldInfo.SetValue(instance, value)); 43 iter.MoveNext(); 44 iter.Current.SafeSet(value => valueFieldInfo.SetValue(instance, value)); 45 return instance; 46 } 43 t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) 44 .Single(fi => fi.Name == "value").SetValue(instance, iter.Current.Value); 45 } 47 46 } 48 49 47 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/StorableDecomposer.cs
r1542 r1553 17 17 EmptyStorableClassAttribute.IsEmpyStorable(type); 18 18 19 } 19 } 20 21 public IEnumerable<Tag> CreateMetaInfo(object o) { 22 return new Tag[] { }; 23 } 20 24 21 25 public IEnumerable<Tag> Decompose(object obj) { … … 25 29 } 26 30 27 public object CreateInstance(Type type ) {31 public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) { 28 32 return Activator.CreateInstance(type, true); 29 33 } 30 34 31 public objectPopulate(object instance, IEnumerable<Tag> objects, Type type) {35 public void Populate(object instance, IEnumerable<Tag> objects, Type type) { 32 36 var memberDict = new Dictionary<string, Tag>(); 33 37 IEnumerator<Tag> iter = objects.GetEnumerator(); … … 37 41 foreach (var mapping in StorableAttribute.GetAutostorableAccessors(instance)) { 38 42 if (memberDict.ContainsKey(mapping.Key)) { 39 m emberDict[mapping.Key].SafeSet(mapping.Value.Set);43 mapping.Value.Set(memberDict[mapping.Key].Value); 40 44 } else if (mapping.Value.DefaultValue != null) { 41 45 mapping.Value.Set(mapping.Value.DefaultValue); 42 46 } 43 47 } 44 return instance;45 48 } 46 47 49 } 48 50 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/TypeDecomposer.cs
r1542 r1553 17 17 } 18 18 19 public IEnumerable<Tag> Decompose(object obj) { 20 Type t = (Type) obj; 21 yield return new Tag("VersionInvariantName", t.VersionInvariantName()); 19 public IEnumerable<Tag> CreateMetaInfo(object o) { 20 yield return new Tag("VersionInvariantName", ((Type)o).VersionInvariantName()); 22 21 } 23 22 24 public object CreateInstance(Type type) {25 return n ull;23 public IEnumerable<Tag> Decompose(object obj) { 24 return new Tag[] { }; 26 25 } 27 26 28 public object Populate(object instance, IEnumerable<Tag> objects, Type type) {29 foreach ( var typeName in objects) {27 public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) { 28 foreach (var typeName in metaInfo) { 30 29 return Type.GetType((string)typeName.Value); 31 30 } 32 return null; 31 return null; 32 } 33 34 public void Populate(object instance, IEnumerable<Tag> objects, Type type) { 33 35 } 34 36 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/X2StringDecomposer.cs
r1542 r1553 72 72 } 73 73 74 public IEnumerable<Tag> CreateMetaInfo(object obj) { 75 yield return new Tag(((DateTime)obj).Ticks); 76 } 77 74 78 public IEnumerable<Tag> Decompose(object obj) { 75 yield return new Tag(((DateTime)obj).Ticks); 76 } 77 78 public object CreateInstance(Type type) { 79 return null; 80 } 81 82 public object Populate(object instance, IEnumerable<Tag> tags, Type type) { 83 foreach (Tag tag in tags) { 79 return new Tag[] { }; 80 } 81 82 public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) { 83 foreach (Tag tag in metaInfo) { 84 84 return new DateTime((long)tag.Value); 85 85 } 86 86 throw new ApplicationException("Not enough components to compose a bool."); 87 } 88 89 public void Populate(object instance, IEnumerable<Tag> tags, Type type) { 87 90 } 88 91 … … 104 107 } 105 108 106 public IEnumerable<Tag> Decompose(object obj) {107 Array a = (Array) 109 public IEnumerable<Tag> CreateMetaInfo(object obj) { 110 Array a = (Array)obj; 108 111 int[] lengths = new int[a.Rank]; 109 112 int[] lowerBounds = new int[a.Rank]; … … 134 137 } 135 138 136 public object CreateInstance(Type type) {137 return n ull;138 } 139 140 public object Populate(object instance, IEnumerable<Tag> tags, Type type) {141 var tagIter = tags.GetEnumerator();139 public IEnumerable<Tag> Decompose(object obj) { 140 return new Tag[] { }; 141 } 142 143 public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) { 144 var tagIter = metaInfo.GetEnumerator(); 142 145 tagIter.MoveNext(); 143 146 var valueIter = ((string) tagIter.Current.Value) … … 175 178 return a; 176 179 } 180 181 public void Populate(object instance, IEnumerable<Tag> tags, Type type) { 182 } 183 177 184 } 178 185 … … 222 229 ImplementsGenericEnumerable(type) && 223 230 HasAddMethod(type); 231 } 232 233 public IEnumerable<Tag> CreateMetaInfo(object o) { 234 return new Tag[] { }; 224 235 } 225 236 … … 245 256 } 246 257 247 public object CreateInstance(Type type ) {258 public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) { 248 259 return Activator.CreateInstance(type, true); 249 260 } 250 261 251 public objectPopulate(object instance, IEnumerable<Tag> tags, Type type) {262 public void Populate(object instance, IEnumerable<Tag> tags, Type type) { 252 263 Type enumerable = GetGenericEnumerableInterface(type); 253 264 Type elementType = enumerable.GetGenericArguments()[0]; … … 259 270 foreach (var value in stringValues) { 260 271 addMethod.Invoke(instance, new[] {numberConverter.Parse(value, elementType)}); 261 } 262 return instance; 263 } 264 272 } 273 } 265 274 } 266 267 275 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/ViewOnly/ViewOnlyFormat.cs
r1542 r1553 127 127 } 128 128 129 protected override string Format(MetaInfoBeginToken metaInfoBeginToken) { 130 return "["; 131 } 132 133 protected override string Format(MetaInfoEndToken metaInfoEndToken) { 134 return "]"; 135 } 136 129 137 public static string Serialize(object o) { 130 138 return Serialize(o, ConfigurationService.Instance.GetDefaultConfig(ViewOnlyFormat.Instance)); -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
r1542 r1553 19 19 public const string TYPECACHE = "TYPECACHE"; 20 20 public const string TYPE = "TYPE"; 21 public const string METAINFO = "METAINFO"; 21 22 } 22 23 … … 34 35 if (type == typeof(NullReferenceToken)) 35 36 return Format((NullReferenceToken)token); 37 if (type == typeof(MetaInfoBeginToken)) 38 return Format((MetaInfoBeginToken)token); 39 if (type == typeof(MetaInfoEndToken)) 40 return Format((MetaInfoEndToken)token); 36 41 throw new ApplicationException("Invalid token of type " + type.FullName); 37 42 } … … 41 46 protected abstract T Format(ReferenceToken referenceToken); 42 47 protected abstract T Format(NullReferenceToken nullReferenceToken); 48 protected abstract T Format(MetaInfoBeginToken metaInfoBeginToken); 49 protected abstract T Format(MetaInfoEndToken metaInfoEndToken); 43 50 } 44 51 … … 126 133 } 127 134 135 protected override string Format(MetaInfoBeginToken metaInfoBeginToken) { 136 string result = Prefix + "<" + XmlStrings.METAINFO + ">"; 137 depth += 1; 138 return result; 139 } 140 141 protected override string Format(MetaInfoEndToken metaInfoEndToken) { 142 depth -= 1; 143 return Prefix + "</" + XmlStrings.METAINFO + ">"; 144 } 145 128 146 public IEnumerable<string> Format(List<TypeMapping> typeCache) { 129 147 yield return "<" + XmlStrings.TYPECACHE + ">"; -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs
r1542 r1553 28 28 {XmlStrings.COMPOSITE, ParseComposite}, 29 29 {XmlStrings.REFERENCE, ParseReference}, 30 {XmlStrings.NULL, ParseNull} 30 {XmlStrings.NULL, ParseNull}, 31 {XmlStrings.METAINFO, ParseMetaInfo}, 31 32 }; 32 33 } … … 89 90 } 90 91 92 private IEnumerator<ISerializationToken> ParseMetaInfo() { 93 yield return new MetaInfoBeginToken(); 94 IEnumerator<ISerializationToken> iterator = GetEnumerator(); 95 while (iterator.MoveNext()) 96 yield return iterator.Current; 97 yield return new MetaInfoEndToken(); 98 } 99 91 100 IEnumerator IEnumerable.GetEnumerator() { 92 101 return GetEnumerator(); -
trunk/sources/HeuristicLab.Persistence/3.3/HeuristicLab.Persistence-3.3.csproj
r1542 r1553 119 119 <Compile Include="Default\Xml\XmlFormat.cs" /> 120 120 <Compile Include="HeuristicLabPersistencePlugin.cs" /> 121 <Compile Include="Core\De Serializer.cs" />121 <Compile Include="Core\Deserializer.cs" /> 122 122 <Compile Include="Core\Tag.cs" /> 123 123 <Compile Include="Interfaces\IFormat.cs" /> … … 126 126 <Compile Include="Interfaces\IFormatter.cs" /> 127 127 <Compile Include="Core\ConfigurationService.cs" /> 128 <Compile Include="Interfaces\Tokens\MetaInfoBeginToken.cs" /> 129 <Compile Include="Interfaces\Tokens\MetaInfoEndToken.cs" /> 128 130 <Compile Include="Interfaces\Tokens\ISerializationToken.cs" /> 129 131 <Compile Include="Interfaces\Tokens\SerializationTokenBase.cs" /> -
trunk/sources/HeuristicLab.Persistence/3.3/Interfaces/IDecomposer.cs
r1542 r1553 22 22 23 23 /// <summary> 24 /// Generate MetaInfo necessary for instance creation. (i.e. 25 /// array dimensions). 26 /// </summary> 27 IEnumerable<Tag> CreateMetaInfo(object obj); 28 29 /// <summary> 24 30 /// Decompose an object into KeyValuePairs, the Key can be null, 25 31 /// the order in which elements are generated is guaranteed to be 26 /// the same as they are supplied in the Compose method.32 /// the same as they will be supplied to the Populate method. 27 33 /// </summary> 28 34 IEnumerable<Tag> Decompose(object obj); 29 35 30 36 /// <summary> 31 /// Create an instance of the object if possible. May return null 32 /// in which case the Populate method must create the instance. 37 /// Create an instance of the object using the provided meta information. 33 38 /// </summary> 34 39 /// <param name="type"></param> 35 40 /// <returns></returns> 36 object CreateInstance(Type type );41 object CreateInstance(Type type, IEnumerable<Tag> metaInfo); 37 42 38 43 /// <summary> … … 41 46 /// the same as they where generated. Keys might be null. 42 47 /// </summary> 43 objectPopulate(object instance, IEnumerable<Tag> tags, Type type);48 void Populate(object instance, IEnumerable<Tag> tags, Type type); 44 49 } 45 50
Note: See TracChangeset
for help on using the changeset viewer.