@@ -250,6 +250,8 @@ private void GenerateClassDestructor(Class @class)
250
250
WriteLine ( "{0}::~{1}()" , QualifiedIdentifier ( @class ) , @class . Name ) ;
251
251
WriteOpenBraceAndIndent ( ) ;
252
252
253
+ PushBlock ( BlockKind . DestructorBody , @class ) ;
254
+
253
255
if ( CLIGenerator . ShouldGenerateClassNativeField ( @class ) )
254
256
{
255
257
WriteLine ( "delete NativePtr;" ) ;
@@ -264,6 +266,8 @@ private void GenerateClassDestructor(Class @class)
264
266
UnindentAndWriteCloseBrace ( ) ;
265
267
}
266
268
269
+ PopBlock ( ) ;
270
+
267
271
UnindentAndWriteCloseBrace ( ) ;
268
272
269
273
PopBlock ( NewLineKind . BeforeNextBlock ) ;
@@ -276,9 +280,13 @@ private void GenerateClassFinalizer(Class @class)
276
280
WriteLine ( "{0}::!{1}()" , QualifiedIdentifier ( @class ) , @class . Name ) ;
277
281
WriteOpenBraceAndIndent ( ) ;
278
282
283
+ PushBlock ( BlockKind . FinalizerBody , @class ) ;
284
+
279
285
if ( CLIGenerator . ShouldGenerateClassNativeField ( @class ) )
280
286
WriteLine ( "delete NativePtr;" ) ;
281
287
288
+ PopBlock ( ) ;
289
+
282
290
UnindentAndWriteCloseBrace ( ) ;
283
291
284
292
PopBlock ( NewLineKind . BeforeNextBlock ) ;
@@ -618,28 +626,30 @@ private void GenerateVariable(Variable variable, Class @class)
618
626
GeneratePropertySetter ( variable , @class , variable . Name , variable . Type ) ;
619
627
}
620
628
621
- private void GenerateClassConstructor ( Class @class )
629
+ private void GenerateClassConstructor ( Class @class , bool withOwnNativeInstanceParam = false )
622
630
{
623
631
string qualifiedIdentifier = QualifiedIdentifier ( @class ) ;
624
632
625
633
Write ( "{0}::{1}(" , qualifiedIdentifier , @class . Name ) ;
626
634
627
635
var nativeType = string . Format ( "::{0}*" , @class . QualifiedOriginalName ) ;
628
- WriteLine ( "{0} native)" , nativeType ) ;
636
+ WriteLine ( ! withOwnNativeInstanceParam ? "{0} native)" : "{0} native, const bool ownNativeInstance )", nativeType ) ;
629
637
630
- var hasBase = GenerateClassConstructorBase ( @class ) ;
638
+ var hasBase = GenerateClassConstructorBase ( @class , null , withOwnNativeInstanceParam ) ;
631
639
632
640
if ( CLIGenerator . ShouldGenerateClassNativeField ( @class ) )
633
641
{
634
642
Indent ( ) ;
635
643
Write ( hasBase ? "," : ":" ) ;
636
644
Unindent ( ) ;
637
645
638
- WriteLine ( " {0}(false)" , Helpers . OwnsNativeInstanceIdentifier ) ;
646
+ WriteLine ( ! withOwnNativeInstanceParam ? " {0}(false)" : " {0}(ownNativeInstance )", Helpers . OwnsNativeInstanceIdentifier ) ;
639
647
}
640
648
641
649
WriteOpenBraceAndIndent ( ) ;
642
650
651
+ PushBlock ( BlockKind . ConstructorBody , @class ) ;
652
+
643
653
const string nativePtr = "native" ;
644
654
645
655
if ( @class . IsRefType )
@@ -654,16 +664,24 @@ private void GenerateClassConstructor(Class @class)
654
664
GenerateStructMarshaling ( @class , nativePtr + "->" ) ;
655
665
}
656
666
667
+ PopBlock ( ) ;
668
+
657
669
UnindentAndWriteCloseBrace ( ) ;
658
- NewLine ( ) ;
659
- WriteLine ( "{0}^ {0}::{1}(::System::IntPtr native)" , qualifiedIdentifier , Helpers . CreateInstanceIdentifier ) ;
660
670
661
- WriteOpenBraceAndIndent ( ) ;
671
+ if ( ! withOwnNativeInstanceParam )
672
+ {
673
+ NewLine ( ) ;
674
+ WriteLine ( "{0}^ {0}::{1}(::System::IntPtr native)" , qualifiedIdentifier , Helpers . CreateInstanceIdentifier ) ;
662
675
663
- WriteLine ( "return gcnew ::{0}(({1}) native.ToPointer());" , qualifiedIdentifier , nativeType ) ;
676
+ WriteOpenBraceAndIndent ( ) ;
664
677
665
- UnindentAndWriteCloseBrace ( ) ;
666
- NewLine ( ) ;
678
+ WriteLine ( "return gcnew ::{0}(({1}) native.ToPointer());" , qualifiedIdentifier , nativeType ) ;
679
+
680
+ UnindentAndWriteCloseBrace ( ) ;
681
+ NewLine ( ) ;
682
+
683
+ GenerateClassConstructor ( @class , true ) ;
684
+ }
667
685
}
668
686
669
687
private void GenerateStructMarshaling ( Class @class , string nativeVar )
@@ -701,7 +719,7 @@ private void GenerateStructMarshaling(Class @class, string nativeVar)
701
719
}
702
720
}
703
721
704
- private bool GenerateClassConstructorBase ( Class @class , Method method = null )
722
+ private bool GenerateClassConstructorBase ( Class @class , Method method = null , bool withOwnNativeInstanceParam = false )
705
723
{
706
724
var hasBase = @class . HasBase && @class . Bases [ 0 ] . IsClass && @class . Bases [ 0 ] . Class . IsGenerated ;
707
725
if ( ! hasBase )
@@ -720,7 +738,7 @@ private bool GenerateClassConstructorBase(Class @class, Method method = null)
720
738
var nativeTypeName = baseClass . Visit ( cppTypePrinter ) ;
721
739
Write ( "({0}*)" , nativeTypeName ) ;
722
740
723
- WriteLine ( "{0})" , method != null ? "nullptr" : "native" ) ;
741
+ WriteLine ( "{0}{1} )" , method != null ? "nullptr" : "native" , ! withOwnNativeInstanceParam ? "" : ", ownNativeInstance ") ;
724
742
725
743
Unindent ( ) ;
726
744
}
@@ -754,7 +772,11 @@ public void GenerateMethod(Method method, Class @class)
754
772
PushBlock ( BlockKind . MethodBody , method ) ;
755
773
756
774
if ( method . IsConstructor && @class . IsRefType )
775
+ {
776
+ PushBlock ( BlockKind . ConstructorBody , @class ) ;
777
+
757
778
WriteLine ( "{0} = true;" , Helpers . OwnsNativeInstanceIdentifier ) ;
779
+ }
758
780
759
781
if ( method . IsProxy )
760
782
goto SkipImpl ;
@@ -770,6 +792,8 @@ public void GenerateMethod(Method method, Class @class)
770
792
GenerateFunctionParams ( @params ) ;
771
793
WriteLine ( ");" ) ;
772
794
}
795
+
796
+ PopBlock ( ) ;
773
797
}
774
798
else
775
799
{
0 commit comments