I am using SWIG to access C++ code from Java.
What is the easiest way to expose a std::string parameter passed by non-const reference?
I have primitives passed by reference exposed as Java arrays, thanks to typemaps.i, and const std::string&s exposed as java.lang.String, thanks to std_string.i. But a non-const std::string& is exposed as opaque pointer type SWIGTYPE_p_std__string.
Current:
// C++ method -> // Java wrapper of C++ method
void foo( int & i ) -> public void foo( int[] i ); // OK
void bar( const std::string & s ) -> public void bar( String s ); // OK
void baz( std::string & s ) -> public void baz( SWIGTYPE_p_std__string s ); // :(
Desired:
void foo( int & i ) -> public void foo( int[] i ); // OK
void bar( const std::string & s ) -> public void bar( String s ); // OK
void baz( std::string & s ) -> public void baz( String[] s ); // OK
UPDATE: I found a solution, described below. However, it took more effort than a few seconds. I’m still interested in hearing about easy approaches.
The best approach I could find was to write my own typemap. I had been hoping for a few trivial SWIG instructions.
In case anyone else needs this, here’s how I did it. Bear in mind that I am not a SWIG expert.
First, you need to define some typemaps to be applied to std::string& arguments. You only have to define these once. (Note: there are additional typemaps that may be required in some configurations.)
Next, for each C++ argument pattern like this …
… you apply the typemaps above with this SWIG directive:
The resulting bindings look like this:
They each require a one-element String array. On entry, the first element may be null. If non-null, it is converted to a UTF-8 std::string value and passed to the C++ function. On exit, the value of the std::string passed by reference is converted back from UTF-8 to a Java String.