swig java 中的简单类型映射示例

发布于 2024-08-31 20:28:14 字数 974 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溇涏 2024-09-07 20:28:14

您应该查看 swig 文档的以下部分:

基本类型映射中还有很多为原语实现的“示例”类型。您可以在 \swig\Lib\java\java.swg
中找到它们
我不知道这是否有效,但也许这样的东西能满足您的需求?

%typemap(jni) time_t "jlong"
%typemap(jtype) time_t "long"
%typemap(jstype) time_t "long"

%typemap(out) time_t %{ $result = (jlong)$1; %}
%typemap(in) time_t "(time_t)$input"

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?

%typemap(jni) time_t "jlong"
%typemap(jtype) time_t "long"
%typemap(jstype) time_t "long"

%typemap(out) time_t %{ $result = (jlong)$1; %}
%typemap(in) time_t "(time_t)$input"
少钕鈤記 2024-09-07 20:28:14

您可以简单地执行而不是使用类型映射。

typedef long long time_t;

You can simply do instead of using typemaps.

typedef long long time_t;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文