From b0082164d1918ced6dea6689c627037b13ebcd58 Mon Sep 17 00:00:00 2001 From: Justin Carpentier <justin.carpentier@inria.fr> Date: Fri, 21 Feb 2020 09:27:56 +0100 Subject: [PATCH] test: start implementing test for return by reference context --- unittest/CMakeLists.txt | 1 + unittest/return_by_ref.cpp | 44 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 unittest/return_by_ref.cpp diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 03d0997..747401e 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -32,6 +32,7 @@ ENDMACRO(ADD_LIB_UNIT_TEST) ADD_LIB_UNIT_TEST(matrix "eigen3") ADD_LIB_UNIT_TEST(geometry "eigen3") ADD_LIB_UNIT_TEST(complex "eigen3") +ADD_LIB_UNIT_TEST(return_by_ref "eigen3") IF(NOT ${EIGEN3_VERSION} VERSION_LESS "3.2.0") ADD_LIB_UNIT_TEST(ref "eigen3") ENDIF() diff --git a/unittest/return_by_ref.cpp b/unittest/return_by_ref.cpp new file mode 100644 index 0000000..ae88b32 --- /dev/null +++ b/unittest/return_by_ref.cpp @@ -0,0 +1,44 @@ +/* + * Copyright 2014-2019, CNRS + * Copyright 2018-2020, INRIA + */ + +#include "eigenpy/eigenpy.hpp" +#include <iostream> + +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; } + Matrix copy() { return mat; } + +protected: + + Matrix mat; +}; + + +BOOST_PYTHON_MODULE(return_by_ref) +{ + using namespace Eigen; + namespace bp = boost::python; + eigenpy::enableEigenPy(); + + + 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); +} + -- GitLab