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
Files:
2 added
16 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/DeSerializer.cs

    r1542 r1553  
    88  public class ParentReference {} 
    99
    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; }
    1115
    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;
    1420
    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>();           
    1832    }
    1933
    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);     
    2338    }
    2439
    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      }
    2846    }
    29   }
    3047
    31   public delegate void Thunk();
     48    public void Populate() {
     49      decomposer.Populate(Obj, customValues, type);
     50    }
     51  } 
    3252
    3353  public class Deserializer {
    3454   
    3555    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;
    4059
    4160    public Deserializer(
    42       IEnumerable<TypeMapping> typeCache) {     
     61      IEnumerable<TypeMapping> typeCache) {
    4362      id2obj = new Dictionary<int, object>();
    44       parentStack = new Stack<CompositeObject>();
     63      parentStack = new Stack<Midwife>();
    4564      typeIds = new Dictionary<int, Type>();
    4665      serializerMapping = CreateSerializers(typeCache);
     
    6079    }
    6180
    62     public object Deserialize(IEnumerable<ISerializationToken> tokens) {
    63       finalFixes = new List<Thunk>();     
     81    public object Deserialize(IEnumerable<ISerializationToken> tokens) {     
    6482      foreach (ISerializationToken token in tokens) {
    6583        Type t = token.GetType();
     
    7492        } else if (t == typeof(NullReferenceToken)) {
    7593          NullHandler((NullReferenceToken)token);
     94        } else if (t == typeof(MetaInfoBeginToken)) {
     95          MetaInfoBegin((MetaInfoBeginToken)token);
     96        } else if (t == typeof(MetaInfoEndToken)) {
     97          MetaInfoEnd((MetaInfoEndToken)token);
    7698        } else {
    7799          throw new ApplicationException("invalid token type");
    78100        }
    79101      }
    80       foreach (Thunk fix in finalFixes) {
    81         fix();
    82       }
    83102      return parentStack.Pop().Obj;
    84103    }
    85104
    86     private void CompositeStartHandler(BeginToken token) {     
     105    private void CompositeStartHandler(BeginToken token) {
    87106      Type type = typeIds[(int)token.TypeId];
    88107      IDecomposer decomposer = null;
    89108      if ( serializerMapping.ContainsKey(type) )
    90         decomposer = serializerMapping[type] as IDecomposer;     
     109        decomposer = serializerMapping[type] as IDecomposer;
    91110      if (decomposer == null)
    92111        throw new ApplicationException(String.Format(
    93112          "No suitable method for deserialization of type \"{0}\" found.",
    94113          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));
    101115    }
    102116
    103     private void CompositeEndHandler(EndToken token) {     
     117    private void CompositeEndHandler(EndToken token) {
    104118      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);
    118122    }
    119123
    120     private void PrimitiveHandler(PrimitiveToken token) {     
     124    private void PrimitiveHandler(PrimitiveToken token) {
    121125      Type type = typeIds[(int)token.TypeId];
    122126      object value = ((IFormatter) serializerMapping[type]).Parse(token.SerialData);
     
    126130    }
    127131
    128     private void ReferenceHandler(ReferenceToken token) {     
     132    private void ReferenceHandler(ReferenceToken token) {
    129133      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);
    135135    }
    136136
    137     private void NullHandler(NullReferenceToken token) {     
     137    private void NullHandler(NullReferenceToken token) {
    138138      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    }
    140156
    141157    private void SetValue(string name, object value) {
    142158      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);       
    146165      }
    147166    }
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs

    r1542 r1553  
    7272      IDecomposer decomposer = configuration.GetDecomposer(value.GetType());
    7373      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));
    7575      throw new ApplicationException(
    7676          String.Format(
     
    9292    }
    9393
    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) {
    9696      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;
    102106        }
     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      }
    103116      yield return new EndToken(name, typeId, id);       
    104117    }
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/Tag.cs

    r1542 r1553  
    44
    55  public class Tag {
    6     internal List<Thunk> globalFinalFixes; // reference to final fixes of Deserializer
    76    public string Name { get; private set; }
    87    public object Value { get; set; }     
     
    1615      Value = value;
    1716    }
    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  }
    2518
    2619}
  • 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}
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/ViewOnly/ViewOnlyFormat.cs

    r1542 r1553  
    127127    }
    128128
     129    protected override string Format(MetaInfoBeginToken metaInfoBeginToken) {
     130      return "[";
     131    }
     132
     133    protected override string Format(MetaInfoEndToken metaInfoEndToken) {
     134      return "]";
     135    }
     136
    129137    public static string Serialize(object o) {
    130138      return Serialize(o, ConfigurationService.Instance.GetDefaultConfig(ViewOnlyFormat.Instance));
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs

    r1542 r1553  
    1919    public const string TYPECACHE = "TYPECACHE";
    2020    public const string TYPE = "TYPE";
     21    public const string METAINFO = "METAINFO";
    2122  }
    2223
     
    3435      if (type == typeof(NullReferenceToken))
    3536        return Format((NullReferenceToken)token);
     37      if (type == typeof(MetaInfoBeginToken))
     38        return Format((MetaInfoBeginToken)token);
     39      if (type == typeof(MetaInfoEndToken))
     40        return Format((MetaInfoEndToken)token);
    3641      throw new ApplicationException("Invalid token of type " + type.FullName);
    3742    }
     
    4146    protected abstract T Format(ReferenceToken referenceToken);
    4247    protected abstract T Format(NullReferenceToken nullReferenceToken);
     48    protected abstract T Format(MetaInfoBeginToken metaInfoBeginToken);
     49    protected abstract T Format(MetaInfoEndToken metaInfoEndToken);
    4350  }
    4451
     
    126133    }
    127134
     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
    128146    public IEnumerable<string> Format(List<TypeMapping> typeCache) {
    129147      yield return "<" + XmlStrings.TYPECACHE + ">";
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs

    r1542 r1553  
    2828                     {XmlStrings.COMPOSITE, ParseComposite},
    2929                     {XmlStrings.REFERENCE, ParseReference},
    30                      {XmlStrings.NULL, ParseNull}
     30                     {XmlStrings.NULL, ParseNull},
     31                     {XmlStrings.METAINFO, ParseMetaInfo},
    3132                   };
    3233    }
     
    8990    }
    9091
     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
    91100    IEnumerator IEnumerable.GetEnumerator() {
    92101      return GetEnumerator();
  • trunk/sources/HeuristicLab.Persistence/3.3/HeuristicLab.Persistence-3.3.csproj

    r1542 r1553  
    119119    <Compile Include="Default\Xml\XmlFormat.cs" />
    120120    <Compile Include="HeuristicLabPersistencePlugin.cs" />
    121     <Compile Include="Core\DeSerializer.cs" />
     121    <Compile Include="Core\Deserializer.cs" />
    122122    <Compile Include="Core\Tag.cs" />
    123123    <Compile Include="Interfaces\IFormat.cs" />
     
    126126    <Compile Include="Interfaces\IFormatter.cs" />
    127127    <Compile Include="Core\ConfigurationService.cs" />
     128    <Compile Include="Interfaces\Tokens\MetaInfoBeginToken.cs" />
     129    <Compile Include="Interfaces\Tokens\MetaInfoEndToken.cs" />
    128130    <Compile Include="Interfaces\Tokens\ISerializationToken.cs" />
    129131    <Compile Include="Interfaces\Tokens\SerializationTokenBase.cs" />
  • trunk/sources/HeuristicLab.Persistence/3.3/Interfaces/IDecomposer.cs

    r1542 r1553  
    2222
    2323    /// <summary>
     24    /// Generate MetaInfo necessary for instance creation. (i.e.
     25    /// array dimensions).
     26    /// </summary>   
     27    IEnumerable<Tag> CreateMetaInfo(object obj);
     28
     29    /// <summary>
    2430    /// Decompose an object into KeyValuePairs, the Key can be null,
    2531    /// 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.
    2733    /// </summary>   
    2834    IEnumerable<Tag> Decompose(object obj);
    2935
    3036    /// <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.
    3338    /// </summary>
    3439    /// <param name="type"></param>
    3540    /// <returns></returns>
    36     object CreateInstance(Type type);
     41    object CreateInstance(Type type, IEnumerable<Tag> metaInfo);
    3742
    3843    /// <summary>
     
    4146    /// the same as they where generated. Keys might be null.
    4247    /// </summary>   
    43     object Populate(object instance, IEnumerable<Tag> tags, Type type);
     48    void Populate(object instance, IEnumerable<Tag> tags, Type type);
    4449  } 
    4550
Note: See TracChangeset for help on using the changeset viewer.