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)

Location:
branches/New Persistence Exploration/Persistence/Persistence
Files:
5 edited

Legend:

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

    r1336 r1339  
    107107      if (FindCompoundSerializer(start.Type) != null) {
    108108        instance = new ParentReference();
    109         compositeStack.Push(new CustomObject(instance));
    110         id2obj.Add(start.Id, instance);       
     109        compositeStack.Push(new CustomObject(instance));       
    111110      } else {       
    112111        instance = Activator.CreateInstance(start.Type, true);
    113112        Dictionary<string, DataMemberAccessor> accessorDict =
    114113          StorableAttribute.GetAutostorableAccessors(instance);
    115         compositeStack.Push(new CompositeObject(instance, accessorDict));
    116         id2obj.Add(start.Id, instance);
     114        compositeStack.Push(new CompositeObject(instance, accessorDict));       
    117115      }
     116      if ( start.Id != null )
     117        id2obj.Add((int)start.Id, instance);
    118118    }
    119119    private void CompositeEndHandler(IParseToken token) {
     
    124124        object deserializedObject =
    125125          compoundSerializer.DeSerialize(customObject.customValues, end.Type);
    126         id2obj[end.Id] = deserializedObject;       
     126        if ( end.Id != null )
     127          id2obj[(int)end.Id] = deserializedObject;       
    127128        SetValue(end.Name, deserializedObject);         
    128129      } else {
  • 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
  • branches/New Persistence Exploration/Persistence/Persistence/Tokens.cs

    r1330 r1339  
    88  public class BeginToken : ISerializationToken {
    99    public readonly DataMemberAccessor Accessor;
    10     public readonly int Id;
    11     public BeginToken(DataMemberAccessor accessor, int id) {
     10    public readonly int? Id;
     11    public BeginToken(DataMemberAccessor accessor, int? id) {
    1212      Accessor = accessor;
    1313      Id = id;
     
    1616  public class EndToken : ISerializationToken {
    1717    public readonly DataMemberAccessor Accessor;
    18     public readonly int Id;
    19     public EndToken(DataMemberAccessor accessor, int id) {
     18    public readonly int? Id;
     19    public EndToken(DataMemberAccessor accessor, int? id) {
    2020      Accessor = accessor;
    2121      Id = id;
     
    5555    public readonly string Name;
    5656    public readonly Type Type;
    57     public readonly int Id;
    58     public CompositeStart(string name, Type type, int id) {
     57    public readonly int? Id;
     58    public CompositeStart(string name, Type type, int? id) {
    5959      Name = name;
    6060      Type = type;
     
    6565    public readonly string Name;
    6666    public readonly Type Type;
    67     public readonly int Id;
    68     public CompositeEnd(string name, Type type, int id) {
     67    public readonly int? Id;
     68    public CompositeEnd(string name, Type type, int? id) {
    6969      Name = name;
    7070      Type = type;
  • branches/New Persistence Exploration/Persistence/Persistence/XmlFormatter.cs

    r1338 r1339  
    11using System.Collections.Generic;
    22using System;
    3 using System.Xml;
    43using System.Text;
    54namespace Persistence {
     
    5352    private string FormatBegin(ISerializationToken token) {     
    5453      BeginToken beginToken = (BeginToken)token;
     54      var attributes = new Dictionary<string, string> {
     55        {"name", beginToken.Accessor.Name},
     56        {"type", beginToken.Accessor.Get().GetType().AssemblyQualifiedName } };
     57      if ( beginToken.Id != null )
     58        attributes.Add("id", beginToken.Id.ToString());                                           
    5559      string result = Prefix +
    56                       FormatNode("COMPOSITE",
    57                                  new Dictionary<string, string>
    58                                    {
    59                                      {"name", beginToken.Accessor.Name},
    60                                      {"type", beginToken.Accessor.Get().GetType().AssemblyQualifiedName},
    61                                      {"id", beginToken.Id.ToString()}
    62                                    }, NodeType.Start) + "\n";
     60                      FormatNode("COMPOSITE", attributes, NodeType.Start) + "\n";
    6361      depth += 1;
    6462      return result;
  • branches/New Persistence Exploration/Persistence/Persistence/XmlParser.cs

    r1338 r1339  
    5959      string name = reader.GetAttribute("name");     
    6060      Type type = Type.GetType(reader.GetAttribute("type"));
    61       int id = int.Parse(reader.GetAttribute("id"));
     61      string idString = reader.GetAttribute("id");
     62      int? id = null;
     63      if (idString != null)
     64        id = int.Parse(idString);     
    6265      yield return new CompositeStart(name, type, id);
    6366      IEnumerator<IParseToken> iterator = GetEnumerator();
Note: See TracChangeset for help on using the changeset viewer.