Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/ValueTypeValueTransformer.cs @ 17266

Last change on this file since 17266 was 17266, checked in by dpiringe, 5 years ago

#3026:

  • changed the namespace from ParameterTest to HeuristicLab.Manufacture
  • added an extension method for Type to get the distance of an interface based on the target type
  • renamed methods ToData, SetValue to Extract, Inject
  • new implementation of the template generation with transformers (not the final name)
  • started to use the new transformers for the instantiation of IOptimizer-objects (out of a template)
  • new transformers: LookupParameterTransformer and DummyTransformer
File size: 1.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8
9namespace HeuristicLab.Manufacture {
10  public class ValueTypeValueTransformer<ValueType, T> : BaseTransformer
11    where ValueType : ValueTypeValue<T>
12    where T : struct {
13
14    public override void InjectData(IItem item, ParameterData data) =>
15      item.Cast<ValueType>().Value = CastValue<T>(data.Default);
16
17    public override ParameterData ExtractData(IItem value) =>
18      new ParameterData() {
19        Default = value.Cast<ValueType>().Value,
20        Range = new object[] { default(T), GetMaxValue() }
21      };
22
23    #region Helper
24    private object GetMaxValue() {
25      TypeCode typeCode = Type.GetTypeCode(typeof(T));
26
27      if (typeof(ValueType).IsEqualTo(typeof(PercentValue)))
28        return 1.0d;
29
30      switch (typeCode) {
31        case TypeCode.Int16: return Int16.MaxValue;
32        case TypeCode.Int32: return Int32.MaxValue;
33        case TypeCode.Int64: return Int64.MaxValue;
34        case TypeCode.UInt16: return UInt16.MaxValue;
35        case TypeCode.UInt32: return UInt32.MaxValue;
36        case TypeCode.UInt64: return UInt64.MaxValue;
37        case TypeCode.Single: return Single.MaxValue;
38        case TypeCode.Double: return Double.MaxValue;
39        case TypeCode.Decimal: return Decimal.MaxValue;
40        case TypeCode.Byte: return Byte.MaxValue;
41        case TypeCode.Boolean: return true;
42        default: return default(T);
43      }
44    }
45    #endregion
46  }
47}
Note: See TracBrowser for help on using the repository browser.