将所有 swig 生成的方法包装在 try { } catch 中
Wrap all swig-generated methods in try { } catch
我有 Android 个带有 C++ 库的项目。 JNI 类 使用 Swig 工具生成。
某些 C++ 方法会抛出标准异常,例如std::invalid_argument
所有标准异常都存在相同的 Java-异常(例如 C++ std::invalid_argument
= java.lang.IllegalArgumentException
)。但是在 C++ 中 throw std::invalid_argument
时,应用程序崩溃。
如何说 SWIG 将 try-catch 块中所有生成的方法包装到 trhow Java-异常而不是 C++ 异常?为了能够在我的 java-code.
中处理这个异常
是否有可能使其“在一行中”,或者我需要明确地为所有方法制作包装器?
感谢您的帮助。
我的痛饮脚本:
run_swig.sh
%module(directors="1") CppDsp
// Anything in the following section is added verbatim to the .cxx wrapper file
%{
#include "cpp-dsp.h"
#include "pipeline_options.h"
%}
//define converters from C++ double pointer to Kotlin double-array
%include carrays.i
%array_functions( double, double_array )
//define converters from C++ long pointer to Kotlin long-array
%array_functions( long long, long_long_array )
%array_functions( signed char, byte_array )
// Process our C++ file (only the public section)
%include "cpp-dsp.h"
%include "pipeline_options.h"
其中,问题是在swig-script.i
文件中,所有#include
都在%exception
定义之后。
工作示例:
%module(directors="1") CppDsp
// Anything in the following section is added verbatim to the .cxx wrapper file
%{
#include "cpp-dsp.h"
#include "pipeline_options.h"
%}
//define converters from C++ double pointer to Kotlin double-array
%include carrays.i
%array_functions( double, double_array )
//define converters from C++ long pointer to Kotlin long-array
%array_functions( long long, long_long_array )
%array_functions( signed char, byte_array )
%exception {
try {
$action
}
catch (const std::exception& e) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, e.what());
return $null;
}
}
// Process our C++ file (only the public section)
// ENSURE, THAT INCLUDES ARE ON THE LAST LINES
%include "cpp-dsp.h"
%include "pipeline_options.h"
我有 Android 个带有 C++ 库的项目。 JNI 类 使用 Swig 工具生成。
某些 C++ 方法会抛出标准异常,例如std::invalid_argument
所有标准异常都存在相同的 Java-异常(例如 C++ std::invalid_argument
= java.lang.IllegalArgumentException
)。但是在 C++ 中 throw std::invalid_argument
时,应用程序崩溃。
如何说 SWIG 将 try-catch 块中所有生成的方法包装到 trhow Java-异常而不是 C++ 异常?为了能够在我的 java-code.
中处理这个异常是否有可能使其“在一行中”,或者我需要明确地为所有方法制作包装器? 感谢您的帮助。
我的痛饮脚本:
run_swig.sh
%module(directors="1") CppDsp
// Anything in the following section is added verbatim to the .cxx wrapper file
%{
#include "cpp-dsp.h"
#include "pipeline_options.h"
%}
//define converters from C++ double pointer to Kotlin double-array
%include carrays.i
%array_functions( double, double_array )
//define converters from C++ long pointer to Kotlin long-array
%array_functions( long long, long_long_array )
%array_functions( signed char, byte_array )
// Process our C++ file (only the public section)
%include "cpp-dsp.h"
%include "pipeline_options.h"
其中,问题是在swig-script.i
文件中,所有#include
都在%exception
定义之后。
工作示例:
%module(directors="1") CppDsp
// Anything in the following section is added verbatim to the .cxx wrapper file
%{
#include "cpp-dsp.h"
#include "pipeline_options.h"
%}
//define converters from C++ double pointer to Kotlin double-array
%include carrays.i
%array_functions( double, double_array )
//define converters from C++ long pointer to Kotlin long-array
%array_functions( long long, long_long_array )
%array_functions( signed char, byte_array )
%exception {
try {
$action
}
catch (const std::exception& e) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, e.what());
return $null;
}
}
// Process our C++ file (only the public section)
// ENSURE, THAT INCLUDES ARE ON THE LAST LINES
%include "cpp-dsp.h"
%include "pipeline_options.h"