Passing std::string& and const std::string& as ref in C#

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Passing std::string& and const std::string& as ref in C#

Nixzoor
This post was updated on .
Hi,

I have following problem. I have a director class where i have following method:

virtual bool somemethod(const string& param1,int a,int b, string& param2);

I would like to pass std::string& and const std::string& as ref in c#. I have found following solution http://stackoverflow.com/questions/13022051/how-to-swig-stdstring-to-c-sharp-ref-string , but it doesn't work for me.

In my .i file i call:
%include "std_string.i"
%include "std_string_ref.i"

%feature("director") DirectorClass;
%include "DirectorClass.h"
----------------------------------
/* -----------------------------------------------------------------------------
 * std_string_ref.i
 *
 * Typemaps for std::string& and const std::string&
 * These are mapped to a C# String and are passed around by reference
 *
 * ----------------------------------------------------------------------------- */

%{
#include <string>
%}

namespace std {

%naturalvar string;

class string;

// string &

%typemap(ctype) std::string & "char**"
%typemap(imtype) std::string & "/*imtype*/ ref string"
%typemap(cstype) std::string & "/*cstype*/ ref string"

//C++
%typemap(in, canthrow=1) std::string &
%{  //typemap in
    std::string temp;
    $1 = &temp;
 %}

//C++
%typemap(argout) std::string &
%{
    //Typemap argout in c++ file.
    //This will convert c++ string to c# string
    *$input = SWIG_csharp_string_callback($1->c_str());
%}

%typemap(argout) const std::string &
%{
    //argout typemap for const std::string&
%}

%typemap(csin) std::string & "ref $csinput"

%typemap(throws, canthrow=1) string &
%{ SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, $1.c_str());
   return $null; %}

}
-------------------------------------------------------------
bool SwigDirector_SomeDirectorClass::somemethod(std::string const ¶m1, int a, int b, std::string ¶m2) {
  bool c_result = SwigValueInit< bool >() ;
  unsigned int jresult = 0 ;
  char * jparam1 = 0 ;
  int ja  ;
  int jb  ;
  char** jparam2 = 0 ;
 
  if (!swig_callbacksomemethod) {
    throw Swig::DirectorPureVirtualException("SomeDirectorClass::somemethod");
  } else {
    jparam1 = SWIG_csharp_string_callback((¶m1)->c_str());
    ja = deviceId;
    jb = key;
    jparam2= (std::string *) ¶m2; <--- here is problem cannot convert from string * to char **
    jresult = (unsigned int) swig_callbackGetConfigItem(jparam, ja, jb, jparam2);
    c_result = jresult ? true : false;
  }
  return c_result;
}

--------------------------------------------------------
So how can i achieve to pass string by reference? I am really new to swig and i don't know a lot about typemaps but i am learning it slowly.

Best Regards,
Nik