使用 SWIG 和 Python 通过引用传递 bool

发布于 2024-10-07 01:01:45 字数 466 浏览 0 评论 0原文

我使用 SWIG 封装了 C++ 库 API,效果很好,但我被“bool &”难住了。范围。

原始 API 如下所示:

void foo(bool & bar);

当我从 Python 调用它时, _wrap.cxx 会退出包装过程。

   int res = SWIG_ConvertPtr(argv[1], &vptr, SWIGTYPE_p_bool, 0);
   _v = SWIG_CheckState(res);
   if (_v) {   

换句话说,swig 无法将我传递的内容转换为 bool 指针。

我试图从 Python 中调用它,如下所示:

   obj = LibObject()
   x = 0
   obj.foo(x)

是否有一个简单的类型映射修复程序?

I've wrapped a C++ library API using SWIG, which works well, but I'm stumped by a "bool &" parameter.

The original API looks like this:

void foo(bool & bar);

when I call it from Python, the _wrap.cxx drops out of the wrap process at

   int res = SWIG_ConvertPtr(argv[1], &vptr, SWIGTYPE_p_bool, 0);
   _v = SWIG_CheckState(res);
   if (_v) {   

In other words, swig can't convert what I'm passing in to a bool pointer.

I'm trying to call it from Python, like so:

   obj = LibObject()
   x = 0
   obj.foo(x)

Is there a simple typemap fix for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

痴者 2024-10-14 01:01:45

这应该有效:

%include <typemaps.i>
%apply bool & INOUT { bool & bar };

每当 SWIG 看到 bool & bar 参数,它应该将其视为输入/输出参数。如果您只需要它作为输出参数,请使用OUTPUT

This should work:

%include <typemaps.i>
%apply bool & INOUT { bool & bar };

Whenever SWIG sees a bool & bar parameter, it should treat it as an in/out parameter. If you only need it as an output parameter, use OUTPUT.

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