[C++-sig] Wrapping classical C API for python access

Markus Schöpflin markus.schoepflin at comsoft.de
Tue Jun 28 13:52:59 CEST 2005


David Abrahams wrote:

> Markus Schöpflin <markus.schoepflin at comsoft.de> writes:

> If you have a particular signature over and over again, you can do it
> with a template:
> 
>   template <void (&f)(int, int*)>
>   int thin_wrapper(int x)
>   {
>      int result;
>      f(x,&result);
>      return result;
>   }
> 
> Then you can wrap
> 
>   thin_wrapper<foo>

I see. Thanks for getting me started.

>>And lastly, would "int bar(int a, int *b)" be convertible to "(ret,
>>b) = bar(1)"?

> Sorry, I don't understand.  A C++ type convertible to a Python
> statement?

Errm, no. What I meant is that the C++ function "int bar(int a, int *b)" 
where the return value of bar and the output parameter b could be turned 
into a tuple on the Python side, which would make the function callable 
like "(ret, b) = bar(1)".

But as I'll have to write wrappers anyway, I'll have to do the conversion 
manually I think. Is there by any chance some built-in support for using 
boost.tuple together with boost.python, which would me enable to just write

boost::tuple<int, int> bar_wrapper(int a)
{
   int b;
   int ret = bar(a, &b);
   return boost::make_tuple(ret, b);
}

and later on

def("bar", bar_wrapper);

Thanks for your help, Dave!

Markus



More information about the Cplusplus-sig mailing list