Browse Source

Add blocks for ctor, dtor, and finalizer bodies. Add constructor that takes a bool from the caller to indicate if the callee should own the pointer passed to it or not

pull/1320/head
Build Agent 6 years ago committed by João Matos
parent
commit
010099021c
  1. 1
      src/Generator/Generators/CLI/CLIHeaders.cs
  2. 10
      src/Generator/Generators/CLI/CLIMarshal.cs
  3. 48
      src/Generator/Generators/CLI/CLISources.cs
  4. 3
      src/Generator/Utils/BlockGenerator.cs

1
src/Generator/Generators/CLI/CLIHeaders.cs

@ -376,6 +376,7 @@ namespace CppSharp.Generators.CLI @@ -376,6 +376,7 @@ namespace CppSharp.Generators.CLI
// Output a default constructor that takes the native pointer.
WriteLine("{0}({1} native);", @class.Name, nativeType);
WriteLine("{0}({1} native, bool ownNativeInstance);", @class.Name, nativeType);
WriteLine("static {0}^ {1}(::System::IntPtr native);",
@class.Name, Helpers.CreateInstanceIdentifier);

10
src/Generator/Generators/CLI/CLIMarshal.cs

@ -275,15 +275,19 @@ namespace CppSharp.Generators.CLI @@ -275,15 +275,19 @@ namespace CppSharp.Generators.CLI
instance += Context.ReturnVarName;
var needsCopy = Context.MarshalKind != MarshalKind.NativeField;
bool ownNativeInstance = false;
if (@class.IsRefType && needsCopy)
{
var name = Generator.GeneratedIdentifier(Context.ReturnVarName);
Context.Before.WriteLine("auto {0} = new ::{1}({2});", name,
@class.QualifiedOriginalName, Context.ReturnVarName);
instance = name;
ownNativeInstance = true;
}
WriteClassInstance(@class, instance);
WriteClassInstance(@class, instance, ownNativeInstance);
return true;
}
@ -295,7 +299,7 @@ namespace CppSharp.Generators.CLI @@ -295,7 +299,7 @@ namespace CppSharp.Generators.CLI
return decl.QualifiedName;
}
public void WriteClassInstance(Class @class, string instance)
public void WriteClassInstance(Class @class, string instance, bool ownNativeInstance = false)
{
if (@class.IsRefType)
Context.Return.Write("({0} == nullptr) ? nullptr : gcnew ",
@ -303,7 +307,7 @@ namespace CppSharp.Generators.CLI @@ -303,7 +307,7 @@ namespace CppSharp.Generators.CLI
Context.Return.Write("{0}(", QualifiedIdentifier(@class));
Context.Return.Write("(::{0}*)", @class.QualifiedOriginalName);
Context.Return.Write("{0})", instance);
Context.Return.Write("{0}{1})", instance, ownNativeInstance ? ", true" : "");
}
public override bool VisitFieldDecl(Field field)

48
src/Generator/Generators/CLI/CLISources.cs

@ -250,6 +250,8 @@ namespace CppSharp.Generators.CLI @@ -250,6 +250,8 @@ namespace CppSharp.Generators.CLI
WriteLine("{0}::~{1}()", QualifiedIdentifier(@class), @class.Name);
WriteOpenBraceAndIndent();
PushBlock(BlockKind.DestructorBody, @class);
if (CLIGenerator.ShouldGenerateClassNativeField(@class))
{
WriteLine("delete NativePtr;");
@ -264,6 +266,8 @@ namespace CppSharp.Generators.CLI @@ -264,6 +266,8 @@ namespace CppSharp.Generators.CLI
UnindentAndWriteCloseBrace();
}
PopBlock();
UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock);
@ -276,9 +280,13 @@ namespace CppSharp.Generators.CLI @@ -276,9 +280,13 @@ namespace CppSharp.Generators.CLI
WriteLine("{0}::!{1}()", QualifiedIdentifier(@class), @class.Name);
WriteOpenBraceAndIndent();
PushBlock(BlockKind.FinalizerBody, @class);
if (CLIGenerator.ShouldGenerateClassNativeField(@class))
WriteLine("delete NativePtr;");
PopBlock();
UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock);
@ -618,16 +626,16 @@ namespace CppSharp.Generators.CLI @@ -618,16 +626,16 @@ namespace CppSharp.Generators.CLI
GeneratePropertySetter(variable, @class, variable.Name, variable.Type);
}
private void GenerateClassConstructor(Class @class)
private void GenerateClassConstructor(Class @class, bool withOwnNativeInstanceParam = false)
{
string qualifiedIdentifier = QualifiedIdentifier(@class);
Write("{0}::{1}(", qualifiedIdentifier, @class.Name);
var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName);
WriteLine("{0} native)", nativeType);
WriteLine(!withOwnNativeInstanceParam ? "{0} native)" : "{0} native, const bool ownNativeInstance)", nativeType);
var hasBase = GenerateClassConstructorBase(@class);
var hasBase = GenerateClassConstructorBase(@class, null, withOwnNativeInstanceParam);
if (CLIGenerator.ShouldGenerateClassNativeField(@class))
{
@ -635,11 +643,13 @@ namespace CppSharp.Generators.CLI @@ -635,11 +643,13 @@ namespace CppSharp.Generators.CLI
Write(hasBase ? "," : ":");
Unindent();
WriteLine(" {0}(false)", Helpers.OwnsNativeInstanceIdentifier);
WriteLine(!withOwnNativeInstanceParam ? " {0}(false)" : " {0}(ownNativeInstance)", Helpers.OwnsNativeInstanceIdentifier);
}
WriteOpenBraceAndIndent();
PushBlock(BlockKind.ConstructorBody, @class);
const string nativePtr = "native";
if (@class.IsRefType)
@ -654,16 +664,24 @@ namespace CppSharp.Generators.CLI @@ -654,16 +664,24 @@ namespace CppSharp.Generators.CLI
GenerateStructMarshaling(@class, nativePtr + "->");
}
PopBlock();
UnindentAndWriteCloseBrace();
NewLine();
WriteLine("{0}^ {0}::{1}(::System::IntPtr native)", qualifiedIdentifier, Helpers.CreateInstanceIdentifier);
WriteOpenBraceAndIndent();
if (!withOwnNativeInstanceParam)
{
NewLine();
WriteLine("{0}^ {0}::{1}(::System::IntPtr native)", qualifiedIdentifier, Helpers.CreateInstanceIdentifier);
WriteLine("return gcnew ::{0}(({1}) native.ToPointer());", qualifiedIdentifier, nativeType);
WriteOpenBraceAndIndent();
UnindentAndWriteCloseBrace();
NewLine();
WriteLine("return gcnew ::{0}(({1}) native.ToPointer());", qualifiedIdentifier, nativeType);
UnindentAndWriteCloseBrace();
NewLine();
GenerateClassConstructor(@class, true);
}
}
private void GenerateStructMarshaling(Class @class, string nativeVar)
@ -701,7 +719,7 @@ namespace CppSharp.Generators.CLI @@ -701,7 +719,7 @@ namespace CppSharp.Generators.CLI
}
}
private bool GenerateClassConstructorBase(Class @class, Method method = null)
private bool GenerateClassConstructorBase(Class @class, Method method = null, bool withOwnNativeInstanceParam = false)
{
var hasBase = @class.HasBase && @class.Bases[0].IsClass && @class.Bases[0].Class.IsGenerated;
if (!hasBase)
@ -720,7 +738,7 @@ namespace CppSharp.Generators.CLI @@ -720,7 +738,7 @@ namespace CppSharp.Generators.CLI
var nativeTypeName = baseClass.Visit(cppTypePrinter);
Write("({0}*)", nativeTypeName);
WriteLine("{0})", method != null ? "nullptr" : "native");
WriteLine("{0}{1})", method != null ? "nullptr" : "native", !withOwnNativeInstanceParam ? "" : ", ownNativeInstance");
Unindent();
}
@ -754,7 +772,11 @@ namespace CppSharp.Generators.CLI @@ -754,7 +772,11 @@ namespace CppSharp.Generators.CLI
PushBlock(BlockKind.MethodBody, method);
if (method.IsConstructor && @class.IsRefType)
{
PushBlock(BlockKind.ConstructorBody, @class);
WriteLine("{0} = true;", Helpers.OwnsNativeInstanceIdentifier);
}
if (method.IsProxy)
goto SkipImpl;
@ -770,6 +792,8 @@ namespace CppSharp.Generators.CLI @@ -770,6 +792,8 @@ namespace CppSharp.Generators.CLI
GenerateFunctionParams(@params);
WriteLine(");");
}
PopBlock();
}
else
{

3
src/Generator/Utils/BlockGenerator.cs

@ -50,6 +50,9 @@ namespace CppSharp @@ -50,6 +50,9 @@ namespace CppSharp
Destructor,
AccessSpecifier,
Fields,
ConstructorBody,
DestructorBody,
FinalizerBody
}
[DebuggerDisplay("{Kind} | {Object}")]

Loading…
Cancel
Save