在您的示例中,由于“constness”上的不匹配,没有应用类型映射。(你的功能需要
const boost::filesystem::path &path
但是你的排版图是
boost::filesystem::path &
,因此无法应用)。
我认为最简单的方法是将路径作为字符串通过语言边界。你可以用下面的类型映射来做到这一点,在Java端调用
toString()
在路径上,并在C++侧传递到Boost路径对象的构造函数:
%module test
%{
#include <boost/filesystem/path.hpp>
#include <iostream>
%}
%typemap(jni) const boost::filesystem::path& "jstring"
%typemap(jstype) const boost::filesystem::path& "java.nio.file.Path"
%typemap(jtype) const boost::filesystem::path& "String"
%typemap(javain) const boost::filesystem::path& "$javainput.toString()"
%typemap(in) const boost::filesystem::path& (boost::filesystem::path tmp) {
const char *str = JCALL2(GetStringUTFChars, jenv, $input, 0);
tmp = str;
$1 = &tmp;
JCALL2(ReleaseStringUTFChars, jenv, $input, str);
}
%inline %{
void test(const boost::filesystem::path& p) {
std::cout << p << std::endl;
}
%}
这样做可以节省更多的JNI调用
in
typemap,它最终不可避免地会调用几个函数来获取一个字符串表示形式。
(这是编译的,但我没有运行它)。