#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections; using System.Collections.Generic; using HeuristicLab.Persistence.Data; namespace HeuristicLab.Persistence { internal abstract class GenericDictionaryTransformer : TransformerBase { public override bool CanTransformType(Type type) { return type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Dictionary<,>)); } public override PersistenceData ToData(object o, PersistenceMapper mapper) { var type = o.GetType(); var data = new DictionaryData(Id); mapper.Cache(o, data); data.TypeId = mapper.GetTypeId(type); var dict = (IDictionary)o; data.KeyIds = new uint[dict.Count]; data.ValueIds = new uint[dict.Count]; int i = 0; foreach (DictionaryEntry element in dict) { data.KeyIds[i] = GetTargetKey(element.Key, mapper); data.ValueIds[i] = GetTargetValue(element.Value, mapper); i++; } data.ComparerId = mapper.GetDataId(type.GetProperty("Comparer").GetValue(o, null)); return data; } public override object ToObject(PersistenceData data, PersistenceMapper mapper) { var composite = (DictionaryData)data; var type = mapper.GetType(composite.TypeId); var o = (IDictionary)Activator.CreateInstance(type, mapper.GetObject(composite.ComparerId)); mapper.Cache(data, o); for (int i = 0; i < composite.KeyIds.Length; i++) { o.Add(GetSourceKey(composite.KeyIds[i], mapper), GetSourceValue(composite.ValueIds[i], mapper)); } return o; } protected abstract uint GetTargetKey(object sourceKey, PersistenceMapper mapper); protected abstract uint GetTargetValue(object sourceValue, PersistenceMapper mapper); protected abstract object GetSourceKey(uint targetKey, PersistenceMapper mapper); protected abstract object GetSourceValue(uint targetValue, PersistenceMapper mapper); } [Transformer("AC7F11D4-3390-4DE2-B6FC-1705DAB627F6", 403)] internal sealed class StringDictionaryTransformer : GenericDictionaryTransformer { public override bool CanTransformType(Type type) { return base.CanTransformType(type) && (type.GetGenericArguments()[0] == typeof(string)); } protected override uint GetTargetKey(object sourceKey, PersistenceMapper mapper) { return mapper.GetStringId((string)sourceKey); } protected override uint GetTargetValue(object sourceValue, PersistenceMapper mapper) { return mapper.GetDataId(sourceValue); } protected override object GetSourceKey(uint targetKey, PersistenceMapper mapper) { return mapper.GetString(targetKey); } protected override object GetSourceValue(uint targetValue, PersistenceMapper mapper) { return mapper.GetObject(targetValue); } } [Transformer("D1EF55D6-843A-42AF-8EBB-49566981C943", 404)] internal sealed class DictionaryTransformer : GenericDictionaryTransformer { protected override uint GetTargetKey(object sourceKey, PersistenceMapper mapper) { return mapper.GetDataId(sourceKey); } protected override uint GetTargetValue(object sourceValue, PersistenceMapper mapper) { return mapper.GetDataId(sourceValue); } protected override object GetSourceKey(uint targetKey, PersistenceMapper mapper) { return mapper.GetObject(targetKey); } protected override object GetSourceValue(uint targetValue, PersistenceMapper mapper) { return mapper.GetObject(targetValue); } } }