diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 667c41929f77cf9115682d532feb54479bf3f98e..cccdc44ead6ac200929ad5fd9327b182875afd41 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -38,6 +38,7 @@ if(NOT NUMPY_WITH_BROKEN_UFUNC_SUPPORT) add_lib_unit_test(user_type) endif() add_lib_unit_test(std_vector) +add_lib_unit_test(user_struct) add_python_unit_test("py-matrix" "unittest/python/test_matrix.py" "unittest") @@ -92,3 +93,7 @@ endif(NOT WIN32) add_python_unit_test("py-std-vector" "unittest/python/test_std_vector.py" "python;unittest") set_tests_properties("py-std-vector" PROPERTIES DEPENDS ${PYWRAP}) + +add_python_unit_test("py-user-struct" "unittest/python/test_user_struct.py" + "python;unittest") +set_tests_properties("py-std-vector" PROPERTIES DEPENDS ${PYWRAP}) diff --git a/unittest/python/test_user_struct.py b/unittest/python/test_user_struct.py new file mode 100644 index 0000000000000000000000000000000000000000..bc9f7f94ff3df8195a03683f009c2659324257f8 --- /dev/null +++ b/unittest/python/test_user_struct.py @@ -0,0 +1,17 @@ +import numpy as np +from user_struct import * + + +x = np.ones(3) +y = np.ones(4) +ms = MyStruct(x, y) +print(ms.x) +print(ms.y) + +ms.x[0] = 0.0 + +ms.x = x # ok +assert np.allclose(ms.x, x) + +ms.y[:] = y +ms.y = y # segfault diff --git a/unittest/user_struct.cpp b/unittest/user_struct.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b26bc38e71711acc108ccc5e714d8e4180e4ef9 --- /dev/null +++ b/unittest/user_struct.cpp @@ -0,0 +1,23 @@ +#include "eigenpy/eigenpy.hpp" + +struct mystruct { + Eigen::Vector3d x_; + Eigen::Vector4d y_; + + mystruct(const Eigen::Vector3d& x, const Eigen::Vector4d& y) : x_(x), y_(y) {} +}; + +BOOST_PYTHON_MODULE(user_struct) { + using namespace Eigen; + namespace bp = boost::python; + eigenpy::enableEigenPy(); + bp::class_<mystruct>("MyStruct", bp::init<const Vector3d&, const Vector4d&>()) + .add_property( + "x", + bp::make_getter(&mystruct::x_, bp::return_internal_reference<>()), + bp::make_setter(&mystruct::x_)) + .add_property( + "y", + bp::make_getter(&mystruct::y_, bp::return_internal_reference<>()), + bp::make_setter(&mystruct::y_)); +}