Java 通过数据序列化的SWIG接口?

Java 通过数据序列化的SWIG接口?,java,c++,swig,Java,C++,Swig,我有一个我想在Java和C++中使用的数据类型: MyObj foo(const MyObj& input); std::vector<MyObj> bar(std::vector<MyObj>& input); 在C++中: std::string toString(const MyObj& mo); MyObj fromString(const std::string& s); 我是否可以创建一个SWIG包装器,我为其编写的代码(在

我有一个我想在Java和C++中使用的数据类型:

MyObj foo(const MyObj& input);
std::vector<MyObj> bar(std::vector<MyObj>& input);
在C++中:

std::string toString(const MyObj& mo);
MyObj fromString(const std::string& s);
我是否可以创建一个SWIG包装器,我为其编写的代码(在SWIG文件之外)甚至不需要知道如何转换为字符串或从字符串转换?这似乎是可能的。我被卡住的地方是:

%typemap(jni) MyObj& "std::string";
%typemap(jtype) MyObj& "String";
%typemap(jstype) MyObj& "MyObj";
%typemap(javain,
   pre="String ret = toString($javainput);\n",
   pgcppname="ret")
   MyObj& "$javaclassname.getCPtr(ret)";
%template(VectorMyObj) std::vector<MyObj>;

有什么想法吗?

解决方案最终涉及到不分配
%typemap(jni)
%typemap(jtype)
(即将它们保留为SWIG的默认值)以及在
std::string
周围编写一个薄包装。SWIG为我的包装器提供了一个
getCPtr
,我可以在我的
javain
typemap中使用

我不知道如何为Java内置程序获取
swigCPtr

%typemap(jni) MyObj& "std::string";
%typemap(jtype) MyObj& "String";
%typemap(jstype) MyObj& "MyObj";
%typemap(javain,
   pre="String ret = toString($javainput);\n",
   pgcppname="ret")
   MyObj& "$javaclassname.getCPtr(ret)";
%template(VectorMyObj) std::vector<MyObj>;
./VectorMyObj.java:74: error: incompatible types: String cannot be converted to SWIGTYPE_p_MyObj
  exampleJNI.VectorMyObj_add(swigCPtr, this, SWIGTYPE_p_MyObj.getCPtr(ret));