.. _program_listing_file__tmp_ws_src_eigenpy_include_eigenpy_tensor_eigen-from-python.hpp: Program Listing for File eigen-from-python.hpp ============================================== |exhale_lsh| :ref:`Return to documentation for file ` (``/tmp/ws/src/eigenpy/include/eigenpy/tensor/eigen-from-python.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // // Copyright (c) 2023 INRIA // #ifndef __eigenpy_tensor_eigen_from_python_hpp__ #define __eigenpy_tensor_eigen_from_python_hpp__ #include "eigenpy/fwd.hpp" #include "eigenpy/eigen-allocator.hpp" #include "eigenpy/numpy-type.hpp" #include "eigenpy/scalar-conversion.hpp" namespace eigenpy { template struct expected_pytype_for_arg > { static PyTypeObject const *get_pytype() { PyTypeObject const *py_type = eigenpy::getPyArrayType(); return py_type; } }; } // namespace eigenpy namespace boost { namespace python { namespace converter { template struct expected_pytype_for_arg > : eigenpy::expected_pytype_for_arg< Eigen::Tensor > {}; template struct rvalue_from_python_data< Eigen::Tensor const &> : ::eigenpy::rvalue_from_python_data< Eigen::Tensor const &> { typedef Eigen::Tensor T; EIGENPY_RVALUE_FROM_PYTHON_DATA_INIT(T const &) }; template struct rvalue_from_python_data const &> : ::eigenpy::rvalue_from_python_data { EIGENPY_RVALUE_FROM_PYTHON_DATA_INIT(Derived const &) }; } // namespace converter } // namespace python } // namespace boost namespace boost { namespace python { namespace detail { template struct referent_storage &> { typedef Eigen::TensorRef RefType; typedef ::eigenpy::details::referent_storage_eigen_ref StorageType; typedef typename ::eigenpy::aligned_storage< referent_size::value>::type type; }; template struct referent_storage &> { typedef Eigen::TensorRef RefType; typedef ::eigenpy::details::referent_storage_eigen_ref StorageType; typedef typename ::eigenpy::aligned_storage< referent_size::value>::type type; }; } // namespace detail } // namespace python } // namespace boost namespace eigenpy { template struct eigen_from_py_impl > { typedef typename TensorType::Scalar Scalar; static void *convertible(PyObject *pyObj); static void construct(PyObject *pyObj, bp::converter::rvalue_from_python_stage1_data *memory); static void registration(); }; template void * eigen_from_py_impl >::convertible( PyObject *pyObj) { if (!call_PyArray_Check(reinterpret_cast(pyObj))) return 0; typedef typename Eigen::internal::traits::Index Index; static const Index NumIndices = TensorType::NumIndices; PyArrayObject *pyArray = reinterpret_cast(pyObj); if (!np_type_is_convertible_into_scalar( EIGENPY_GET_PY_ARRAY_TYPE(pyArray))) return 0; if (!(PyArray_NDIM(pyArray) == NumIndices || NumIndices == Eigen::Dynamic)) return 0; #ifdef NPY_1_8_API_VERSION if (!(PyArray_FLAGS(pyArray))) #else if (!(PyArray_FLAGS(pyArray) & NPY_ALIGNED)) #endif { return 0; } return pyArray; } template void eigen_from_py_impl >::construct( PyObject *pyObj, bp::converter::rvalue_from_python_stage1_data *memory) { eigen_from_py_construct(pyObj, memory); } template void eigen_from_py_impl >::registration() { bp::converter::registry::push_back( reinterpret_cast(&eigen_from_py_impl::convertible), &eigen_from_py_impl::construct, bp::type_id() #ifndef BOOST_PYTHON_NO_PY_SIGNATURES , &eigenpy::expected_pytype_for_arg::get_pytype #endif ); } template struct eigen_from_py_converter_impl > { static void registration() { EigenFromPy::registration(); // Add conversion to Eigen::TensorBase typedef Eigen::TensorBase TensorBase; EigenFromPy::registration(); // Add conversion to Eigen::TensorRef typedef Eigen::TensorRef RefType; EigenFromPy::registration(); // Add conversion to Eigen::TensorRef typedef const Eigen::TensorRef ConstRefType; EigenFromPy::registration(); } }; template struct EigenFromPy > : EigenFromPy { typedef EigenFromPy EigenFromPyDerived; typedef Eigen::TensorBase Base; static void registration() { bp::converter::registry::push_back( reinterpret_cast(&EigenFromPy::convertible), &EigenFromPy::construct, bp::type_id() #ifndef BOOST_PYTHON_NO_PY_SIGNATURES , &eigenpy::expected_pytype_for_arg::get_pytype #endif ); } }; template struct EigenFromPy > { typedef Eigen::TensorRef RefType; typedef typename TensorType::Scalar Scalar; static void *convertible(PyObject *pyObj) { if (!call_PyArray_Check(pyObj)) return 0; PyArrayObject *pyArray = reinterpret_cast(pyObj); if (!PyArray_ISWRITEABLE(pyArray)) return 0; return EigenFromPy::convertible(pyObj); } static void registration() { bp::converter::registry::push_back( reinterpret_cast(&EigenFromPy::convertible), &eigen_from_py_construct, bp::type_id() #ifndef BOOST_PYTHON_NO_PY_SIGNATURES , &eigenpy::expected_pytype_for_arg::get_pytype #endif ); } }; template struct EigenFromPy > { typedef const Eigen::TensorRef ConstRefType; typedef typename TensorType::Scalar Scalar; static void *convertible(PyObject *pyObj) { return EigenFromPy::convertible(pyObj); } static void registration() { bp::converter::registry::push_back( reinterpret_cast(&EigenFromPy::convertible), &eigen_from_py_construct, bp::type_id() #ifndef BOOST_PYTHON_NO_PY_SIGNATURES , &eigenpy::expected_pytype_for_arg::get_pytype #endif ); } }; } // namespace eigenpy #endif // __eigenpy_tensor_eigen_from_python_hpp__