Passing bool by reference using SWIG and Python

孤者浪人 提交于 2020-01-06 04:34:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/4383719/passing-bool-by-reference-using-swig-and-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!