使用 SWIG 和 Python 通过引用传递 bool [英] Passing bool by reference using SWIG and Python

查看:46
本文介绍了使用 SWIG 和 Python 通过引用传递 bool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 SWIG 封装了一个 C++ 库 API,效果很好,但我被bool &"难住了范围.

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

原始 API 如下所示:

The original API looks like this:

void foo(bool & bar);

void foo(bool & bar);

当我从 Python 调用它时,_wrap.cxx 在

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) {   

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

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

我正在尝试从 Python 中调用它,如下所示:

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?

推荐答案

这应该有效:

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

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

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.

这篇关于使用 SWIG 和 Python 通过引用传递 bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆