Browse Source

Add option to conditionally generate bindings

pg
josetr 3 years ago
parent
commit
7a3e5e9144
  1. 41
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 2
      src/Generator/Options.cs

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

@ -253,13 +253,21 @@ namespace CppSharp.Generators.CSharp
var classes = EnumerateClasses().ToList(); var classes = EnumerateClasses().ToList();
if (classes.FindAll(cls => cls.IsValueType && cls.Name == parentName && context.QualifiedLogicalName == cls.Namespace.QualifiedLogicalName).Any()) if (classes.FindAll(cls => cls.IsValueType && cls.Name == parentName && context.QualifiedLogicalName == cls.Namespace.QualifiedLogicalName).Any())
keyword = "struct"; keyword = "struct";
if (Options.GenerateTypesOnly && Options.PutAllGlobalsInGlobalClass)
parentName = "Globals";
WriteLine($"public unsafe partial {keyword} {parentName}"); WriteLine($"public unsafe partial {keyword} {parentName}");
WriteOpenBraceAndIndent(); WriteOpenBraceAndIndent();
if (Options.GenerateBindings)
{
PushBlock(BlockKind.InternalsClass); PushBlock(BlockKind.InternalsClass);
GenerateClassInternalHead(new Class { Name = parentName }); GenerateClassInternalHead(new Class { Name = parentName });
WriteOpenBraceAndIndent(); WriteOpenBraceAndIndent();
if (Options.GenerateBindings)
{
// Generate all the internal function declarations. // Generate all the internal function declarations.
foreach (var function in context.Functions) foreach (var function in context.Functions)
{ {
@ -268,6 +276,7 @@ namespace CppSharp.Generators.CSharp
GenerateInternalFunction(function); GenerateInternalFunction(function);
} }
}
UnindentAndWriteCloseBrace(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
@ -278,6 +287,7 @@ namespace CppSharp.Generators.CSharp
GenerateFunction(function, parentName); GenerateFunction(function, parentName);
} }
}
foreach (var variable in context.Variables.Where( foreach (var variable in context.Variables.Where(
v => v.IsGenerated && v.Access == AccessSpecifier.Public)) v => v.IsGenerated && v.Access == AccessSpecifier.Public))
@ -409,7 +419,7 @@ namespace CppSharp.Generators.CSharp
if (!@class.IsGenerated) if (!@class.IsGenerated)
goto exit; goto exit;
if (ShouldGenerateClassNativeField(@class)) if (Options.GenerateBindings && ShouldGenerateClassNativeField(@class))
{ {
PushBlock(BlockKind.Field); PushBlock(BlockKind.Field);
if (@class.IsValueType) if (@class.IsValueType)
@ -483,9 +493,12 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
WriteLine($"private bool __{prop.Field.OriginalName}_OwnsNativeMemory = false;"); WriteLine($"private bool __{prop.Field.OriginalName}_OwnsNativeMemory = false;");
} }
if (Options.GenerateBindings)
{
GenerateClassConstructors(@class); GenerateClassConstructors(@class);
GenerateClassMethods(@class.Methods); GenerateClassMethods(@class.Methods);
}
GenerateClassVariables(@class); GenerateClassVariables(@class);
GenerateClassProperties(@class); GenerateClassProperties(@class);
@ -512,6 +525,9 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
GenerateClassSpecifier(@class); GenerateClassSpecifier(@class);
var shouldInheritFromIDisposable = !@class.HasBase; var shouldInheritFromIDisposable = !@class.HasBase;
if (Options.GenerateTypesOnly)
shouldInheritFromIDisposable = false;
if (shouldInheritFromIDisposable) if (shouldInheritFromIDisposable)
Write(" : IDisposable"); Write(" : IDisposable");
@ -570,6 +586,8 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
public void GenerateClassInternals(Class @class) public void GenerateClassInternals(Class @class)
{ {
if (!Options.GenerateBindings)
return;
var sequentialLayout = Options.GenerateSequentialLayout && CanUseSequentialLayout(@class); var sequentialLayout = Options.GenerateSequentialLayout && CanUseSequentialLayout(@class);
PushBlock(BlockKind.InternalsClass); PushBlock(BlockKind.InternalsClass);
@ -800,7 +818,7 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
} }
} }
if (@class.IsGenerated && isBindingGen && @class.IsRefType && !@class.IsOpaque) if (Options.GenerateBindings && @class.IsGenerated && isBindingGen && @class.IsRefType && !@class.IsOpaque)
{ {
bases.Add("IDisposable"); bases.Add("IDisposable");
} }
@ -1549,13 +1567,26 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
if (prop.IsVirtual && !isOverride && !prop.IsPure) if (prop.IsVirtual && !isOverride && !prop.IsPure)
Write("virtual "); Write("virtual ");
WriteLine($"{printedType} {GetPropertyName(prop)}"); Write($"{printedType} {GetPropertyName(prop)}");
} }
else else
{ {
WriteLine($@"{printedType} { Write($@"{printedType} {
prop.ExplicitInterfaceImpl.Name}.{GetPropertyName(prop)}"); prop.ExplicitInterfaceImpl.Name}.{GetPropertyName(prop)}");
} }
if (Options.GenerateTypesOnly)
{
if (prop.HasSetter)
Write(@$" {{ get; set; }}");
else
Write(@$" {{ get; }}");
NewLine();
PopBlock(NewLineKind.Never);
continue;
}
WriteLine(String.Empty);
WriteOpenBraceAndIndent(); WriteOpenBraceAndIndent();
if (prop.Field != null) if (prop.Field != null)

2
src/Generator/Options.cs

@ -93,6 +93,8 @@ namespace CppSharp
/// <c>true</c> to generate class templates; otherwise, <c>false</c>. /// <c>true</c> to generate class templates; otherwise, <c>false</c>.
/// </value> /// </value>
public bool GenerateClassTemplates { get; set; } public bool GenerateClassTemplates { get; set; }
public bool GenerateTypesOnly { get; set; } = false;
internal bool GenerateBindings => !GenerateTypesOnly;
public bool AllowRenaming { get; set; } = true; public bool AllowRenaming { get; set; } = true;
public bool PutAllGlobalsInGlobalClass { get; set; } = false; public bool PutAllGlobalsInGlobalClass { get; set; } = false;
public bool GenerateInternalImports; public bool GenerateInternalImports;

Loading…
Cancel
Save