Changeset 13367 for branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs
- Timestamp:
- 11/24/15 16:59:57 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs
r13358 r13367 20 20 #endregion 21 21 22 using Google.ProtocolBuffers; 22 23 using System; 24 using System.Collections.Generic; 23 25 using System.Drawing; 26 using System.Linq; 24 27 25 28 namespace HeuristicLab.Persistence { … … 43 46 } 44 47 45 #region Scalar Value Boxes48 #region Scalar Box Transformers 46 49 public abstract class BoolBoxTransformer<T> : BoxTransformer<T> { 47 50 protected override void Populate(Box.Builder box, T value, Mapper mapper) { … … 68 71 protected abstract T ToValueType(int value, Type type, Mapper mapper); 69 72 } 73 public abstract class LongBoxTransformer<T> : BoxTransformer<T> { 74 protected override void Populate(Box.Builder box, T value, Mapper mapper) { 75 var b = LongBox.CreateBuilder(); 76 b.Value = ToBoxType(value, mapper); 77 box.SetExtension<LongBox>(LongBox.Long, b.Build()); 78 } 79 protected override T Extract(Box box, Type type, Mapper mapper) { 80 return ToValueType(box.GetExtension(LongBox.Long).Value, type, mapper); 81 } 82 protected abstract long ToBoxType(T value, Mapper mapper); 83 protected abstract T ToValueType(long value, Type type, Mapper mapper); 84 } 70 85 public abstract class UnsignedIntBoxTransformer<T> : BoxTransformer<T> { 71 86 protected override void Populate(Box.Builder box, T value, Mapper mapper) { … … 80 95 protected abstract T ToValueType(uint value, Type type, Mapper mapper); 81 96 } 82 public abstract class LongBoxTransformer<T> : BoxTransformer<T> {83 protected override void Populate(Box.Builder box, T value, Mapper mapper) {84 var b = LongBox.CreateBuilder();85 b.Value = ToBoxType(value, mapper);86 box.SetExtension<LongBox>(LongBox.Long, b.Build());87 }88 protected override T Extract(Box box, Type type, Mapper mapper) {89 return ToValueType(box.GetExtension(LongBox.Long).Value, type, mapper);90 }91 protected abstract long ToBoxType(T value, Mapper mapper);92 protected abstract T ToValueType(long value, Type type, Mapper mapper);93 }94 97 public abstract class UnsignedLongBoxTransformer<T> : BoxTransformer<T> { 95 98 protected override void Populate(Box.Builder box, T value, Mapper mapper) { … … 139 142 protected abstract string ToBoxType(T value, Mapper mapper); 140 143 protected abstract T ToValueType(string value, Type type, Mapper mapper); 144 } 145 public abstract class BytesBoxTransformer<T> : BoxTransformer<T> { 146 protected override void Populate(Box.Builder box, T value, Mapper mapper) { 147 var b = BytesBox.CreateBuilder(); 148 b.Value = ByteString.CopyFrom(ToBoxType(value, mapper)); 149 box.SetExtension<BytesBox>(BytesBox.Bytes, b.Build()); 150 } 151 protected override T Extract(Box box, Type type, Mapper mapper) { 152 return ToValueType(box.GetExtension(BytesBox.Bytes).Value.ToByteArray(), type, mapper); 153 } 154 protected abstract byte[] ToBoxType(T value, Mapper mapper); 155 protected abstract T ToValueType(byte[] value, Type type, Mapper mapper); 156 } 157 #endregion 158 159 #region Array Box Transformers 160 public abstract class BoolArrayBoxTransformer<T> : BoxTransformer<T> { 161 protected override void Populate(Box.Builder box, T value, Mapper mapper) { 162 var b = BoolArrayBox.CreateBuilder(); 163 b.AddRangeValues(ToBoxType(value, mapper)); 164 box.SetExtension<BoolArrayBox>(BoolArrayBox.BoolArray, b.Build()); 165 } 166 protected override T Extract(Box box, Type type, Mapper mapper) { 167 return ToValueType(box.GetExtension(BoolArrayBox.BoolArray).ValuesList, type, mapper); 168 } 169 protected abstract IEnumerable<bool> ToBoxType(T value, Mapper mapper); 170 protected abstract T ToValueType(IEnumerable<bool> value, Type type, Mapper mapper); 171 } 172 public abstract class IntArrayBoxTransformer<T> : BoxTransformer<T> { 173 protected override void Populate(Box.Builder box, T value, Mapper mapper) { 174 var b = IntArrayBox.CreateBuilder(); 175 b.AddRangeValues(ToBoxType(value, mapper)); 176 box.SetExtension<IntArrayBox>(IntArrayBox.IntArray, b.Build()); 177 } 178 protected override T Extract(Box box, Type type, Mapper mapper) { 179 return ToValueType(box.GetExtension(IntArrayBox.IntArray).ValuesList, type, mapper); 180 } 181 protected abstract IEnumerable<int> ToBoxType(T value, Mapper mapper); 182 protected abstract T ToValueType(IEnumerable<int> value, Type type, Mapper mapper); 141 183 } 142 184 #endregion … … 226 268 protected override TimeSpan ToValueType(long value, Type type, Mapper mapper) { return new TimeSpan(value); } 227 269 } 270 [Transformer("0B540EAC-79AB-40CF-8277-D2C81135FEB6", 217)] 271 internal sealed class ColorTransformers : IntBoxTransformer<Color> { 272 protected override int ToBoxType(Color value, Mapper mapper) { return value.ToArgb(); } 273 protected override Color ToValueType(int value, Type type, Mapper mapper) { return Color.FromArgb(value); } 274 } 228 275 #endregion 229 276 … … 251 298 #endregion 252 299 253 #region Color254 [Transformer("0B540EAC-79AB-40CF-8277-D2C81135FEB6", 217)]255 internal sealed class ColorTransformers : IntBoxTransformer<Color> {256 protected override int ToBoxType(Color value, Mapper mapper) { return value.ToArgb(); }257 protected override Color ToValueType(int value, Type type, Mapper mapper) { return Color.FromArgb(value); }258 }259 #endregion260 261 300 #region Type 262 301 [Transformer("8D17FD28-383B-44E9-9BBF-B19D351C5E38", 218)] … … 269 308 } 270 309 #endregion 310 311 #region Primitive Array Types 312 [Transformer("F25A73B2-6B67-4493-BD59-B836AF4455D1", 300)] 313 internal sealed class BoolArrayTransformer : BoolArrayBoxTransformer<bool[]> { 314 protected override IEnumerable<bool> ToBoxType(bool[] value, Mapper mapper) { return value; } 315 protected override bool[] ToValueType(IEnumerable<bool> value, Type type, Mapper mapper) { return value.ToArray(); } 316 } 317 [Transformer("5F6DC3BC-4433-4AE9-A636-4BD126F7DACD", 306)] 318 internal sealed class IntArrayTransformer : IntArrayBoxTransformer<int[]> { 319 protected override IEnumerable<int> ToBoxType(int[] value, Mapper mapper) { return value; } 320 protected override int[] ToValueType(IEnumerable<int> value, Type type, Mapper mapper) { return value.ToArray(); } 321 } 322 #endregion 271 323 }
Note: See TracChangeset
for help on using the changeset viewer.