gpt4 book ai didi

c++ - boost python 对象的生命周期

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:12:09 25 4
gpt4 key购买 nike

cpp:

#include <boost/python.hpp>

using namespace boost;
using namespace boost::python;

struct Foo
{
virtual ~Foo() {}
virtual void Print() = 0;
};

struct FooWrap : Foo, wrapper<Foo>
{
void Print()
{
this->get_override("Print")();
}
};

void ProcessFoo(Foo *obj) { obj->Print(); }

BOOST_PYTHON_MODULE(hello_ext)
{
class_<FooWrap, boost::noncopyable>("Foo")
.def("Print", pure_virtual(&Foo::Print));
def("ProcessFoo", &ProcessFoo);
}

python :

import hello_ext

class NewFoo(hello_ext.Foo):
def Print(self):
print 'Print call'

hello_ext.ProcessFoo( NewFoo() )

一切正常,ProcessFoo 调用中有 Print call 文本。但我想存储所有传递给 ProcessFoo 的指针,例如:

std::vector<Foo*> data;
void ProcessFoo(Foo *obj) { data.push_back(obj); obj->Print(); }

从函数指针退出后变得无效,我不能从 vector 中使用它。使该指针的生命周期更长的最佳方法是什么?使用共享指针或告诉 python 不要删除对象(如果它删除它?)

最佳答案

如果你想存储这个指针,你必须增加底层 python 对象 (PyObject) 的引用计数。为此,你必须实现你的 void ProcessFoo(Foo *obj) 来获取 python 对象而不是 C++ 对象,否则 boost::python 将在他的适应中为你剥离 python 对象,你无法再控制它的生命周期。

如果你这样做,你还必须明确地转换为你的 C++ 类型(但使用 boost::python 这不是那么麻烦)。

using namespace boost::python;
std::vector< std::pair<object, Foo&> > myVec;

void ProcessFoo(object o )
{
Foo& x = extract<Foo&>(o);
// ... do you add to container here, but remember, to add the object o
// too, otherwise the refernce counter will be decremented and the object
// may go away.
myVec.push_back( std::make_pair( o, x ) );
}

关于c++ - boost python 对象的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11586173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com