Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SWIG with methods that take std::string as a parameter

Tags:

c++

c#

swig

I used SWIG to wrap my c++ class. Some methods have a const std::string& as a parameter. SWIG creates a type called SWIGTYPE_p_std__string however you cannot just pass a normal string for this when invoking the method in c#. The below example is just a modified example that comes with the SWIG package.:

public void setName(SWIGTYPE_p_std__string name) 
{
    examplePINVOKE.Shape_setName(swigCPtr, SWIGTYPE_p_std__string.getCPtr(name));
    if (examplePINVOKE.SWIGPendingException.Pending) throw examplePINVOKE.SWIGPendingException.Retrieve();
}

In my interface file I just have:

/* File : example.i */
%module example

%{
#include "example.h"
%}

/* Let's just grab the original header file here */
%include "example.h"

And the method that is being wrapped in C++ is:

void Shape::setName(const std::string& name)
{
    mName = name;
}

Is there some sort of typemap I have to put in the interface file? If so, how do I do that?

like image 282
Seth Avatar asked Feb 03 '12 01:02

Seth


1 Answers

I was trying to solve this myself when I found your question.

You need to include

%include "std_string.i" 

in your .i file. See:

  • STL/C++ library

    for more details.

like image 90
puppet Avatar answered Oct 01 '22 17:10

puppet