Changeset 1701


Ignore:
Timestamp:
04/29/09 13:52:20 (12 years ago)
Author:
epitzer
Message:

Replace value comparison with references comparison in serializer. (#605)

Location:
trunk/sources/HeuristicLab.Persistence
Files:
2 edited

Legend:

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

    r1679 r1701  
    77using HeuristicLab.Persistence.Default.Decomposers.Storable;
    88using System.Text;
     9using System.Runtime.InteropServices;
    910
    1011namespace HeuristicLab.Persistence.Core {
     12
     13  public class ReferenceEqualityComparer : IEqualityComparer<object> {
     14
     15    public bool Equals(object a, object b) {
     16      return Object.ReferenceEquals(a, b);
     17    }
     18
     19    public int GetHashCode(object obj) {
     20      GCHandle handle = GCHandle.Alloc(obj, GCHandleType.Weak);
     21      int address = GCHandle.ToIntPtr(handle).ToInt32();
     22      handle.Free();
     23      return address;
     24    }   
     25
     26  }
    1127
    1228  public class Serializer : IEnumerable<ISerializationToken> {
     
    4359      this.rootName = rootName;
    4460      this.configuration = configuration;
    45       obj2id = new Dictionary<object, int> { { new object(), 0 } };
     61      obj2id = new Dictionary<object,int>(new ReferenceEqualityComparer()) { { new object(), 0 } };
    4662      typeCache = new Dictionary<Type, int>();
    4763    }
     
    5975      if (value == null)
    6076        return NullReferenceEnumerator(accessor.Name);
     77      Type type = value.GetType();
    6178      if (obj2id.ContainsKey(value))
    6279        return ReferenceEnumerator(accessor.Name, obj2id[value]);
    63       if (!typeCache.ContainsKey(value.GetType()))
    64         typeCache.Add(value.GetType(), typeCache.Count);
    65       int typeId = typeCache[value.GetType()];
     80      if (!typeCache.ContainsKey(type))
     81        typeCache.Add(type, typeCache.Count);
     82      int typeId = typeCache[type];
    6683      int? id = null;
    67       if (!value.GetType().IsValueType) {
     84      if ( ! type.IsValueType) {
    6885        id = obj2id.Count;
    6986        obj2id.Add(value, (int)id);
    7087      }
    71       IFormatter formatter = configuration.GetFormatter(value.GetType());
     88      IFormatter formatter = configuration.GetFormatter(type);
    7289      if (formatter != null)
    7390        return PrimitiveEnumerator(accessor.Name, typeId, formatter.Format(value), id);
    74       IDecomposer decomposer = configuration.GetDecomposer(value.GetType());
     91      IDecomposer decomposer = configuration.GetDecomposer(type);
    7592      if (decomposer != null)
    7693        return CompositeEnumerator(accessor.Name, decomposer.Decompose(value), id, typeId, decomposer.CreateMetaInfo(value));
  • trunk/sources/HeuristicLab.Persistence/UnitTests/UseCases.cs

    r1684 r1701  
    3636    [Storable]
    3737    private ulong _ulong = 123456;
     38  }
     39
     40  public class IntWrapper {
     41
     42    [Storable]
     43    public int Value;
     44
     45    private IntWrapper() { }
     46
     47    public IntWrapper(int value) {
     48      this.Value = value;
     49    }
     50
     51    public override bool Equals(object obj) {
     52      if (obj as IntWrapper == null)
     53        return false;
     54      return Value.Equals(((IntWrapper)obj).Value);
     55    }
     56    public override int GetHashCode() {
     57      return Value.GetHashCode();
     58    }
     59
    3860  }
    3961
     
    449471    }
    450472
     473    [TestMethod]
     474    public void TestAliasingWithOverriddenEquals() {
     475      List<IntWrapper> ints = new List<IntWrapper>();
     476      ints.Add(new IntWrapper(1));
     477      ints.Add(new IntWrapper(1));
     478      Assert.AreEqual(ints[0], ints[1]);
     479      Assert.AreNotSame(ints[0], ints[1]);
     480      XmlGenerator.Serialize(ints, tempFile);
     481      List<IntWrapper> newInts = (List<IntWrapper>)XmlParser.DeSerialize(tempFile);
     482      Assert.AreEqual(newInts[0].Value, 1);
     483      Assert.AreEqual(newInts[1].Value, 1);
     484      Assert.AreEqual(newInts[0], newInts[1]);
     485      Assert.AreNotSame(newInts[0], newInts[1]);
     486    }
    451487
    452488    [ClassInitialize]
Note: See TracChangeset for help on using the changeset viewer.