Browse Source

Do not generate the destructor/finalizer pair if the destructor of the native class is protected.

Had to fix line endings (LF) in the CLI* files.
pull/237/head
Elias Holzer 11 years ago
parent
commit
6145bf600b
  1. 13
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs
  2. 13
      src/Generator/Generators/CLI/CLISourcesTemplate.cs
  3. 6
      tests/CLITemp/CLITemp.cs
  4. 9
      tests/CLITemp/CLITemp.h

13
src/Generator/Generators/CLI/CLIHeadersTemplate.cs

@ -382,20 +382,22 @@ namespace CppSharp.Generators.CLI @@ -382,20 +382,22 @@ namespace CppSharp.Generators.CLI
GenerateMethod(ctor);
}
if (@class.IsRefType)
if (Options.GenerateFinalizers && @class.IsRefType)
{
var destructor = @class.Destructors
.FirstOrDefault(d => d.Parameters.Count == 0 && d.Access == AccessSpecifier.Public);
if (destructor != null)
{
GenerateClassDestructor(@class);
GenerateClassFinalizer(@class);
}
}
PopIndent();
}
private void GenerateClassDestructor(Class @class)
{
if (!Options.GenerateFinalizers)
return;
PushBlock(CLIBlockKind.Destructor);
WriteLine("~{0}();", @class.Name);
PopBlock(NewLineKind.BeforeNextBlock);
@ -403,9 +405,6 @@ namespace CppSharp.Generators.CLI @@ -403,9 +405,6 @@ namespace CppSharp.Generators.CLI
private void GenerateClassFinalizer(Class @class)
{
if (!Options.GenerateFinalizers)
return;
PushBlock(CLIBlockKind.Finalizer);
WriteLine("!{0}();", @class.Name);
PopBlock(NewLineKind.BeforeNextBlock);

13
src/Generator/Generators/CLI/CLISourcesTemplate.cs

@ -205,12 +205,17 @@ namespace CppSharp.Generators.CLI @@ -205,12 +205,17 @@ namespace CppSharp.Generators.CLI
GenerateClassConstructor(@class, isIntPtr: false);
GenerateClassConstructor(@class, isIntPtr: true);
if (@class.IsRefType)
if (Options.GenerateFinalizers && @class.IsRefType)
{
var destructor = @class.Destructors
.FirstOrDefault(d => d.Parameters.Count == 0 && d.Access == AccessSpecifier.Public);
if (destructor != null)
{
GenerateClassDestructor(@class);
GenerateClassFinalizer(@class);
}
}
}
private void GenerateClassProperties(Class @class, Class realOwner)
{
@ -229,9 +234,6 @@ namespace CppSharp.Generators.CLI @@ -229,9 +234,6 @@ namespace CppSharp.Generators.CLI
private void GenerateClassDestructor(Class @class)
{
if (!Options.GenerateFinalizers)
return;
PushBlock(CLIBlockKind.Destructor);
WriteLine("{0}::~{1}()", QualifiedIdentifier(@class), @class.Name);
@ -247,9 +249,6 @@ namespace CppSharp.Generators.CLI @@ -247,9 +249,6 @@ namespace CppSharp.Generators.CLI
private void GenerateClassFinalizer(Class @class)
{
if (!Options.GenerateFinalizers)
return;
PushBlock(CLIBlockKind.Finalizer);
WriteLine("{0}::!{1}()", QualifiedIdentifier(@class), @class.Name);

6
tests/CLITemp/CLITemp.cs

@ -11,6 +11,12 @@ namespace CppSharp.Tests @@ -11,6 +11,12 @@ namespace CppSharp.Tests
{
}
public override void Setup(Driver driver)
{
driver.Options.GenerateFinalizers = true;
base.Setup(driver);
}
public override void Preprocess(Driver driver, ASTContext ctx)
{
}

9
tests/CLITemp/CLITemp.h

@ -15,3 +15,12 @@ struct DLL_API Types @@ -15,3 +15,12 @@ struct DLL_API Types
typedef int AttributedFuncType(int, int) ATTR;
AttributedFuncType AttributedSum;
};
// Tests code generator to not generate a destructor/finalizer pair
// if the destructor of the C++ class is not public.
class DLL_API TestProtectedDestructors
{
~TestProtectedDestructors();
};
TestProtectedDestructors::~TestProtectedDestructors() {}
Loading…
Cancel
Save