@@ -10,13 +10,16 @@ namespace CppSharp.Types
10
10
{
11
11
public class TypeMapDatabase : ITypeMapDatabase
12
12
{
13
- public IDictionary < string , TypeMap > TypeMaps { get ; set ; }
14
13
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 ) ;
15
18
16
19
public TypeMapDatabase ( BindingContext bindingContext )
17
20
{
18
21
Context = bindingContext ;
19
- TypeMaps = new Dictionary < string , TypeMap > ( ) ;
22
+ GlobalTypeMaps = new Dictionary < GeneratorKind , Dictionary < string , TypeMap > > ( ) ;
20
23
foreach ( var assembly in AppDomain . CurrentDomain . GetAssemblies ( ) )
21
24
{
22
25
try
@@ -32,33 +35,22 @@ public TypeMapDatabase(BindingContext bindingContext)
32
35
}
33
36
}
34
37
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 )
37
39
{
38
- foreach ( var type in types )
40
+ if ( ! globalTypeMaps . TryGetValue ( kind , out Dictionary < T , TypeMap > typeMap ) )
39
41
{
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 ) ;
57
44
}
45
+ return typeMap ;
58
46
}
59
47
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 )
61
52
{
53
+ var typeMaps = TypeMapsByKind ( typeMapsCache , kind ) ;
62
54
// Looks up the type in the cache map.
63
55
if ( typeMaps . ContainsKey ( type ) )
64
56
{
@@ -113,7 +105,7 @@ public bool FindTypeMap(Type type, out TypeMap typeMap)
113
105
typePrinter . PushScope ( typePrintScopeKind ) ;
114
106
var typeName = type . Visit ( typePrinter ) ;
115
107
typePrinter . PopScope ( ) ;
116
- if ( FindTypeMap ( typeName , out typeMap ) )
108
+ if ( FindTypeMap ( typeName , kind , out typeMap ) )
117
109
{
118
110
typeMap . Type = type ;
119
111
typeMaps [ type ] = typeMap ;
@@ -127,11 +119,33 @@ public bool FindTypeMap(Type type, out TypeMap typeMap)
127
119
}
128
120
129
121
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 ) ;
131
126
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 ;
134
129
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
+ }
136
150
}
137
151
}
0 commit comments