std-unique-ptr.hpp
Go to the documentation of this file.
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"
11 
12 #include <boost/python.hpp>
13 
14 #include <memory>
15 #include <type_traits>
16 
17 namespace eigenpy {
18 
19 namespace details {
20 
22 template <typename T>
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 
34 template <typename T>
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 
46 template <typename T>
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 
57 template <typename T>
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 
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 
88  template <typename T>
89  struct apply {
90  struct type {
92 
93  PyObject* operator()(T x) const {
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 
111 struct StdUniquePtrCallPolicies : bp::default_call_policies {
113 };
114 
117 struct ReturnInternalStdUniquePtr : bp::return_internal_reference<> {
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 
137 template <typename T>
138 struct to_python_value<const std::unique_ptr<T>&>
140  std::unique_ptr<T> >::type {};
141 
142 } // namespace python
143 } // namespace boost
144 
145 #endif // ifndef __eigenpy_utils_std_unique_ptr_hpp__
eigenpy::details::InternalStdUniquePtrConverter::apply::type::get_pytype
PyTypeObject const * get_pytype() const
Definition: std-unique-ptr.hpp:97
eigenpy::details::internal_unique_ptr_to_python
std::enable_if<!is_python_primitive_type< T >::value, PyObject * >::type internal_unique_ptr_to_python(std::unique_ptr< T > &x)
Definition: std-unique-ptr.hpp:48
eigenpy::details::StdUniquePtrResultConverter
result_converter of StdUniquePtrCallPolicies
Definition: std-unique-ptr.hpp:68
PyStr_Check
#define PyStr_Check
Definition: python-compat.hpp:18
fwd.hpp
boost
Definition: alignment.hpp:48
eigenpy::StdUniquePtrCallPolicies::result_converter
details::StdUniquePtrResultConverter result_converter
Definition: std-unique-ptr.hpp:112
eigenpy::details::StdUniquePtrResultConverter::apply
Definition: std-unique-ptr.hpp:70
eigenpy
Definition: alignment.hpp:14
eigenpy::details::InternalStdUniquePtrConverter::apply::type::element_type
remove_cvref< T >::type::element_type element_type
Definition: std-unique-ptr.hpp:91
eigenpy::details::InternalStdUniquePtrConverter::apply
Definition: std-unique-ptr.hpp:89
eigenpy::details::unique_ptr_to_python
std::enable_if<!is_python_primitive_type< T >::value, PyObject * >::type unique_ptr_to_python(std::unique_ptr< T > &&x)
Transfer std::unique_ptr ownership to an owning holder.
Definition: std-unique-ptr.hpp:24
python
Definition: python.py:1
eigenpy::details::InternalStdUniquePtrConverter::apply::type
Definition: std-unique-ptr.hpp:90
python-compat.hpp
test_matrix.value
float value
Definition: test_matrix.py:161
test_user_struct.x
x
Definition: test_user_struct.py:4
eigenpy::ReturnInternalStdUniquePtr
Definition: std-unique-ptr.hpp:117
eigenpy::details::StdUniquePtrResultConverter::apply::type::operator()
PyObject * operator()(T &&x) const
Definition: std-unique-ptr.hpp:74
eigenpy::ReturnInternalStdUniquePtr::result_converter
details::InternalStdUniquePtrConverter result_converter
Definition: std-unique-ptr.hpp:118
eigenpy::ReturnInternalStdUniquePtr::postcall
static PyObject * postcall(ArgumentPackage const &args_, PyObject *result)
Definition: std-unique-ptr.hpp:121
eigenpy::details::InternalStdUniquePtrConverter::apply::type::operator()
PyObject * operator()(T x) const
Definition: std-unique-ptr.hpp:93
eigenpy::details::StdUniquePtrResultConverter::apply::type::get_pytype
PyTypeObject const * get_pytype() const
Definition: std-unique-ptr.hpp:78
eigenpy::StdUniquePtrCallPolicies
Definition: std-unique-ptr.hpp:111
eigenpy::details::InternalStdUniquePtrConverter
result_converter of ReturnInternalStdUniquePtr
Definition: std-unique-ptr.hpp:87
traits.hpp
eigenpy::details::remove_cvref
Trait to remove const&.
Definition: traits.hpp:19
eigenpy::details::StdUniquePtrResultConverter::apply::type
Definition: std-unique-ptr.hpp:71
eigenpy::details::StdUniquePtrResultConverter::apply::type::element_type
T::element_type element_type
Definition: std-unique-ptr.hpp:72


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Fri Jun 14 2024 02:15:58