From db30cde06d87badab8ee19d01f845cf0dacd2d46 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Sun, 17 Aug 2014 22:29:23 +0300 Subject: [PATCH] Handled indirect return params in type maps. Signed-off-by: Dimitar Dobrev --- .../Generators/CSharp/CSharpTextTemplate.cs | 24 +++++++++++++++++-- src/Generator/Types/TypeMap.cs | 9 +++++++ tests/CSharpTemp/CSharpTemp.Tests.cs | 6 +++++ tests/CSharpTemp/CSharpTemp.cpp | 19 +++++++++++++-- tests/CSharpTemp/CSharpTemp.cs | 5 ++++ tests/CSharpTemp/CSharpTemp.h | 18 +++++++++++--- 6 files changed, 74 insertions(+), 7 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index c08924a6..48ecca97 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -2262,8 +2262,28 @@ namespace CppSharp.Generators.CSharp Class retClass; indirectRetType.Type.Desugar().TryGetClass(out retClass); - WriteLine("var {0} = new {1}.Internal();", GeneratedIdentifier("ret"), - QualifiedIdentifier(retClass.OriginalClass ?? retClass)); + TypeMap typeMap; + string construct = null; + if (Driver.TypeDatabase.FindTypeMap(retClass, out typeMap)) + construct = typeMap.CSharpConstruct(); + + if (construct == null) + { + WriteLine("var {0} = new {1}.Internal();", GeneratedIdentifier("ret"), + QualifiedIdentifier(retClass.OriginalClass ?? retClass)); + } + else + { + if (string.IsNullOrWhiteSpace(construct)) + WriteLine("{0} {1};", + typeMap.CSharpSignature(new CSharpTypePrinterContext + { + Type = indirectRetType.Type.Desugar() + }), + GeneratedIdentifier("ret")); + else + WriteLine("var {0} = {1};", construct); + } } var names = new List(); diff --git a/src/Generator/Types/TypeMap.cs b/src/Generator/Types/TypeMap.cs index 74dd080d..d4574978 100644 --- a/src/Generator/Types/TypeMap.cs +++ b/src/Generator/Types/TypeMap.cs @@ -70,6 +70,15 @@ namespace CppSharp.Types } + /// + /// Used to construct a new instance of the mapped type. + /// + /// + public virtual string CSharpConstruct() + { + return null; + } + #endregion #region C++/CLI backend diff --git a/tests/CSharpTemp/CSharpTemp.Tests.cs b/tests/CSharpTemp/CSharpTemp.Tests.cs index 928c0c55..d818affb 100644 --- a/tests/CSharpTemp/CSharpTemp.Tests.cs +++ b/tests/CSharpTemp/CSharpTemp.Tests.cs @@ -150,4 +150,10 @@ public class CSharpTempTests : GeneratorTestFixture { new HasPrivateOverride().PrivateOverride(); } + + [Test] + public void TestQFlags() + { + Assert.AreEqual(TestFlag.Flag2, new ComplexType().ReturnsQFlags); + } } \ No newline at end of file diff --git a/tests/CSharpTemp/CSharpTemp.cpp b/tests/CSharpTemp/CSharpTemp.cpp index 74c8cc20..f2c1e3f2 100644 --- a/tests/CSharpTemp/CSharpTemp.cpp +++ b/tests/CSharpTemp/CSharpTemp.cpp @@ -130,14 +130,29 @@ long P::prop() return m_property + 100; } +template +QFlags::QFlags(T t) : flag(t) +{ +} + +template +QFlags::operator T() +{ + return flag; +} + +ComplexType::ComplexType() : qFlags(QFlags(TestFlag::Flag2)) +{ +} + int ComplexType::check() { return 5; } -QFlags ComplexType::returnsQFlags() +QFlags ComplexType::returnsQFlags() { - return QFlags(); + return qFlags; } void ComplexType::takesQFlags(const QFlags f) diff --git a/tests/CSharpTemp/CSharpTemp.cs b/tests/CSharpTemp/CSharpTemp.cs index 64745c44..4083e326 100644 --- a/tests/CSharpTemp/CSharpTemp.cs +++ b/tests/CSharpTemp/CSharpTemp.cs @@ -13,6 +13,11 @@ namespace CppSharp.Tests [TypeMap("QFlags")] public class QFlags : TypeMap { + public override string CSharpConstruct() + { + return string.Empty; + } + public override string CSharpSignature(CSharpTypePrinterContext ctx) { TemplateArgument templateArgument = diff --git a/tests/CSharpTemp/CSharpTemp.h b/tests/CSharpTemp/CSharpTemp.h index 9f9e6733..bcce5810 100644 --- a/tests/CSharpTemp/CSharpTemp.h +++ b/tests/CSharpTemp/CSharpTemp.h @@ -102,18 +102,30 @@ public: Proprietor::Proprietor() {} template -class QFlags +class DLL_API QFlags { public: - QFlags() {} + QFlags(T t); + operator T(); +private: + T flag; +}; + +enum class TestFlag +{ + Flag1, + Flag2 }; class DLL_API ComplexType { public: + ComplexType(); int check(); - QFlags returnsQFlags(); + QFlags returnsQFlags(); void takesQFlags(const QFlags f); +private: + QFlags qFlags; }; class DLL_API P : Proprietor