Skip to content

Commit bf5a6ea

Browse files
committed
TypeMapDatabase: heavy refactor: group typemaps by GeneratorKind
1 parent c1f392f commit bf5a6ea

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

src/Generator/Types/TypeMap.cs

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ public virtual void CLITypeReference(CLITypeReferenceCollector collector, ASTRec
156156
public interface ITypeMapDatabase
157157
{
158158
bool FindTypeMap(Type decl, out TypeMap typeMap);
159+
bool FindTypeMap(Type decl, GeneratorKind kind, out TypeMap typeMap);
159160
bool FindTypeMap(Declaration declaration, out TypeMap typeMap);
161+
bool FindTypeMap(Declaration declaration, GeneratorKind kind, out TypeMap typeMap);
160162
}
161163
}

src/Generator/Types/TypeMapDatabase.cs

+42-28
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ namespace CppSharp.Types
1010
{
1111
public class TypeMapDatabase : ITypeMapDatabase
1212
{
13-
public IDictionary<string, TypeMap> TypeMaps { get; set; }
1413
private readonly BindingContext Context;
14+
private readonly Dictionary<GeneratorKind, Dictionary<Type, TypeMap>> typeMapsCache = new();
15+
16+
public Dictionary<GeneratorKind, Dictionary<string, TypeMap>> GlobalTypeMaps { get; private set; }
17+
public Dictionary<string, TypeMap> TypeMaps => TypeMapsByKind(GlobalTypeMaps, Context.Options.GeneratorKind);
1518

1619
public TypeMapDatabase(BindingContext bindingContext)
1720
{
1821
Context = bindingContext;
19-
TypeMaps = new Dictionary<string, TypeMap>();
22+
GlobalTypeMaps = new Dictionary<GeneratorKind, Dictionary<string, TypeMap>>();
2023
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
2124
{
2225
try
@@ -32,33 +35,22 @@ public TypeMapDatabase(BindingContext bindingContext)
3235
}
3336
}
3437

35-
private void SetupTypeMaps(IEnumerable<System.Type> types,
36-
BindingContext bindingContext)
38+
public static Dictionary<T, TypeMap> TypeMapsByKind<T>(Dictionary<GeneratorKind, Dictionary<T, TypeMap>> globalTypeMaps, GeneratorKind kind)
3739
{
38-
foreach (var type in types)
40+
if (!globalTypeMaps.TryGetValue(kind, out Dictionary<T, TypeMap> typeMap))
3941
{
40-
var attrs = type.GetCustomAttributes(typeof(TypeMapAttribute), true);
41-
foreach (TypeMapAttribute attr in attrs)
42-
{
43-
if (string.IsNullOrEmpty(attr.GeneratorKindID) ||
44-
attr.GeneratorKindID == bindingContext.Options.GeneratorKind.ID)
45-
{
46-
var typeMap = (TypeMap)Activator.CreateInstance(type);
47-
typeMap.Context = bindingContext;
48-
typeMap.TypeMapDatabase = this;
49-
50-
// Custom types won't be overwritten by CppSharp ones.
51-
if (!TypeMaps.ContainsKey(attr.Type))
52-
{
53-
TypeMaps.Add(attr.Type, typeMap);
54-
}
55-
}
56-
}
42+
typeMap = new Dictionary<T, TypeMap>();
43+
globalTypeMaps.Add(kind, typeMap);
5744
}
45+
return typeMap;
5846
}
5947

60-
public bool FindTypeMap(Type type, out TypeMap typeMap)
48+
public bool FindTypeMap(Type type, out TypeMap typeMap) =>
49+
FindTypeMap(type, Context.Options.GeneratorKind, out typeMap);
50+
51+
public bool FindTypeMap(Type type, GeneratorKind kind, out TypeMap typeMap)
6152
{
53+
var typeMaps = TypeMapsByKind(typeMapsCache, kind);
6254
// Looks up the type in the cache map.
6355
if (typeMaps.ContainsKey(type))
6456
{
@@ -113,7 +105,7 @@ public bool FindTypeMap(Type type, out TypeMap typeMap)
113105
typePrinter.PushScope(typePrintScopeKind);
114106
var typeName = type.Visit(typePrinter);
115107
typePrinter.PopScope();
116-
if (FindTypeMap(typeName, out typeMap))
108+
if (FindTypeMap(typeName, kind, out typeMap))
117109
{
118110
typeMap.Type = type;
119111
typeMaps[type] = typeMap;
@@ -127,11 +119,33 @@ public bool FindTypeMap(Type type, out TypeMap typeMap)
127119
}
128120

129121
public bool FindTypeMap(Declaration declaration, out TypeMap typeMap) =>
130-
FindTypeMap(new TagType(declaration), out typeMap);
122+
FindTypeMap(declaration, Context.Options.GeneratorKind, out typeMap);
123+
124+
public bool FindTypeMap(Declaration declaration, GeneratorKind kind, out TypeMap typeMap) =>
125+
FindTypeMap(new TagType(declaration), kind, out typeMap);
131126

132-
public bool FindTypeMap(string name, out TypeMap typeMap) =>
133-
TypeMaps.TryGetValue(name, out typeMap) && typeMap.IsEnabled;
127+
public bool FindTypeMap(string name, GeneratorKind kind, out TypeMap typeMap) =>
128+
TypeMapsByKind(GlobalTypeMaps, kind).TryGetValue(name, out typeMap) && typeMap.IsEnabled;
134129

135-
private Dictionary<Type, TypeMap> typeMaps = new Dictionary<Type, TypeMap>();
130+
private void SetupTypeMaps(IEnumerable<System.Type> types, BindingContext bindingContext)
131+
{
132+
foreach (var type in types)
133+
{
134+
var attrs = type.GetCustomAttributes(typeof(TypeMapAttribute), true);
135+
foreach (TypeMapAttribute attr in attrs)
136+
{
137+
var kind = string.IsNullOrEmpty(attr.GeneratorKindID) ? Context.Options.GeneratorKind : GeneratorKind.FindGeneratorKindByID(attr.GeneratorKindID);
138+
var typeMaps = TypeMapsByKind(GlobalTypeMaps, kind);
139+
// Custom types won't be overwritten by CppSharp ones.
140+
if (!typeMaps.ContainsKey(attr.Type))
141+
{
142+
var typeMap = (TypeMap)Activator.CreateInstance(type);
143+
typeMap.Context = bindingContext;
144+
typeMap.TypeMapDatabase = this;
145+
typeMaps.Add(attr.Type, typeMap);
146+
}
147+
}
148+
}
149+
}
136150
}
137151
}

0 commit comments

Comments
 (0)