Skip to content

Commit 8f29857

Browse files
authored
Merge pull request #399 from jorisv/topic/fix_std_vector_api
core: fix StdVectorPythonVisitor::expose API
2 parents e1d426e + bc5afd0 commit 8f29857

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

include/eigenpy/registration_class.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class registration_class {
3030
return *this;
3131
}
3232

33+
template <class DerivedVisitor>
34+
self& def(bp::def_visitor<DerivedVisitor> const& visitor) {
35+
static_cast<DerivedVisitor const&>(visitor).visit(*this);
36+
return *this;
37+
}
38+
3339
/// \see boost::python::class_::def(char const* name, F f)
3440
template <class F>
3541
self& def(char const* name, F f) {

include/eigenpy/std-vector.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ struct build_list<vector_type, true> {
6767
}
6868
};
6969

70-
/// \brief Change the behaviour of indexing (method __getitem__ in Python).
70+
/// \brief Change the behavior of indexing (method __getitem__ in Python).
7171
/// This is suitable for container of Eigen matrix objects if you want to mutate
7272
/// them.
7373
template <typename Container>
@@ -418,14 +418,16 @@ struct StdVectorPythonVisitor {
418418
expose(class_name, doc_string, EmptyPythonVisitor());
419419
}
420420

421-
template <typename Visitor>
422-
static void expose(const std::string &class_name, const Visitor &visitor) {
421+
template <typename DerivedVisitor>
422+
static void expose(const std::string &class_name,
423+
const bp::def_visitor<DerivedVisitor> &visitor) {
423424
expose(class_name, "", visitor);
424425
}
425426

426-
template <typename Visitor>
427+
template <typename DerivedVisitor>
427428
static void expose(const std::string &class_name,
428-
const std::string &doc_string, const Visitor &visitor) {
429+
const std::string &doc_string,
430+
const bp::def_visitor<DerivedVisitor> &visitor) {
429431
// Apply visitor on already registered type or if type is not already
430432
// registered, we define and apply the visitor on it
431433
auto add_std_visitor =

unittest/std_vector.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ void setZero(std::vector<MatType, Eigen::aligned_allocator<MatType> > &Ms) {
3030
}
3131
}
3232

33+
struct CustomTestStruct {
34+
bool operator==(const CustomTestStruct &) const { return true; }
35+
};
36+
3337
BOOST_PYTHON_MODULE(std_vector) {
3438
namespace bp = boost::python;
3539
using namespace eigenpy;
@@ -58,4 +62,10 @@ BOOST_PYTHON_MODULE(std_vector) {
5862
.def(boost::python::vector_indexing_suite<
5963
std::vector<Eigen::Matrix2d> >());
6064
exposeStdVectorEigenSpecificType<Eigen::Matrix2d>("Mat2d");
65+
66+
// Test API regression:
67+
// Exposing a `std::vector` with documentation doesn't clash with
68+
// exposing a `std::vector` with a visitor
69+
StdVectorPythonVisitor<std::vector<CustomTestStruct> >::expose(
70+
"StdVec_CustomTestStruct", "some documentation");
6171
}

0 commit comments

Comments
 (0)