Changeset 1553 for trunk/sources/HeuristicLab.Persistence/3.3/Default
- Timestamp:
- 04/14/09 13:23:08 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3/Default
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
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();
Note: See TracChangeset
for help on using the changeset viewer.