我有一组用 C++ 编写并使用 pybind11 导出到 Python 的模块。所有这些模块都应该能够独立使用,但它们使用一组在实用程序库中定义的通用自定义类型。
在每个模块中都有类似于下面的代码。标Color.hpp
头定义了实用程序库中使用的类型。
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <string>
#include "Color.hpp"
std::vector<Color> buncha_colors(int n, std::string &color) {
std::vector<Color> out;
for (;n-- > 0;) {
out.push_back(Color(color));
}
return out;
}
PYBIND11_MODULE(pb11_example_module, m) {
m.def("buncha_colors", &buncha_colors);
}
当然,这是行不通的。Pybind 不知道如何对Color
对象进行类型转换。答案(或希望不是)是将Color
类定义为模块的一部分。之后,pybind 能够进行自动类型转换。
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <string>
#include "Colors.hpp"
std::vector<Color> buncha_colors(int n, std::string &color) {
std::vector<Color> out;
for (;n-- > 0;) {
out.push_back(Color(color));
}
return out;
}
PYBIND11_MODULE(pb11_example_module, m) {
pybind11::class_<Color>(m, "Color")
.def(pybind11::init<std::string&>())
.def("name", &Color::name);
m.def("buncha_colors", &buncha_colors);
}
理想情况下,我想将所有这些自定义实用程序类型和相关函数保存在一个单独的模块中,与所有使用它们的模块分开。但是我需要在使用它的每个模块中定义类型转换,或者以其他方式引用它。我该怎么做?我不想要pb11_example_module.Color
等等utils.Color
。我不知道它们的兼容性,这似乎是错误的方式。