Changeset 13407
- Timestamp:
- 11/26/15 17:07:28 (9 years ago)
- Location:
- branches/PersistenceOverhaul
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/StaticCache.cs
r13387 r13407 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Drawing; 24 25 using System.Linq; 25 using HeuristicLab.PluginInfrastructure;26 26 using Google.ProtocolBuffers; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using System.Drawing;28 using HeuristicLab.PluginInfrastructure; 29 29 30 30 namespace HeuristicLab.Persistence { … … 68 68 RegisterType(new Guid("E84C326A-7E14-4F28-AEFF-BC16CC671655"), typeof(KeyValuePair<,>)); 69 69 RegisterType(new Guid("F0280B55-25E8-4981-B309-D675D081402A"), typeof(string)); 70 // Enum? 71 RegisterType(new Guid("859B7E34-3A07-41A3-AE93-79DCE5A1DB2C"), typeof(Type)); 70 72 71 RegisterType(new Guid("D15AD28B-203A-460E-815C-F7230C4B1F75"), typeof(bool[])); 73 72 RegisterType(new Guid("EE318DC4-580D-4DB1-9AAD-988B0E50A3DB"), typeof(byte[])); -
branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs
r13387 r13407 20 20 #endregion 21 21 22 using Google.ProtocolBuffers;23 22 using System; 24 23 using System.Collections.Generic; 25 24 using System.Drawing; 26 25 using System.Linq; 26 using Google.ProtocolBuffers; 27 27 28 28 namespace HeuristicLab.Persistence { … … 383 383 #region Enum 384 384 [Transformer("93FF076B-BC4B-4C39-8C40-15E004468C98", 218)] 385 internal sealed class EnumTransformer : UnsignedIntBoxTransformer<object>{385 internal sealed class EnumTransformer : Transformer { 386 386 public override bool CanTransformType(Type type) { 387 387 return typeof(Enum).IsAssignableFrom(type); 388 388 } 389 protected override uint ToBoxType(object value, Mapper mapper) { 390 return mapper.GetStringId(Enum.Format(value.GetType(), value, "G")); 391 } 392 protected override object ToValueType(uint value, Type type, Mapper mapper) { 393 return Enum.Parse(type, mapper.GetString(value)); 389 390 public override Box ToBox(object o, Mapper mapper) { 391 var boxBuilder = Box.CreateBuilder(); 392 var enumBuilder = new UnsignedIntBox.Builder(); 393 394 boxBuilder.TransformerId = mapper.GetTransformerId(this); 395 boxBuilder.TypeId = mapper.GetStringId(o.GetType().AssemblyQualifiedName); 396 enumBuilder.Value = mapper.GetStringId(Enum.Format(o.GetType(), o, "G")); 397 398 boxBuilder.SetExtension(UnsignedIntBox.UnsignedInt, enumBuilder.Build()); 399 return boxBuilder.Build(); 400 } 401 402 public override object ToObject(Box box, Mapper mapper) { 403 uint value = box.GetExtension(UnsignedIntBox.UnsignedInt).Value; 404 return Enum.Parse(Type.GetType(mapper.GetString(box.TypeId)), mapper.GetString(value)); 394 405 } 395 406 } … … 398 409 #region Type 399 410 [Transformer("8D17FD28-383B-44E9-9BBF-B19D351C5E38", 219)] 400 internal sealed class TypeTransformer : UnsignedIntBoxTransformer<Type>{411 internal sealed class TypeTransformer : Transformer { 401 412 public override bool CanTransformType(Type type) { 402 413 return typeof(Type).IsAssignableFrom(type); 403 414 } 404 protected override uint ToBoxType(Type value, Mapper mapper) { return mapper.GetTypeId(value); } 405 protected override Type ToValueType(uint value, Type type, Mapper mapper) { return mapper.GetType(value); } 415 public override Box ToBox(object o, Mapper mapper) { 416 var boxBuilder = Box.CreateBuilder(); 417 418 boxBuilder.TransformerId = mapper.GetTransformerId(this); 419 boxBuilder.TypeId = mapper.GetStringId(((Type)o).AssemblyQualifiedName); 420 421 return boxBuilder.Build(); 422 } 423 424 public override object ToObject(Box box, Mapper mapper) { 425 return Type.GetType(mapper.GetString(box.TypeId)); 426 } 406 427 } 407 428 #endregion -
branches/PersistenceOverhaul/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCasesPersistenceNew.cs
r13386 r13407 154 154 } 155 155 156 public enum TestEnum { va1, va2, va3, va8 } 156 public enum TestEnum { va1, va2, va3, va8 }; 157 157 158 158 [StorableClass("26BA37F6-926D-4665-A10A-1F39E1CF6468")] … … 314 314 Assert.AreEqual(test, result); 315 315 } 316 317 [TestMethod] 318 [TestCategory("Persistence")] 319 [TestProperty("Time", "short")] 320 public void TestEnumSimple() { 321 SimpleEnum se = SimpleEnum.two; 322 ComplexEnum ce = ComplexEnum.three; 323 ProtoBufSerializer serializer = new ProtoBufSerializer(); 324 serializer.Serialize(se, tempFile); 325 object o = serializer.Deserialize(tempFile); 326 SimpleEnum result = (SimpleEnum)o; 327 Assert.AreEqual(se, result); 328 } 329 330 [TestMethod] 331 [TestCategory("Persistence")] 332 [TestProperty("Time", "short")] 333 public void TestEnumComplex() { 334 ComplexEnum ce = ComplexEnum.three; 335 ProtoBufSerializer serializer = new ProtoBufSerializer(); 336 serializer.Serialize(ce, tempFile); 337 object o = serializer.Deserialize(tempFile); 338 ComplexEnum result = (ComplexEnum)o; 339 Assert.AreEqual(ce, result); 340 } 341 342 [TestMethod] 343 [TestCategory("Persistence")] 344 [TestProperty("Time", "short")] 345 public void TestType() { 346 Type test = typeof(HeuristicLab.Algorithms.GeneticAlgorithm.GeneticAlgorithm); 347 ProtoBufSerializer serializer = new ProtoBufSerializer(); 348 serializer.Serialize(test, tempFile); 349 object o = serializer.Deserialize(tempFile); 350 Type result = (Type)o; 351 Assert.AreEqual(test, result); 352 } 353 316 354 #endregion 317 355
Note: See TracChangeset
for help on using the changeset viewer.