[13358] | 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.Linq;
|
---|
| 25 | using HeuristicLab.PluginInfrastructure;
|
---|
| 26 | using Google.ProtocolBuffers;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Persistence {
|
---|
| 30 | public sealed class StaticCache {
|
---|
| 31 | private static object locker = new object();
|
---|
| 32 |
|
---|
| 33 | private Dictionary<Guid, ITransformer> guid2Transformer;
|
---|
| 34 | private Dictionary<ITransformer, Guid> transformer2Guid;
|
---|
| 35 | private Dictionary<Guid, Type> guid2Type;
|
---|
| 36 | private Dictionary<Type, Guid> type2Guid;
|
---|
| 37 | private Dictionary<Type, TypeInfo> typeInfos;
|
---|
[13367] | 38 | private ExtensionRegistry extensionRegistry;
|
---|
[13358] | 39 |
|
---|
| 40 | internal StaticCache() {
|
---|
| 41 | Initialize();
|
---|
| 42 | }
|
---|
| 43 | public void Initialize() {
|
---|
| 44 | lock (locker) {
|
---|
| 45 | guid2Transformer = new Dictionary<Guid, ITransformer>();
|
---|
| 46 | transformer2Guid = new Dictionary<ITransformer, Guid>();
|
---|
| 47 | guid2Type = new Dictionary<Guid, Type>();
|
---|
| 48 | type2Guid = new Dictionary<Type, Guid>();
|
---|
| 49 | typeInfos = new Dictionary<Type, TypeInfo>();
|
---|
[13367] | 50 | extensionRegistry = ExtensionRegistry.CreateInstance();
|
---|
[13358] | 51 |
|
---|
[13367] | 52 | foreach (var transformer in ApplicationManager.Manager.GetInstances<ITransformer>().OrderBy(x => x.Priority)) {
|
---|
[13358] | 53 | guid2Transformer.Add(transformer.Guid, transformer);
|
---|
| 54 | transformer2Guid.Add(transformer, transformer.Guid);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | RegisterType(new Guid("94AD8522-3F55-4580-A6F8-2D2AAEDD4B8C"), typeof(bool));
|
---|
| 58 | RegisterType(new Guid("4A1C0FD5-423D-4F96-AB22-A496578C25AC"), typeof(byte));
|
---|
| 59 | RegisterType(new Guid("C4B00F0B-FED7-439F-B1B2-8A0048B64882"), typeof(sbyte));
|
---|
| 60 | RegisterType(new Guid("9F451811-3DE1-43AB-8B74-D7E03851857B"), typeof(short));
|
---|
| 61 | RegisterType(new Guid("46244D54-0145-49F7-9CF3-9CDB7FB5F240"), typeof(ushort));
|
---|
| 62 | RegisterType(new Guid("1FDDE40C-09E3-491F-8FBB-32BB3C885E9E"), typeof(char));
|
---|
| 63 | RegisterType(new Guid("EE3E0F9C-A5C2-4461-AF36-28BD8F26E6FB"), typeof(int));
|
---|
| 64 | RegisterType(new Guid("69476D18-D285-43E9-BC7C-6CC9E9F2321E"), typeof(uint));
|
---|
| 65 | RegisterType(new Guid("7C7BC5EC-F001-4BA0-9F85-50DCBAA9AE81"), typeof(long));
|
---|
| 66 | RegisterType(new Guid("AC808D5A-63BB-457C-9B92-C9B83DA2B139"), typeof(ulong));
|
---|
| 67 | RegisterType(new Guid("BF22653A-026C-4367-BBBA-2125AECF6C08"), typeof(float));
|
---|
| 68 | RegisterType(new Guid("8B49821A-3ADC-4715-9DB1-08E2F3CFDF15"), typeof(double));
|
---|
| 69 | RegisterType(new Guid("7BB386BF-6FD4-443D-A6C1-387096798C67"), typeof(DateTime));
|
---|
| 70 | RegisterType(new Guid("724A2D49-7E7B-455B-BBA9-4214C64E8A21"), typeof(TimeSpan));
|
---|
| 71 | foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) {
|
---|
| 72 | foreach (var t in asm.GetTypes().Where(x => StorableClassAttribute.IsStorableClass(x)))
|
---|
| 73 | RegisterType(StorableClassAttribute.GetStorableClassAttribute(t).Guid, t);
|
---|
| 74 | }
|
---|
[13367] | 75 |
|
---|
| 76 | RegisterExtension(BoolBox.Bool);
|
---|
| 77 | RegisterExtension(IntBox.Int);
|
---|
| 78 | RegisterExtension(LongBox.Long);
|
---|
| 79 | RegisterExtension(UnsignedIntBox.UnsignedInt);
|
---|
| 80 | RegisterExtension(UnsignedLongBox.UnsignedLong);
|
---|
| 81 | RegisterExtension(FloatBox.Float);
|
---|
| 82 | RegisterExtension(DoubleBox.Double);
|
---|
| 83 | RegisterExtension(StringBox.String);
|
---|
| 84 | RegisterExtension(BytesBox.Bytes);
|
---|
| 85 | RegisterExtension(BoolArrayBox.BoolArray);
|
---|
| 86 | RegisterExtension(IntArrayBox.IntArray);
|
---|
| 87 | RegisterExtension(MatrixBox.Matrix);
|
---|
| 88 | RegisterExtension(BoolMatrixBox.BoolMatrix);
|
---|
| 89 | RegisterExtension(IntMatrixBox.IntMatrix);
|
---|
| 90 | RegisterExtension(DictionaryBox.Dictionary);
|
---|
[13358] | 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public void RegisterType(Guid guid, Type type) {
|
---|
| 95 | lock (locker) {
|
---|
| 96 | guid2Type.Add(guid, type);
|
---|
| 97 | type2Guid.Add(type, guid);
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[13367] | 100 | public void RegisterExtension<TExtension>(GeneratedExtensionBase<TExtension> extension) {
|
---|
| 101 | extensionRegistry.Add(extension);
|
---|
| 102 | }
|
---|
[13358] | 103 |
|
---|
| 104 | public ITransformer GetTransformer(Guid guid) {
|
---|
| 105 | return guid2Transformer[guid];
|
---|
| 106 | }
|
---|
| 107 | public Guid GetGuid(ITransformer transformer) {
|
---|
| 108 | return transformer2Guid[transformer];
|
---|
| 109 | }
|
---|
| 110 | public Type GetType(Guid guid) {
|
---|
| 111 | return guid2Type[guid];
|
---|
| 112 | }
|
---|
| 113 | public Guid GetGuid(Type type) {
|
---|
| 114 | return type2Guid[type];
|
---|
| 115 | }
|
---|
| 116 | public TypeInfo GetTypeInfo(Type type) {
|
---|
| 117 | lock (locker) {
|
---|
| 118 | TypeInfo typeInfo;
|
---|
| 119 | if (!typeInfos.TryGetValue(type, out typeInfo)) {
|
---|
| 120 | var transformer = guid2Transformer.Values.First(x => x.CanTransformType(type));
|
---|
| 121 | typeInfo = new TypeInfo(type, transformer);
|
---|
| 122 | typeInfos.Add(type, typeInfo);
|
---|
| 123 | }
|
---|
| 124 | return typeInfo;
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
[13367] | 127 | public ExtensionRegistry GetExtensionRegistry() {
|
---|
| 128 | return extensionRegistry;
|
---|
| 129 | }
|
---|
[13358] | 130 | }
|
---|
| 131 | }
|
---|