Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1321


Ignore:
Timestamp:
03/09/09 18:36:38 (15 years ago)
Author:
epitzer
Message:

automatic cloning with the help of Storable attributes. (#506)

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

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence.sln

    r1264 r1321  
    44Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Persistence", "Persistence\Persistence.csproj", "{102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}"
    55EndProject
     6Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PersistenceTest", "PersistenceTest\PersistenceTest.csproj", "{019D994A-9E37-4A4C-BBDD-914AB9A9634C}"
     7EndProject
     8Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{2F65B17D-22F7-4A48-8762-74BB8BE3E0E5}"
     9  ProjectSection(SolutionItems) = preProject
     10    LocalTestRun.testrunconfig = LocalTestRun.testrunconfig
     11    Persistence.vsmdi = Persistence.vsmdi
     12  EndProjectSection
     13EndProject
    614Global
     15  GlobalSection(TestCaseManagementSettings) = postSolution
     16    CategoryFile = Persistence.vsmdi
     17  EndGlobalSection
    718  GlobalSection(SolutionConfigurationPlatforms) = preSolution
    819    Debug|Any CPU = Debug|Any CPU
     
    1829    {102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}.Release|Any CPU.Build.0 = Release|Any CPU
    1930    {102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}.Release|x86.ActiveCfg = Release|Any CPU
     31    {019D994A-9E37-4A4C-BBDD-914AB9A9634C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
     32    {019D994A-9E37-4A4C-BBDD-914AB9A9634C}.Debug|Any CPU.Build.0 = Debug|Any CPU
     33    {019D994A-9E37-4A4C-BBDD-914AB9A9634C}.Debug|x86.ActiveCfg = Debug|Any CPU
     34    {019D994A-9E37-4A4C-BBDD-914AB9A9634C}.Release|Any CPU.ActiveCfg = Release|Any CPU
     35    {019D994A-9E37-4A4C-BBDD-914AB9A9634C}.Release|Any CPU.Build.0 = Release|Any CPU
     36    {019D994A-9E37-4A4C-BBDD-914AB9A9634C}.Release|x86.ActiveCfg = Release|Any CPU
    2037  EndGlobalSection
    2138  GlobalSection(SolutionProperties) = preSolution
  • branches/New Persistence Exploration/Persistence/Persistence/NewSerializationTest.cs

    r1320 r1321  
    7878      DeSerializer deSerializer = new DeSerializer();       
    7979      object o = deSerializer.DeSerialize(parser);
     80      Root t = StorableAttribute.Clone(r);
    8081      Console.Out.WriteLine(Util.AutoFormat(o, true));     
    8182    }
     
    9596      DeSerializer deSerializer = new DeSerializer();
    9697      object o = deSerializer.DeSerialize(parser);
    97       Console.Out.WriteLine(Util.AutoFormat(o, true));
     98      Manager n = StorableAttribute.Clone(m);
     99      Console.Out.WriteLine(Util.AutoFormat(o, true));     
    98100    }
    99101
    100102    public static void Main() {
    101       //Test1();
     103      Test1();
    102104      Test2();
    103105      Console.In.ReadLine();
  • branches/New Persistence Exploration/Persistence/Persistence/StorableAttribute.cs

    r1264 r1321  
    33using System.Text;
    44using System.Reflection;
     5using System.Collections;
    56
    67namespace Persistence {
     
    4445      }
    4546      return autoStorableAccessors;
     47    }
     48
     49    private static MethodInfo MemberwiseClone;
     50
     51    public static StorableAttribute() {
     52      foreach (MethodInfo mi in typeof(object).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic)) {
     53        if (mi.Name == "MemberwiseClone") {
     54          MemberwiseClone = mi;
     55        }
     56      }
     57    }
     58
     59    public static T Clone<T>(T obj) {
     60      List<ICompoundSerializer> compoundSerializers =
     61        InterfaceInstantiatior.InstantiateAll<ICompoundSerializer>();
     62      return Clone(obj, compoundSerializers);
     63    }
     64
     65    public static T Clone<T>(T obj, IEnumerable<ICompoundSerializer> compoundSerializers) {
     66      Dictionary<object, object> twins = new Dictionary<object, object>();     
     67      return Clone(obj, twins, compoundSerializers);
    4668    }   
     69
     70    public static T Clone<T>(T obj, Dictionary<object, object> twins,
     71      IEnumerable<ICompoundSerializer> compoundSerializers) {
     72      if (obj == null)
     73        return default(T);
     74      ValueType t = obj as ValueType;
     75      if (t != null) {
     76        return (T)obj;
     77      }
     78      if (twins.ContainsKey(obj))
     79        return (T)twins[obj];
     80      ICloneable c = obj as ICloneable;
     81      if (c != null) {
     82        T newClone = (T)c.Clone();     
     83        twins[obj] = newClone;
     84        return newClone;
     85      }
     86      T newInstance = (T)Activator.CreateInstance(obj.GetType());
     87      twins[obj] = newInstance;
     88      Dictionary<string, DataMemberAccessor> oldAccessors = GetAutostorableAccessors(obj);
     89      if (oldAccessors.Count == 0) {       
     90        foreach (ICompoundSerializer serializer in compoundSerializers) {
     91          if ( serializer.CanSerialize(obj.GetType()) ) {
     92            IEnumerable subObjects = serializer.Serialize(obj);
     93            IEnumerable clonedSubObjects = Functional.Map(subObjects,
     94              (x) => Clone(x, twins, compoundSerializers));
     95            return (T)serializer.DeSerialize(clonedSubObjects, obj.GetType());
     96          }
     97        }
     98        // TODO: fallback with memberwise clone?
     99        throw new ApplicationException("cannot continue cloning, reached a dead end");
     100      }
     101      Dictionary<string, DataMemberAccessor> newAccessors = GetAutostorableAccessors(newInstance);
     102      foreach (KeyValuePair<string, DataMemberAccessor> nameAccess in oldAccessors) {
     103        newAccessors[nameAccess.Key].Set(Clone(nameAccess.Value.Get(), twins, compoundSerializers));
     104      }
     105      return newInstance;
     106    }
    47107  }
    48108
  • branches/New Persistence Exploration/Persistence/Persistence/Util.cs

    r1318 r1321  
    33using System.Text;
    44using System.Reflection;
     5using System.Collections;
    56
    67namespace Persistence {
     8  public class Functional {
     9
     10    public delegate object Mapper(object x);
     11
     12    public static IEnumerable Map(IEnumerable enumeration, Mapper m) {
     13      foreach (object t in enumeration) {
     14        yield return m(t);
     15      }
     16    }
     17
     18  }
    719  public class InterfaceInstantiatior {
    820    public static List<T> InstantiateAll<T>() {
Note: See TracChangeset for help on using the changeset viewer.