|
| 1 | +// |
| 2 | +// Copyright (c) 2024 INRIA |
| 3 | +// |
| 4 | + |
| 5 | +#ifndef __eigenpy_utils_std_unique_ptr_hpp__ |
| 6 | +#define __eigenpy_utils_std_unique_ptr_hpp__ |
| 7 | + |
| 8 | +#include "eigenpy/fwd.hpp" |
| 9 | +#include "eigenpy/utils/traits.hpp" |
| 10 | +#include "eigenpy/utils/python-compat.hpp" |
| 11 | + |
| 12 | +#include <boost/python.hpp> |
| 13 | + |
| 14 | +#include <memory> |
| 15 | +#include <type_traits> |
| 16 | + |
| 17 | +namespace eigenpy { |
| 18 | + |
| 19 | +namespace details { |
| 20 | + |
| 21 | +/// Transfer std::unique_ptr ownership to an owning holder |
| 22 | +template <typename T> |
| 23 | +typename std::enable_if<!is_python_primitive_type<T>::value, PyObject*>::type |
| 24 | +unique_ptr_to_python(std::unique_ptr<T>&& x) { |
| 25 | + typedef bp::objects::pointer_holder<std::unique_ptr<T>, T> holder_t; |
| 26 | + if (!x) { |
| 27 | + return bp::detail::none(); |
| 28 | + } else { |
| 29 | + return bp::objects::make_ptr_instance<T, holder_t>::execute(x); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/// Convert and copy the primitive value to python |
| 34 | +template <typename T> |
| 35 | +typename std::enable_if<is_python_primitive_type<T>::value, PyObject*>::type |
| 36 | +unique_ptr_to_python(std::unique_ptr<T>&& x) { |
| 37 | + if (!x) { |
| 38 | + return bp::detail::none(); |
| 39 | + } else { |
| 40 | + return bp::to_python_value<const T&>()(*x); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/// std::unique_ptr keep the ownership but a reference to the std::unique_ptr |
| 45 | +/// value is created |
| 46 | +template <typename T> |
| 47 | +typename std::enable_if<!is_python_primitive_type<T>::value, PyObject*>::type |
| 48 | +internal_unique_ptr_to_python(std::unique_ptr<T>& x) { |
| 49 | + if (!x) { |
| 50 | + return bp::detail::none(); |
| 51 | + } else { |
| 52 | + return bp::detail::make_reference_holder::execute(x.get()); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// Convert and copy the primitive value to python |
| 57 | +template <typename T> |
| 58 | +typename std::enable_if<is_python_primitive_type<T>::value, PyObject*>::type |
| 59 | +internal_unique_ptr_to_python(std::unique_ptr<T>& x) { |
| 60 | + if (!x) { |
| 61 | + return bp::detail::none(); |
| 62 | + } else { |
| 63 | + return bp::to_python_value<const T&>()(*x); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// result_converter of StdUniquePtrCallPolicies |
| 68 | +struct StdUniquePtrResultConverter { |
| 69 | + template <typename T> |
| 70 | + struct apply { |
| 71 | + struct type { |
| 72 | + typedef typename T::element_type element_type; |
| 73 | + |
| 74 | + PyObject* operator()(T&& x) const { |
| 75 | + return unique_ptr_to_python(std::forward<T>(x)); |
| 76 | + } |
| 77 | +#ifndef BOOST_PYTHON_NO_PY_SIGNATURES |
| 78 | + PyTypeObject const* get_pytype() const { |
| 79 | + return bp::to_python_value<const element_type&>().get_pytype(); |
| 80 | + } |
| 81 | +#endif |
| 82 | + }; |
| 83 | + }; |
| 84 | +}; |
| 85 | + |
| 86 | +/// result_converter of ReturnInternalStdUniquePtr |
| 87 | +struct InternalStdUniquePtrConverter { |
| 88 | + template <typename T> |
| 89 | + struct apply { |
| 90 | + struct type { |
| 91 | + typedef typename remove_cvref<T>::type::element_type element_type; |
| 92 | + |
| 93 | + PyObject* operator()(T x) const { |
| 94 | + return internal_unique_ptr_to_python(x); |
| 95 | + } |
| 96 | +#ifndef BOOST_PYTHON_NO_PY_SIGNATURES |
| 97 | + PyTypeObject const* get_pytype() const { |
| 98 | + return bp::to_python_value<const element_type&>().get_pytype(); |
| 99 | + } |
| 100 | +#endif |
| 101 | + }; |
| 102 | + }; |
| 103 | +}; |
| 104 | + |
| 105 | +} // namespace details |
| 106 | + |
| 107 | +/// CallPolicies to get std::unique_ptr value from a function |
| 108 | +/// that return an std::unique_ptr. |
| 109 | +/// If the object inside the std::unique_ptr is a class or an union |
| 110 | +/// it will be moved. In other case, it will be copied. |
| 111 | +struct StdUniquePtrCallPolicies : bp::default_call_policies { |
| 112 | + typedef details::StdUniquePtrResultConverter result_converter; |
| 113 | +}; |
| 114 | + |
| 115 | +/// Variant of \see bp::return_internal_reference that extract std::unique_ptr |
| 116 | +/// content reference before converting it into a PyObject |
| 117 | +struct ReturnInternalStdUniquePtr : bp::return_internal_reference<> { |
| 118 | + typedef details::InternalStdUniquePtrConverter result_converter; |
| 119 | + |
| 120 | + template <class ArgumentPackage> |
| 121 | + static PyObject* postcall(ArgumentPackage const& args_, PyObject* result) { |
| 122 | + // Don't run return_internal_reference postcall on primitive type |
| 123 | + if (PyInt_Check(result) || PyBool_Check(result) || PyFloat_Check(result) || |
| 124 | + PyStr_Check(result) || PyComplex_Check(result)) { |
| 125 | + return result; |
| 126 | + } |
| 127 | + return bp::return_internal_reference<>::postcall(args_, result); |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +} // namespace eigenpy |
| 132 | + |
| 133 | +namespace boost { |
| 134 | +namespace python { |
| 135 | + |
| 136 | +/// Specialize to_python_value for std::unique_ptr |
| 137 | +template <typename T> |
| 138 | +struct to_python_value<const std::unique_ptr<T>&> |
| 139 | + : eigenpy::details::StdUniquePtrResultConverter::apply< |
| 140 | + std::unique_ptr<T> >::type {}; |
| 141 | + |
| 142 | +} // namespace python |
| 143 | +} // namespace boost |
| 144 | + |
| 145 | +#endif // ifndef __eigenpy_utils_std_unique_ptr_hpp__ |
0 commit comments