-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathreturn_by_ref.cpp
50 lines (39 loc) · 1.38 KB
/
return_by_ref.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* Copyright 2020 INRIA
*/
#include <iostream>
#include "eigenpy/eigenpy.hpp"
template <typename Matrix>
struct Base {
Base(const Eigen::DenseIndex rows, const Eigen::DenseIndex cols)
: mat(rows, cols) {}
void show() { std::cout << mat << std::endl; }
Matrix& ref() { return mat; }
const Matrix& const_ref() { return mat; }
Matrix copy() { return mat; }
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;
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;
expose_matrix_class<VectorType>("Vector");
expose_matrix_class<MatrixType>("Matrix");
expose_matrix_class<RowMatrixType>("RowMatrix");
}