Browse Source

Added better support for destructors.

Also changed the native identifier mangling scheme to add some shorthand for the type of special method. This makes it simpler to read through the generated binding code.

Hopefully fixes #142.
pull/146/merge
triton 12 years ago
parent
commit
3fd94bf482
  1. 8
      src/AST/Class.cs
  2. 29
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 11
      tests/CSharpTemp/CSharpTemp.Tests.cs
  4. 2
      tests/CSharpTemp/CSharpTemp.cpp
  5. 9
      tests/CSharpTemp/CSharpTemp.h

8
src/AST/Class.cs

@ -166,6 +166,14 @@ namespace CppSharp.AST @@ -166,6 +166,14 @@ namespace CppSharp.AST
}
}
public IEnumerable<Method> Destructors
{
get
{
return Methods.Where(method => method.IsDestructor);
}
}
public IEnumerable<Method> Operators
{
get

29
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -526,6 +526,11 @@ namespace CppSharp.Generators.CSharp @@ -526,6 +526,11 @@ namespace CppSharp.Generators.CSharp
tryAddOverload(ctor);
}
foreach (var dtor in @class.Destructors)
{
tryAddOverload(dtor);
}
foreach (var method in @class.Methods)
{
if (ASTUtils.CheckIgnoreMethod(method))
@ -1637,7 +1642,14 @@ namespace CppSharp.Generators.CSharp @@ -1637,7 +1642,14 @@ namespace CppSharp.Generators.CSharp
WriteStartBraceIndent();
if (ShouldGenerateClassNativeField(@class))
{
var dtor = @class.Methods.FirstOrDefault(method => method.IsDestructor);
if (dtor != null)
WriteLine("Internal.{0}({1});", GetFunctionNativeIdentifier(dtor),
Helpers.InstanceIdentifier);
WriteLine("Marshal.FreeHGlobal({0});", Helpers.InstanceIdentifier);
}
if (hasBaseClass)
WriteLine("base.Dispose(disposing);");
@ -2358,7 +2370,22 @@ namespace CppSharp.Generators.CSharp @@ -2358,7 +2370,22 @@ namespace CppSharp.Generators.CSharp
public static string GetFunctionNativeIdentifier(Function function)
{
var identifier = SafeIdentifier(function.Name);
var functionName = function.Name;
var method = function as Method;
if (method != null)
{
if (method.IsConstructor)
functionName = "ctor_";
else if (method.IsCopyConstructor)
functionName = "cctor_";
else if (method.IsDestructor)
functionName = "dtor_";
functionName += GetMethodIdentifier(method);
}
var identifier = SafeIdentifier(functionName);
if (function.IsOperator)
identifier = "Operator" + function.OperatorKind;

11
tests/CSharpTemp/CSharpTemp.Tests.cs

@ -85,4 +85,15 @@ public class CSharpTempTests @@ -85,4 +85,15 @@ public class CSharpTempTests
.GetCustomAttributes(typeof(ObsoleteAttribute), false).Length,
Is.GreaterThan(0));
}
[Test]
public void TestDestructors()
{
Assert.AreEqual(0, CSharpTemp.TestDestructors.Marker);
var dtors = new TestDestructors();
Assert.AreEqual(0xf00d, CSharpTemp.TestDestructors.Marker);
dtors.Dispose();
Assert.AreEqual(0xcafe, CSharpTemp.TestDestructors.Marker);
}
}

2
tests/CSharpTemp/CSharpTemp.cpp

@ -189,3 +189,5 @@ void P::setIsBool(bool value) @@ -189,3 +189,5 @@ void P::setIsBool(bool value)
{
}
int TestDestructors::Marker = 0;

9
tests/CSharpTemp/CSharpTemp.h

@ -125,3 +125,12 @@ public: @@ -125,3 +125,12 @@ public:
private:
ComplexType m_complexType;
};
// Tests destructors
struct DLL_API TestDestructors
{
static int Marker;
TestDestructors() { Marker = 0xf00d; }
~TestDestructors() { Marker = 0xcafe; }
};

Loading…
Cancel
Save