swig java 中的简单类型映射示例
我正在尝试使用 swig 包装本机 C++ 库,但我一直试图将 C 中的 time_t 转换为 Java 中的 long 。我已经成功地将 swig 与 python 结合使用,但到目前为止我无法让上面的类型映射在 Java 中工作。 在Python中,它看起来像这样,
%typemap(in) time_t
{
if (PyLong_Check($input))
$1 = (time_t) PyLong_AsLong($input);
else if (PyInt_Check($input))
$1 = (time_t) PyInt_AsLong($input);
else if (PyFloat_Check($input))
$1 = (time_t) PyFloat_AsDouble($input);
else {
PyErr_SetString(PyExc_TypeError,"Expected a large number");
return NULL;
}
}
%typemap(out) time_t
{
$result = PyLong_FromLong((long)$1);
}
我猜从Java到C的in映射将是:
%typemap(in) time_t {
$1 = (time_t) $input;
}
我将如何完成从C到Java的out映射?
%typemap(out) time_t ???
我需要像下面这样的类型图吗?
%typemap(jni)
%typemap(jtype)
%typemap(jstype)
我需要这个来包装 C 函数,如下所示:
time_t manipulate_time (time_t dt);
I am trying to wrap a native C++ library using swig, and I am stuck at trying to convert time_t
in C, to long
in Java. I have successfully used swig with python, but so far I am unable to get the above typemap to work in Java.
In python it looks like this
%typemap(in) time_t
{
if (PyLong_Check($input))
$1 = (time_t) PyLong_AsLong($input);
else if (PyInt_Check($input))
$1 = (time_t) PyInt_AsLong($input);
else if (PyFloat_Check($input))
$1 = (time_t) PyFloat_AsDouble($input);
else {
PyErr_SetString(PyExc_TypeError,"Expected a large number");
return NULL;
}
}
%typemap(out) time_t
{
$result = PyLong_FromLong((long)$1);
}
I guess the in map from Java to C would be:
%typemap(in) time_t {
$1 = (time_t) $input;
}
How would I complete the out map from C to Java?
%typemap(out) time_t ???
Would I need typemaps like the ones below?
%typemap(jni)
%typemap(jtype)
%typemap(jstype)
I need this in order to wrap C functions like this:
time_t manipulate_time (time_t dt);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该查看 swig 文档的以下部分:
基本类型映射中还有很多为原语实现的“示例”类型。您可以在 \swig\Lib\java\java.swg
中找到它们
我不知道这是否有效,但也许这样的东西能满足您的需求?
You should take a look at these sections of swig documentation:
There are also a lot of "examples" in the basic typemaps which are implemented for primitive types. You can find them in \swig\Lib\java\java.swg
I don't know if this working or not, but maybe something like this will suit your needs?
您可以简单地执行而不是使用类型映射。
You can simply do instead of using typemaps.