Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/ListTransformer.cs @ 13326

Last change on this file since 13326 was 13326, checked in by swagner, 7 years ago

#2520: Created plugin for new persistence implementation

File size: 5.0 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Persistence.Data;
25
26namespace HeuristicLab.Persistence {
27  internal abstract class GenericListTransformer<TSource, TTarget> : TransformerBase {
28    public override bool CanTransformType(Type type) {
29      return type == typeof(List<TSource>);
30    }
31    public override PersistenceData ToData(object o, PersistenceMapper mapper) {
32      var data = new ArrayData<TTarget>(Id);
33      mapper.Cache(o, data);
34      data.TypeId = mapper.GetTypeId(o.GetType());
35
36      var enumerable = (IEnumerable<TSource>)o;
37      var componentIds = new List<TTarget>();
38      foreach (var component in enumerable) {
39        componentIds.Add(GetTarget(component, mapper));
40      }
41      data.Values = componentIds.ToArray();
42      return data;
43    }
44
45    public override object ToObject(PersistenceData data, PersistenceMapper mapper) {
46      var collection = (ArrayData<TTarget>)data;
47      var type = mapper.GetType(collection.TypeId);
48
49      var o = Activator.CreateInstance(type, true);
50      mapper.Cache(data, o);
51      dynamic list = o;
52      foreach (var component in collection.Values) {
53        list.Add(GetSource(component, mapper));
54      }
55      return o;
56    }
57
58    protected abstract TTarget GetTarget(TSource source, PersistenceMapper mapper);
59    protected abstract TSource GetSource(TTarget target, PersistenceMapper mapper);
60  }
61
62  internal abstract class PrimitiveListTransformer<T> : GenericListTransformer<T, T> {
63    protected override T GetTarget(T source, PersistenceMapper mapper) {
64      return source;
65    }
66    protected override T GetSource(T target, PersistenceMapper mapper) {
67      return target;
68    }
69  }
70
71  [Transformer("C34E96D4-8890-4AA6-BF40-D8A439ABBF2E", 401)]
72  internal sealed class DoubleListTransformer : PrimitiveListTransformer<double> { }
73
74  [Transformer("B551DCEF-64FA-4AA9-87EE-0C49F44B6903", 402)]
75  internal sealed class StringListTransformer : PrimitiveListTransformer<string> { }
76
77  [Transformer("16A662E1-BB7A-4A0B-B646-6B8B6C8B89F9", 407)]
78  internal sealed class ListTransformer : GenericListTransformer<object, uint> {
79    public override bool CanTransformType(Type type) {
80      return type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(List<>));
81    }
82
83    protected override uint GetTarget(object source, PersistenceMapper mapper) {
84      return mapper.GetDataId(source);
85    }
86    protected override object GetSource(uint target, PersistenceMapper mapper) {
87      return mapper.GetObject(target);
88    }
89  }
90
91  [Transformer("DEA9A14A-034C-46B6-BD96-676B90C21159", 408)]
92  internal sealed class HashSetTransformer : GenericListTransformer<object, uint> {
93    public override bool CanTransformType(Type type) {
94      return type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(HashSet<>));
95    }
96
97    protected override uint GetTarget(object source, PersistenceMapper mapper) {
98      return mapper.GetDataId(source);
99    }
100    protected override object GetSource(uint target, PersistenceMapper mapper) {
101      return mapper.GetObject(target);
102    }
103  }
104}
105
106
107//using System;
108//using System.Collections.Generic;
109//using HeuristicLab.Persistence.Data;
110
111//namespace HeuristicLab.Persistence {
112//  internal abstract class ListTransformer<T> : TransformerBase {
113//    public override bool CanTransformType(Type type) {
114//      return type == typeof(List<T>);
115//    }
116//    public override PersistenceData ToData(object o, PersistenceMapper mapper) {
117//      var list = (List<T>)o;
118//      var data = new ArrayData<T>(Id);
119//      mapper.Cache(o, data);
120//      data.Values = list.ToArray();
121//      return data;
122//    }
123
124//    public override object ToObject(PersistenceData data, PersistenceMapper mapper) {
125//      var array = (ArrayData<T>)data;
126//      var o = new List<T>();
127//      mapper.Cache(data, o);
128//      o.AddRange(array.Values);
129//      return o;
130//    }
131//  }
132
133//  [Transformer("C34E96D4-8890-4AA6-BF40-D8A439ABBF2E", 401)]
134//  internal sealed class DoubleListTransformer : ListTransformer<double> { }
135
136//  [Transformer("B551DCEF-64FA-4AA9-87EE-0C49F44B6903", 402)]
137//  internal sealed class StringListTransformer : ListTransformer<string> { }
138//}
Note: See TracBrowser for help on using the repository browser.