Changeset 1276


Ignore:
Timestamp:
03/06/09 14:57:19 (11 years ago)
Author:
epitzer
Message:

Cleaner distinction between custom serialization and composite serialization. (#506)

File:
1 edited

Legend:

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

    r1274 r1276  
    221221  }
    222222
    223   public class DeSerializer {   
    224 
    225     class AccessibleObject {
     223  public class DeSerializer {
     224
     225    interface IAccessibleObject {
     226      object Obj { get; }
     227    }
     228
     229    class CustomObject : IAccessibleObject {
     230      public object Obj { get { return this.obj; } }
     231      private object obj;
     232      public List<object> customValues;
     233      public CustomObject(object obj) {
     234        this.obj = obj;
     235        this.customValues = new List<object>();
     236      }
     237      public void AddValue(object value) {
     238        customValues.Add(value);
     239      }
     240    }
     241
     242    class CompositeObject : IAccessibleObject{
     243      public object Obj { get { return this.obj; } }
    226244      public object obj;
    227245      public Dictionary<string, DataMemberAccessor> accessorDict;
    228       public List<object> customValues;
    229       public AccessibleObject(object obj, Dictionary<string, DataMemberAccessor> accessorDict) {
     246      public CompositeObject(object obj, Dictionary<string, DataMemberAccessor> accessorDict) {
    230247        this.obj = obj;
    231         this.accessorDict = new Dictionary<string, DataMemberAccessor>();
    232         this.customValues = new List<object>();
     248        this.accessorDict = new Dictionary<string, DataMemberAccessor>();       
    233249        foreach (KeyValuePair<string, DataMemberAccessor> pair in accessorDict) {
    234250          this.accessorDict.Add(
     
    238254      }
    239255      public void SetValue(string name, object value) {
    240         if (name == "") {
    241           customValues.Add(value);
    242         } else {
    243           accessorDict[name].Set(value);
    244         }
     256        accessorDict[name].Set(value);       
    245257      }
    246258      public void SetAllDefaultValues() {
     259        throw new NotImplementedException();
    247260      }
    248261    }
     
    252265    private Dictionary<int, object> id2obj;
    253266    private Dictionary<Type, Handler> handlers;
    254     private Stack<AccessibleObject> compositeStack;   
     267    private Stack<IAccessibleObject> compositeStack;   
    255268
    256269    private Dictionary<Type, IPrimitiveSerializer> primitiveSerializers;
     
    259272    public DeSerializer() {
    260273      id2obj = new Dictionary<int, object>();
    261       compositeStack = new Stack<AccessibleObject>();
     274      compositeStack = new Stack<IAccessibleObject>();
    262275      handlers = new Dictionary<Type, Handler>();
    263276      handlers.Add(typeof(CompositeStart), new Handler(CompositeStartHandler));
     
    278291        handlers[token.GetType()].Invoke(token);
    279292      }
    280       return compositeStack.Pop().obj;
     293      return compositeStack.Pop().Obj;
    281294    }
    282295    private void CompositeStartHandler(IParseToken token) {
     
    284297      object instance = null;
    285298      if (this.FindCustomSerializer(start.Type) != null) {
    286         instance = null;
    287         compositeStack.Push(new AccessibleObject(null, new Dictionary<string, DataMemberAccessor>()));
    288         id2obj.Add(start.Id, null);
     299        instance = new object();
     300        compositeStack.Push(new CustomObject(instance));
     301        id2obj.Add(start.Id, instance);
    289302        // TODO: add warning proxy
    290303      } else {
     
    292305        Dictionary<string, DataMemberAccessor> accessorDict =
    293306        StorableAttribute.GetAutostorableAccessors(instance);
    294         compositeStack.Push(new AccessibleObject(instance, accessorDict));
     307        compositeStack.Push(new CompositeObject(instance, accessorDict));
    295308        id2obj.Add(start.Id, instance);
    296309      }
    297310    }
    298311    private void CompositeEndHandler(IParseToken token) {
    299       CompositeEnd end = (CompositeEnd)token;
    300       AccessibleObject accessibleObject = compositeStack.Pop();
     312      CompositeEnd end = (CompositeEnd)token;     
    301313      ICustomSerializer customSerializer = this.FindCustomSerializer(end.Type);
    302314      if (customSerializer != null) {
     315        CustomObject customObject = (CustomObject)compositeStack.Pop(); 
    303316        this.SetValue(end.Name,
    304           customSerializer.DeSerialize(accessibleObject.customValues, end.Type));
     317          customSerializer.DeSerialize(customObject.customValues, end.Type));
    305318      } else {
    306         this.SetValue(end.Name, accessibleObject.obj);
     319        CompositeObject compositeObject = (CompositeObject)compositeStack.Pop();
     320        this.SetValue(end.Name, compositeObject.obj);
    307321      }
    308322    }
     
    329343    private void SetValue(string name, object value) {
    330344       if (compositeStack.Count == 0) {
    331         compositeStack.Push(new AccessibleObject(value, new Dictionary<string,DataMemberAccessor>()));
    332       } else {
    333         compositeStack.Peek().SetValue(name, value);
     345        compositeStack.Push(new CompositeObject(value, new Dictionary<string,DataMemberAccessor>()));
     346       } else {
     347         object accessibleObject = compositeStack.Peek();
     348         if ( accessibleObject is CompositeObject ) {
     349           ((CompositeObject)accessibleObject).SetValue(name, value);
     350         } else if (accessibleObject is CustomObject) {           
     351           ((CustomObject)accessibleObject).AddValue(value);
     352         }
    334353      }     
    335354    }
Note: See TracChangeset for help on using the changeset viewer.