diff --git a/unittest/return_by_ref.cpp b/unittest/return_by_ref.cpp index ae88b32b947ae0bf3b9c0dc639b5badb9cd6bac8..12f24c4b3668f3c89271d2f36fa3534e7c258116 100644 --- a/unittest/return_by_ref.cpp +++ b/unittest/return_by_ref.cpp @@ -20,6 +20,7 @@ struct Base } Matrix & ref() { return mat; } + const Matrix & const_ref() { return mat; } Matrix copy() { return mat; } protected: @@ -27,18 +28,30 @@ protected: Matrix mat; }; +template<typename MatrixType> +void expose_matrix_class(const std::string & name) +{ + using namespace Eigen; + namespace bp = boost::python; + + bp::class_<Base<MatrixType> >(name.c_str(),bp::init<DenseIndex,DenseIndex>()) + .def("show",&Base<MatrixType>::show) + .def("ref",&Base<MatrixType>::ref, bp::return_internal_reference<>()) + .def("const_ref",&Base<MatrixType>::const_ref, bp::return_internal_reference<>()) + .def("copy",&Base<MatrixType>::copy); +} BOOST_PYTHON_MODULE(return_by_ref) { using namespace Eigen; - namespace bp = boost::python; eigenpy::enableEigenPy(); - + typedef Eigen::Matrix<double,Eigen::Dynamic,1> VectorType; + typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> MatrixType; typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> RowMatrixType; - bp::class_<Base<RowMatrixType> >("Matrix",bp::init<DenseIndex,DenseIndex>()) - .def("show",&Base<RowMatrixType>::show) - .def("ref",&Base<RowMatrixType>::ref, bp::return_internal_reference<>()) - .def("copy",&Base<RowMatrixType>::copy); + + expose_matrix_class<VectorType>("Vector"); + expose_matrix_class<MatrixType>("Matrix"); + expose_matrix_class<RowMatrixType>("RowMatrix"); }