Browse Source

Fixed the generated C# when an injected class is returned.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>

# Conflicts:
#	src/Generator/Generators/CSharp/CSharpTypePrinter.cs
pull/890/head
Dimitar Dobrev 8 years ago
parent
commit
0e341db99c
  1. 7
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 8
      src/Generator/Generators/CSharp/CSharpSources.cs
  3. 11
      tests/CSharp/CSharp.Tests.cs
  4. 42
      tests/CSharp/CSharpTemplates.h

7
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -373,6 +373,13 @@ namespace CppSharp.Generators.CSharp @@ -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;
}

8
src/Generator/Generators/CSharp/CSharpSources.cs

@ -2273,7 +2273,8 @@ namespace CppSharp.Generators.CSharp @@ -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 @@ -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 @@ -2311,7 +2313,7 @@ namespace CppSharp.Generators.CSharp
}
else
{
GenerateInternalFunctionCall(method);
GenerateInternalFunctionCall(method, returnType: returnType);
}
}
else if (@class.IsValueType)

11
tests/CSharp/CSharp.Tests.cs

@ -777,6 +777,17 @@ public unsafe class CSharpTests : GeneratorTestFixture @@ -777,6 +777,17 @@ public unsafe class CSharpTests : GeneratorTestFixture
}
}
[Test]
public void TestReturnInjectedClass()
{
using (var dependentValueFields = new DependentValueFields<int>())
{
dependentValueFields.DependentValue = 5;
Assert.That(dependentValueFields.ReturnInjectedClass().DependentValue,
Is.EqualTo(dependentValueFields.DependentValue));
}
}
[Test]
public void TestAbstractImplementatonsInPrimaryAndSecondaryBases()
{

42
tests/CSharp/CSharpTemplates.h

@ -97,6 +97,13 @@ class DerivedChangesTypeName : public IndependentFields<X> @@ -97,6 +97,13 @@ class DerivedChangesTypeName : public IndependentFields<X>
{
};
template <typename T>
class DLL_API DependentValueFieldForArray
{
private:
T field;
};
template <typename T>
class DLL_API DependentValueFields
{
@ -104,6 +111,11 @@ public: @@ -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: @@ -111,6 +123,34 @@ private:
};
};
template <typename T>
DependentValueFields<T>::DependentValueFields()
{
}
template <typename T>
DependentValueFields<T>::~DependentValueFields()
{
}
template <typename T>
T DependentValueFields<T>::getDependentValue()
{
return field;
}
template <typename T>
void DependentValueFields<T>::setDependentValue(const T& value)
{
field = value;
}
template <typename T>
DependentValueFields<T>& DependentValueFields<T>::returnInjectedClass()
{
return *this;
}
template <typename T>
class DLL_API DependentPointerFields
{
@ -291,7 +331,7 @@ private: @@ -291,7 +331,7 @@ private:
NestedTemplate<int> nestedTemplate;
DependentValueFields<DependentValueFields<int*>> nestedDependentPointer1;
DependentValueFields<DependentValueFields<char*>> nestedDependentPointer2;
DependentValueFields<char[3]> dependentFieldArray;
DependentValueFieldForArray<char[3]> dependentFieldArray;
void completeSpecializationInParameter(DependentValueFields<float> p1,
DependentValueFields<int*> p2,
DependentValueFields<float*> p3);

Loading…
Cancel
Save