c++-sig@python.org
[Top] [All Lists]

Re: [C++-sig] How to implement a cross-module C-API

Subject: Re: [C++-sig] How to implement a cross-module C-API
From: Stefan Seefeld
Date: Mon, 05 Sep 2005 11:31:56 -0400
Thomas Schipolowski wrote:

> I would add some free functions to my C++ module that offer a minimal 
> C-API for some of the classes defined there. These functions would 
> accept a PyObject* reference as argument. Internally, they would 
> construct a boost::python::object from the PyObject* and try to extract 
> the expected C++ object type from it. Once I have the C++ object back, I 
> could call its methods to perform the desired operations. All results 
> would be returned as plain C structs or arrays.
> 
> In order to get access to the API functions in my C program, I would 
> package them in a static table of function pointers, place a void* ptr 
> to the table in a Python CObject, and make the CObject visible to my C 
> program by exporting a function (to python) that delivers the CObject on 
> request.

That sounds plausible. You are essentially generating your own vtbls
in C that act as C wrappers around the C++ core. I'm not quite
sure I would embedd these vtbls into the objects you are wrapping, as that
is a bit invasive. Instead, you may create a dictionary that maps python
type objects to the structures holding your vtbl and provide a simple
lookup mechanism that queries the PyTypeObject pointer from a given python
object and look up the C vtbl with that.

> Can this work? The fact that there is a "extern C" declaration for using 
> C functions in C++ makes me wonder if it is possible/safe to access a 
> function compiled with C++ from a program compiled with a C compiler?

Yes, this should work.

Regards,
                Stefan

_______________________________________________
C++-sig mailing list
C++-sig@xxxxxxxxxx
http://mail.python.org/mailman/listinfo/c++-sig

<Prev in Thread] Current Thread [Next in Thread>