#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 GenericStackTransformer : TransformerBase { public override bool CanTransformType(Type type) { return type == typeof(Stack); } public override PersistenceData ToData(object o, PersistenceMapper mapper) { var data = new ArrayData(Id); mapper.Cache(o, data); data.TypeId = mapper.GetTypeId(o.GetType()); var enumerable = (IEnumerable)o; var componentIds = new List(); foreach (var component in enumerable) { componentIds.Add(GetTarget(component, mapper)); } componentIds.Reverse(); data.Values = componentIds.ToArray(); return data; } public override object ToObject(PersistenceData data, PersistenceMapper mapper) { var collection = (ArrayData)data; var type = mapper.GetType(collection.TypeId); var o = Activator.CreateInstance(type, true); mapper.Cache(data, o); dynamic stack = o; foreach (var component in collection.Values) { stack.Push(GetSource(component, mapper)); } return o; } protected abstract TTarget GetTarget(TSource source, PersistenceMapper mapper); protected abstract TSource GetSource(TTarget target, PersistenceMapper mapper); } internal abstract class PrimitiveStackTransformer : GenericStackTransformer { protected override T GetTarget(T source, PersistenceMapper mapper) { return source; } protected override T GetSource(T target, PersistenceMapper mapper) { return target; } } [Transformer("305FE51F-CE6A-43A7-A0FC-CCFFC1378680", 405)] internal sealed class DoubleStackTransformer : PrimitiveStackTransformer { } [Transformer("E84A21DF-BDAF-4A24-9EF6-934ACDB82423", 406)] internal sealed class StackTransformer : GenericStackTransformer { public override bool CanTransformType(Type type) { return type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Stack<>)); } protected override uint GetTarget(object source, PersistenceMapper mapper) { return mapper.GetDataId(source); } protected override object GetSource(uint target, PersistenceMapper mapper) { return mapper.GetObject(target); } } }