Swig Pass Std::vector by ref / out from C# to C++

Given the C++ function

void Foo(unsigned int _x, unsigned int _y, std::vector< unsigned int > &_results)

And the Swig interface file to map std::vector to type VectorUInt32 in C#

%include "std_vector.i"

namespace std {
   %template(VectorUInt32) vector<unsigned int>;
};

I get the following result in C# code:

public static void Foo(uint _x, uint _y, VectorUInt32 _results)

Which is great, but what I was really hoping for was this:

public static void Foo(uint _x, uint _y, out VectorUInt32 _results)

Does anyone know how to map the std::vector from C++ to C# as a ref or out param?

Upvotes: 1

Views: 1282

Answers (1)

Shame on you Stackoverflow for not having an answer!

Anyway, the answer is, if anyone else is interested... If you create the VectorUInt32 type in C# and pass it to the C++ function it is passed by reference, so can be modified inside C++ without a ref or out param.

The interface becomes:

C++

void Foo(unsigned int _x, unsigned int _y, std::vector< unsigned int > &_results)

Interface file

%include "std_vector.i"

namespace std {
   %template(VectorUInt32) vector<unsigned int>;
};

C#

public static void Foo(uint _x, uint _y, VectorUInt32 _results)

Usage

// Create and pass vector to C++
var vec = new VectorUInt32();
Foo(x, y, vec);
// do something with vec, its been operated on!

Upvotes: 1

Related Questions