Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/DictionaryTransformer.cs @ 16612

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

#2520: Created plugin for new persistence implementation

File size: 4.3 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;
24using System.Collections.Generic;
25using HeuristicLab.Persistence.Data;
26
27namespace HeuristicLab.Persistence {
28  internal abstract class GenericDictionaryTransformer : TransformerBase {
29    public override bool CanTransformType(Type type) {
30      return type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Dictionary<,>));
31    }
32    public override PersistenceData ToData(object o, PersistenceMapper mapper) {
33      var type = o.GetType();
34      var data = new DictionaryData(Id);
35      mapper.Cache(o, data);
36      data.TypeId = mapper.GetTypeId(type);
37
38      var dict = (IDictionary)o;
39      data.KeyIds = new uint[dict.Count];
40      data.ValueIds = new uint[dict.Count];
41      int i = 0;
42      foreach (DictionaryEntry element in dict) {
43        data.KeyIds[i] = GetTargetKey(element.Key, mapper);
44        data.ValueIds[i] = GetTargetValue(element.Value, mapper);
45        i++;
46      }
47      data.ComparerId = mapper.GetDataId(type.GetProperty("Comparer").GetValue(o, null));
48      return data;
49    }
50
51    public override object ToObject(PersistenceData data, PersistenceMapper mapper) {
52      var composite = (DictionaryData)data;
53      var type = mapper.GetType(composite.TypeId);
54      var o = (IDictionary)Activator.CreateInstance(type, mapper.GetObject(composite.ComparerId));
55      mapper.Cache(data, o);
56      for (int i = 0; i < composite.KeyIds.Length; i++) {
57        o.Add(GetSourceKey(composite.KeyIds[i], mapper), GetSourceValue(composite.ValueIds[i], mapper));
58      }
59      return o;
60    }
61
62    protected abstract uint GetTargetKey(object sourceKey, PersistenceMapper mapper);
63    protected abstract uint GetTargetValue(object sourceValue, PersistenceMapper mapper);
64    protected abstract object GetSourceKey(uint targetKey, PersistenceMapper mapper);
65    protected abstract object GetSourceValue(uint targetValue, PersistenceMapper mapper);
66  }
67
68  [Transformer("AC7F11D4-3390-4DE2-B6FC-1705DAB627F6", 403)]
69  internal sealed class StringDictionaryTransformer : GenericDictionaryTransformer {
70    public override bool CanTransformType(Type type) {
71      return base.CanTransformType(type) && (type.GetGenericArguments()[0] == typeof(string));
72    }
73    protected override uint GetTargetKey(object sourceKey, PersistenceMapper mapper) {
74      return mapper.GetStringId((string)sourceKey);
75    }
76    protected override uint GetTargetValue(object sourceValue, PersistenceMapper mapper) {
77      return mapper.GetDataId(sourceValue);
78    }
79    protected override object GetSourceKey(uint targetKey, PersistenceMapper mapper) {
80      return mapper.GetString(targetKey);
81    }
82    protected override object GetSourceValue(uint targetValue, PersistenceMapper mapper) {
83      return mapper.GetObject(targetValue);
84    }
85  }
86
87  [Transformer("D1EF55D6-843A-42AF-8EBB-49566981C943", 404)]
88  internal sealed class DictionaryTransformer : GenericDictionaryTransformer {
89    protected override uint GetTargetKey(object sourceKey, PersistenceMapper mapper) {
90      return mapper.GetDataId(sourceKey);
91    }
92    protected override uint GetTargetValue(object sourceValue, PersistenceMapper mapper) {
93      return mapper.GetDataId(sourceValue);
94    }
95    protected override object GetSourceKey(uint targetKey, PersistenceMapper mapper) {
96      return mapper.GetObject(targetKey);
97    }
98    protected override object GetSourceValue(uint targetValue, PersistenceMapper mapper) {
99      return mapper.GetObject(targetValue);
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.