是否有一种标准的方法来包装boost :: optional<>默认情况下,在Java中作为swig为SWIGTYPE_p_boost__optionalT_double_t
和SWIGTYPE_p_boost__optionalT_std__string_t
生成类型boost::optional<double>
和boost::optional<std::string>
??
对于python,可以按照Can you SWIG a boost::optional<>?
中的说明完成谢谢
答案 0 :(得分:4)
以下是完整示例,其中包括NULL
和boost::optional<double>
的字体图。
boost::optional<std::string>
java的示例用法:
%module example
%{
#include <boost/optional.hpp>
%}
namespace boost {
template<typename T> class optional {};
}
// boost::optional<double>
%typemap(jni) boost::optional<double> "jobject"
%typemap(jtype) boost::optional<double> "Double"
%typemap(jstype) boost::optional<double> "Double"
%typemap(in, noblock=1) boost::optional<double> {
if ($input) {
jclass sbufClass = JCALL1(GetObjectClass, jenv, $input);
jmethodID mid = JCALL3(GetMethodID, jenv, sbufClass, "doubleValue", "()D");
jdouble val = (jdouble)JCALL2(CallDoubleMethod, jenv, $input, mid);
if (JCALL0(ExceptionCheck, jenv)) return $null;
$1 = (double)val;
}
}
%typemap(out, noblock=1) boost::optional<double> {
jclass clazz = JCALL1(FindClass, jenv, "java/lang/Double");
jmethodID mid = JCALL3(GetMethodID, jenv, clazz, "<init>", "(D)V");
jobject obj = $1 ? JCALL3(NewObject, jenv, clazz, mid, *$1) : 0;
$result = obj;
}
%typemap(javain) boost::optional<double> "$javainput"
%typemap(javaout) boost::optional<double> {
return $jnicall;
}
%template() boost::optional<double>;
// boost::optional<std::string>
%include <std_string.i>
%typemap(jni) boost::optional<std::string> "jstring"
%typemap(jtype) boost::optional<std::string> "String"
%typemap(jstype) boost::optional<std::string> "String"
%typemap(in) boost::optional<std::string> %{
if ($input) {
const char *pstr = (const char *)jenv->GetStringUTFChars($input, 0);
if (!pstr) return $null;
$1 = std::string(pstr);
jenv->ReleaseStringUTFChars($input, pstr);
}
%}
%typemap(out) boost::optional<std::string>
%{ $result = $1 ? jenv->NewStringUTF($1->c_str()) : 0; %}
%typemap(javain) boost::optional<std::string> "$javainput"
%typemap(javaout) boost::optional<std::string> {
return $jnicall;
}
%template() boost::optional<std::string>;
%inline %{
#include <iostream>
boost::optional<double> optional_double_test(boost::optional<double> x) {
if (x)
std::cout << "C++ " << *x << std::endl;
else
std::cout << "C++ " << "NULL" << std::endl;
return x;
}
boost::optional<std::string> optional_string_test(boost::optional<std::string> x) {
if (x)
std::cout << "C++ " << *x << std::endl;
else
std::cout << "C++ " << "NULL" << std::endl;
return x;
}
%}