From 0e341db99cb10fc9c4da20337822448255ac3c9c Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Thu, 3 Aug 2017 00:12:19 +0300 Subject: [PATCH] Fixed the generated C# when an injected class is returned. Signed-off-by: Dimitar Dobrev # Conflicts: # src/Generator/Generators/CSharp/CSharpTypePrinter.cs --- .../Generators/CSharp/CSharpMarshal.cs | 7 ++++ .../Generators/CSharp/CSharpSources.cs | 8 ++-- tests/CSharp/CSharp.Tests.cs | 11 +++++ tests/CSharp/CSharpTemplates.h | 42 ++++++++++++++++++- 4 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 7664af1a..89e780ce 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -373,6 +373,13 @@ namespace CppSharp.Generators.CSharp Context.Before.WriteLine("else {0} = {1}.{2}({3});", ret, qualifiedClass, Helpers.CreateInstanceIdentifier, Context.ReturnVarName); } + Class returnedClass; + var pointee = Context.ReturnType.Type.Desugar().GetFinalPointee().Desugar(); + if (pointee.TryGetClass(out returnedClass) && returnedClass.IsDependent) + { + var result = returnedClass.Visit(typePrinter); + return $"{ret} as {result}{result.NameSuffix}"; + } return ret; } diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index e93332bf..6b348430 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -2273,7 +2273,8 @@ namespace CppSharp.Generators.CSharp method.IsConstructor; this.GenerateMember(@class, c => GenerateMethodBody( c is ClassTemplateSpecialization ? - c.Methods.First(m => m.InstantiatedFrom == method) : method), isVoid); + c.Methods.First(m => m.InstantiatedFrom == method) : method, + method.OriginalReturnType), isVoid); } SkipImpl: @@ -2288,7 +2289,8 @@ namespace CppSharp.Generators.CSharp PopBlock(NewLineKind.BeforeNextBlock); } - private void GenerateMethodBody(Method method) + private void GenerateMethodBody(Method method, + QualifiedType returnType = default(QualifiedType)) { var @class = (Class) method.Namespace; if (@class.IsRefType) @@ -2311,7 +2313,7 @@ namespace CppSharp.Generators.CSharp } else { - GenerateInternalFunctionCall(method); + GenerateInternalFunctionCall(method, returnType: returnType); } } else if (@class.IsValueType) diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index cccfb5f1..02e678e9 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -777,6 +777,17 @@ public unsafe class CSharpTests : GeneratorTestFixture } } + [Test] + public void TestReturnInjectedClass() + { + using (var dependentValueFields = new DependentValueFields()) + { + dependentValueFields.DependentValue = 5; + Assert.That(dependentValueFields.ReturnInjectedClass().DependentValue, + Is.EqualTo(dependentValueFields.DependentValue)); + } + } + [Test] public void TestAbstractImplementatonsInPrimaryAndSecondaryBases() { diff --git a/tests/CSharp/CSharpTemplates.h b/tests/CSharp/CSharpTemplates.h index e9aa4d17..31c0ad61 100644 --- a/tests/CSharp/CSharpTemplates.h +++ b/tests/CSharp/CSharpTemplates.h @@ -97,6 +97,13 @@ class DerivedChangesTypeName : public IndependentFields { }; +template +class DLL_API DependentValueFieldForArray +{ +private: + T field; +}; + template class DLL_API DependentValueFields { @@ -104,6 +111,11 @@ public: class Nested { }; + DependentValueFields(); + ~DependentValueFields(); + DependentValueFields& returnInjectedClass(); + T getDependentValue(); + void setDependentValue(const T& value); private: T field; union { @@ -111,6 +123,34 @@ private: }; }; +template +DependentValueFields::DependentValueFields() +{ +} + +template +DependentValueFields::~DependentValueFields() +{ +} + +template +T DependentValueFields::getDependentValue() +{ + return field; +} + +template +void DependentValueFields::setDependentValue(const T& value) +{ + field = value; +} + +template +DependentValueFields& DependentValueFields::returnInjectedClass() +{ + return *this; +} + template class DLL_API DependentPointerFields { @@ -291,7 +331,7 @@ private: NestedTemplate nestedTemplate; DependentValueFields> nestedDependentPointer1; DependentValueFields> nestedDependentPointer2; - DependentValueFields dependentFieldArray; + DependentValueFieldForArray dependentFieldArray; void completeSpecializationInParameter(DependentValueFields p1, DependentValueFields p2, DependentValueFields p3);