From 5f0840d1175bccb0cc94586e68237f613ff716a6 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Sun, 13 Nov 2016 16:57:06 +0200 Subject: [PATCH] Fixed the mapping of std::string not to destroy values when a reference (&) is returned. Signed-off-by: Dimitar Dobrev --- src/Generator/Types/Std/Stdlib.cs | 4 +-- tests/Common/Common.Tests.cs | 2 ++ tests/Common/Common.cpp | 33 +++++++++++--------- tests/Common/Common.h | 51 ++++++++++++++++--------------- 4 files changed, 49 insertions(+), 41 deletions(-) diff --git a/src/Generator/Types/Std/Stdlib.cs b/src/Generator/Types/Std/Stdlib.cs index 69bb9bad..70dfa48c 100644 --- a/src/Generator/Types/Std/Stdlib.cs +++ b/src/Generator/Types/Std/Stdlib.cs @@ -92,13 +92,13 @@ namespace CppSharp.Types.Std public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) { - var type = ctx.ReturnType.Type; + var type = ctx.ReturnType.Type.Desugar(); ClassTemplateSpecialization basicString = GetBasicString(type); Declaration c_str = basicString.Methods.FirstOrDefault(m => m.OriginalName == "c_str"); if (!c_str.IsGenerated) c_str = basicString.Properties.First(p => p.OriginalName == "c_str"); var typePrinter = new CSharpTypePrinter(ctx.Context); - if (type.IsPointer() || ctx.Declaration is Field) + if (type.IsAddress() || ctx.Declaration is Field) { ctx.Return.Write("{0}.{1}({2}).{3}{4}", basicString.Visit(typePrinter), Helpers.CreateInstanceIdentifier, diff --git a/tests/Common/Common.Tests.cs b/tests/Common/Common.Tests.cs index 8e805590..96b1edde 100644 --- a/tests/Common/Common.Tests.cs +++ b/tests/Common/Common.Tests.cs @@ -665,6 +665,8 @@ This is a very long string. This is a very long string. This is a very long stri Assert.That(hasStdString.testStdString(t), Is.EqualTo(t + "_test")); hasStdString.s = t; Assert.That(hasStdString.s, Is.EqualTo(t)); + Assert.That(hasStdString.stdString, Is.EqualTo(t)); + Assert.That(hasStdString.stdString, Is.EqualTo(t)); } } diff --git a/tests/Common/Common.cpp b/tests/Common/Common.cpp index a21fae10..7fd6334d 100644 --- a/tests/Common/Common.cpp +++ b/tests/Common/Common.cpp @@ -234,40 +234,40 @@ void Hello::EnumOutRef(int value, CS_OUT Enum& e) void Hello::EnumInOut(CS_IN_OUT Enum* e) { - if (*e == Enum::E) - *e = Enum::F; + if (*e == Enum::E) + *e = Enum::F; } void Hello::EnumInOutRef(CS_IN_OUT Enum& e) { - if (e == Enum::E) - e = Enum::F; + if (e == Enum::E) + e = Enum::F; } void Hello::StringOut(CS_OUT const char** str) { - *str = "HelloStringOut"; + *str = "HelloStringOut"; } void Hello::StringOutRef(CS_OUT const char*& str) { - str = "HelloStringOutRef"; + str = "HelloStringOutRef"; } void Hello::StringInOut(CS_IN_OUT const char** str) { - if (strcmp(*str, "Hello") == 0) - *str = "StringInOut"; - else - *str = "Failed"; + if (strcmp(*str, "Hello") == 0) + *str = "StringInOut"; + else + *str = "Failed"; } void Hello::StringInOutRef(CS_IN_OUT const char*& str) { - if (strcmp(str, "Hello") == 0) - str = "StringInOutRef"; - else - str = "Failed"; + if (strcmp(str, "Hello") == 0) + str = "StringInOutRef"; + else + str = "Failed"; } void Hello::StringTypedef(const TypedefChar* str) @@ -430,6 +430,11 @@ std::string HasStdString::testStdString(std::string s) return s + "_test"; } +std::string& HasStdString::getStdString() +{ + return s; +} + TypeMappedIndex::TypeMappedIndex() { } diff --git a/tests/Common/Common.h b/tests/Common/Common.h index a7177f74..39c1c039 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -315,7 +315,7 @@ DLL_API TestMoveOperatorToClass operator+(const TestMoveOperatorToClass& b1, // Not a valid operator overload for Foo2 in managed code - comparison operators need to return bool. DLL_API int operator==(const Foo2& a, const Foo2& b) { - return 0; + return 0; } // Tests delegates @@ -381,13 +381,13 @@ struct DLL_API TestStaticClass { static int Add(int a, int b); - static int GetOneTwoThree(); + static int GetOneTwoThree(); protected: - static int _Mult(int a, int b); + static int _Mult(int a, int b); - static int GetFourFiveSix(); + static int GetFourFiveSix(); private: TestStaticClass(); @@ -414,9 +414,9 @@ int TestStaticClassDerived::Foo() { return 0; } class DLL_API TestNotStaticClass { public: - static TestNotStaticClass StaticFunction(); + static TestNotStaticClass StaticFunction(); private: - TestNotStaticClass(); + TestNotStaticClass(); }; TestNotStaticClass::TestNotStaticClass() @@ -425,7 +425,7 @@ TestNotStaticClass::TestNotStaticClass() TestNotStaticClass TestNotStaticClass::StaticFunction() { - return TestNotStaticClass(); + return TestNotStaticClass(); } class HasIgnoredField @@ -473,8 +473,8 @@ struct EmptyNamedNestedEnum typedef unsigned long foo_t; typedef struct DLL_API SomeStruct { - SomeStruct(); - foo_t p; + SomeStruct(); + foo_t p; } SomeStruct; SomeStruct::SomeStruct() : p(1) {} @@ -485,11 +485,11 @@ class DLL_API SomeClassExtendingTheStruct : public SomeStruct namespace SomeNamespace { - class DLL_API AbstractClass - { - public: - virtual void AbstractMethod() = 0; - }; + class DLL_API AbstractClass + { + public: + virtual void AbstractMethod() = 0; + }; } // Test operator overloads @@ -498,11 +498,11 @@ class DLL_API ClassWithOverloadedOperators public: ClassWithOverloadedOperators(); - operator char(); - operator int(); - operator short(); + operator char(); + operator int(); + operator short(); - virtual bool operator<(const ClassWithOverloadedOperators &other) const; + virtual bool operator<(const ClassWithOverloadedOperators &other) const; }; ClassWithOverloadedOperators::ClassWithOverloadedOperators() {} @@ -627,7 +627,7 @@ class DLL_API TestFixedArrays { public: TestFixedArrays(); - VoidPtrRetFunctionTypedef Array[10]; + VoidPtrRetFunctionTypedef Array[10]; }; TestFixedArrays::TestFixedArrays() {} @@ -655,7 +655,7 @@ int TestGetterSetterToProperties::getWidth() { return 640; } int TestGetterSetterToProperties::getHeight() { return 480; } // Tests conversion operators of classes -class DLL_API ClassA +class DLL_API ClassA { public: ClassA(int value); @@ -759,6 +759,7 @@ class DLL_API HasStdString public: std::string testStdString(std::string s); std::string s; + std::string& getStdString(); }; class DLL_API InternalCtorAmbiguity @@ -832,10 +833,10 @@ void DLL_API funcTryRefTypeOut(CS_OUT RefTypeClassPassTry classTry); #define CS_VALUE_TYPE struct CS_VALUE_TYPE ValueTypeArrays { - float firstValueTypeArrray[ARRAY_LENGTH]; - int secondValueTypeArray[ARRAY_LENGTH]; - char thirdValueTypeArray[ARRAY_LENGTH]; - size_t size; + float firstValueTypeArrray[ARRAY_LENGTH]; + int secondValueTypeArray[ARRAY_LENGTH]; + char thirdValueTypeArray[ARRAY_LENGTH]; + size_t size; }; class DLL_API HasVirtualProperty @@ -1070,7 +1071,7 @@ void TemplateWithVirtual::v() template int FunctionTemplateWithDependentTypeDefaultExpr(size_t size = sizeof(T)) { - return size; + return size; } class DLL_API DerivedFromTemplateInstantiationWithVirtual : public TemplateWithVirtual