Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/StaticCache.cs @ 16123

Last change on this file since 16123 was 14739, checked in by gkronber, 8 years ago

#2520: added a sketch for a unit test for conversion (and set svn:ignore properties)

File size: 14.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 System.Drawing;
26using System.Linq;
27using Google.ProtocolBuffers;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Persistence {
32  public sealed class StaticCache {
33    private static object locker = new object();
34
35    private Dictionary<Guid, ITransformer> guid2Transformer;
36    private Dictionary<ITransformer, Guid> transformer2Guid;
37    private Dictionary<Guid, Type> guid2Type;
38    private Dictionary<Type, Guid> type2Guid;
39    private Dictionary<Type, TypeInfo> typeInfos;
40    private ExtensionRegistry extensionRegistry;
41
42    internal StaticCache() {
43      guid2Transformer = new Dictionary<Guid, ITransformer>();
44      transformer2Guid = new Dictionary<ITransformer, Guid>();
45      guid2Type = new Dictionary<Guid, Type>();
46      type2Guid = new Dictionary<Type, Guid>();
47      typeInfos = new Dictionary<Type, TypeInfo>();
48      extensionRegistry = ExtensionRegistry.CreateInstance();
49
50      foreach (var transformer in ApplicationManager.Manager.GetInstances<ITransformer>())
51        RegisterTransformer(transformer);
52
53
54      //
55      // 
56      // 
57      // 
58      // 
59      // 
60      // 
61      // 
62      // 
63      // 
64      //  "0F24C888-AF34-4318-9472-187B9AA85F3C"
65
66
67
68      RegisterType(new Guid("ECAEA154-6BFF-419F-8BE6-2565E9314825"), typeof(object));
69      RegisterType(new Guid("94AD8522-3F55-4580-A6F8-2D2AAEDD4B8C"), typeof(bool));
70      RegisterType(new Guid("4A1C0FD5-423D-4F96-AB22-A496578C25AC"), typeof(byte));
71      RegisterType(new Guid("C4B00F0B-FED7-439F-B1B2-8A0048B64882"), typeof(sbyte));
72      RegisterType(new Guid("9F451811-3DE1-43AB-8B74-D7E03851857B"), typeof(short));
73      RegisterType(new Guid("46244D54-0145-49F7-9CF3-9CDB7FB5F240"), typeof(ushort));
74      RegisterType(new Guid("1FDDE40C-09E3-491F-8FBB-32BB3C885E9E"), typeof(char));
75      RegisterType(new Guid("EE3E0F9C-A5C2-4461-AF36-28BD8F26E6FB"), typeof(int));
76      RegisterType(new Guid("69476D18-D285-43E9-BC7C-6CC9E9F2321E"), typeof(uint));
77      RegisterType(new Guid("7C7BC5EC-F001-4BA0-9F85-50DCBAA9AE81"), typeof(long));
78      RegisterType(new Guid("AC808D5A-63BB-457C-9B92-C9B83DA2B139"), typeof(ulong));
79      RegisterType(new Guid("BF22653A-026C-4367-BBBA-2125AECF6C08"), typeof(float));
80      RegisterType(new Guid("8B49821A-3ADC-4715-9DB1-08E2F3CFDF15"), typeof(double));
81      RegisterType(new Guid("5C6CB86C-81DA-4334-9196-6571C6706240"), typeof(decimal));
82
83      RegisterType(new Guid("7BB386BF-6FD4-443D-A6C1-387096798C67"), typeof(DateTime));
84      RegisterType(new Guid("724A2D49-7E7B-455B-BBA9-4214C64E8A21"), typeof(TimeSpan));
85      RegisterType(new Guid("ECC12A57-DA8D-43D9-9EC7-FCAC878A4D69"), typeof(Font));
86      RegisterType(new Guid("E8348C94-9817-4164-9C98-377689F83F30"), typeof(Bitmap));
87      RegisterType(new Guid("05551382-E894-4218-B860-FEE1D92CA07D"), typeof(Nullable<>));
88      RegisterType(new Guid("4CC0D44E-65B2-4DF1-A333-30F058CB78CA"), typeof(Tuple<>));
89      RegisterType(new Guid("5D451A64-EBD9-411F-A07E-232250B33784"), typeof(Tuple<,>));
90      RegisterType(new Guid("EEBD2D26-56CC-45EA-900A-DD29B9F598CE"), typeof(Tuple<,,>));
91      RegisterType(new Guid("B8EEEFA7-8576-42DD-937C-AFBB66297B56"), typeof(Tuple<,,,>));
92      RegisterType(new Guid("4C3C8E0A-7DCF-4814-B798-AECB86E1E4E3"), typeof(Tuple<,,,,>));
93      RegisterType(new Guid("EAA4FF91-B90C-47CB-AC0F-66AB66D41186"), typeof(Tuple<,,,,,>));
94      RegisterType(new Guid("EB51B342-826F-4004-B2C6-F2A03A8C35C9"), typeof(Tuple<,,,,,,>));
95      RegisterType(new Guid("14138614-45DB-4A0D-9E59-AE9373241D6B"), typeof(Tuple<,,,,,,,>));
96      RegisterType(new Guid("4DB2ED2B-D9F2-4695-B555-A2CF42740740"), typeof(Color));
97      RegisterType(new Guid("9BC74087-D5C0-4C39-99B4-D7465F478492"), typeof(Point));
98      RegisterType(new Guid("E84C326A-7E14-4F28-AEFF-BC16CC671655"), typeof(KeyValuePair<,>));
99      RegisterType(new Guid("F0280B55-25E8-4981-B309-D675D081402A"), typeof(string));
100      //
101      RegisterType(new Guid("9CF55419-439B-4A90-B2ED-8C7F7768EB61"), typeof(Array));
102      // RegisterType(new Guid("D15AD28B-203A-460E-815C-F7230C4B1F75"), typeof(bool[]));
103      // RegisterType(new Guid("EE318DC4-580D-4DB1-9AAD-988B0E50A3DB"), typeof(byte[]));
104      // RegisterType(new Guid("326B9484-361F-46F8-B8A6-46C781A8C51F"), typeof(sbyte[]));
105      // RegisterType(new Guid("C854783D-4C91-43C8-A19A-F96012494F8A"), typeof(short[]));
106      // RegisterType(new Guid("C47FB329-2A1D-4EB7-AFB1-1CF72AB30E98"), typeof(ushort[]));
107      // RegisterType(new Guid("9681D044-B50C-4907-B23E-BDFEBC6C3FFC"), typeof(char[]));
108      // RegisterType(new Guid("0FC62D91-D63F-4507-8696-885D4C07285A"), typeof(int[]));
109      // RegisterType(new Guid("C43A25A0-9B99-4DA4-9279-7495FE10428E"), typeof(uint[]));
110      // RegisterType(new Guid("7AC4DDAC-0E13-428E-BF49-7BE7B76746CB"), typeof(long[]));
111      // RegisterType(new Guid("8D9283A7-FE49-4AEA-A09F-D53CF3BE520A"), typeof(ulong[]));
112      // RegisterType(new Guid("BB9CFA0B-DD29-4C44-85C6-3A6289C55448"), typeof(float[]));
113      // RegisterType(new Guid("3B3AA69E-1641-4202-AE1D-7EEFF60FA355"), typeof(double[]));
114      // RegisterType(new Guid("7DF325A6-2553-4915-B7C6-D98191107CB7"), typeof(string[]));
115
116      RegisterType(new Guid("E451A99B-0CFD-48E6-9BEF-0AE9D1E73628"), typeof(IList));
117      RegisterType(new Guid("B7313EE2-045F-41C9-AEC9-7B1A8CDBF90F"), typeof(IEnumerable));
118      RegisterType(new Guid("D5265599-7380-4A28-AB0D-9F336EF290C5"), typeof(IEnumerable<>));
119      RegisterType(new Guid("DB2A785C-5803-42C7-A01F-E1FD845A83A1"), typeof(List<>));
120      RegisterType(new Guid("EB98A564-BEDC-458A-9E9B-4BF3CDAFB9BE"), typeof(Stack<>));
121      RegisterType(new Guid("D5E3118B-957A-43B5-A740-1BBAA0EAB666"), typeof(Stack));
122      RegisterType(new Guid("8CBC5F9F-5671-4805-A690-DD034AEB7932"), typeof(ArrayList));
123      RegisterType(new Guid("80CAB5BF-06C2-4DB2-9CF0-EE6E93D40B30"), typeof(Queue));
124      RegisterType(new Guid("C528E123-F3DD-4711-83E2-A26E2A9A85E9"), typeof(Queue<>));
125      RegisterType(new Guid("7E2F3277-7216-4295-A498-9ACA43527D5B"), typeof(HashSet<>));
126      RegisterType(new Guid("E92C35AD-32B1-4F37-B8D2-BE2F5FEB465B"), typeof(Dictionary<,>));
127      RegisterType(new Guid("18504ADF-2332-4497-8CA0-B2F00CA2EBA2"), typeof(Type));
128
129      var allTypes = new Dictionary<string, Type>(5000);
130      foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) {
131        if (asm.FullName == "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") {
132          foreach (var t in asm.GetTypes()) {
133            if (typeof(IEqualityComparer).IsAssignableFrom(t))
134              allTypes.Add(t.FullName, t);
135
136            else if (typeof(IEqualityComparer<>).IsAssignableFrom(t))
137              allTypes.Add(t.FullName, t);
138          }
139        } else {
140          foreach (var t in asm.GetTypes()) {
141            if (StorableTypeAttribute.IsStorableType(t)) {
142              RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(t).Guid, t);
143            }
144          }
145        }
146      }
147      RegisterType(new Guid("67C35B3B-DC23-4929-9BA9-0A0CD5FE17F5"), allTypes["System.StringComparer"]);
148      RegisterType(new Guid("C7FDE8F0-EF3F-4A4D-8D87-5559C32A8B90"), allTypes["System.CultureAwareComparer"]);
149      RegisterType(new Guid("4AF3A1F6-49CD-4172-8394-FE464889250E"), allTypes["System.CultureAwareRandomizedComparer"]);
150      RegisterType(new Guid("6A80741F-066D-460B-B333-4F766A4D4AEF"), allTypes["System.OrdinalComparer"]);
151      RegisterType(new Guid("4C7F657C-B69E-4D1A-8812-3254D6219FD2"), allTypes["System.OrdinalRandomizedComparer"]);
152      RegisterType(new Guid("0549A3DF-1FD4-487A-B06B-8C572DFFFBEB"), allTypes["System.Security.PermissionTokenKeyComparer"]);
153      RegisterType(new Guid("94BB9853-8D25-4DD8-A915-FEF2EFF538BC"), allTypes["System.Resources.FastResourceComparer"]);
154      RegisterType(new Guid("65D2DE74-8BDF-4C74-9005-81A2C3991DC5"), allTypes["System.Collections.CompatibleComparer"]);
155      RegisterType(new Guid("075827F4-07D9-4E50-9D00-C9FF3E7DCF9A"), allTypes["System.Collections.IEqualityComparer"]);
156      RegisterType(new Guid("3DA30D50-2337-4487-AECD-F2DB3ECED834"), allTypes["System.Collections.StructuralEqualityComparer"]);
157      RegisterType(new Guid("C8BF5C03-1614-46AC-8730-2F231CAD4232"), allTypes["System.Collections.Generic.EqualityComparer`1"]);
158      RegisterType(new Guid("FB247FE0-9203-4196-B4D3-71AB18CD12C5"), allTypes["System.Collections.Generic.GenericEqualityComparer`1"]);
159      RegisterType(new Guid("06443E04-FB1D-41E1-BAE2-1AB3728BDCC9"), allTypes["System.Collections.Generic.NullableEqualityComparer`1"]);
160      RegisterType(new Guid("D9029E74-C511-4D28-B8C5-D0E299EC45A5"), allTypes["System.Collections.Generic.ObjectEqualityComparer`1"]);
161      RegisterType(new Guid("C6D867E2-3CD2-4C59-8BA6-F18F00DDB997"), allTypes["System.Collections.Generic.ByteEqualityComparer"]);
162      RegisterType(new Guid("02F8D180-BABE-4073-82E0-7FE77814F53D"), allTypes["System.Collections.Generic.EnumEqualityComparer`1"]);
163      RegisterType(new Guid("8A19DA46-FE80-4776-9DB4-B17E6182D104"), allTypes["System.Collections.Generic.SByteEnumEqualityComparer`1"]);
164      RegisterType(new Guid("F6F6EFB9-B631-4ACC-9326-F492A6A63011"), allTypes["System.Collections.Generic.ShortEnumEqualityComparer`1"]);
165      RegisterType(new Guid("D3D279FE-169E-46B6-93AE-589BB4F24A4D"), allTypes["System.Collections.Generic.LongEnumEqualityComparer`1"]);
166      RegisterType(new Guid("BA0AB604-C052-4E08-8F9E-A5D8098F16A6"), allTypes["System.Collections.Generic.RandomizedStringEqualityComparer"]);
167      RegisterType(new Guid("00C8C940-63D9-43FF-99BA-9C69301BF043"), allTypes["System.Collections.Generic.RandomizedObjectEqualityComparer"]);
168      RegisterType(new Guid("DAF22757-7FCC-49AC-B148-F3DD7E9E0A3B"), allTypes["System.Collections.Generic.IEqualityComparer`1"]);
169
170      //var registeredTypes = type2Guid.Keys.ToArray();
171      //foreach (var t in registeredTypes) {
172      //  AddBaseTypesRec(t);
173      //}
174
175      RegisterExtension(BoolBox.Bool);
176      RegisterExtension(IntBox.Int);
177      RegisterExtension(LongBox.Long);
178      RegisterExtension(UnsignedIntBox.UnsignedInt);
179      RegisterExtension(UnsignedLongBox.UnsignedLong);
180      RegisterExtension(FloatBox.Float);
181      RegisterExtension(DoubleBox.Double);
182      RegisterExtension(StringBox.String);
183      RegisterExtension(BytesBox.Bytes);
184      RegisterExtension(BoolArrayBox.BoolArray);
185      RegisterExtension(ByteArrayBox.ByteArray);
186      RegisterExtension(IntArrayBox.IntArray);
187      RegisterExtension(LongArrayBox.LongArray);
188      RegisterExtension(UnsignedIntArrayBox.UnsignedIntArray);
189      RegisterExtension(UnsignedLongArrayBox.UnsignedLongArray);
190      RegisterExtension(FloatArrayBox.FloatArray);
191      RegisterExtension(DoubleArrayBox.DoubleArray);
192      RegisterExtension(StringArrayBox.StringArray);
193      RegisterExtension(MatrixBox.Matrix);
194      RegisterExtension(BoolMatrixBox.BoolMatrix);
195      RegisterExtension(IntMatrixBox.IntMatrix);
196      RegisterExtension(DictionaryBox.Dictionary);
197      RegisterExtension(StorableClassBox.StorableClass);
198      RegisterExtension(TypeBox.Type);
199    }
200
201    //private void AddBaseTypesRec(MemberSelection t) {
202    //  foreach (var interfaceType in t.GetInterfaces()) {
203    //    RegisterTypeAndFullName(interfaceType);
204    //    AddBaseTypesRec(interfaceType);
205    //  }
206    //  if (t.BaseType != null) {
207    //    RegisterTypeAndFullName(t.BaseType);
208    //    AddBaseTypesRec(t.BaseType);
209    //  }
210    //}
211
212    public void RegisterTransformer(ITransformer transformer) {
213      lock (locker) {
214        guid2Transformer.Add(transformer.Guid, transformer);
215        transformer2Guid.Add(transformer, transformer.Guid);
216      }
217    }
218    public void RegisterType(Guid guid, Type type) {
219      lock (locker) {
220        guid2Type.Add(guid, type);
221        type2Guid.Add(type, guid);
222      }
223    }
224    // mainly for testing
225    public void DeregisterType(Guid guid) {
226      lock (locker) {
227        type2Guid.Remove(guid2Type[guid]);
228        guid2Type.Remove(guid);
229      }
230    }
231    //public void RegisterTypeAndFullName(MemberSelection type) {
232    //  lock (locker) {
233    //    var key = type.AssemblyQualifiedName ?? type.Name;
234    //    if (!guid2Type.ContainsKey(key)) {
235    //      guid2Type.Add(key, type);
236    //      type2Guid.Add(type, key);
237    //    }
238    //  }
239    //}
240    public void RegisterExtension<TExtension>(GeneratedExtensionBase<TExtension> extension) {
241      lock (locker) {
242        extensionRegistry.Add(extension);
243      }
244    }
245
246    public ITransformer GetTransformer(Guid guid) {
247      return guid2Transformer[guid];
248    }
249    public Guid GetGuid(ITransformer transformer) {
250      return transformer2Guid[transformer];
251    }
252    public Type GetType(Guid guid) {
253      return guid2Type[guid];
254    }
255    public Guid GetGuid(Type type) {
256      return type2Guid[type];
257    }
258    public TypeInfo GetTypeInfo(Type type) {
259      lock (locker) {
260        TypeInfo typeInfo;
261        if (!typeInfos.TryGetValue(type, out typeInfo)) {
262          var transformer = guid2Transformer.Values.OrderBy(x => x.Priority).First(x => x.CanTransformType(type));
263          typeInfo = new TypeInfo(type, transformer);
264          typeInfos.Add(type, typeInfo);
265        }
266        return typeInfo;
267      }
268    }
269    public ExtensionRegistry GetExtensionRegistry() {
270      return extensionRegistry;
271    }
272  }
273}
Note: See TracBrowser for help on using the repository browser.