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

拜拜、爱过 提交于 2019-12-01 00:30:35

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/18793952/boost-python-how-do-i-provide-a-custom-constructor-wrapper-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!