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
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.
C++-sig mailing list