简体   繁体   English

如何在C ++中分配在python中创建的对象?

[英]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 我有一个简单的Node类,带有getChild和getParent

The child can be assigned via the addChild function 可以通过addChild函数分配子项

Then this function automatically set the parent on the relevant class (so from the c++ side) 然后,此函数自动在相关类上设置父级(因此从c ++端开始)

Unfortunately when I do that I lose the python reference.. 不幸的是,当我这样做时,我失去了python参考。

I guess the code should be better to understand : 我猜代码应该更好理解:

the basic main class MyNode : 基本的主类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 : 然后提升python绑定代码

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 : 完成我的python测试代码

>>> 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. 问题肯定在addFunction中,以及我如何使用boost :: make_shared设置父对象。 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 但是我仍然不确定如何修改addChild函数

Any idea ? 任何想法 ?

You can't do this: 您不能这样做:

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

Without boost::enable_shared_from_this . 没有boost::enable_shared_from_this See What is the usefulness of `enable_shared_from_this`? 请参阅`enable_shared_from_this`的作用是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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