return_by_ref.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 INRIA
3  */
4 
5 #include <iostream>
6 
7 #include "eigenpy/eigenpy.hpp"
8 
9 template <typename Matrix>
10 struct Base {
11  Base(const Eigen::DenseIndex rows, const Eigen::DenseIndex cols)
12  : mat(rows, cols) {}
13 
14  void show() { std::cout << mat << std::endl; }
15 
16  Matrix& ref() { return mat; }
17  const Matrix& const_ref() { return mat; }
18  Matrix copy() { return mat; }
19 
20  protected:
21  Matrix mat;
22 };
23 
24 template <typename MatrixType>
25 void expose_matrix_class(const std::string& name) {
26  using namespace Eigen;
27  namespace bp = boost::python;
28 
29  bp::class_<Base<MatrixType> >(name.c_str(),
30  bp::init<DenseIndex, DenseIndex>())
31  .def("show", &Base<MatrixType>::show)
32  .def("ref", &Base<MatrixType>::ref, bp::return_internal_reference<>())
33  .def("const_ref", &Base<MatrixType>::const_ref,
34  bp::return_internal_reference<>())
35  .def("copy", &Base<MatrixType>::copy);
36 }
37 
38 BOOST_PYTHON_MODULE(return_by_ref) {
39  using namespace Eigen;
41 
42  typedef Eigen::Matrix<double, Eigen::Dynamic, 1> VectorType;
43  typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> MatrixType;
44  typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
45  RowMatrixType;
46 
47  expose_matrix_class<VectorType>("Vector");
48  expose_matrix_class<MatrixType>("Matrix");
49  expose_matrix_class<RowMatrixType>("RowMatrix");
50 }
Matrix & ref()
Base(const Eigen::DenseIndex rows, const Eigen::DenseIndex cols)
Matrix mat
Definition: complex.cpp:7
void EIGENPY_DLLAPI enableEigenPy()
Definition: eigenpy.cpp:29
void expose_matrix_class(const std::string &name)
BOOST_PYTHON_MODULE(return_by_ref)
const Matrix & const_ref()
Matrix copy()
void show()


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Fri Jun 2 2023 02:10:26