[C++-sig] Wrapping already constructed c++ objects.

Shahrouz Zolfaghari shahrouz at student.chalmers.se
Sun Jul 10 02:14:19 CEST 2005


Hello,

I would like to know if it is possible to use boost.python to wrap an
already created c++ object and make it available though the embedded
python interpreter?

So the instance "foo" the class "Foo" should be exposed to the python
interpreter as a module.

Here is a little example with the wrapping manually done, using Python/C api:

class Foo
{
public:
        void method() { ... }
};

Foo foo;

PyObject * foo_method_wrap(PyObject *self,PyObject * argv)
{
        foo.method();
        return Py_None;
}

static PyMethodDef foo_methods[] = {
    {"method",  foo_method_wrap, METH_VARARGS, "Call foo.method()."},
    {NULL, NULL, 0, NULL}
};

int main()
{
        Py_Initialize();
        Py_InitModule("foo",foo_methods);
        PyRun_SimpleString( "import foo\n"
                            "foo.method()\n");
        Py_Finalize();
}

Thanks,
Eracce




More information about the Cplusplus-sig mailing list