【问题标题】:SWIG interface via data serialization?通过数据序列化的 SWIG 接口?
【发布时间】:2016-02-26 22:15:40
【问题描述】:

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

MyObj foo(const MyObj& input);
std::vector<MyObj> bar(std::vector<MyObj>& input);

诀窍在于MyObj 是在第 3 方(封闭源代码)工具中定义的,我可以在 Java 和 C++ 中使用该工具(他们也使用过 SWIG)。我的第一种方法是尝试利用他们的 SWIG 包装器来实现我的目的,但现在我正在探索第二种方法,它是利用而不是我可以将他们的数据序列化为字符串。也就是说,我有这些功能:

在 Java 中:

String toString(MyObj mo);
MyObj fromString(String s);

在 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>;

在 javac 中产生了这个错误:

./VectorMyObj.java:74: error: incompatible types: String cannot be converted to SWIGTYPE_p_MyObj
  exampleJNI.VectorMyObj_add(swigCPtr, this, SWIGTYPE_p_MyObj.getCPtr(ret));

有什么想法吗?

【问题讨论】:

    标签: java c++ swig


    【解决方案1】:

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

    我不知道如何为 Java 内置函数获取 swigCPtr

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 2023-03-23
      相关资源
      最近更新 更多