source: branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/ValueTransformer.cs @ 13326

Last change on this file since 13326 was 13326, checked in by swagner, 4 years ago

#2520: Created plugin for new persistence implementation

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Drawing;
24using HeuristicLab.Persistence.Data;
25
26namespace HeuristicLab.Persistence {
27  internal abstract class ValueTransformer<TSource, TTarget> : TransformerBase {
28    public override bool CanTransformType(Type type) {
29      return typeof(TSource) == type;
30    }
31    public override PersistenceData ToData(object o, PersistenceMapper mapper) {
32      return GetTarget((TSource)o, mapper);
33    }
34
35    public override object ToObject(PersistenceData data, PersistenceMapper mapper) {
36      return GetSource((ValueData<TTarget>)data, mapper);
37    }
38
39    protected abstract ValueData<TTarget> GetTarget(TSource source, PersistenceMapper mapper);
40    protected abstract TSource GetSource(ValueData<TTarget> target, PersistenceMapper mapper);
41  }
42
43  internal abstract class ValueTypeTransformer<T> : ValueTransformer<T, T> {
44    protected override ValueData<T> GetTarget(T source, PersistenceMapper mapper) {
45      var data = new ValueData<T>(Id);
46      data.TypeId = mapper.GetTypeId(source.GetType());
47      data.Value = source;
48      return data;
49    }
50    protected override T GetSource(ValueData<T> target, PersistenceMapper mapper) {
51      return target.Value;
52    }
53  }
54
55  #region Primitive Value Types
56  [Transformer("9FA1C6A8-517E-4623-AC1B-7E9AEF6ED13D", 200)]
57  internal sealed class BoolTransformer : ValueTypeTransformer<bool> { }
58
59  [Transformer("059633E9-12CB-43EC-8544-57746536E281", 201)]
60  internal sealed class ByteTransformer : ValueTypeTransformer<byte> { }
61
62  [Transformer("5DC2F3AC-0852-4B57-A861-D29CC814A94C", 202)]
63  internal sealed class SByteTransformer : ValueTypeTransformer<sbyte> { }
64
65  [Transformer("B862E642-A94A-4870-8065-06126A35A9E1", 203)]
66  internal sealed class ShortTransformer : ValueTypeTransformer<short> { }
67
68  [Transformer("D1D3062D-F1BB-4189-AE50-D6986E1DEB4E", 204)]
69  internal sealed class UShortTransformer : ValueTypeTransformer<ushort> { }
70
71  [Transformer("6C444645-3062-4D15-AD01-E6E1B0692A2B", 205)]
72  internal sealed class CharTransformer : ValueTypeTransformer<char> { }
73
74  [Transformer("649E73B2-EFA6-4E01-BCB4-4B29D652C9CB", 206)]
75  internal sealed class IntTransformer : ValueTypeTransformer<int> { }
76
77  [Transformer("BCF25010-81A2-49BC-88CC-407D3F393D5B", 207)]
78  internal sealed class UIntTransformer : ValueTypeTransformer<uint> { }
79
80  [Transformer("B6F6ACAE-755C-47EE-B8BF-7CDECBE19C30", 208)]
81  internal sealed class LongTransformer : ValueTypeTransformer<long> { }
82
83  [Transformer("82333ACA-F041-44E0-B365-27C399594BA7", 209)]
84  internal sealed class ULongTransformer : ValueTypeTransformer<ulong> { }
85
86  [Transformer("8FE91ECF-2261-4934-BECD-C38923B7A703", 210)]
87  internal sealed class FloatTransformer : ValueTypeTransformer<float> { }
88
89  [Transformer("070D23EA-7F38-46B7-A667-219BEF914E49", 211)]
90  internal sealed class DoubleTransformer : ValueTypeTransformer<double> { }
91
92  [Transformer("80368686-0531-4D52-B550-E772B8842E6D", 212)]
93  internal sealed class DecimalTransformer : ValueTypeTransformer<decimal> { }
94
95  [Transformer("BA55C7A6-C91E-4351-A889-E4A7E647F16D", 213)]
96  internal sealed class DateTimeTransformer : ValueTypeTransformer<DateTime> { }
97
98  [Transformer("0C91441B-2D97-432B-B493-D6EC483FC5AD", 214)]
99  internal sealed class TimeSpanTransformer : ValueTypeTransformer<TimeSpan> { }
100  #endregion
101
102  #region String
103  [Transformer("E75A594C-0034-4DAB-B28E-8F84F9F6DE8D", 215)]
104  internal sealed class StringTransformer : ValueTransformer<string, uint> {
105    protected override ValueData<uint> GetTarget(string source, PersistenceMapper mapper) {
106      var data = new ValueData<uint>(Id);
107      mapper.Cache(source, data);
108      data.TypeId = mapper.GetTypeId(source.GetType());
109      data.Value = mapper.GetStringId(source);
110      return data;
111    }
112    protected override string GetSource(ValueData<uint> target, PersistenceMapper mapper) {
113      var source = mapper.GetString(target.Value);
114      mapper.Cache(target, source);
115      return source;
116    }
117  }
118  #endregion
119
120  #region Enum
121  [Transformer("93FF076B-BC4B-4C39-8C40-15E004468C98", 216)]
122  internal sealed class EnumTransformer : ValueTransformer<object, uint> {
123    public override bool CanTransformType(Type type) {
124      return typeof(Enum).IsAssignableFrom(type);
125    }
126    protected override ValueData<uint> GetTarget(object source, PersistenceMapper mapper) {
127      var type = source.GetType();
128      var data = new ValueData<uint>(Id);
129      data.TypeId = mapper.GetTypeId(type);
130      data.Value = mapper.GetStringId(Enum.Format(type, source, "G"));
131      return data;
132    }
133    protected override object GetSource(ValueData<uint> target, PersistenceMapper mapper) {
134      return Enum.Parse(mapper.GetType(target.TypeId), mapper.GetString(target.Value));
135    }
136  }
137  #endregion
138
139  #region Color
140  [Transformer("0B540EAC-79AB-40CF-8277-D2C81135FEB6", 217)]
141  internal sealed class ColorTransformer : ValueTransformer<Color, int> {
142    protected override ValueData<int> GetTarget(Color source, PersistenceMapper mapper) {
143      var data = new ValueData<int>(Id);
144      data.TypeId = mapper.GetTypeId(source.GetType());
145      data.Value = source.ToArgb();
146      return data;
147    }
148    protected override Color GetSource(ValueData<int> target, PersistenceMapper mapper) {
149      return Color.FromArgb(target.Value);
150    }
151  }
152  #endregion
153
154  #region Type
155  [Transformer("8D17FD28-383B-44E9-9BBF-B19D351C5E38", 218)]
156  internal sealed class TypeTransformer : ValueTransformer<Type, ushort> {
157    public override bool CanTransformType(Type type) {
158      return typeof(Type).IsAssignableFrom(type);
159    }
160    protected override ValueData<ushort> GetTarget(Type source, PersistenceMapper mapper) {
161      var data = new ValueData<ushort>(Id);
162      mapper.Cache(source, data);
163      data.Value = mapper.GetTypeId(source);
164      return data;
165    }
166    protected override Type GetSource(ValueData<ushort> target, PersistenceMapper mapper) {
167      var type = mapper.GetType(target.Value);
168      mapper.Cache(target, type);
169      return type;
170    }
171  }
172  #endregion
173}
Note: See TracBrowser for help on using the repository browser.