Changeset 1683


Ignore:
Timestamp:
04/28/09 12:45:32 (12 years ago)
Author:
epitzer
Message:

Fix StackDecomposer, reverse collection before serialization (#603)

Location:
trunk/sources/HeuristicLab.Persistence
Files:
3 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/EnumerableDecomposer.cs

    r1679 r1683  
    5353    }
    5454  }
    55 
    56 
    57   [EmptyStorableClass]
    58   public class StackDecomposer : IDecomposer {
    59 
    60     public int Priority {
    61       get { return 100; }
    62     }
    63 
    64 
    65     public bool CanDecompose(Type type) {
    66       return type.IsGenericType &&
    67         type.GetGenericTypeDefinition() == typeof(Stack<>);       
    68     }
    69 
    70     public IEnumerable<Tag> CreateMetaInfo(object o) {
    71       return new Tag[] { };
    72     }
    73 
    74     public IEnumerable<Tag> Decompose(object obj) {
    75       foreach (object o in (IEnumerable)obj) {
    76         yield return new Tag(null, o);
    77       }
    78     }
    79 
    80     public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
    81       return Activator.CreateInstance(type, true);
    82     }
    83 
    84     public void Populate(object instance, IEnumerable<Tag> tags, Type type) {     
    85       MethodInfo addMethod = type.GetMethod("Push");     
    86       try {
    87         foreach (var tag in tags)
    88           addMethod.Invoke(instance, new[] { tag.Value });
    89       } catch (Exception e) {
    90         throw new PersistenceException("Exception caught while trying to populate enumerable.", e);
    91       }
    92     }
    93   }
    9455}
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/String2XmlFormatter.cs

    r1679 r1683  
    99
    1010namespace HeuristicLab.Persistence.Default.Xml.Primitive {
    11 
    12   [EmptyStorableClass]
    13   public class TimeSpan2XmlFormatter : PrimitiveXmlFormatterBase<TimeSpan> {
    14 
    15     public override XmlString Format(TimeSpan o) {
    16       return new XmlString(o.ToString());
    17     }
    18 
    19     public override TimeSpan Parse(XmlString t) {
    20       try {
    21         return TimeSpan.Parse(t.Data);
    22       } catch (FormatException x) {
    23         throw new PersistenceException("Cannot parse TimeSpan string representation.", x);
    24       } catch (OverflowException x) {
    25         throw new PersistenceException("Overflow during TimeSpan parsing.", x);
    26       }
    27     }
    28   }
    29 
    30   [EmptyStorableClass]
    31   public class Guid2XmlFormatter : PrimitiveXmlFormatterBase<Guid> {
    32 
    33     public override XmlString Format(Guid o) {
    34       return new XmlString(o.ToString("D", CultureInfo.InvariantCulture));
    35     }
    36 
    37     public override Guid Parse(XmlString t) {
    38       try {
    39         return new Guid(t.Data);
    40       } catch (FormatException x) {
    41         throw new PersistenceException("Cannot parse Guid string representation.", x);
    42       } catch (OverflowException x) {
    43         throw new PersistenceException("Overflow during Guid parsing.", x);
    44       }
    45     }
    46   }
    4711
    4812  [EmptyStorableClass]
  • trunk/sources/HeuristicLab.Persistence/3.3/HeuristicLab.Persistence-3.3.csproj

    r1659 r1683  
    9696    <Compile Include="Core\PersistenceException.cs" />
    9797    <Compile Include="Default\DebugString\Formatters\Char2DebugStringFormatter.cs" />
     98    <Compile Include="Default\Decomposers\StackDecomposer.cs" />
    9899    <Compile Include="Default\Decomposers\Number2StringConverter.cs" />
    99100    <Compile Include="Default\Decomposers\Storable\EmptyStorableClassAttribute.cs" />
     
    138139    <Compile Include="Default\Decomposers\Storable\DataMemberAccessor.cs" />
    139140    <Compile Include="Default\Xml\Compact\IntList2XmlFormatter.cs" />
     141    <Compile Include="Default\Xml\Primitive\Guid2XmlFormatter.cs" />
     142    <Compile Include="Default\Xml\Primitive\TimeSpan2XmlFormatter.cs" />
    140143    <Compile Include="Default\Xml\XmlFormatterBase.cs" />
    141144    <Compile Include="Default\Xml\Primitive\PrimitiveXmlFormatterBase.cs" />
  • trunk/sources/HeuristicLab.Persistence/UnitTests/UseCases.cs

    r1680 r1683  
    7575  public class Root : RootBase {
    7676    [Storable]
     77    public Stack<int> intStack = new Stack<int>();
     78    [Storable]
    7779    public int[] i = new[] { 3, 4, 5, 6 };
    7880    [Storable(Name="Test String")]
     
    150152    public void ComplexStorable() {
    151153      Root r = new Root();
     154      r.intStack.Push(1);
     155      r.intStack.Push(2);
     156      r.intStack.Push(3);
    152157      r.selfReferences = new List<Root> { r, r };
    153158      r.c = new Custom { r = r };
     
    211216      Assert.AreEqual(newR.multiDimArray[1, 1], 4);
    212217      Assert.AreEqual(newR.multiDimArray[1, 2], 6);
     218      Assert.AreEqual(newR.intStack.Pop(), 3);
     219      Assert.AreEqual(newR.intStack.Pop(), 2);
     220      Assert.AreEqual(newR.intStack.Pop(), 1);
    213221      Assert.IsFalse(newR.boolean);
    214222      Assert.IsTrue((DateTime.Now - newR.dateTime).TotalSeconds < 10);
Note: See TracChangeset for help on using the changeset viewer.