问题描述
我需要包装以下结构
struct dataStruct {
const std::vector<int>& data;
bool valid_data = true;
}
在我的包装文件中,在 PYBIND11_MODULE
下等等我有
In my wrapper file, under PYBIND11_MODULE
and so forth I have
py::class_<dataStruct>(m, "dataStruct")
.def(py::init<>())
.def_readwrite("data", &dataStruct::data)
.def_readwrite("valid_data", &dataStruct::valid_data);
我收到两个错误
no instance of function template "pybind11::class_<type_, options...>::def_readwrite [with type_=dataStruct, options=<>]" matches the argument list -- argument types are: (const char [7], <error-type>) -- object type is: pybind11::class_<dataStruct>
pointer to member of type "const std::vector<int, std::allocator<int>> &" is not allowed
我知道这是由于结构中的指针造成的,但是在查看 pybind11 文档时,我无法找到如何处理结构中的指针.
I know this is due to the pointer in the struct, but when looking at the pybind11 documentation I could not find out how to deal with pointers when they are within a struct.
推荐答案
我之前所做的是围绕 dataStruct
编写一个包装结构,您可以将其暴露给 Python.这将允许您保留向量的额外副本作为实际的 Python 列表.
What I have done before is to write a wrapper struct around dataStruct
that you expose to Python. This will allow you to keep an extra copy of the vector as an actual Python list.
struct dataStructPy : dataStruct {
const py::list & data_py;
}
然后您可以将 data_py
设置为属性而不是读写,并且您可以编写在 std::vector
和 之间进行转换的 get/set 函数py::list
.这允许您在 C++ 端使用 std::vector
而在 Python 端使用标准 Python list
.此类函数可能如下所示:
You can then set data_py
as a property instead of a readwrite and you can write get/set functions that do a conversion between std::vector
and py::list
. This allows you to use a std::vector
on the C++ side while using the standard Python list
on the Python side. Such functions might look like:
py::list dataStructPy::data_py_get()
{
py::list list;
for(const auto & x : this->data) {
list.append(x);
}
return list;
}
void dataStructPy::data_py_set(const py::list & data_py)
{
this->data.clear();
for(int i = 0; i < py::len(data_py); ++i) {
this->data.emplace_back(data_py[i]);
}
}
然后您可以将 dataStructPy
暴露给 Python 并将其称为 dataStruct
(您可以看到我们仍然暴露了 &dataStruct::valid_data
):
You can then expose dataStructPy
to Python and call it dataStruct
(you can see we still expose &dataStruct::valid_data
):
py::class_<dataStructPy>(m, "dataStruct")
.def(py::init<>())
.def_readwrite("valid_data", &dataStruct::valid_data);
.add_property("data", &dataStructPy::data_py_get, &dataStructPy::data_py_set);
在 Python 中,执行以下操作
In Python, doing the following
x = dataStruct()
x.data = [1, 2, 3, 4]
将导致 C++ x.data_py = py::list{1, 2, 3, 4};
和 x.data = std::vector{1, 2,3, 4};
will result in the C++ x.data_py = py::list{1, 2, 3, 4};
and x.data = std::vector{1, 2, 3, 4};
注意:这仅适用于赋值运算符 (=).如果在 Python 方面,如果你执行 x.data[3] = 5
它不会更新 C++ 向量,那么你需要创建一个对象,该对象使用 [] 运算符从 Python 访问向量的内存.
NOTE: This only works for the assignment operator (=). If on the Python side, if you do x.data[3] = 5
it will not update the C++ vector, to do that you would need to create an object that accesses the vector's memory from Python using the [] operator.
这篇关于Pybind11:用指针成员包装结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!