Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/13/09 10:58:33 (15 years ago)
Author:
epitzer
Message:

support composite value types (#506)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs

    r1330 r1339  
    1212    private readonly Dictionary<Type, IPrimitiveSerializer> primitiveSerializers;
    1313    private readonly List<ICompoundSerializer> compoundSerializers;
     14    private readonly Dictionary<Type, int> typeCache;
    1415
    1516    public Serializer(object obj) :
     
    3435      this.compoundSerializers = new List<ICompoundSerializer>(compoundSerializers);
    3536      obj2id = new Dictionary<object, int> {{new object(), 0}};
    36         }
     37      typeCache = new Dictionary<Type, int>();
     38    }
    3739
    3840    IEnumerator IEnumerable.GetEnumerator() {
     
    4648      while (iterator.MoveNext())
    4749        yield return iterator.Current;
     50      Console.WriteLine("TypeCache:f");
     51      foreach ( var pair in typeCache )
     52        Console.WriteLine(pair.Key);
    4853    }
    4954
    5055    private IEnumerator<ISerializationToken> Serialize(DataMemberAccessor accessor) {
     56
    5157      object value = accessor.Get();
     58
    5259      if (value == null) {
    5360        yield return new NullReferenceToken(accessor.Name);
    54       } else if (obj2id.ContainsKey(value)) {
     61        yield break;
     62      }
     63
     64      if (obj2id.ContainsKey(value)) {
    5565        yield return new ReferenceToken(accessor.Name, obj2id[value]);
    56       } else if (primitiveSerializers.ContainsKey(value.GetType())) {
    57         int? id = null;
    58         if ( ! value.GetType().IsValueType ) {
    59           id = obj2id.Count;
    60           obj2id.Add(value, (int)id);
    61         }
     66        yield break;
     67      }
     68
     69      if ( ! typeCache.ContainsKey(value.GetType()))
     70        typeCache.Add(value.GetType(), typeCache.Count);
     71
     72      int? id = null;
     73      if ( ! value.GetType().IsValueType) {
     74        id = obj2id.Count;
     75        obj2id.Add(value, (int)id);
     76      }
     77
     78      if (primitiveSerializers.ContainsKey(value.GetType())) {
     79       
    6280        yield return new PrimitiveToken(
    6381          accessor,
    6482          primitiveSerializers[value.GetType()].Serialize(value),
    65           id);     
    66       } else {
    67         int id = obj2id.Count;
    68         obj2id.Add(value, id);
    69         yield return new BeginToken(accessor, id);
    70         ICompoundSerializer customSerializer = FindCompoundSerializer(value.GetType());
    71         if (customSerializer != null) {
    72           foreach (object o in customSerializer.Serialize(value)) {
    73             IEnumerator<ISerializationToken> iterator = Serialize(new DataMemberAccessor(o));
    74             while (iterator.MoveNext())
    75               yield return iterator.Current;
    76           }
    77         } else { // composite serialization
    78           int nSubComponents = 0;
    79           foreach (KeyValuePair<string, DataMemberAccessor> mapping in
    80             StorableAttribute.GetAutostorableAccessors(value)) {
    81             nSubComponents += 1;
    82             IEnumerator<ISerializationToken> iterator = Serialize(mapping.Value);                       
    83             while (iterator.MoveNext()) {                           
    84               yield return iterator.Current;
    85             }                       
    86           }
    87           if (nSubComponents == 0) {
    88             throw new ApplicationException(String.Format(
    89               "Composite value of type \"{0}\" contains no subcomponents",
    90               value.GetType().FullName));
    91           }
     83          id);
     84        yield break;
     85      }
     86     
     87      yield return new BeginToken(accessor, id);
     88      ICompoundSerializer customSerializer = FindCompoundSerializer(value.GetType());
     89
     90      if (customSerializer != null) {
     91        foreach (object o in customSerializer.Serialize(value)) {
     92          IEnumerator<ISerializationToken> iterator = Serialize(new DataMemberAccessor(o));
     93          while (iterator.MoveNext())
     94            yield return iterator.Current;
    9295        }
    9396        yield return new EndToken(accessor, id);
     97        yield break;
    9498      }
     99
     100      int nSubComponents = 0;
     101      foreach (KeyValuePair<string, DataMemberAccessor> mapping in
     102        StorableAttribute.GetAutostorableAccessors(value)) {
     103        nSubComponents += 1;
     104        IEnumerator<ISerializationToken> iterator = Serialize(mapping.Value);
     105        while (iterator.MoveNext()) {
     106          yield return iterator.Current;
     107        }
     108      }
     109      if (nSubComponents == 0) {
     110        throw new ApplicationException(String.Format(
     111                                         "Composite value of type \"{0}\" contains no subcomponents",
     112                                         value.GetType().FullName));
     113      }
     114      yield return new EndToken(accessor, id);
    95115    }
    96116
Note: See TracChangeset for help on using the changeset viewer.