Wer*_*ner 2 boost memory-management list boost-python
好吧,我已经检查了一段时间,找不到答案。
\n\n我想附加一个暴露给 python 的对象,比如 Foo:
\n\nstruct Foo {\n Foo(){ std::cout << "Creating a Foo object" << std::endl;}\n virtual ~Foo(){ std::cout << "Destroying a Foo object" << std::endl;}\n};\nRun Code Online (Sandbox Code Playgroud)\n\n我使用 Foo 继承的对象,有时我想将它们附加到 python 列表中。为此,我创建了一个 FooWrapper,它继承自 Foo 并使用复制构造函数
\n\nstruct FooWrapper : public Foo {\n FooWrapper(const Foo& foo):Foo(foo){ std::cout << "Creating a copy from foo using FooWrapper" << std::endl;} \n virtual ~FooWrapper(){ std::cout << "Destroying a FooWrapper object" << std::endl;}\n};\nRun Code Online (Sandbox Code Playgroud)\n\n这是暴露给Python的:
\n\nBOOST_PYTHON_MODULE(foo_module)\n{\n using namespace py = boost::python;\n py::class_<FooWrapper>("FooWrapper", py::no_init)\xe2\x80\xa6\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我有一个方法将最终的 Foo 对象作为 FooWrapper 附加到 python 列表中,例如:
\n\nvoid appendFoosToList(py::list &list)\n{\n for ( const Foo* foo : m_foos )\n {\n list.append( FooWrapper( *foo ) );\n }\n} \nRun Code Online (Sandbox Code Playgroud)\n\n我怎样才能做到这一点,而不是创建一个临时对象然后复制到列表,而是将该对象附加到列表中,而不必复制临时对象?
\n\n我读过很多文档(boost_faq,boost_python_wiki),很多次我遇到这个运行时错误:
\n\n\n\n\n类型错误: 找不到 C++ 类型的 to_python (按值) 转换器:\n
\n\nBPL 无法从 Python 对象获取 C++ 值。
\n\n例如,当调用 extract(.attr(" len ")()) \n 来获取对象长度时,您省略了“()”。
\n
并且没能找到解决方案。
\n\n我找不到关于此的明确文档,所以我来到这里作为最后的手段。
\n简而言之,在空闲存储上分配包装器并使用结果manage_new_object转换将所有权转移到 Python 对象。这将导致 Boost.Python 在构造 Python 对象时复制指针,而不是复制被指针。
C++ 对象嵌入到 Python 对象中。这是HeldType通过 公开类时提供的class_,默认为公开的 C++ 类型。通常,在公开函数时,可以使用CallPolicy实例来扩充返回的 C++ 类型并将其嵌入到 Python 对象中。特别是,将 CallPolicy 实例return_value_policy与ResultConverterGenerator一起使用允许嵌入类型成为指针,并且 Python 对象将管理所有权。manage_new_object
以下函数可用于将对象的所有权转移到 Python,而无需复制被指向对象:
/// @brief Transfer ownership to a Python object. If the transfer fails,
/// then object will be destroyed and an exception is thrown.
template <typename T>
boost::python::object transfer_to_python(T* t)
{
// Transfer ownership to a smart pointer, allowing for proper cleanup
// incase Boost.Python throws.
std::unique_ptr<T> ptr(t);
// Use the manage_new_object generator to transfer ownership to Python.
namespace python = boost::python;
typename python::manage_new_object::apply<T*>::type converter;
// Transfer ownership to the Python handler and release ownership
// from C++.
python::handle<> handle(converter(*ptr));
ptr.release();
return python::object(handle);
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
void appendFoosToList(boost::python::list& list)
{
for (const Foo* foo : m_foos)
{
list.append(transfer_to_python(new FooWrapper(*foo)));
}
}
Run Code Online (Sandbox Code Playgroud)
这是演示此方法的完整示例:
#include <iostream>
#include <boost/python.hpp>
// Mocks...
class spam
{
public:
spam() { std::cout << "spam(): " << this << std::endl; }
spam(const spam&)
{
std::cout << "spam(const spam&): " << this << std::endl;
}
~spam() { std::cout << "~spam(): " << this << std::endl; }
};
/// @brief Transfer ownership to a Python object. If the transfer fails,
/// then object will be destroyed and an exception is thrown.
template <typename T>
boost::python::object transfer_to_python(T* t)
{
// Transfer ownership to a smart pointer, allowing for proper cleanup
// incase Boost.Python throws.
std::unique_ptr<T> ptr(t);
// Use the manage_new_object generator to transfer ownership to Python.
namespace python = boost::python;
typename python::manage_new_object::apply<T*>::type converter;
// Transfer ownership to the Python handler and release ownership
// from C++.
python::handle<> handle(converter(*ptr));
ptr.release();
return python::object(handle);
}
void append_to_list(boost::python::list& list)
{
list.append(transfer_to_python(new spam()));
}
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::class_<spam>("Spam", python::no_init);
python::def("append_to_list", &append_to_list);
}
Run Code Online (Sandbox Code Playgroud)
互动使用:
>>> import example
>>> spams = []
>>> example.append_to_list(spams)
spam(): 0x25cbd90
>>> assert(type(spams[0]) is example.Spam)
>>> del spams
~spam(): 0x25cbd90
Run Code Online (Sandbox Code Playgroud)