在@kazemakase 和@jagerman(后者通过pybind11 论坛)的帮助下,我已经弄清楚了。类本身应该有一个可以从某些输入复制的构造函数,这里使用迭代器:
#include <vector>
#include <assert.h>
#include <iterator>
template <class T> class Matrix3D
{
public:
std::vector<T> data;
std::vector<size_t> shape;
std::vector<size_t> strides;
Matrix3D<T>() = default;
template<class Iterator>
Matrix3D<T>(const std::vector<size_t> &shape, Iterator first, Iterator last);
};
template <class T>
template<class Iterator>
Matrix3D<T>::Matrix3D(const std::vector<size_t> &shape_, Iterator first, Iterator last)
{
shape = shape_;
assert( shape.size() == 3 );
strides.resize(3);
strides[0] = shape[2]*shape[1];
strides[1] = shape[2];
strides[2] = 1;
int size = shape[0] * shape[1] * shape[2];
assert( last-first == size );
data.resize(size);
std::copy(first, last, data.begin());
}
直接包装以下签名的函数:
Matrix3D<double> func ( const Matrix3D<double>& );
需要以下包装代码
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
namespace py = pybind11;
namespace pybind11 { namespace detail {
template <typename T> struct type_caster<Matrix3D<T>>
{
public:
PYBIND11_TYPE_CASTER(Matrix3D<T>, _("Matrix3D<T>"));
// Conversion part 1 (Python -> C++)
bool load(py::handle src, bool convert)
{
if ( !convert and !py::array_t<T>::check_(src) )
return false;
auto buf = py::array_t<T, py::array::c_style | py::array::forcecast>::ensure(src);
if ( !buf )
return false;
auto dims = buf.ndim();
if ( dims != 3 )
return false;
std::vector<size_t> shape(3);
for ( int i = 0 ; i < 3 ; ++i )
shape[i] = buf.shape()[i];
value = Matrix3D<T>(shape, buf.data(), buf.data()+buf.size());
return true;
}
//Conversion part 2 (C++ -> Python)
static py::handle cast(const Matrix3D<T>& src, py::return_value_policy policy, py::handle parent)
{
std::vector<size_t> shape (3);
std::vector<size_t> strides(3);
for ( int i = 0 ; i < 3 ; ++i ) {
shape [i] = src.shape [i];
strides[i] = src.strides[i]*sizeof(T);
}
py::array a(std::move(shape), std::move(strides), src.data.data() );
return a.release();
}
};
}} // namespace pybind11::detail
PYBIND11_PLUGIN(example) {
py::module m("example", "Module description");
m.def("func", &func, "Function description" );
return m.ptr();
}
请注意,现在也可以进行函数重载。例如,如果存在具有以下签名的重载函数:
Matrix3D<int > func ( const Matrix3D<int >& );
Matrix3D<double> func ( const Matrix3D<double>& );
将需要以下包装函数定义:
m.def("func", py::overload_cast<Matrix3D<int >&>(&func), "Function description" );
m.def("func", py::overload_cast<Matrix3D<double>&>(&func), "Function description" );