[13326] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
| 24 | using System.Collections.Generic;
|
---|
| 25 | using HeuristicLab.Persistence.Data;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Persistence {
|
---|
| 28 | internal abstract class GenericStackTransformer<TSource, TTarget> : TransformerBase {
|
---|
| 29 | public override bool CanTransformType(Type type) {
|
---|
| 30 | return type == typeof(Stack<TSource>);
|
---|
| 31 | }
|
---|
| 32 | public override PersistenceData ToData(object o, PersistenceMapper mapper) {
|
---|
| 33 | var data = new ArrayData<TTarget>(Id);
|
---|
| 34 | mapper.Cache(o, data);
|
---|
| 35 | data.TypeId = mapper.GetTypeId(o.GetType());
|
---|
| 36 |
|
---|
| 37 | var enumerable = (IEnumerable<TSource>)o;
|
---|
| 38 | var componentIds = new List<TTarget>();
|
---|
| 39 | foreach (var component in enumerable) {
|
---|
| 40 | componentIds.Add(GetTarget(component, mapper));
|
---|
| 41 | }
|
---|
| 42 | componentIds.Reverse();
|
---|
| 43 | data.Values = componentIds.ToArray();
|
---|
| 44 | return data;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public override object ToObject(PersistenceData data, PersistenceMapper mapper) {
|
---|
| 48 | var collection = (ArrayData<TTarget>)data;
|
---|
| 49 | var type = mapper.GetType(collection.TypeId);
|
---|
| 50 |
|
---|
| 51 | var o = Activator.CreateInstance(type, true);
|
---|
| 52 | mapper.Cache(data, o);
|
---|
| 53 | dynamic stack = o;
|
---|
| 54 | foreach (var component in collection.Values) {
|
---|
| 55 | stack.Push(GetSource(component, mapper));
|
---|
| 56 | }
|
---|
| 57 | return o;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | protected abstract TTarget GetTarget(TSource source, PersistenceMapper mapper);
|
---|
| 61 | protected abstract TSource GetSource(TTarget target, PersistenceMapper mapper);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | internal abstract class PrimitiveStackTransformer<T> : GenericStackTransformer<T, T> {
|
---|
| 65 | protected override T GetTarget(T source, PersistenceMapper mapper) {
|
---|
| 66 | return source;
|
---|
| 67 | }
|
---|
| 68 | protected override T GetSource(T target, PersistenceMapper mapper) {
|
---|
| 69 | return target;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | [Transformer("305FE51F-CE6A-43A7-A0FC-CCFFC1378680", 405)]
|
---|
| 74 | internal sealed class DoubleStackTransformer : PrimitiveStackTransformer<double> { }
|
---|
| 75 |
|
---|
| 76 | [Transformer("E84A21DF-BDAF-4A24-9EF6-934ACDB82423", 406)]
|
---|
| 77 | internal sealed class StackTransformer : GenericStackTransformer<object, uint> {
|
---|
| 78 | public override bool CanTransformType(Type type) {
|
---|
| 79 | return type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Stack<>));
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | protected override uint GetTarget(object source, PersistenceMapper mapper) {
|
---|
| 83 | return mapper.GetDataId(source);
|
---|
| 84 | }
|
---|
| 85 | protected override object GetSource(uint target, PersistenceMapper mapper) {
|
---|
| 86 | return mapper.GetObject(target);
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|