Skip to content

Commit 190cbfa

Browse files
IIFEBuild Agent
andauthored
Handle returned pointers to std::vector in C++/CLI
Co-authored-by: Build Agent <[email protected]>
1 parent 7b6fb6e commit 190cbfa

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/Generator/Types/Std/Stdlib.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,10 @@ public override void CLIMarshalToManaged(MarshalContext ctx)
564564
ctx.Before.WriteLine(
565565
"auto {0} = gcnew System::Collections::Generic::List<{1}>();",
566566
tmpVarName, managedType);
567+
568+
string retVarName = ctx.ReturnType.Type.Desugar().IsPointer() ? $"*{ctx.ReturnVarName}" : ctx.ReturnVarName;
567569
ctx.Before.WriteLine("for(auto _element : {0})",
568-
ctx.ReturnVarName);
570+
retVarName);
569571
ctx.Before.WriteOpenBraceAndIndent();
570572
{
571573
var elementCtx = new MarshalContext(ctx.Context, ctx.Indentation)

tests/CLI/CLI.Tests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,17 @@ public void TestChangePassedMappedTypeNonConstRefParam()
5353
Assert.AreEqual("ChangePassedMappedTypeNonConstRefParam", val);
5454
}
5555
}
56+
57+
[Test]
58+
public void TestVectorPointerGetter()
59+
{
60+
using (VectorPointerGetter v = new VectorPointerGetter())
61+
{
62+
var list = v.VecPtr;
63+
64+
Assert.AreEqual(1, list.Count);
65+
66+
Assert.AreEqual("VectorPointerGetter", list[0]);
67+
}
68+
}
5669
}

tests/CLI/CLI.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,24 @@ void TestMappedTypeNonConstRefParamConsumer::ChangePassedMappedTypeNonConstRefPa
4343
{
4444
v = "ChangePassedMappedTypeNonConstRefParam";
4545
}
46+
47+
VectorPointerGetter::VectorPointerGetter()
48+
{
49+
vecPtr = new std::vector<std::string>();
50+
vecPtr->push_back("VectorPointerGetter");
51+
}
52+
53+
VectorPointerGetter::~VectorPointerGetter()
54+
{
55+
if (vecPtr)
56+
{
57+
auto tempVec = vecPtr;
58+
delete vecPtr;
59+
tempVec = nullptr;
60+
}
61+
}
62+
63+
std::vector<std::string>* VectorPointerGetter::GetVecPtr()
64+
{
65+
return vecPtr;
66+
}

tests/CLI/CLI.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "NestedEnumInClassTest/NestedEnumConsumer.h"
88

99
#include <ostream>
10+
#include <vector>
1011

1112
// Tests for C++ types
1213
struct DLL_API Types
@@ -77,3 +78,15 @@ class DLL_API TestMappedTypeNonConstRefParamConsumer
7778
public:
7879
void ChangePassedMappedTypeNonConstRefParam(TestMappedTypeNonConstRefParam&);
7980
};
81+
82+
class DLL_API VectorPointerGetter
83+
{
84+
public:
85+
VectorPointerGetter();
86+
~VectorPointerGetter();
87+
88+
std::vector<std::string>* GetVecPtr();
89+
90+
private:
91+
std::vector<std::string>* vecPtr;
92+
};

0 commit comments

Comments
 (0)