Skip to content
Snippets Groups Projects
Unverified Commit a56ca1a2 authored by Joris Vaillant's avatar Joris Vaillant
Browse files

unique_ptr: Move object

parent 87a8f81a
No related branches found
No related tags found
No related merge requests found
...@@ -6,31 +6,53 @@ ...@@ -6,31 +6,53 @@
#define __eigenpy_utils_std_unique_ptr_hpp__ #define __eigenpy_utils_std_unique_ptr_hpp__
#include "eigenpy/fwd.hpp" #include "eigenpy/fwd.hpp"
#include "eigenpy/utils/traits.hpp"
#include <boost/python.hpp> #include <boost/python.hpp>
#include <memory> #include <memory>
#include <iostream>
namespace eigenpy { namespace eigenpy {
namespace details {
template <typename T>
typename std::enable_if<is_class_or_union_remove_cvref<T>::value,
PyObject*>::type
unique_ptr_to_python(std::unique_ptr<T>&& x) {
if (!x) {
return bp::detail::none();
} else {
return bp::detail::make_owning_holder::execute(x.release());
}
}
template <typename T>
typename std::enable_if<!is_class_or_union_remove_cvref<T>::value,
PyObject*>::type
unique_ptr_to_python(std::unique_ptr<T>&& x) {
if (!x) {
return bp::detail::none();
} else {
return bp::to_python_value<const T&>()(*x);
}
}
} // namespace details
/// result_converter of StdUniquePtrCallPolicies /// result_converter of StdUniquePtrCallPolicies
struct StdUniquePtrResultConverter { struct StdUniquePtrResultConverter {
template <class T> template <typename T>
struct apply { struct apply {
struct type { struct type {
typedef typename bp::detail::value_arg<T>::type argument_type; typedef typename T::element_type element_type;
/// TODO, this work by copy PyObject* operator()(T&& x) const {
/// We maybe can transfer the ownership to Python for class type return details::unique_ptr_to_python(std::forward<T>(x));
/// and when argument_type is an lvalue ref
PyObject* operator()(argument_type x) const {
return bp::to_python_value<const typename T::element_type&>()(*x);
} }
#ifndef BOOST_PYTHON_NO_PY_SIGNATURES #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
PyTypeObject const* get_pytype() const { PyTypeObject const* get_pytype() const {
return bp::to_python_value<const typename T::element_type&>() return bp::to_python_value<const element_type&>().get_pytype();
.get_pytype();
} }
#endif #endif
BOOST_STATIC_CONSTANT(bool, uses_registry = true); BOOST_STATIC_CONSTANT(bool, uses_registry = true);
...@@ -38,8 +60,10 @@ struct StdUniquePtrResultConverter { ...@@ -38,8 +60,10 @@ struct StdUniquePtrResultConverter {
}; };
}; };
/// Access CallPolicie to get std::unique_ptr value from a functio /// CallPolicies to get std::unique_ptr value from a function
/// that return an std::unique_ptr /// that return an std::unique_ptr.
/// If the object inside the std::unique_ptr is a class or an union
/// it will be moved. In other case, it will be copied.
struct StdUniquePtrCallPolicies : bp::default_call_policies { struct StdUniquePtrCallPolicies : bp::default_call_policies {
typedef StdUniquePtrResultConverter result_converter; typedef StdUniquePtrResultConverter result_converter;
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment