Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/14/09 13:23:08 (15 years ago)
Author:
epitzer
Message:

Replace final fixes for broken parent references with separation of instance creation with meta information. (#548)

Location:
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/ArrayDecomposer.cs

    r1542 r1553  
    1616    }
    1717
     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
    1829    public IEnumerable<Tag> Decompose(object array) {
    1930      Array a = (Array)array;     
    20       yield return new Tag("rank", a.Rank);
    2131      int[] lengths = new int[a.Rank];
    2232      int[] lowerBounds = new int[a.Rank];
    2333      for (int i = 0; i < a.Rank; i++) {
    2434        lengths[i] = a.GetLength(i);
    25         yield return new Tag("length_" + i, a.GetLength(i));
    2635      }
    2736      for (int i = 0; i < a.Rank; i++) {
    2837        lowerBounds[i] = a.GetLowerBound(i);
    29         yield return new Tag("lowerBound_" + i, a.GetLowerBound(i));
    3038      }
    3139      int[] positions = (int[])lowerBounds.Clone();
     
    4452    }
    4553
    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();
    5256      e.MoveNext();
    5357      int rank = (int)e.Current.Value;
     
    6165        e.MoveNext();
    6266        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);
    6380      }     
    64       Array a = Array.CreateInstance(t.GetElementType(), lengths, lowerBounds);
    6581      int[] positions = (int[])lowerBounds.Clone();
     82      IEnumerator<Tag> e = elements.GetEnumerator();
    6683      while (e.MoveNext()) {
    6784        int[] currentPositions = positions;
    68         e.Current.SafeSet(value => a.SetValue(value, currentPositions));       
     85        a.SetValue(e.Current.Value, currentPositions);
    6986        positions[0] += 1;
    70         for (int i = 0; i < rank-1; i++) {
     87        for (int i = 0; i < a.Rank-1; i++) {
    7188          if (positions[i] >= lengths[i]+lowerBounds[i]) {
    7289            positions[i] = lowerBounds[i];
     
    7794        }
    7895      }
    79       return a;
    8096    }
    8197  }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/DictionaryDecomposer.cs

    r1542 r1553  
    55using System.Collections.Generic;
    66
    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   }
     7namespace HeuristicLab.Persistence.Default.Decomposers { 
    408 
    419  public class DictionaryDecomposer : IDecomposer {
     
    5018    }
    5119
     20    public IEnumerable<Tag> CreateMetaInfo(object o) {
     21      return new Tag[] { };
     22    }
     23
    5224    public IEnumerable<Tag> Decompose(object o) {
    5325      IDictionary dict = (IDictionary)o;     
     
    5830    }
    5931
    60     public object CreateInstance(Type t) {
     32    public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
    6133      return Activator.CreateInstance(t, true);
    6234    }
    6335
    64     public object Populate(object instance, IEnumerable<Tag> o, Type t) {
     36    public void Populate(object instance, IEnumerable<Tag> o, Type t) {
    6537      IDictionary dict = (IDictionary)instance;
    6638      IEnumerator<Tag> iter = o.GetEnumerator();
     
    6941        iter.MoveNext();
    7042        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      }     
    7645    }
    7746  }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/EnumDecomposer.cs

    r1542 r1553  
    1616    }
    1717
    18     public IEnumerable<Tag> Decompose(object obj) {     
     18    public IEnumerable<Tag> CreateMetaInfo(object obj) {
    1919      yield return new Tag(Enum.GetName(obj.GetType(), obj));
    2020    }
    2121
    22     public object CreateInstance(Type t) {
    23       return null;
     22    public IEnumerable<Tag> Decompose(object obj) {
     23      return new Tag[] { };
    2424    }
    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();
    2828      it.MoveNext();
    2929      return Enum.Parse(t, (string)it.Current.Value);
    3030    }
    3131   
     32    public void Populate(object instance, IEnumerable<Tag> elements, Type t) {     
     33    }
    3234  }
    33  
    3435}
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/EnumerableDecomposer.cs

    r1542 r1553  
    66using System.Collections.Generic;
    77
    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   }
     8namespace HeuristicLab.Persistence.Default.Decomposers {
    509
    5110  public class EnumerableDecomposer : IDecomposer {
     
    6827    }
    6928
     29    public IEnumerable<Tag> CreateMetaInfo(object o) {
     30      return new Tag[] { };
     31    }
     32
    7033    public IEnumerable<Tag> Decompose(object obj) {
    7134      foreach (object o in (IEnumerable)obj) {
     
    7437    }
    7538
    76     public object CreateInstance(Type type) {
     39    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
    7740      return Activator.CreateInstance(type, true);
    7841    }
    7942
    80     public object Populate(object instance, IEnumerable<Tag> tags, Type type) {
     43    public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
    8144      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 });
    8847    }
    89 
    90   } 
    91 
     48  }
    9249}
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/KeyValuePairDecomposer.cs

    r1542 r1553  
    2121    }
    2222
    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) {
    2428      Type t = o.GetType();
    2529      yield return new Tag("key", t.GetProperty("Key").GetValue(o, null));
     
    2731    }
    2832
    29     public object CreateInstance(Type type) {
     33    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
    3034      return Activator.CreateInstance(type, true);
    3135    }
    3236
    33     public object Populate(object instance, IEnumerable<Tag> o, Type t) {
     37    public void Populate(object instance, IEnumerable<Tag> o, Type t) {
    3438      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);
    3542      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    }
    4746  }
    48 
    4947}
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/StorableDecomposer.cs

    r1542 r1553  
    1717        EmptyStorableClassAttribute.IsEmpyStorable(type);
    1818
    19     }   
     19    }
     20
     21    public IEnumerable<Tag> CreateMetaInfo(object o) {
     22      return new Tag[] { };
     23    }
    2024
    2125    public IEnumerable<Tag> Decompose(object obj) {
     
    2529    }
    2630
    27     public object CreateInstance(Type type) {
     31    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
    2832      return Activator.CreateInstance(type, true);
    2933    }
    3034
    31     public object Populate(object instance, IEnumerable<Tag> objects, Type type) {           
     35    public void Populate(object instance, IEnumerable<Tag> objects, Type type) {           
    3236      var memberDict = new Dictionary<string, Tag>();     
    3337      IEnumerator<Tag> iter = objects.GetEnumerator();     
     
    3741      foreach (var mapping in StorableAttribute.GetAutostorableAccessors(instance)) {
    3842        if (memberDict.ContainsKey(mapping.Key)) {
    39           memberDict[mapping.Key].SafeSet(mapping.Value.Set);
     43          mapping.Value.Set(memberDict[mapping.Key].Value);         
    4044        } else if (mapping.Value.DefaultValue != null) {
    4145          mapping.Value.Set(mapping.Value.DefaultValue);
    4246        }
    4347      }
    44       return instance;
    4548    }
    46 
    4749  }
    4850}
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/TypeDecomposer.cs

    r1542 r1553  
    1717    }
    1818
    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());
    2221    }
    2322
    24     public object CreateInstance(Type type) {
    25       return null;
     23    public IEnumerable<Tag> Decompose(object obj) {
     24      return new Tag[] { };
    2625    }
    2726
    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) {
    3029        return Type.GetType((string)typeName.Value);
    3130      }
    32       return null;
     31      return null;     
     32    }
     33
     34    public void Populate(object instance, IEnumerable<Tag> objects, Type type) {     
    3335    }
    3436  }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/X2StringDecomposer.cs

    r1542 r1553  
    7272    }
    7373
     74    public IEnumerable<Tag> CreateMetaInfo(object obj) {
     75      yield return new Tag(((DateTime)obj).Ticks);
     76    }
     77
    7478    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) {
    8484        return new DateTime((long)tag.Value);
    8585      }
    8686      throw new ApplicationException("Not enough components to compose a bool.");
     87    }
     88
     89    public void Populate(object instance, IEnumerable<Tag> tags, Type type) {     
    8790    }
    8891
     
    104107    }
    105108
    106     public IEnumerable<Tag> Decompose(object obj) {
    107       Array a = (Array) obj;
     109    public IEnumerable<Tag> CreateMetaInfo(object obj) {
     110      Array a = (Array)obj;
    108111      int[] lengths = new int[a.Rank];
    109112      int[] lowerBounds = new int[a.Rank];
     
    134137    }
    135138
    136     public object CreateInstance(Type type) {
    137       return null;
    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();
    142145      tagIter.MoveNext();
    143146      var valueIter = ((string) tagIter.Current.Value)
     
    175178      return a;
    176179    }
     180
     181    public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
     182    }
     183
    177184  }
    178185
     
    222229        ImplementsGenericEnumerable(type) &&
    223230        HasAddMethod(type);
     231    }
     232
     233    public IEnumerable<Tag> CreateMetaInfo(object o) {
     234      return new Tag[] { };
    224235    }
    225236
     
    245256    }
    246257
    247     public object CreateInstance(Type type) {
     258    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
    248259      return Activator.CreateInstance(type, true);
    249260    }
    250261
    251     public object Populate(object instance, IEnumerable<Tag> tags, Type type) {
     262    public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
    252263      Type enumerable = GetGenericEnumerableInterface(type);
    253264      Type elementType = enumerable.GetGenericArguments()[0];     
     
    259270      foreach (var value in stringValues) {
    260271        addMethod.Invoke(instance, new[] {numberConverter.Parse(value, elementType)});
    261       }     
    262       return instance;     
    263     }
    264    
     272      }
     273    }
    265274  }
    266 
    267275}
Note: See TracChangeset for help on using the changeset viewer.