Skip to content

Commit 257e0af

Browse files
authored
WIP: Expose boost::none_t type (and std::nullopt_t when available) (#367)
* optional: expose to-value converter for boost::none_t (and std::nullopt_t if possible) + add src/optional.cpp to expose none types + this allows to have None as a default arg value for functions (also, shows up in stubs) + also allows to have none_t return/argument type * Run pre-commit
1 parent d971a84 commit 257e0af

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ set(${PROJECT_NAME}_SOURCES
207207
src/quaternion.cpp
208208
src/geometry-conversion.cpp
209209
src/std-vector.cpp
210+
src/optional.cpp
210211
src/version.cpp)
211212

212213
add_library(${PROJECT_NAME} SHARED ${${PROJECT_NAME}_SOURCES}

include/eigenpy/optional.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ struct nullopt_helper<std::optional> {
5656
};
5757
#endif
5858

59+
template <typename NoneType>
60+
struct NoneToPython {
61+
static PyObject *convert(const NoneType &) { Py_RETURN_NONE; }
62+
63+
static void registration() {
64+
bp::to_python_converter<NoneType, NoneToPython, false>();
65+
}
66+
};
67+
5968
template <typename T,
6069
template <typename> class OptionalTpl = EIGENPY_DEFAULT_OPTIONAL>
6170
struct OptionalToPython {

src/eigenpy.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ void exposeMatrixComplexFloat();
2222
void exposeMatrixComplexDouble();
2323
void exposeMatrixComplexLongDouble();
2424

25+
void exposeNoneType();
26+
2527
/* Enable Eigen-Numpy serialization for a set of standard MatrixBase instances.
2628
*/
2729
void enableEigenPy() {
@@ -54,6 +56,8 @@ void enableEigenPy() {
5456
exposeMatrixComplexFloat();
5557
exposeMatrixComplexDouble();
5658
exposeMatrixComplexLongDouble();
59+
60+
exposeNoneType();
5761
}
5862

5963
} // namespace eigenpy

src/optional.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// Copyright 2023 CNRS, INRIA
2+
3+
#include "eigenpy/optional.hpp"
4+
5+
namespace eigenpy {
6+
void exposeNoneType() {
7+
detail::NoneToPython<boost::none_t>::registration();
8+
#ifdef EIGENPY_WITH_CXX17_SUPPORT
9+
detail::NoneToPython<std::nullopt_t>::registration();
10+
#endif
11+
}
12+
} // namespace eigenpy

unittest/bind_optional.cpp.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ BOOST_PYTHON_MODULE(@MODNAME@) {
7474
bp::make_setter(&mystruct::msg));
7575

7676
bp::def("none_if_zero", none_if_zero, bp::args("i"));
77-
bp::def("create_if_true", create_if_true, bp::args("flag", "b"));
77+
bp::def("create_if_true", create_if_true, (bp::arg("flag"), bp::arg("b") = OPT_NONE));
7878
bp::def("random_mat_if_true", random_mat_if_true, bp::args("flag"));
7979
}

0 commit comments

Comments
 (0)