简体   繁体   中英

How to assign in c++ an object created in python?

I've basically have a very simple node test case I'm trying to fix.

I have a simple Node class with a getChild and a getParent

The child can be assigned via the addChild function

Then this function automatically set the parent on the relevant class (so from the c++ side)

Unfortunately when I do that I lose the python reference..

I guess the code should be better to understand :

the basic main class MyNode :

class MyNode
{
public:
    MyNode(): m_name("unknown") {}
    MyNode(std::string name): m_name(name) {}
    MyNode(MyNode * const o) : m_name(o->m_name) {}
    virtual ~MyNode() {}

    std::string getName() const { return m_name; }
    void setName(std::string name) { m_name = name; }
    boost::shared_ptr<MyNode> getChild() const { return m_child; }
    const boost::shared_ptr<MyNode> & getParent() const { return m_parent; }

    void addChild(boost::shared_ptr<MyNode> node) {
        m_child = node;
        node->m_parent = boost::make_shared<MyNode>(this);
    }

private:
    std::string m_name;
    boost::shared_ptr<MyNode> m_parent;
    boost::shared_ptr<MyNode> m_child;
};

Then the boost python bindings code :

class_< MyNode, boost::shared_ptr<MyNode>/*, MyNodeWrapper*/ >("MyNode", init<std::string>())
    .add_property( "Name", &MyNode::getName, &MyNode::setName )
    .add_property( "Child", &MyNode::getChild )
    .add_property( "Parent", make_function(&MyNode::getParent, return_internal_reference<>()))
    .def( "addChild", &MyNode::addChild )
    ;

To finish my python test code :

>>> p=MyNode("p")
>>> o=MyNode("o")
>>> p.addChild(o)
>>> o.Parent
<hello_ext.MyNode object at 0x01C055A8>   << this is not equal to p
>>> p
<hello_ext.MyNode object at 0x01C06510>   << as you can see p here
>>> o.Parent
<hello_ext.MyNode object at 0x01C055A8>   << but at least the pointer doesn't change each time the Parent is called !
>>> p.Child == o                          << so for the child it works
True
>>> o.Parent == p                         << but it doeesn't work for Parent
False

The issue is certainly in the addFunction and how I use boost::make_shared to set the parent. I unfortunately have no idea what's going on.. I've tried to use a boost wrapper:

struct MyNodeWrapper : public MyNode, public boost::python::wrapper<MyNode>
{
    MyNodeWrapper( PyObject * a_PyObj ) : m_Self( a_PyObj ) {}
    MyNodeWrapper( PyObject * a_PyObj, const MyNode & a_Vec ) : MyNode( a_Vec ), m_Self( a_PyObj ) {}
    MyNodeWrapper( PyObject * a_PyObj, std::string name ) : MyNode( name ) , m_Self( a_PyObj ) {}

    PyObject * m_Self;
};

But still I'm not sure how I should modify the addChild function

Any idea ?

You can't do this:

    node->m_parent = boost::make_shared<MyNode>(this);

Without boost::enable_shared_from_this . See What is the usefulness of `enable_shared_from_this`?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM