Skip to content

Commit 76d8182

Browse files
committed
Simplified type maps by using static objects to disable as needed.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 8427ff8 commit 76d8182

File tree

4 files changed

+23
-48
lines changed

4 files changed

+23
-48
lines changed

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public override bool VisitClassDecl(Class @class)
381381
var typeMaps = new List<System.Type>();
382382
var keys = new List<string>();
383383
// disable the type maps, if any, for this class because of copy ctors, operators and others
384-
this.DisableTypeMap(@class, typeMaps, keys);
384+
this.DisableTypeMap(@class);
385385

386386
PushBlock(BlockKind.Class);
387387
GenerateDeclarationCommon(@class);
@@ -443,8 +443,8 @@ public override bool VisitClassDecl(Class @class)
443443
UnindentAndWriteCloseBrace();
444444
PopBlock(NewLineKind.BeforeNextBlock);
445445

446-
for (int i = 0; i < typeMaps.Count; i++)
447-
Context.TypeMaps.TypeMaps.Add(keys[i], typeMaps[i]);
446+
foreach (var typeMap in Context.TypeMaps.TypeMaps.Values)
447+
typeMap.IsEnabled = true;
448448

449449
return true;
450450
}
@@ -694,11 +694,11 @@ public override void GenerateClassSpecifier(Class @class)
694694
{
695695
var typeMaps = new List<System.Type>();
696696
var keys = new List<string>();
697-
this.DisableTypeMap(@base.Class, typeMaps, keys);
697+
this.DisableTypeMap(@base.Class);
698698
var printedBase = @base.Type.Desugar().Visit(TypePrinter);
699699
bases.Add(printedBase.Type);
700-
for (int i = 0; i < typeMaps.Count; i++)
701-
Context.TypeMaps.TypeMaps.Add(keys[i], typeMaps[i]);
700+
foreach (var typeMap in Context.TypeMaps.TypeMaps.Values)
701+
typeMap.IsEnabled = true;
702702
}
703703
}
704704

src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ namespace CppSharp.Generators.CSharp
88
{
99
public static class CSharpSourcesExtensions
1010
{
11-
public static void DisableTypeMap(this CSharpSources gen, Class @class,
12-
List<System.Type> typeMaps, List<string> keys)
11+
public static void DisableTypeMap(this CSharpSources gen, Class @class)
1312
{
1413
var mapped = @class.OriginalClass ?? @class;
15-
DisableSingleTypeMap(mapped, typeMaps, keys, gen.Context);
14+
DisableSingleTypeMap(mapped, gen.Context);
1615
if (mapped.IsDependent)
1716
foreach (var specialization in mapped.Specializations)
18-
DisableSingleTypeMap(specialization, typeMaps, keys, gen.Context);
17+
DisableSingleTypeMap(specialization, gen.Context);
1918
}
2019

2120
public static void GenerateNativeConstructorsByValue(
@@ -93,25 +92,16 @@ public static void GenerateMember(this CSharpSources gen,
9392
}
9493
}
9594

96-
private static void DisableSingleTypeMap(Class mapped,
97-
List<System.Type> typeMaps, List<string> keys, BindingContext context)
95+
private static void DisableSingleTypeMap(Class mapped, BindingContext context)
9896
{
9997
var names = new List<string> { mapped.OriginalName };
10098
foreach (TypePrintScopeKind kind in Enum.GetValues(typeof(TypePrintScopeKind)))
10199
{
102100
var cppTypePrinter = new CppTypePrinter { PrintScopeKind = kind };
103101
names.Add(mapped.Visit(cppTypePrinter));
104102
}
105-
foreach (var name in names)
106-
{
107-
if (context.TypeMaps.TypeMaps.ContainsKey(name))
108-
{
109-
keys.Add(name);
110-
typeMaps.Add(context.TypeMaps.TypeMaps[name]);
111-
context.TypeMaps.TypeMaps.Remove(name);
112-
break;
113-
}
114-
}
103+
foreach (var name in names.Where(context.TypeMaps.TypeMaps.ContainsKey))
104+
context.TypeMaps.TypeMaps[name].IsEnabled = false;
115105
}
116106

117107
private static void WriteTemplateSpecializationCheck(CSharpSources gen,

src/Generator/Types/TypeMap.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class TypeMap
4040
public Declaration Declaration { get; set; }
4141
public ITypeMapDatabase TypeMapDatabase { get; set; }
4242

43+
public bool IsEnabled { get; set; } = true;
44+
4345
public virtual bool IsIgnored
4446
{
4547
get { return false; }
@@ -122,7 +124,6 @@ public interface ITypeMapDatabase
122124
bool FindTypeMapRecursive(Type type, out TypeMap typeMap);
123125
bool FindTypeMap(Type decl, out TypeMap typeMap);
124126
bool FindTypeMap(Declaration decl, out TypeMap typeMap);
125-
bool FindTypeMap(string name, out TypeMap typeMap);
126127
}
127128

128129

src/Generator/Types/TypeMapDatabase.cs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ namespace CppSharp.Types
1313
{
1414
public class TypeMapDatabase : ITypeMapDatabase
1515
{
16-
public IDictionary<string, System.Type> TypeMaps { get; set; }
16+
public IDictionary<string, TypeMap> TypeMaps { get; set; }
1717

1818
public TypeMapDatabase()
1919
{
20-
TypeMaps = new Dictionary<string, System.Type>();
20+
TypeMaps = new Dictionary<string, TypeMap>();
2121
}
2222

2323
public void SetupTypeMaps(GeneratorKind generatorKind)
@@ -41,14 +41,16 @@ public void SetupTypeMaps(GeneratorKind generatorKind)
4141

4242
private void SetupTypeMaps(IEnumerable<System.Type> types, GeneratorKind generatorKind)
4343
{
44-
foreach (var typeMap in types)
44+
foreach (var type in types)
4545
{
46-
var attrs = typeMap.GetCustomAttributes(typeof(TypeMapAttribute), true);
46+
var attrs = type.GetCustomAttributes(typeof(TypeMapAttribute), true);
4747
foreach (TypeMapAttribute attr in attrs)
4848
{
4949
if (attr.GeneratorKind == 0 || attr.GeneratorKind == generatorKind)
5050
{
51-
TypeMaps[attr.Type] = typeMap;
51+
var typeMap = (TypeMap) Activator.CreateInstance(type);
52+
typeMap.TypeMapDatabase = this;
53+
this.TypeMaps[attr.Type] = typeMap;
5254
}
5355
}
5456
}
@@ -164,27 +166,9 @@ public bool FindTypeMapRecursive(Type type, out TypeMap typeMap)
164166
}
165167
}
166168

167-
public bool FindTypeMap(string name, out TypeMap typeMap)
169+
private bool FindTypeMap(string name, out TypeMap typeMap)
168170
{
169-
if (string.IsNullOrWhiteSpace(name))
170-
{
171-
typeMap = null;
172-
return false;
173-
}
174-
175-
System.Type type;
176-
TypeMaps.TryGetValue(name, out type);
177-
178-
if (type == null)
179-
{
180-
typeMap = null;
181-
return false;
182-
}
183-
184-
typeMap = (TypeMap)Activator.CreateInstance(type);
185-
typeMap.TypeMapDatabase = this;
186-
187-
return true;
171+
return TypeMaps.TryGetValue(name, out typeMap) && typeMap.IsEnabled;
188172
}
189173
}
190174
}

0 commit comments

Comments
 (0)