#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Google.ProtocolBuffers;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.Persistence {
public sealed class StaticCache {
private static object locker = new object();
private Dictionary guid2Transformer;
private Dictionary transformer2Guid;
private Dictionary guid2Type;
private Dictionary type2Guid;
private Dictionary typeInfos;
private ExtensionRegistry extensionRegistry;
internal StaticCache() {
guid2Transformer = new Dictionary();
transformer2Guid = new Dictionary();
guid2Type = new Dictionary();
type2Guid = new Dictionary();
typeInfos = new Dictionary();
extensionRegistry = ExtensionRegistry.CreateInstance();
foreach (var transformer in ApplicationManager.Manager.GetInstances())
RegisterTransformer(transformer);
// ECC12A57-DA8D-43D9-9EC7-FCAC878A4D69
// E8348C94-9817-4164-9C98-377689F83F30
// 05551382-E894-4218-B860-FEE1D92CA07D
// 4CC0D44E-65B2-4DF1-A333-30F058CB78CA
// 5D451A64-EBD9-411F-A07E-232250B33784
// EEBD2D26-56CC-45EA-900A-DD29B9F598CE
// B8EEEFA7-8576-42DD-937C-AFBB66297B56
// 4C3C8E0A-7DCF-4814-B798-AECB86E1E4E3
// EAA4FF91-B90C-47CB-AC0F-66AB66D41186
// EB51B342-826F-4004-B2C6-F2A03A8C35C9
// 14138614-45DB-4A0D-9E59-AE9373241D6B
// RegisterType(new Guid("94AD8522-3F55-4580-A6F8-2D2AAEDD4B8C"), typeof(bool));
// RegisterType(new Guid("4A1C0FD5-423D-4F96-AB22-A496578C25AC"), typeof(byte));
// RegisterType(new Guid("C4B00F0B-FED7-439F-B1B2-8A0048B64882"), typeof(sbyte));
// RegisterType(new Guid("9F451811-3DE1-43AB-8B74-D7E03851857B"), typeof(short));
// RegisterType(new Guid("46244D54-0145-49F7-9CF3-9CDB7FB5F240"), typeof(ushort));
// RegisterType(new Guid("1FDDE40C-09E3-491F-8FBB-32BB3C885E9E"), typeof(char));
// RegisterType(new Guid("EE3E0F9C-A5C2-4461-AF36-28BD8F26E6FB"), typeof(int));
// RegisterType(new Guid("69476D18-D285-43E9-BC7C-6CC9E9F2321E"), typeof(uint));
// RegisterType(new Guid("7C7BC5EC-F001-4BA0-9F85-50DCBAA9AE81"), typeof(long));
// RegisterType(new Guid("AC808D5A-63BB-457C-9B92-C9B83DA2B139"), typeof(ulong));
// RegisterType(new Guid("BF22653A-026C-4367-BBBA-2125AECF6C08"), typeof(float));
// RegisterType(new Guid("8B49821A-3ADC-4715-9DB1-08E2F3CFDF15"), typeof(double));
// RegisterType(new Guid("7BB386BF-6FD4-443D-A6C1-387096798C67"), typeof(DateTime));
// RegisterType(new Guid("724A2D49-7E7B-455B-BBA9-4214C64E8A21"), typeof(TimeSpan));
// RegisterType(new Guid("4DB2ED2B-D9F2-4695-B555-A2CF42740740"), typeof(Color));
// RegisterType(new Guid("9BC74087-D5C0-4C39-99B4-D7465F478492"), typeof(Point));
// RegisterType(new Guid("E84C326A-7E14-4F28-AEFF-BC16CC671655"), typeof(KeyValuePair<,>));
// RegisterType(new Guid("F0280B55-25E8-4981-B309-D675D081402A"), typeof(string));
//
// RegisterType(new Guid("9CF55419-439B-4A90-B2ED-8C7F7768EB61"), typeof(Array));
// RegisterType(new Guid("D15AD28B-203A-460E-815C-F7230C4B1F75"), typeof(bool[]));
// RegisterType(new Guid("EE318DC4-580D-4DB1-9AAD-988B0E50A3DB"), typeof(byte[]));
// RegisterType(new Guid("326B9484-361F-46F8-B8A6-46C781A8C51F"), typeof(sbyte[]));
// RegisterType(new Guid("C854783D-4C91-43C8-A19A-F96012494F8A"), typeof(short[]));
// RegisterType(new Guid("C47FB329-2A1D-4EB7-AFB1-1CF72AB30E98"), typeof(ushort[]));
// RegisterType(new Guid("9681D044-B50C-4907-B23E-BDFEBC6C3FFC"), typeof(char[]));
// RegisterType(new Guid("0FC62D91-D63F-4507-8696-885D4C07285A"), typeof(int[]));
// RegisterType(new Guid("C43A25A0-9B99-4DA4-9279-7495FE10428E"), typeof(uint[]));
// RegisterType(new Guid("7AC4DDAC-0E13-428E-BF49-7BE7B76746CB"), typeof(long[]));
// RegisterType(new Guid("8D9283A7-FE49-4AEA-A09F-D53CF3BE520A"), typeof(ulong[]));
// RegisterType(new Guid("BB9CFA0B-DD29-4C44-85C6-3A6289C55448"), typeof(float[]));
// RegisterType(new Guid("3B3AA69E-1641-4202-AE1D-7EEFF60FA355"), typeof(double[]));
// RegisterType(new Guid("7DF325A6-2553-4915-B7C6-D98191107CB7"), typeof(string[]));
// RegisterType(new Guid("DB2A785C-5803-42C7-A01F-E1FD845A83A1"), typeof(List<>));
// RegisterType(new Guid("EB98A564-BEDC-458A-9E9B-4BF3CDAFB9BE"), typeof(Stack<>));
// RegisterType(new Guid("D5E3118B-957A-43B5-A740-1BBAA0EAB666"), typeof(System.Collections.Stack));
// RegisterType(new Guid("7E2F3277-7216-4295-A498-9ACA43527D5B"), typeof(HashSet<>));
// RegisterType(new Guid("E92C35AD-32B1-4F37-B8D2-BE2F5FEB465B"), typeof(Dictionary<,>));
RegisterTypeAndFullName(typeof(bool));
RegisterTypeAndFullName(typeof(byte));
RegisterTypeAndFullName(typeof(sbyte));
RegisterTypeAndFullName(typeof(short));
RegisterTypeAndFullName(typeof(ushort));
RegisterTypeAndFullName(typeof(char));
RegisterTypeAndFullName(typeof(int));
RegisterTypeAndFullName(typeof(uint));
RegisterTypeAndFullName(typeof(long));
RegisterTypeAndFullName(typeof(ulong));
RegisterTypeAndFullName(typeof(float));
RegisterTypeAndFullName(typeof(double));
RegisterTypeAndFullName(typeof(DateTime));
RegisterTypeAndFullName(typeof(TimeSpan));
RegisterTypeAndFullName(typeof(Color));
RegisterTypeAndFullName(typeof(Point));
RegisterTypeAndFullName(typeof(KeyValuePair<,>));
RegisterTypeAndFullName(typeof(string));
RegisterTypeAndFullName(typeof(Array));
RegisterTypeAndFullName(typeof(bool[]));
RegisterTypeAndFullName(typeof(byte[]));
RegisterTypeAndFullName(typeof(sbyte[]));
RegisterTypeAndFullName(typeof(short[]));
RegisterTypeAndFullName(typeof(ushort[]));
RegisterTypeAndFullName(typeof(char[]));
RegisterTypeAndFullName(typeof(int[]));
RegisterTypeAndFullName(typeof(uint[]));
RegisterTypeAndFullName(typeof(long[]));
RegisterTypeAndFullName(typeof(ulong[]));
RegisterTypeAndFullName(typeof(float[]));
RegisterTypeAndFullName(typeof(double[]));
RegisterTypeAndFullName(typeof(string[]));
RegisterTypeAndFullName(typeof(List<>));
RegisterTypeAndFullName(typeof(Stack<>));
RegisterTypeAndFullName(typeof(System.Collections.Stack));
RegisterTypeAndFullName(typeof(HashSet<>));
RegisterTypeAndFullName(typeof(Dictionary<,>));
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) {
foreach (var t in asm.GetTypes().Where(x => StorableClassAttribute.IsStorableClass(x)))
RegisterTypeAndFullName(t);
}
var registeredTypes = type2Guid.Keys.ToArray();
foreach (var t in registeredTypes) {
AddBaseTypesRec(t);
}
RegisterExtension(BoolBox.Bool);
RegisterExtension(IntBox.Int);
RegisterExtension(LongBox.Long);
RegisterExtension(UnsignedIntBox.UnsignedInt);
RegisterExtension(UnsignedLongBox.UnsignedLong);
RegisterExtension(FloatBox.Float);
RegisterExtension(DoubleBox.Double);
RegisterExtension(StringBox.String);
RegisterExtension(BytesBox.Bytes);
RegisterExtension(BoolArrayBox.BoolArray);
RegisterExtension(ByteArrayBox.ByteArray);
RegisterExtension(IntArrayBox.IntArray);
RegisterExtension(LongArrayBox.LongArray);
RegisterExtension(UnsignedIntArrayBox.UnsignedIntArray);
RegisterExtension(UnsignedLongArrayBox.UnsignedLongArray);
RegisterExtension(FloatArrayBox.FloatArray);
RegisterExtension(DoubleArrayBox.DoubleArray);
RegisterExtension(StringArrayBox.StringArray);
RegisterExtension(MatrixBox.Matrix);
RegisterExtension(BoolMatrixBox.BoolMatrix);
RegisterExtension(IntMatrixBox.IntMatrix);
RegisterExtension(DictionaryBox.Dictionary);
RegisterExtension(StorableClassBox.StorableClass);
RegisterExtension(TypeBox.Type);
}
private void AddBaseTypesRec(Type t) {
foreach (var interfaceType in t.GetInterfaces()) {
RegisterTypeAndFullName(interfaceType);
AddBaseTypesRec(interfaceType);
}
if (t.BaseType != null) {
RegisterTypeAndFullName(t.BaseType);
AddBaseTypesRec(t.BaseType);
}
}
public void RegisterTransformer(ITransformer transformer) {
lock (locker) {
guid2Transformer.Add(transformer.Guid, transformer);
transformer2Guid.Add(transformer, transformer.Guid);
}
}
// public void RegisterType(Guid guid, Type type) {
// lock (locker) {
// guid2Type.Add(guid, type);
// type2Guid.Add(type, guid);
// }
// }
public void RegisterTypeAndFullName(Type type) {
lock (locker) {
var key = type.AssemblyQualifiedName ?? type.Name;
if (!guid2Type.ContainsKey(key)) {
guid2Type.Add(key, type);
type2Guid.Add(type, key);
}
}
}
public void RegisterExtension(GeneratedExtensionBase extension) {
lock (locker) {
extensionRegistry.Add(extension);
}
}
public ITransformer GetTransformer(Guid guid) {
return guid2Transformer[guid];
}
public Guid GetGuid(ITransformer transformer) {
return transformer2Guid[transformer];
}
public Type GetType(string guid) {
return guid2Type[guid];
}
public string GetGuid(Type type) {
return type2Guid[type];
}
public TypeInfo GetTypeInfo(Type type) {
lock (locker) {
TypeInfo typeInfo;
if (!typeInfos.TryGetValue(type, out typeInfo)) {
var transformer = guid2Transformer.Values.OrderBy(x => x.Priority).First(x => x.CanTransformType(type));
typeInfo = new TypeInfo(type, transformer);
typeInfos.Add(type, typeInfo);
}
return typeInfo;
}
}
public ExtensionRegistry GetExtensionRegistry() {
return extensionRegistry;
}
}
}