Browse Source

Fixed the mapping of std::string not to destroy values when a reference (&) is returned.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/719/head 0.7.11
Dimitar Dobrev 9 years ago
parent
commit
5f0840d117
  1. 4
      src/Generator/Types/Std/Stdlib.cs
  2. 2
      tests/Common/Common.Tests.cs
  3. 33
      tests/Common/Common.cpp
  4. 51
      tests/Common/Common.h

4
src/Generator/Types/Std/Stdlib.cs

@ -92,13 +92,13 @@ namespace CppSharp.Types.Std
public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
{ {
var type = ctx.ReturnType.Type; var type = ctx.ReturnType.Type.Desugar();
ClassTemplateSpecialization basicString = GetBasicString(type); ClassTemplateSpecialization basicString = GetBasicString(type);
Declaration c_str = basicString.Methods.FirstOrDefault(m => m.OriginalName == "c_str"); Declaration c_str = basicString.Methods.FirstOrDefault(m => m.OriginalName == "c_str");
if (!c_str.IsGenerated) if (!c_str.IsGenerated)
c_str = basicString.Properties.First(p => p.OriginalName == "c_str"); c_str = basicString.Properties.First(p => p.OriginalName == "c_str");
var typePrinter = new CSharpTypePrinter(ctx.Context); 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}", ctx.Return.Write("{0}.{1}({2}).{3}{4}",
basicString.Visit(typePrinter), Helpers.CreateInstanceIdentifier, basicString.Visit(typePrinter), Helpers.CreateInstanceIdentifier,

2
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")); Assert.That(hasStdString.testStdString(t), Is.EqualTo(t + "_test"));
hasStdString.s = t; hasStdString.s = t;
Assert.That(hasStdString.s, Is.EqualTo(t)); Assert.That(hasStdString.s, Is.EqualTo(t));
Assert.That(hasStdString.stdString, Is.EqualTo(t));
Assert.That(hasStdString.stdString, Is.EqualTo(t));
} }
} }

33
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) void Hello::EnumInOut(CS_IN_OUT Enum* e)
{ {
if (*e == Enum::E) if (*e == Enum::E)
*e = Enum::F; *e = Enum::F;
} }
void Hello::EnumInOutRef(CS_IN_OUT Enum& e) void Hello::EnumInOutRef(CS_IN_OUT Enum& e)
{ {
if (e == Enum::E) if (e == Enum::E)
e = Enum::F; e = Enum::F;
} }
void Hello::StringOut(CS_OUT const char** str) void Hello::StringOut(CS_OUT const char** str)
{ {
*str = "HelloStringOut"; *str = "HelloStringOut";
} }
void Hello::StringOutRef(CS_OUT const char*& str) void Hello::StringOutRef(CS_OUT const char*& str)
{ {
str = "HelloStringOutRef"; str = "HelloStringOutRef";
} }
void Hello::StringInOut(CS_IN_OUT const char** str) void Hello::StringInOut(CS_IN_OUT const char** str)
{ {
if (strcmp(*str, "Hello") == 0) if (strcmp(*str, "Hello") == 0)
*str = "StringInOut"; *str = "StringInOut";
else else
*str = "Failed"; *str = "Failed";
} }
void Hello::StringInOutRef(CS_IN_OUT const char*& str) void Hello::StringInOutRef(CS_IN_OUT const char*& str)
{ {
if (strcmp(str, "Hello") == 0) if (strcmp(str, "Hello") == 0)
str = "StringInOutRef"; str = "StringInOutRef";
else else
str = "Failed"; str = "Failed";
} }
void Hello::StringTypedef(const TypedefChar* str) void Hello::StringTypedef(const TypedefChar* str)
@ -430,6 +430,11 @@ std::string HasStdString::testStdString(std::string s)
return s + "_test"; return s + "_test";
} }
std::string& HasStdString::getStdString()
{
return s;
}
TypeMappedIndex::TypeMappedIndex() TypeMappedIndex::TypeMappedIndex()
{ {
} }

51
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. // 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) DLL_API int operator==(const Foo2& a, const Foo2& b)
{ {
return 0; return 0;
} }
// Tests delegates // Tests delegates
@ -381,13 +381,13 @@ struct DLL_API TestStaticClass
{ {
static int Add(int a, int b); static int Add(int a, int b);
static int GetOneTwoThree(); static int GetOneTwoThree();
protected: protected:
static int _Mult(int a, int b); static int _Mult(int a, int b);
static int GetFourFiveSix(); static int GetFourFiveSix();
private: private:
TestStaticClass(); TestStaticClass();
@ -414,9 +414,9 @@ int TestStaticClassDerived::Foo() { return 0; }
class DLL_API TestNotStaticClass class DLL_API TestNotStaticClass
{ {
public: public:
static TestNotStaticClass StaticFunction(); static TestNotStaticClass StaticFunction();
private: private:
TestNotStaticClass(); TestNotStaticClass();
}; };
TestNotStaticClass::TestNotStaticClass() TestNotStaticClass::TestNotStaticClass()
@ -425,7 +425,7 @@ TestNotStaticClass::TestNotStaticClass()
TestNotStaticClass TestNotStaticClass::StaticFunction() TestNotStaticClass TestNotStaticClass::StaticFunction()
{ {
return TestNotStaticClass(); return TestNotStaticClass();
} }
class HasIgnoredField class HasIgnoredField
@ -473,8 +473,8 @@ struct EmptyNamedNestedEnum
typedef unsigned long foo_t; typedef unsigned long foo_t;
typedef struct DLL_API SomeStruct typedef struct DLL_API SomeStruct
{ {
SomeStruct(); SomeStruct();
foo_t p; foo_t p;
} SomeStruct; } SomeStruct;
SomeStruct::SomeStruct() : p(1) {} SomeStruct::SomeStruct() : p(1) {}
@ -485,11 +485,11 @@ class DLL_API SomeClassExtendingTheStruct : public SomeStruct
namespace SomeNamespace namespace SomeNamespace
{ {
class DLL_API AbstractClass class DLL_API AbstractClass
{ {
public: public:
virtual void AbstractMethod() = 0; virtual void AbstractMethod() = 0;
}; };
} }
// Test operator overloads // Test operator overloads
@ -498,11 +498,11 @@ class DLL_API ClassWithOverloadedOperators
public: public:
ClassWithOverloadedOperators(); ClassWithOverloadedOperators();
operator char(); operator char();
operator int(); operator int();
operator short(); operator short();
virtual bool operator<(const ClassWithOverloadedOperators &other) const; virtual bool operator<(const ClassWithOverloadedOperators &other) const;
}; };
ClassWithOverloadedOperators::ClassWithOverloadedOperators() {} ClassWithOverloadedOperators::ClassWithOverloadedOperators() {}
@ -627,7 +627,7 @@ class DLL_API TestFixedArrays
{ {
public: public:
TestFixedArrays(); TestFixedArrays();
VoidPtrRetFunctionTypedef Array[10]; VoidPtrRetFunctionTypedef Array[10];
}; };
TestFixedArrays::TestFixedArrays() {} TestFixedArrays::TestFixedArrays() {}
@ -655,7 +655,7 @@ int TestGetterSetterToProperties::getWidth() { return 640; }
int TestGetterSetterToProperties::getHeight() { return 480; } int TestGetterSetterToProperties::getHeight() { return 480; }
// Tests conversion operators of classes // Tests conversion operators of classes
class DLL_API ClassA class DLL_API ClassA
{ {
public: public:
ClassA(int value); ClassA(int value);
@ -759,6 +759,7 @@ class DLL_API HasStdString
public: public:
std::string testStdString(std::string s); std::string testStdString(std::string s);
std::string s; std::string s;
std::string& getStdString();
}; };
class DLL_API InternalCtorAmbiguity class DLL_API InternalCtorAmbiguity
@ -832,10 +833,10 @@ void DLL_API funcTryRefTypeOut(CS_OUT RefTypeClassPassTry classTry);
#define CS_VALUE_TYPE #define CS_VALUE_TYPE
struct CS_VALUE_TYPE ValueTypeArrays struct CS_VALUE_TYPE ValueTypeArrays
{ {
float firstValueTypeArrray[ARRAY_LENGTH]; float firstValueTypeArrray[ARRAY_LENGTH];
int secondValueTypeArray[ARRAY_LENGTH]; int secondValueTypeArray[ARRAY_LENGTH];
char thirdValueTypeArray[ARRAY_LENGTH]; char thirdValueTypeArray[ARRAY_LENGTH];
size_t size; size_t size;
}; };
class DLL_API HasVirtualProperty class DLL_API HasVirtualProperty
@ -1070,7 +1071,7 @@ void TemplateWithVirtual<T>::v()
template <typename T> template <typename T>
int FunctionTemplateWithDependentTypeDefaultExpr(size_t size = sizeof(T)) { int FunctionTemplateWithDependentTypeDefaultExpr(size_t size = sizeof(T)) {
return size; return size;
} }
class DLL_API DerivedFromTemplateInstantiationWithVirtual : public TemplateWithVirtual<int> class DLL_API DerivedFromTemplateInstantiationWithVirtual : public TemplateWithVirtual<int>

Loading…
Cancel
Save