.. _program_listing_file_include_eigenpy_register.hpp: Program Listing for File register.hpp ===================================== |exhale_lsh| :ref:`Return to documentation for file ` (``include/eigenpy/register.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // // Copyright (c) 2020 INRIA // #ifndef __eigenpy_register_hpp__ #define __eigenpy_register_hpp__ #include #include #include #include #include "eigenpy/exception.hpp" #include "eigenpy/fwd.hpp" #include "eigenpy/numpy.hpp" namespace eigenpy { struct EIGENPY_DLLAPI Register { static PyArray_Descr *getPyArrayDescr(PyTypeObject *py_type_ptr); static PyArray_Descr *getPyArrayDescrFromTypeNum(const int type_num); template static PyArray_Descr *getPyArrayDescrFromScalarType() { if (!isNumpyNativeType()) { const std::type_info &info = typeid(Scalar); if (instance().type_to_py_type_bindings.find(&info) != instance().type_to_py_type_bindings.end()) { PyTypeObject *py_type = instance().type_to_py_type_bindings[&info]; return instance().py_array_descr_bindings[py_type]; } else return nullptr; } else { PyArray_Descr *new_descr = call_PyArray_DescrFromType(NumpyEquivalentType::type_code); return new_descr; } } template static bool isRegistered() { return isRegistered(Register::getPyType()); } static bool isRegistered(PyTypeObject *py_type_ptr); static int getTypeCode(PyTypeObject *py_type_ptr); template static PyTypeObject *getPyType() { if (!isNumpyNativeType()) { const PyTypeObject *const_py_type_ptr = bp::converter::registered_pytype::get_pytype(); if (const_py_type_ptr == NULL) { std::stringstream ss; ss << "The type " << typeid(Scalar).name() << " does not have a registered converter inside Boot.Python." << std::endl; throw std::invalid_argument(ss.str()); } PyTypeObject *py_type_ptr = const_cast(const_py_type_ptr); return py_type_ptr; } else { PyArray_Descr *new_descr = call_PyArray_DescrFromType(NumpyEquivalentType::type_code); return new_descr->typeobj; } } template static PyArray_Descr *getPyArrayDescr() { if (!isNumpyNativeType()) { return getPyArrayDescr(getPyType()); } else { return call_PyArray_DescrFromType(NumpyEquivalentType::type_code); } } template static int getTypeCode() { if (isNumpyNativeType()) return NumpyEquivalentType::type_code; else { const std::type_info &info = typeid(Scalar); if (instance().type_to_py_type_bindings.find(&info) != instance().type_to_py_type_bindings.end()) { PyTypeObject *py_type = instance().type_to_py_type_bindings[&info]; int code = instance().py_array_code_bindings[py_type]; return code; } else return -1; // type not registered } } static int registerNewType( PyTypeObject *py_type_ptr, const std::type_info *type_info_ptr, const int type_size, const int alignment, PyArray_GetItemFunc *getitem, PyArray_SetItemFunc *setitem, PyArray_NonzeroFunc *nonzero, PyArray_CopySwapFunc *copyswap, PyArray_CopySwapNFunc *copyswapn, PyArray_DotFunc *dotfunc, PyArray_FillFunc *fill, PyArray_FillWithScalarFunc *fillwithscalar); static Register &instance(); private: Register() {}; struct Compare_PyTypeObject { bool operator()(const PyTypeObject *a, const PyTypeObject *b) const { return std::string(a->tp_name) < std::string(b->tp_name); } }; struct Compare_TypeInfo { bool operator()(const std::type_info *a, const std::type_info *b) const { return std::string(a->name()) < std::string(b->name()); } }; typedef std::map MapInfo; MapInfo type_to_py_type_bindings; typedef std::map MapDescr; MapDescr py_array_descr_bindings; typedef std::map MapCode; MapCode py_array_code_bindings; }; } // namespace eigenpy #endif // __eigenpy_register_hpp__