Skip to content

Commit cd76498

Browse files
committed
Minor refactoring in ctor generation for CLI sources.
1 parent 3ff9fb2 commit cd76498

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

src/Generator/Generators/CLI/CLISources.cs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,9 @@ private void GenerateClassConstructors(Class @class)
184184
if (@class.IsStatic)
185185
return;
186186

187-
// Output a default constructor that takes the native pointer.
187+
// Output the default constructors taking the native pointer and ownership info.
188188
GenerateClassConstructor(@class);
189+
GenerateClassConstructor(@class, true);
189190

190191
if (@class.IsRefType)
191192
{
@@ -630,7 +631,7 @@ private void GenerateClassConstructor(Class @class, bool withOwnNativeInstancePa
630631
Write("{0}::{1}(", qualifiedIdentifier, @class.Name);
631632

632633
var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName);
633-
WriteLine(!withOwnNativeInstanceParam ? "{0} native)" : "{0} native, const bool ownNativeInstance)", nativeType);
634+
WriteLine(!withOwnNativeInstanceParam ? "{0} native)" : "{0} native, bool ownNativeInstance)", nativeType);
634635

635636
var hasBase = GenerateClassConstructorBase(@class, null, withOwnNativeInstanceParam);
636637

@@ -677,11 +678,6 @@ private void GenerateClassConstructor(Class @class, bool withOwnNativeInstancePa
677678

678679
UnindentAndWriteCloseBrace();
679680
NewLine();
680-
681-
if (!withOwnNativeInstanceParam)
682-
{
683-
GenerateClassConstructor(@class, true);
684-
}
685681
}
686682

687683
private void GenerateStructMarshaling(Class @class, string nativeVar)
@@ -719,29 +715,29 @@ private void GenerateStructMarshaling(Class @class, string nativeVar)
719715
}
720716
}
721717

722-
private bool GenerateClassConstructorBase(Class @class, Method method = null, bool withOwnNativeInstanceParam = false)
718+
private bool GenerateClassConstructorBase(Class @class, Method method = null,
719+
bool withOwnNativeInstanceParam = false)
723720
{
724-
var hasBase = @class.HasBase && @class.Bases[0].IsClass && @class.Bases[0].Class.IsGenerated;
725-
if (!hasBase)
721+
if (!@class.NeedsBase)
726722
return false;
727723

728-
if (!@class.IsValueType)
729-
{
730-
Indent();
724+
if (@class.IsValueType)
725+
return true;
731726

732-
var baseClass = @class.Bases[0].Class;
733-
Write(": {0}(", QualifiedIdentifier(baseClass));
727+
Indent();
734728

735-
// We cast the value to the base clas type since otherwise there
736-
// could be ambiguous call to overloaded constructors.
737-
var cppTypePrinter = new CppTypePrinter(Context);
738-
var nativeTypeName = baseClass.Visit(cppTypePrinter);
739-
Write("({0}*)", nativeTypeName);
729+
Write($": {QualifiedIdentifier(@class.BaseClass)}(");
740730

741-
WriteLine("{0}{1})", method != null ? "nullptr" : "native", !withOwnNativeInstanceParam ? "" : ", ownNativeInstance");
731+
// We cast the value to the base class type since otherwise there
732+
// could be ambiguous call to overloaded constructors.
733+
var cppTypePrinter = new CppTypePrinter(Context);
734+
var nativeTypeName = @class.BaseClass.Visit(cppTypePrinter);
742735

743-
Unindent();
744-
}
736+
Write($"({nativeTypeName}*)");
737+
WriteLine("{0}{1})", method != null ? "nullptr" : "native",
738+
!withOwnNativeInstanceParam ? "" : ", ownNativeInstance");
739+
740+
Unindent();
745741

746742
return true;
747743
}

0 commit comments

Comments
 (0)