开发者

Passing bool by reference using SWIG and Python

开发者 https://www.devze.com 2023-01-29 15:17 出处:网络
I\'ve wrapped a C++ library API using SWIG, which works well, but I\'m stumped by a \"bool &\" parameter.

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 wor开发者_开发技巧ds, 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消