Stuart Berg
Stuart Berg

Reputation: 18162

boost-python: How do I provide a custom constructor wrapper function?

I'm using boost-python to create python bindings for a C++ class named CppClass. When necessary, I can route calls to "normal" member functions through little wrapper functions that preprocess the arguments (e.g. extract C++ types from the python args), like so:

class CppClass
{
public:
    CppClass(SpecialParameters p);
    void doSomething(int x, float y);
};

using namespace boost::python; // For extract, tuple, init, class_, etc.

class WrapperFuncs
{
public:
    static void doSomething(CppClass & c, tuple t)
    {
        // Special extraction: Convert python arg ( a tuple) into C++ args.
        int x = extract<int>(t.attr("__getitem__")(0));
        float y = extract<float>(t.attr("__getitem__")(1));
        c.doSomething(x,y);
    }
};

class_<CppClass, boost::shared_ptr<CppClass> >
    ("CppClass", init<SpecialParameters>())
    .def("doSomething", &WrapperFuncs::doSomething, (arg("t")))

But how do I do the same for the CppClass constructor?

Upvotes: 14

Views: 8135

Answers (1)

Stuart Berg
Stuart Berg

Reputation: 18162

Use no_init followed by a .def for __init__ using boost::python::make_constructor().

class WrapperFuncs
{
public:
    static boost::shared_ptr<CppClass> initWrapper( object const & p )
    {
        SpecialParameters sp = ... // do complicated extraction here.
        return boost::shared_ptr<CppClass>( new CppClass(sp) );
    }

    static void doSomething(CppClass & c, tuple t) { /*...*/ }
};

class_<CppClass, boost::shared_ptr<CppClass> >
    ("CppClass", no_init)
    .def("__init__", make_constructor(&WrapperFuncs::initWrapper))
    .def("doSomething", &WrapperFuncs::doSomething, (arg("t")))

This section of the python wiki explains how to do this, but it didn't quite work for me because it didn't mention no_init. In my case, no_init was required.

Upvotes: 21

Related Questions