|
| 1 | +/// Copyright (c) 2023 CNRS INRIA |
| 2 | +/// Definitions for exposing boost::optional<T> types. |
| 3 | +/// Also works with std::optional. |
| 4 | + |
| 5 | +#ifndef __eigenpy_optional_hpp__ |
| 6 | +#define __eigenpy_optional_hpp__ |
| 7 | + |
| 8 | +#include "eigenpy/fwd.hpp" |
| 9 | +#include "eigenpy/eigen-from-python.hpp" |
| 10 | +#include <boost/optional.hpp> |
| 11 | +#ifdef EIGENPY_WITH_CXX17_SUPPORT |
| 12 | +#include <optional> |
| 13 | +#endif |
| 14 | + |
| 15 | +#ifndef EIGENPY_DEFAULT_OPTIONAL |
| 16 | +#define EIGENPY_DEFAULT_OPTIONAL boost::optional |
| 17 | +#endif |
| 18 | + |
| 19 | +namespace boost { |
| 20 | +namespace python { |
| 21 | +namespace converter { |
| 22 | + |
| 23 | +template <typename T> |
| 24 | +struct expected_pytype_for_arg<boost::optional<T> > |
| 25 | + : expected_pytype_for_arg<T> {}; |
| 26 | + |
| 27 | +#ifdef EIGENPY_WITH_CXX17_SUPPORT |
| 28 | +template <typename T> |
| 29 | +struct expected_pytype_for_arg<std::optional<T> > : expected_pytype_for_arg<T> { |
| 30 | +}; |
| 31 | +#endif |
| 32 | + |
| 33 | +} // namespace converter |
| 34 | +} // namespace python |
| 35 | +} // namespace boost |
| 36 | + |
| 37 | +namespace eigenpy { |
| 38 | +namespace detail { |
| 39 | + |
| 40 | +/// Helper struct to decide which type is the "none" type for a specific |
| 41 | +/// optional<T> implementation. |
| 42 | +template <template <typename> class OptionalTpl> |
| 43 | +struct nullopt_helper {}; |
| 44 | + |
| 45 | +template <> |
| 46 | +struct nullopt_helper<boost::optional> { |
| 47 | + typedef boost::none_t type; |
| 48 | + static type value() { return boost::none; } |
| 49 | +}; |
| 50 | + |
| 51 | +#ifdef EIGENPY_WITH_CXX17_SUPPORT |
| 52 | +template <> |
| 53 | +struct nullopt_helper<std::optional> { |
| 54 | + typedef std::nullopt_t type; |
| 55 | + static type value() { return std::nullopt; } |
| 56 | +}; |
| 57 | +#endif |
| 58 | + |
| 59 | +template <typename T, |
| 60 | + template <typename> class OptionalTpl = EIGENPY_DEFAULT_OPTIONAL> |
| 61 | +struct OptionalToPython { |
| 62 | + static PyObject *convert(const OptionalTpl<T> &obj) { |
| 63 | + if (obj) |
| 64 | + return bp::incref(bp::object(*obj).ptr()); |
| 65 | + else { |
| 66 | + return bp::incref(bp::object().ptr()); // None |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + static PyTypeObject const *get_pytype() { |
| 71 | + return bp::converter::registered_pytype<T>::get_pytype(); |
| 72 | + } |
| 73 | + |
| 74 | + static void registration() { |
| 75 | + bp::to_python_converter<OptionalTpl<T>, OptionalToPython, true>(); |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +template <typename T, |
| 80 | + template <typename> class OptionalTpl = EIGENPY_DEFAULT_OPTIONAL> |
| 81 | +struct OptionalFromPython { |
| 82 | + static void *convertible(PyObject *obj_ptr); |
| 83 | + |
| 84 | + static void construct(PyObject *obj_ptr, |
| 85 | + bp::converter::rvalue_from_python_stage1_data *memory); |
| 86 | + |
| 87 | + static void registration(); |
| 88 | +}; |
| 89 | + |
| 90 | +template <typename T, template <typename> class OptionalTpl> |
| 91 | +void *OptionalFromPython<T, OptionalTpl>::convertible(PyObject *obj_ptr) { |
| 92 | + if (obj_ptr == Py_None) { |
| 93 | + return obj_ptr; |
| 94 | + } |
| 95 | + bp::extract<T> bp_obj(obj_ptr); |
| 96 | + if (!bp_obj.check()) |
| 97 | + return 0; |
| 98 | + else |
| 99 | + return obj_ptr; |
| 100 | +} |
| 101 | + |
| 102 | +template <typename T, template <typename> class OptionalTpl> |
| 103 | +void OptionalFromPython<T, OptionalTpl>::construct( |
| 104 | + PyObject *obj_ptr, bp::converter::rvalue_from_python_stage1_data *memory) { |
| 105 | + // create storage |
| 106 | + using rvalue_storage_t = |
| 107 | + bp::converter::rvalue_from_python_storage<OptionalTpl<T> >; |
| 108 | + void *storage = |
| 109 | + reinterpret_cast<rvalue_storage_t *>(reinterpret_cast<void *>(memory)) |
| 110 | + ->storage.bytes; |
| 111 | + |
| 112 | + if (obj_ptr == Py_None) { |
| 113 | + new (storage) OptionalTpl<T>(nullopt_helper<OptionalTpl>::value()); |
| 114 | + } else { |
| 115 | + const T value = bp::extract<T>(obj_ptr); |
| 116 | + new (storage) OptionalTpl<T>(value); |
| 117 | + } |
| 118 | + |
| 119 | + memory->convertible = storage; |
| 120 | +} |
| 121 | + |
| 122 | +template <typename T, template <typename> class OptionalTpl> |
| 123 | +void OptionalFromPython<T, OptionalTpl>::registration() { |
| 124 | + bp::converter::registry::push_back( |
| 125 | + &convertible, &construct, bp::type_id<OptionalTpl<T> >(), |
| 126 | + bp::converter::expected_pytype_for_arg<OptionalTpl<T> >::get_pytype); |
| 127 | +} |
| 128 | + |
| 129 | +} // namespace detail |
| 130 | + |
| 131 | +/// Register converters for the type `optional<T>` to Python. |
| 132 | +/// By default \tparam optional is `EIGENPY_DEFAULT_OPTIONAL`. |
| 133 | +template <typename T, |
| 134 | + template <typename> class OptionalTpl = EIGENPY_DEFAULT_OPTIONAL> |
| 135 | +struct OptionalConverter { |
| 136 | + static void registration() { |
| 137 | + detail::OptionalToPython<T, OptionalTpl>::registration(); |
| 138 | + detail::OptionalFromPython<T, OptionalTpl>::registration(); |
| 139 | + } |
| 140 | +}; |
| 141 | + |
| 142 | +} // namespace eigenpy |
| 143 | + |
| 144 | +#endif // __eigenpy_optional_hpp__ |
0 commit comments