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

David Abrahams dave at boost-consulting.com
Tue Jun 28 13:19:30 CEST 2005


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

> David Abrahams wrote:
>
>> Markus Schöpflin <markus.schoepflin at comsoft.de> writes:
>
>>>I'm currently in the process of evaluating how feasible it would be to use 
>>>boost.python to expose a classical C API to python.
>>>
>
>> Decide what interface you'd like to see in Python; that's your first
>> step.
>> 
>>   http://article.gmane.org/gmane.comp.python.c++/964
>> 
>> might give you an idea.
>
> I did some reading and I think the typemap facility offered by SWIG would 
> be just what I need. (Thanks for the pointer, Petrucio)
>
> In short words, it allows me to convert "void foo(int a, int *b)" into a 
> python function foo(int a) returning int b.
>
> Is there some built-in support in BP for this? 

Nope.

> If not, how would I go about doing this? Do I have to write a
> wrapper function for each C function?

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>

> 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?

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list