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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using Google.ProtocolBuffers;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.PluginInfrastructure;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Persistence {
|
---|
31 | public sealed class StaticCache {
|
---|
32 | private static object locker = new object();
|
---|
33 |
|
---|
34 | private Dictionary<Guid, ITransformer> guid2Transformer;
|
---|
35 | private Dictionary<ITransformer, Guid> transformer2Guid;
|
---|
36 | private Dictionary<Guid, Type> guid2Type;
|
---|
37 | private Dictionary<Type, Guid> type2Guid;
|
---|
38 | private Dictionary<Type, TypeInfo> typeInfos;
|
---|
39 | private ExtensionRegistry extensionRegistry;
|
---|
40 |
|
---|
41 | internal StaticCache() {
|
---|
42 | guid2Transformer = new Dictionary<Guid, ITransformer>();
|
---|
43 | transformer2Guid = new Dictionary<ITransformer, Guid>();
|
---|
44 | guid2Type = new Dictionary<Guid, Type>();
|
---|
45 | type2Guid = new Dictionary<Type, Guid>();
|
---|
46 | typeInfos = new Dictionary<Type, TypeInfo>();
|
---|
47 | extensionRegistry = ExtensionRegistry.CreateInstance();
|
---|
48 |
|
---|
49 | foreach (var transformer in ApplicationManager.Manager.GetInstances<ITransformer>().OrderBy(x => x.Priority))
|
---|
50 | RegisterTransformer(transformer);
|
---|
51 |
|
---|
52 | RegisterType(new Guid("94AD8522-3F55-4580-A6F8-2D2AAEDD4B8C"), typeof(bool));
|
---|
53 | RegisterType(new Guid("4A1C0FD5-423D-4F96-AB22-A496578C25AC"), typeof(byte));
|
---|
54 | RegisterType(new Guid("C4B00F0B-FED7-439F-B1B2-8A0048B64882"), typeof(sbyte));
|
---|
55 | RegisterType(new Guid("9F451811-3DE1-43AB-8B74-D7E03851857B"), typeof(short));
|
---|
56 | RegisterType(new Guid("46244D54-0145-49F7-9CF3-9CDB7FB5F240"), typeof(ushort));
|
---|
57 | RegisterType(new Guid("1FDDE40C-09E3-491F-8FBB-32BB3C885E9E"), typeof(char));
|
---|
58 | RegisterType(new Guid("EE3E0F9C-A5C2-4461-AF36-28BD8F26E6FB"), typeof(int));
|
---|
59 | RegisterType(new Guid("69476D18-D285-43E9-BC7C-6CC9E9F2321E"), typeof(uint));
|
---|
60 | RegisterType(new Guid("7C7BC5EC-F001-4BA0-9F85-50DCBAA9AE81"), typeof(long));
|
---|
61 | RegisterType(new Guid("AC808D5A-63BB-457C-9B92-C9B83DA2B139"), typeof(ulong));
|
---|
62 | RegisterType(new Guid("BF22653A-026C-4367-BBBA-2125AECF6C08"), typeof(float));
|
---|
63 | RegisterType(new Guid("8B49821A-3ADC-4715-9DB1-08E2F3CFDF15"), typeof(double));
|
---|
64 | RegisterType(new Guid("7BB386BF-6FD4-443D-A6C1-387096798C67"), typeof(DateTime));
|
---|
65 | RegisterType(new Guid("724A2D49-7E7B-455B-BBA9-4214C64E8A21"), typeof(TimeSpan));
|
---|
66 | RegisterType(new Guid("4DB2ED2B-D9F2-4695-B555-A2CF42740740"), typeof(Color));
|
---|
67 | RegisterType(new Guid("9BC74087-D5C0-4C39-99B4-D7465F478492"), typeof(Point));
|
---|
68 | RegisterType(new Guid("E84C326A-7E14-4F28-AEFF-BC16CC671655"), typeof(KeyValuePair<,>));
|
---|
69 | RegisterType(new Guid("F0280B55-25E8-4981-B309-D675D081402A"), typeof(string));
|
---|
70 |
|
---|
71 | RegisterType(new Guid("D15AD28B-203A-460E-815C-F7230C4B1F75"), typeof(bool[]));
|
---|
72 | RegisterType(new Guid("EE318DC4-580D-4DB1-9AAD-988B0E50A3DB"), typeof(byte[]));
|
---|
73 | RegisterType(new Guid("326B9484-361F-46F8-B8A6-46C781A8C51F"), typeof(sbyte[]));
|
---|
74 | RegisterType(new Guid("C854783D-4C91-43C8-A19A-F96012494F8A"), typeof(short[]));
|
---|
75 | RegisterType(new Guid("C47FB329-2A1D-4EB7-AFB1-1CF72AB30E98"), typeof(ushort[]));
|
---|
76 | RegisterType(new Guid("9681D044-B50C-4907-B23E-BDFEBC6C3FFC"), typeof(char[]));
|
---|
77 | RegisterType(new Guid("0FC62D91-D63F-4507-8696-885D4C07285A"), typeof(int[]));
|
---|
78 | RegisterType(new Guid("C43A25A0-9B99-4DA4-9279-7495FE10428E"), typeof(uint[]));
|
---|
79 | RegisterType(new Guid("7AC4DDAC-0E13-428E-BF49-7BE7B76746CB"), typeof(long[]));
|
---|
80 | RegisterType(new Guid("8D9283A7-FE49-4AEA-A09F-D53CF3BE520A"), typeof(ulong[]));
|
---|
81 | RegisterType(new Guid("BB9CFA0B-DD29-4C44-85C6-3A6289C55448"), typeof(float[]));
|
---|
82 | RegisterType(new Guid("3B3AA69E-1641-4202-AE1D-7EEFF60FA355"), typeof(double[]));
|
---|
83 | RegisterType(new Guid("7DF325A6-2553-4915-B7C6-D98191107CB7"), typeof(string[]));
|
---|
84 | foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) {
|
---|
85 | foreach (var t in asm.GetTypes().Where(x => StorableClassAttribute.IsStorableClass(x)))
|
---|
86 | RegisterType(StorableClassAttribute.GetStorableClassAttribute(t).Guid, t);
|
---|
87 | }
|
---|
88 |
|
---|
89 | RegisterExtension(BoolBox.Bool);
|
---|
90 | RegisterExtension(IntBox.Int);
|
---|
91 | RegisterExtension(LongBox.Long);
|
---|
92 | RegisterExtension(UnsignedIntBox.UnsignedInt);
|
---|
93 | RegisterExtension(UnsignedLongBox.UnsignedLong);
|
---|
94 | RegisterExtension(FloatBox.Float);
|
---|
95 | RegisterExtension(DoubleBox.Double);
|
---|
96 | RegisterExtension(StringBox.String);
|
---|
97 | RegisterExtension(BytesBox.Bytes);
|
---|
98 | RegisterExtension(BoolArrayBox.BoolArray);
|
---|
99 | RegisterExtension(IntArrayBox.IntArray);
|
---|
100 | RegisterExtension(LongArrayBox.LongArray);
|
---|
101 | RegisterExtension(UnsignedIntArrayBox.UnsignedIntArray);
|
---|
102 | RegisterExtension(UnsignedLongArrayBox.UnsignedLongArray);
|
---|
103 | RegisterExtension(FloatArrayBox.FloatArray);
|
---|
104 | RegisterExtension(DoubleArrayBox.DoubleArray);
|
---|
105 | RegisterExtension(StringArrayBox.StringArray);
|
---|
106 | RegisterExtension(MatrixBox.Matrix);
|
---|
107 | RegisterExtension(BoolMatrixBox.BoolMatrix);
|
---|
108 | RegisterExtension(IntMatrixBox.IntMatrix);
|
---|
109 | RegisterExtension(DictionaryBox.Dictionary);
|
---|
110 | }
|
---|
111 |
|
---|
112 | public void RegisterTransformer(ITransformer transformer) {
|
---|
113 | lock (locker) {
|
---|
114 | guid2Transformer.Add(transformer.Guid, transformer);
|
---|
115 | transformer2Guid.Add(transformer, transformer.Guid);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | public void RegisterType(Guid guid, Type type) {
|
---|
119 | lock (locker) {
|
---|
120 | guid2Type.Add(guid, type);
|
---|
121 | type2Guid.Add(type, guid);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | public void RegisterExtension<TExtension>(GeneratedExtensionBase<TExtension> extension) {
|
---|
125 | lock (locker) {
|
---|
126 | extensionRegistry.Add(extension);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | public ITransformer GetTransformer(Guid guid) {
|
---|
131 | return guid2Transformer[guid];
|
---|
132 | }
|
---|
133 | public Guid GetGuid(ITransformer transformer) {
|
---|
134 | return transformer2Guid[transformer];
|
---|
135 | }
|
---|
136 | public Type GetType(Guid guid) {
|
---|
137 | return guid2Type[guid];
|
---|
138 | }
|
---|
139 | public Guid GetGuid(Type type) {
|
---|
140 | return type2Guid[type];
|
---|
141 | }
|
---|
142 | public TypeInfo GetTypeInfo(Type type) {
|
---|
143 | lock (locker) {
|
---|
144 | TypeInfo typeInfo;
|
---|
145 | if (!typeInfos.TryGetValue(type, out typeInfo)) {
|
---|
146 | var transformer = guid2Transformer.Values.First(x => x.CanTransformType(type));
|
---|
147 | typeInfo = new TypeInfo(type, transformer);
|
---|
148 | typeInfos.Add(type, typeInfo);
|
---|
149 | }
|
---|
150 | return typeInfo;
|
---|
151 | }
|
---|
152 | }
|
---|
153 | public ExtensionRegistry GetExtensionRegistry() {
|
---|
154 | return extensionRegistry;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|