Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/16/09 12:40:41 (16 years ago)
Author:
epitzer
Message:

Stronger typing for formatters with the help of generics. Separate format and serial data type. (#548)

Location:
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/DoubleArray2XmlFormatters.cs

    r1542 r1564  
    44
    55namespace HeuristicLab.Persistence.Default.Xml.Compact {
    6 
    7   [EmptyStorableClass]
    8   public class DoubleArray2XmlFormatter : NumberArray2XmlFormatterBase {
    9 
    10     public override Type Type {
    11       get {
    12         return typeof(double[]);
    13       }
    14     }
    15 
     6 
     7  public abstract class DoubleArray2XmlFormatterBase<T> : NumberArray2XmlFormatterBase<T> {
     8   
    169    protected override string FormatValue(object o) {
    1710      return ((double)o).ToString("r", CultureInfo.InvariantCulture);
     
    2417
    2518  [EmptyStorableClass]
    26   public class Double2DArray2XmlFormatter : DoubleArray2XmlFormatter {
    27     public override Type Type {
    28       get {
    29         return typeof(double[,]);
    30       }
    31     }
    32   }
     19  public class Double1DArray2XmlFormatter : DoubleArray2XmlFormatterBase<double[]> { }
     20
    3321
    3422  [EmptyStorableClass]
    35   public class Double3DArray2XmlFormatter : DoubleArray2XmlFormatter {
    36     public override Type Type {
    37       get {
    38         return typeof(double[, ,]);
    39       }
    40     }
    41   }
     23  public class Double2DArray2XmlFormatter : DoubleArray2XmlFormatterBase<double[,]> { }
     24
     25  [EmptyStorableClass]
     26  public class Double3DArray2XmlFormatter : DoubleArray2XmlFormatterBase<double[,,]> { }
    4227 
    4328}
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/DoubleList2XmlFormatter.cs

    r1542 r1564  
    88
    99  [EmptyStorableClass]
    10   public class DoubleList2XmlFormatter : NumberEnumeration2XmlFormatterBase {
    11 
    12     public override Type Type {
    13       get {
    14         return typeof(List<double>);
    15       }
    16     }
     10  public class DoubleList2XmlFormatter : NumberEnumeration2XmlFormatterBase<List<double>> {   
    1711
    1812    protected override void Add(IEnumerable enumeration, object o) {
     
    2014    }
    2115
    22     protected override object Instantiate() {
     16    protected override IEnumerable Instantiate() {
    2317      return new List<double>();
    2418    }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/IntArray2XmlFormatters.cs

    r1542 r1564  
    33
    44namespace HeuristicLab.Persistence.Default.Xml.Compact {
    5 
    6   [EmptyStorableClass]
    7   public class IntArray2XmlFormatter : NumberArray2XmlFormatterBase {
    8 
    9     public override Type Type {
    10       get {
    11         return typeof(int[]);
    12       }
    13     }
     5 
     6  public abstract class IntArray2XmlFormatterBase<T> : NumberArray2XmlFormatterBase<T> {   
    147
    158    protected override string FormatValue(object o) {
     
    2215  }
    2316
     17  [EmptyStorableClass]
     18  public class Int1DArray2XmlFormatter : IntArray2XmlFormatterBase<int[]> { }
    2419
    2520  [EmptyStorableClass]
    26   public class Int2DArray2XmlFormatter : IntArray2XmlFormatter {
    27     public override Type Type {
    28       get {
    29         return typeof(int[,]);
    30       }
    31     }
    32   }
    33 
     21  public class Int2DArray2XmlFormatter : IntArray2XmlFormatterBase<int[,]> { }
    3422
    3523  [EmptyStorableClass]
    36   public class Int3DArray2XmlFormatter : IntArray2XmlFormatter {
    37     public override Type Type {
    38       get {
    39         return typeof(int[, ,]);
    40       }
    41     }
    42   }
     24  public class Int3DArray2XmlFormatter : IntArray2XmlFormatterBase<int[,,]> { }
    4325 
    4426}
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/IntList2XmlFormatter.cs

    r1542 r1564  
    77
    88  [EmptyStorableClass]
    9   public class IntList2XmlFormatter : NumberEnumeration2XmlFormatterBase {
    10 
    11     public override Type Type {
    12       get {
    13         return typeof(List<int>);
    14       }
    15     }
    16 
     9  public class IntList2XmlFormatter : NumberEnumeration2XmlFormatterBase<List<int>> {
     10   
    1711    protected override void Add(IEnumerable enumeration, object o) {
    1812      ((List<int>)enumeration).Add((int)o);
    1913    }
    2014
    21     protected override object Instantiate() {
     15    protected override IEnumerable Instantiate() {
    2216      return new List<int>();
    2317    }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberArray2XmlFormatterBase.cs

    r1542 r1564  
    66namespace HeuristicLab.Persistence.Default.Xml.Compact {
    77                                                             
    8   public abstract class NumberArray2XmlFormatterBase : IFormatter {
     8  public abstract class NumberArray2XmlFormatterBase<T> : FormatterBase<T, XmlString> {
    99
    10     public abstract Type Type { get; }
    11     public IFormat Format { get { return XmlFormat.Instance; } }
    1210    protected virtual string Separator { get { return ";"; } }
    1311    protected abstract string FormatValue(object o);
    1412    protected abstract object ParseValue(string o);
    1513
    16     public object DoFormat(object obj) {
    17       Array a = (Array)obj;
     14    public override XmlString Format(T t) {
     15      object o = (object)t;
     16      Array a = (Array)o;
    1817      int[] lengths = new int[a.Rank];
    1918      int[] lowerBounds = new int[a.Rank];
     
    4443        }
    4544      }     
    46       return sb.ToString();
     45      return new XmlString(sb.ToString());
    4746    }
    4847
    49     public object Parse(object o) {
     48    public override T Parse(XmlString x) {
    5049      IEnumerator values =
    51         ((string)o)
    52         .Split(new[] { Separator },
     50        x.Data.Split(new[] { Separator },
    5351        StringSplitOptions.RemoveEmptyEntries).GetEnumerator();
    5452      values.MoveNext();
     
    6462        lowerBounds[i] = int.Parse((string)values.Current);
    6563      }
    66       Array a = Array.CreateInstance(this.Type.GetElementType(), lengths, lowerBounds);
     64      Array a = Array.CreateInstance(this.SourceType.GetElementType(), lengths, lowerBounds);
    6765      int[] positions = new int[rank];
    6866      while (values.MoveNext()) {
     
    7876        }
    7977      }
    80       return a;
     78      object o = a;
     79      return (T)o;
    8180    }
    8281  }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberEnumeration2XmlFormatterBase.cs

    r1542 r1564  
    66namespace HeuristicLab.Persistence.Default.Xml.Compact {
    77 
    8   public abstract class NumberEnumeration2XmlFormatterBase : IFormatter {
     8  public abstract class NumberEnumeration2XmlFormatterBase<T> : FormatterBase<T, XmlString> where T : IEnumerable {
    99
    10     public abstract Type Type { get; }
    11     public IFormat Format { get { return XmlFormat.Instance; } }
    1210    protected virtual string Separator { get { return ";"; } }
    1311    protected abstract void Add(IEnumerable enumeration, object o);
    14     protected abstract object Instantiate();
     12    protected abstract IEnumerable Instantiate();
    1513    protected abstract string FormatValue(object o);
    1614    protected abstract object ParseValue(string o);
    1715
    18     public object DoFormat(object o) {
     16    public override XmlString Format(T t) {
    1917      StringBuilder sb = new StringBuilder();
    20       foreach (var value in (IEnumerable)o) {
     18      foreach (var value in (IEnumerable)t) {
    2119        sb.Append(FormatValue(value));
    2220        sb.Append(Separator);
    2321      }
    24       return sb.ToString();
     22      return new XmlString(sb.ToString());
    2523    }
    2624
    27     public object Parse(object o) {
    28       IEnumerable enumeration = (IEnumerable)Instantiate();
    29       string[] values = ((string)o).Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
     25    public override T Parse(XmlString x) {
     26      IEnumerable enumeration = Instantiate();
     27      string[] values = x.Data.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
    3028      foreach (var value in values) {
    3129        Add(enumeration, ParseValue(value));
    3230      }
    33       return enumeration;
     31      return (T)enumeration;
    3432    }
    3533  }
Note: See TracChangeset for help on using the changeset viewer.