Browse Source

Simplified type maps by using static objects to disable as needed.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1142/head
Dimitar Dobrev 7 years ago
parent
commit
76d8182fe6
  1. 12
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 22
      src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs
  3. 3
      src/Generator/Types/TypeMap.cs
  4. 34
      src/Generator/Types/TypeMapDatabase.cs

12
src/Generator/Generators/CSharp/CSharpSources.cs

@ -381,7 +381,7 @@ namespace CppSharp.Generators.CSharp @@ -381,7 +381,7 @@ namespace CppSharp.Generators.CSharp
var typeMaps = new List<System.Type>();
var keys = new List<string>();
// disable the type maps, if any, for this class because of copy ctors, operators and others
this.DisableTypeMap(@class, typeMaps, keys);
this.DisableTypeMap(@class);
PushBlock(BlockKind.Class);
GenerateDeclarationCommon(@class);
@ -443,8 +443,8 @@ namespace CppSharp.Generators.CSharp @@ -443,8 +443,8 @@ namespace CppSharp.Generators.CSharp
UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock);
for (int i = 0; i < typeMaps.Count; i++)
Context.TypeMaps.TypeMaps.Add(keys[i], typeMaps[i]);
foreach (var typeMap in Context.TypeMaps.TypeMaps.Values)
typeMap.IsEnabled = true;
return true;
}
@ -694,11 +694,11 @@ namespace CppSharp.Generators.CSharp @@ -694,11 +694,11 @@ namespace CppSharp.Generators.CSharp
{
var typeMaps = new List<System.Type>();
var keys = new List<string>();
this.DisableTypeMap(@base.Class, typeMaps, keys);
this.DisableTypeMap(@base.Class);
var printedBase = @base.Type.Desugar().Visit(TypePrinter);
bases.Add(printedBase.Type);
for (int i = 0; i < typeMaps.Count; i++)
Context.TypeMaps.TypeMaps.Add(keys[i], typeMaps[i]);
foreach (var typeMap in Context.TypeMaps.TypeMaps.Values)
typeMap.IsEnabled = true;
}
}

22
src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs

@ -8,14 +8,13 @@ namespace CppSharp.Generators.CSharp @@ -8,14 +8,13 @@ namespace CppSharp.Generators.CSharp
{
public static class CSharpSourcesExtensions
{
public static void DisableTypeMap(this CSharpSources gen, Class @class,
List<System.Type> typeMaps, List<string> keys)
public static void DisableTypeMap(this CSharpSources gen, Class @class)
{
var mapped = @class.OriginalClass ?? @class;
DisableSingleTypeMap(mapped, typeMaps, keys, gen.Context);
DisableSingleTypeMap(mapped, gen.Context);
if (mapped.IsDependent)
foreach (var specialization in mapped.Specializations)
DisableSingleTypeMap(specialization, typeMaps, keys, gen.Context);
DisableSingleTypeMap(specialization, gen.Context);
}
public static void GenerateNativeConstructorsByValue(
@ -93,8 +92,7 @@ namespace CppSharp.Generators.CSharp @@ -93,8 +92,7 @@ namespace CppSharp.Generators.CSharp
}
}
private static void DisableSingleTypeMap(Class mapped,
List<System.Type> typeMaps, List<string> keys, BindingContext context)
private static void DisableSingleTypeMap(Class mapped, BindingContext context)
{
var names = new List<string> { mapped.OriginalName };
foreach (TypePrintScopeKind kind in Enum.GetValues(typeof(TypePrintScopeKind)))
@ -102,16 +100,8 @@ namespace CppSharp.Generators.CSharp @@ -102,16 +100,8 @@ namespace CppSharp.Generators.CSharp
var cppTypePrinter = new CppTypePrinter { PrintScopeKind = kind };
names.Add(mapped.Visit(cppTypePrinter));
}
foreach (var name in names)
{
if (context.TypeMaps.TypeMaps.ContainsKey(name))
{
keys.Add(name);
typeMaps.Add(context.TypeMaps.TypeMaps[name]);
context.TypeMaps.TypeMaps.Remove(name);
break;
}
}
foreach (var name in names.Where(context.TypeMaps.TypeMaps.ContainsKey))
context.TypeMaps.TypeMaps[name].IsEnabled = false;
}
private static void WriteTemplateSpecializationCheck(CSharpSources gen,

3
src/Generator/Types/TypeMap.cs

@ -40,6 +40,8 @@ namespace CppSharp.Types @@ -40,6 +40,8 @@ namespace CppSharp.Types
public Declaration Declaration { get; set; }
public ITypeMapDatabase TypeMapDatabase { get; set; }
public bool IsEnabled { get; set; } = true;
public virtual bool IsIgnored
{
get { return false; }
@ -122,7 +124,6 @@ namespace CppSharp.Types @@ -122,7 +124,6 @@ namespace CppSharp.Types
bool FindTypeMapRecursive(Type type, out TypeMap typeMap);
bool FindTypeMap(Type decl, out TypeMap typeMap);
bool FindTypeMap(Declaration decl, out TypeMap typeMap);
bool FindTypeMap(string name, out TypeMap typeMap);
}

34
src/Generator/Types/TypeMapDatabase.cs

@ -13,11 +13,11 @@ namespace CppSharp.Types @@ -13,11 +13,11 @@ namespace CppSharp.Types
{
public class TypeMapDatabase : ITypeMapDatabase
{
public IDictionary<string, System.Type> TypeMaps { get; set; }
public IDictionary<string, TypeMap> TypeMaps { get; set; }
public TypeMapDatabase()
{
TypeMaps = new Dictionary<string, System.Type>();
TypeMaps = new Dictionary<string, TypeMap>();
}
public void SetupTypeMaps(GeneratorKind generatorKind)
@ -41,14 +41,16 @@ namespace CppSharp.Types @@ -41,14 +41,16 @@ namespace CppSharp.Types
private void SetupTypeMaps(IEnumerable<System.Type> types, GeneratorKind generatorKind)
{
foreach (var typeMap in types)
foreach (var type in types)
{
var attrs = typeMap.GetCustomAttributes(typeof(TypeMapAttribute), true);
var attrs = type.GetCustomAttributes(typeof(TypeMapAttribute), true);
foreach (TypeMapAttribute attr in attrs)
{
if (attr.GeneratorKind == 0 || attr.GeneratorKind == generatorKind)
{
TypeMaps[attr.Type] = typeMap;
var typeMap = (TypeMap) Activator.CreateInstance(type);
typeMap.TypeMapDatabase = this;
this.TypeMaps[attr.Type] = typeMap;
}
}
}
@ -164,27 +166,9 @@ namespace CppSharp.Types @@ -164,27 +166,9 @@ namespace CppSharp.Types
}
}
public bool FindTypeMap(string name, out TypeMap typeMap)
private bool FindTypeMap(string name, out TypeMap typeMap)
{
if (string.IsNullOrWhiteSpace(name))
{
typeMap = null;
return false;
}
System.Type type;
TypeMaps.TryGetValue(name, out type);
if (type == null)
{
typeMap = null;
return false;
}
typeMap = (TypeMap)Activator.CreateInstance(type);
typeMap.TypeMapDatabase = this;
return true;
return TypeMaps.TryGetValue(name, out typeMap) && typeMap.IsEnabled;
}
}
}

Loading…
Cancel
Save