Various changes and tweaks to run better
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
using Upsilon.BaseTypes;
|
||||
|
||||
@@ -7,33 +7,29 @@ namespace Upsilon.BoundTypes
|
||||
{
|
||||
public static class BoundTypeHandler
|
||||
{
|
||||
private static readonly Dictionary<string, BoundTypeDefinition> DefaultTypeDefinitions =
|
||||
new Dictionary<string, BoundTypeDefinition>
|
||||
private static void AddDefaults(ConcurrentDictionary<string, BoundTypeDefinition> dic)
|
||||
{
|
||||
{"string", new BoundTypeDefinition(Type.String, typeof(string))},
|
||||
{
|
||||
"number",
|
||||
new BoundTypeDefinition(Type.Number,
|
||||
new[] {typeof(int), typeof(long), typeof(float), typeof(double)})
|
||||
},
|
||||
{"bool", new BoundTypeDefinition(Type.Boolean, typeof(bool))},
|
||||
{"table", new BoundTypeDefinition(Type.Table, typeof(IEnumerator))},
|
||||
{"function", new BoundTypeDefinition(Type.Function, new System.Type[0])},
|
||||
};
|
||||
dic.TryAdd("string", new BoundTypeDefinition(Type.String, typeof(string)));
|
||||
dic.TryAdd("number", new BoundTypeDefinition(Type.Number,
|
||||
new[] {typeof(int), typeof(long), typeof(float), typeof(double)}));
|
||||
dic.TryAdd("bool", new BoundTypeDefinition(Type.Boolean, typeof(bool)));
|
||||
dic.TryAdd("table", new BoundTypeDefinition(Type.Table, typeof(IEnumerator)));
|
||||
dic.TryAdd("function", new BoundTypeDefinition(Type.Function, new System.Type[0]));
|
||||
}
|
||||
|
||||
private static Dictionary<string, BoundTypeDefinition> _typeDefinitions =
|
||||
new Dictionary<string, BoundTypeDefinition>(DefaultTypeDefinitions);
|
||||
private static readonly ConcurrentDictionary<string, BoundTypeDefinition> TypeDefinitions = Reset();
|
||||
|
||||
public static void Reset()
|
||||
public static ConcurrentDictionary<string, BoundTypeDefinition> Reset()
|
||||
{
|
||||
_typeDefinitions =
|
||||
new Dictionary<string, BoundTypeDefinition>(DefaultTypeDefinitions);
|
||||
var dic = new ConcurrentDictionary<string, BoundTypeDefinition>();
|
||||
AddDefaults(dic);
|
||||
return dic;
|
||||
}
|
||||
|
||||
public static BoundTypeDefinition GetTypeDefinition(string key)
|
||||
{
|
||||
var normalizedName = key.ToLowerInvariant();
|
||||
if (_typeDefinitions.TryGetValue(normalizedName, out var bt))
|
||||
if (TypeDefinitions.TryGetValue(normalizedName, out var bt))
|
||||
{
|
||||
return bt;
|
||||
}
|
||||
@@ -42,20 +38,13 @@ namespace Upsilon.BoundTypes
|
||||
|
||||
public static BoundTypeDefinition GetTypeDefinition(System.Type type)
|
||||
{
|
||||
return _typeDefinitions.Values.FirstOrDefault(x => x.ValidInternalTypes.Contains(type));
|
||||
return TypeDefinitions.Values.FirstOrDefault(x => x.ValidInternalTypes.Contains(type));
|
||||
}
|
||||
|
||||
public static void LoadUserDataTypeDefinition(UserDataBoundTypeDefinition def)
|
||||
{
|
||||
var key = def.Name.ToLowerInvariant();
|
||||
if (_typeDefinitions.ContainsKey(key))
|
||||
{
|
||||
_typeDefinitions[key] = def;
|
||||
}
|
||||
else
|
||||
{
|
||||
_typeDefinitions.Add(key, def);
|
||||
}
|
||||
TypeDefinitions.AddOrUpdate(key, def, (s, definition) => definition);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user