Changeset 1434 for branches/New Persistence Exploration/Persistence
- Timestamp:
- 03/26/09 17:56:55 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/Core/DeSerializer.cs
r1425 r1434 5 5 namespace HeuristicLab.Persistence.Core { 6 6 7 struct ParentReference {}7 public struct ParentReference {} 8 8 delegate void Setter(object value); 9 9 … … 18 18 } 19 19 20 public void AddValue(string name, object value) { 21 customValues.Add(new Tag(name, value)); 20 public void AddValue(string name, object value, List<DeSerializer.Thunk> finalFixes) { 21 Tag t = new Tag(name, value); 22 t.finalFixes = finalFixes; 23 customValues.Add(t); 22 24 } 23 25 … … 31 33 32 34 private delegate void Handler(ISerializationToken token); 33 p rivatedelegate void Thunk();35 public delegate void Thunk(); 34 36 35 37 private readonly Dictionary<int, object> id2obj; … … 136 138 137 139 private void NullHandler(ISerializationToken token) { 138 NullReferenceToken n il= (NullReferenceToken)token;139 SetValue(n il.Name, null);140 NullReferenceToken nullToken = (NullReferenceToken)token; 141 SetValue(nullToken.Name, null); 140 142 } 141 143 … … 144 146 parentStack.Push(new CompositeObject(value)); 145 147 } else { 146 parentStack.Peek().AddValue(name, value );148 parentStack.Peek().AddValue(name, value, finalFixes); 147 149 } 148 150 } -
branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/ArrayDecomposer.cs
r1421 r1434 40 40 int[] positions = new int[rank]; 41 41 while (e.MoveNext()) { 42 a.SetValue(e.Current.Value, positions); 42 int[] currentPositions = positions; 43 e.Current.SafeSet((value) => a.SetValue(value, currentPositions)); 43 44 positions[0] += 1; 44 45 for (int i = 0; i < rank-1; i++) { -
branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/DictionaryDecomposer.cs
r1421 r1434 6 6 7 7 namespace HeuristicLab.Persistence.Default.Decomposers { 8 9 class DictionaryAdder { 10 11 bool keyIsSet, valueIsSet; 12 object key; 13 object value; 14 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 value) { 23 key = value; 24 keyIsSet = true; 25 check(); 26 } 27 28 public void SetValue(object value) { 29 this.value = value; 30 valueIsSet = true; 31 check(); 32 } 33 34 private void check() { 35 if ( keyIsSet && valueIsSet ) 36 dict.Add(key, value); 37 } 38 39 } 8 40 9 41 public class DictionaryDecomposer : IDecomposer { … … 29 61 IEnumerator<Tag> iter = o.GetEnumerator(); 30 62 while (iter.MoveNext()) { 31 object key = iter.Current.Value;63 Tag key = iter.Current; 32 64 iter.MoveNext(); 33 object value = iter.Current.Value; 34 dict.Add(key, value); 65 Tag value = iter.Current; 66 DictionaryAdder da = new DictionaryAdder(dict); 67 key.SafeSet(da.SetKey); 68 value.SafeSet(da.SetValue); 35 69 } 36 70 return dict; -
branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/EnumerableDecomposer.cs
r1421 r1434 7 7 8 8 namespace HeuristicLab.Persistence.Default.Decomposers { 9 10 public class EnumerableDecomposer : IDecomposer { 9 10 public abstract class EnumerableDecomposer : IDecomposer { 11 12 public abstract void PerformAdd(object instance, MethodInfo addMethod, Tag tag); 11 13 12 14 public bool CanDecompose(Type type) { … … 19 21 BindingFlags.NonPublic | 20 22 BindingFlags.Instance, 21 null, Type.EmptyTypes, null) != null; 23 null, Type.EmptyTypes, null) != null; 22 24 } 23 25 … … 29 31 30 32 public object CreateInstance(Type type) { 31 return Activator.CreateInstance(type, true); 33 return Activator.CreateInstance(type, true); 32 34 } 33 35 34 public object Populate(object instance, IEnumerable<Tag> objects, Type type) {36 public object Populate(object instance, IEnumerable<Tag> tags, Type type) { 35 37 MethodInfo addMethod = type.GetMethod("Add"); 36 foreach (var pair in objects) {37 addMethod.Invoke(instance, new[] {pair.Value});38 foreach (var tag in tags) { 39 PerformAdd(instance, addMethod, tag); 38 40 } 39 41 return instance; … … 42 44 } 43 45 46 /// <summary> 47 /// Does never re-order elements but cannot reconstruct enumerables that directly 48 /// or indirectly contain references to not-yet-contructed parent objects (e.g. arrays). 49 /// </summary> 50 public class SafeEnumerableDecomposer : EnumerableDecomposer { 51 public override void PerformAdd(object instance, MethodInfo addMethod, Tag tag) { 52 addMethod.Invoke(instance, new[] { tag.Value }); 53 } 54 } 55 /// <summary> 56 /// May re-order elements if the enumerable contains references to 57 /// not-yet-constructed parent objects (e.g. arrays). 58 /// </summary> 59 public class RobustEnumerableDecomposer : EnumerableDecomposer { 60 public override void PerformAdd(object instance, MethodInfo addMethod, Tag tag) { 61 tag.SafeSet((value) => addMethod.Invoke(instance, new[] { value })); 62 } 63 } 64 44 65 } -
branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/KeyValuePairDecomposer.cs
r1421 r1434 5 5 using HeuristicLab.Persistence.Core; 6 6 using HeuristicLab.Persistence.Interfaces; 7 using System.Reflection; 7 8 8 9 namespace HeuristicLab.Persistence.Default.Decomposers { … … 23 24 24 25 public object CreateInstance(Type type) { 25 return null;26 return Activator.CreateInstance(type, true); 26 27 } 27 28 … … 29 30 IEnumerator<Tag> iter = o.GetEnumerator(); 30 31 iter.MoveNext(); 31 object key = iter.Current.Value; 32 FieldInfo keyFieldInfo = 33 t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) 34 .Single((fi) => fi.Name == "key"); 35 FieldInfo valueFieldInfo = 36 t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) 37 .Single((fi) => fi.Name == "value"); 38 iter.Current.SafeSet((value) => keyFieldInfo.SetValue(instance, value)); 32 39 iter.MoveNext(); 33 object value = iter.Current.Value;34 return Activator.CreateInstance(t, new object[] { key, value });40 iter.Current.SafeSet((value) => valueFieldInfo.SetValue(instance, value)); 41 return instance; 35 42 } 36 43 } -
branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/StorableDecomposer.cs
r1423 r1434 28 28 29 29 public object Populate(object instance, IEnumerable<Tag> objects, Type type) { 30 var memberDict = new Dictionary<string, object>();30 var memberDict = new Dictionary<string, Tag>(); 31 31 IEnumerator<Tag> iter = objects.GetEnumerator(); 32 32 while (iter.MoveNext()) { 33 memberDict.Add((string)iter.Current.Name, iter.Current .Value);33 memberDict.Add((string)iter.Current.Name, iter.Current); 34 34 } 35 35 foreach (var mapping in StorableAttribute.GetAutostorableAccessors(instance)) { 36 if ( memberDict.ContainsKey(mapping.Key) )37 m apping.Value.Set(memberDict[mapping.Key]);38 else if ( mapping.Value.DefaultValue != null )36 if (memberDict.ContainsKey(mapping.Key)) { 37 memberDict[mapping.Key].SafeSet(mapping.Value.Set); 38 } else if (mapping.Value.DefaultValue != null) { 39 39 mapping.Value.Set(mapping.Value.DefaultValue); 40 } 40 41 } 41 42 return instance; -
branches/New Persistence Exploration/Persistence/Persistence/Interfaces/IDecomposer.cs
r1419 r1434 2 2 using System.Collections; 3 3 using System.Collections.Generic; 4 using HeuristicLab.Persistence.Core; 4 5 5 6 namespace HeuristicLab.Persistence.Interfaces { 6 7 7 public struct Tag { 8 public string Name; 9 public object Value; 8 public class Tag { 9 public List<DeSerializer.Thunk> finalFixes; 10 public string Name { get; private set; } 11 public object Value; 12 10 13 public Tag(string name, object value) { 11 Name = name;12 Value = value;14 this.Name = name; 15 this.Value = value; 13 16 } 14 17 public Tag(object value) { 15 Name = null; 16 Value = value; 18 this.Name = null; 19 this.Value = value; 20 } 21 public void SafeSet(DataMemberAccessor.Setter setter) { 22 if ( Value != null && Value.GetType() == typeof(ParentReference)) 23 finalFixes.Add(() => setter(Value)); 24 else 25 setter(Value); 17 26 } 18 27 } -
branches/New Persistence Exploration/Persistence/Test/NewSerializationTest.cs
r1429 r1434 144 144 return clone; 145 145 } 146 } 146 } 147 148 public class C { 149 [Storable] 150 public C[][] allCs; 151 public KeyValuePair<List<C>, C> kvpList; 152 } 147 153 148 154 public class NewSerializationTest { … … 162 168 } 163 169 170 public static void Test3() { 171 C c = new C(); 172 C[][] cs = new C[2][]; 173 cs[0] = new C[] { c }; 174 cs[1] = new C[] { c }; 175 c.allCs = cs; 176 c.kvpList = new KeyValuePair<List<C>, C>(new List<C> { c }, c); 177 XmlGenerator.Serialize(cs, "test3"); 178 object o = XmlParser.DeSerialize("test3"); 179 Console.Out.WriteLine(Util.AutoFormat(o, true)); 180 Console.WriteLine(ViewOnlyGenerator.Serialize(cs)); 181 Console.WriteLine(ViewOnlyGenerator.Serialize(o)); 182 } 183 164 184 public static void Test2() { 165 185 Manager m = new Manager(); … … 190 210 191 211 192 public static void Main() { 212 public static void Main() { 193 213 Test1(); 194 214 //Test2(); 215 Test3(); 195 216 //SpeedTest(); 196 217 //SpeedTest2();
Note: See TracChangeset
for help on using the changeset viewer.